diff --git a/packages/gooddata-eval/src/gooddata_eval/core/agentic/_catalog.py b/packages/gooddata-eval/src/gooddata_eval/core/agentic/_catalog.py index fcd94b43a..caadc6354 100644 --- a/packages/gooddata-eval/src/gooddata_eval/core/agentic/_catalog.py +++ b/packages/gooddata-eval/src/gooddata_eval/core/agentic/_catalog.py @@ -83,6 +83,10 @@ class CatalogMetricAlert: """Date dataset an ANOMALY alert detects on; ``None`` means unasserted.""" setup_turns: tuple[str, ...] = () """Messages sent in the same conversation before the question, not scored.""" + alternatives: tuple[CatalogMetricAlert, ...] = () + """Complete expectations any one of which passes (``any_of``); empty means this one alone.""" + check_first_proposal: tuple[str, ...] = () + """Fields (``filters``, ``recipients``) also scored on the first proposal, before the simulated user replies.""" @classmethod def from_dict(cls, d: dict) -> CatalogMetricAlert: diff --git a/packages/gooddata-eval/src/gooddata_eval/core/agentic/alert_skill.py b/packages/gooddata-eval/src/gooddata_eval/core/agentic/alert_skill.py index 16d314e3f..a31aa5685 100644 --- a/packages/gooddata-eval/src/gooddata_eval/core/agentic/alert_skill.py +++ b/packages/gooddata-eval/src/gooddata_eval/core/agentic/alert_skill.py @@ -3,9 +3,11 @@ from __future__ import annotations +import dataclasses import json import os import re +import string import time from dataclasses import dataclass, field from typing import Any @@ -106,7 +108,16 @@ "alert_sensitivity_correct", "alert_trigger_interval_correct", "alert_date_dataset_correct", + "alert_initial_filters_correct", + "alert_initial_recipients_correct", ) +# Fields `check_first_proposal` may name; each one is scored on the first proposal as well. +_FIRST_PROPOSAL_FIELDS = ("filters", "recipients") +# Labels of the `any_of` combinations in the simulated user's rule 9; an item lists at most this many. +_COMBINATION_LABELS = string.ascii_uppercase +# The only CatalogMetricAlert fields `any_of` options may differ in. Rule 9 states them per +# combination and rules 1 and 5 defer to it; rules 1-8 state every other field once. +_ANY_OF_FIELDS = ("threshold", "threshold_from", "threshold_to", "filters") # The only check of an item that expects no alert; it replaces every other check. _NO_ALERT_CHECK = "alert_not_proposed" # Score names published only by an item that states the matching expectation (gdc-nas combo_report). @@ -141,6 +152,8 @@ "trigger interval", "date_dataset_id", "date dataset", + "check_first_proposal", + "check first proposal", ) # A no-alert run stops after this many agent turns: the simulated user only declines. Measured on # 2026-11-25 with the cap at the default budget of 6 (6 runs, a churn-rate and a website-traffic @@ -228,9 +241,7 @@ def _check_threshold(expected: CatalogMetricAlert, actual_args: dict) -> bool: act_from = _to_number(actual_args.get("from_value", actual_args.get("fromValue"))) act_to = _to_number(actual_args.get("to_value", actual_args.get("toValue"))) return exp_from == act_from and exp_to == act_to - exp_thr = _to_number(expected.threshold) - act_thr = _to_number(actual_args.get("threshold")) - return exp_thr == act_thr + return _to_number(expected.threshold) == _to_number(actual_args.get("threshold")) def _check_trigger(expected: CatalogMetricAlert, actual_args: dict) -> bool: @@ -248,9 +259,8 @@ def _check_trigger(expected: CatalogMetricAlert, actual_args: dict) -> bool: return exp_trigger == act_api -def _check_filters(expected: CatalogMetricAlert, actual_args: dict) -> bool: - exp_filters = expected.filters - act_filters = actual_args.get("filters", actual_args.get("attribute_filters")) or [] +def _filters_match(exp_filters: list | str | None, act_filters: list) -> bool: + """``None`` asserts nothing, ``[]`` asserts no filter, a list must match entry by entry.""" if exp_filters is None: return True if not exp_filters: @@ -260,6 +270,11 @@ def _check_filters(expected: CatalogMetricAlert, actual_args: dict) -> bool: return _deep_subset(exp_filters, act_filters) +def _check_filters(expected: CatalogMetricAlert, actual_args: dict) -> bool: + act_filters = actual_args.get("filters", actual_args.get("attribute_filters")) or [] + return _filters_match(expected.filters, act_filters) + + def _attribute_label_ids(items: list, *, side: str) -> list[str]: """Canonicalise group-by entries to bare label ids, whatever spelling they arrive in. @@ -482,6 +497,65 @@ def _resolve_internal_recipient_ids(sdk: GoodDataSdk, emails: list[str]) -> set[ return ids +_AUTOMATION_PAGE_SIZE = 500 + + +def _automation_ids(host: str, token: str, workspace_id: str) -> set[str] | None: + """Ids of every automation of the workspace, page by page, or None when they cannot all be listed. + + A partial list would make an automation that existed before the run look new to the cleanup, + so any failure, including a page that adds nothing, returns None. + """ + ids: set[str] = set() + page = 0 + try: + while True: + resp = httpx.get( + f"{host.rstrip('/')}/api/v1/entities/workspaces/{workspace_id}/automations", + params={"size": _AUTOMATION_PAGE_SIZE, "page": page}, + headers={"Authorization": f"Bearer {token}"}, + timeout=30.0, + ) + resp.raise_for_status() + body = resp.json() + # A body without a `data` list is not an empty workspace: read as one, it would make + # an alert that existed before the run look new to the cleanup. + data = body.get("data") if isinstance(body, dict) else None + if not isinstance(data, list): + raise ValueError(f"page {page} has no data list") + page_ids = {item.get("id") for item in data if isinstance(item, dict)} + if len(page_ids) != len(data) or not all(isinstance(i, str) and i for i in page_ids): + raise ValueError(f"page {page} lists an automation without an id") + if len(data) < _AUTOMATION_PAGE_SIZE: + return ids | page_ids + if page_ids <= ids: + raise ValueError(f"page {page} repeats automations already listed") + ids |= page_ids + page += 1 + except Exception as exc: + print(f"[CLEANUP] Listing automations failed: {exc}") + return None + + +def _parsed_arguments(tc: ToolCallEvent) -> dict: + """A tool call's arguments, or ``{}`` when they are not a JSON object.""" + args = tc.parsed_arguments() + return args if isinstance(args, dict) else {} + + +def _named_automation_ids(tool_call_events: list[ToolCallEvent]) -> set[str]: + """``automation_id`` values the agent passed to the proposal and create tools. + + A create call can succeed on the server while its event is missing from the turn the + harness reads, so the id the agent named is the only handle left for cleanup. + """ + names = (_PROPOSAL_TOOL, "create_metric_alert") + # Filtered before the set: a rejected call may carry an unhashable value such as a list, and + # this runs in the cleanup, where an error would drop the run and the other named ids. + ids = (_parsed_arguments(tc).get("automation_id") for tc in tool_call_events if tc.function_name in names) + return {i for i in ids if isinstance(i, str) and i} + + def _resolve_current_user_id(host: str, token: str) -> str | None: """Id of the user that owns ``token``, or None if the profile cannot be read. @@ -575,13 +649,18 @@ def generate_simulated_alert_response( ) metric = expected.metric_id or "not specified" + # Rule 9 lists the values of the `any_of` combinations; rule 1 points there instead of + # stating the item's own value, which no combination may share. + in_rule_9 = "one of the combinations in rule 9" operator = expected.operator # BETWEEN / NOT_BETWEEN carry their value in threshold_from/threshold_to, so `threshold` is # None for them. Rule 3 asks the sim-user to verify the threshold, and reporting "not # specified" made it demand the agent delete both bounds of a BETWEEN condition — an # impossible request that burned every iteration without the alert ever being created. threshold: str | float | int - if expected.operator in ("BETWEEN", "NOT_BETWEEN") and ( + if any(_states_threshold(a) for a in expected.alternatives): + threshold = in_rule_9 + elif expected.operator in ("BETWEEN", "NOT_BETWEEN") and ( expected.threshold_from is not None or expected.threshold_to is not None ): threshold = f"between {expected.threshold_from} and {expected.threshold_to}" @@ -614,7 +693,9 @@ def generate_simulated_alert_response( " Do not wait for the agent to ask — state it alongside the metric and condition answers.\n" ) trigger = expected.trigger - filters = expected.filters + alternative_filters = any(a.filters is not None for a in expected.alternatives) + # The combinations' filters replace the item's own, so rule 1 must not state those as well. + filters = None if alternative_filters else expected.filters # "not specified" is the normalizer's stand-in for an absent trigger, which the product # persists as its ALWAYS default and `_check_trigger` asserts as ALWAYS. Both the cadence to @@ -630,7 +711,13 @@ def generate_simulated_alert_response( # share one: telling the sim-user "you want NO filters" on an unstated expectation makes it # refuse filters the request genuinely implies (e.g. "orders from the United States"), which # quietly turns that fixture into a weaker test rather than a failing one. - if filters: + if alternative_filters: + filters_rule = ( + "5. The filters you want depend on the combination in rule 9 you accept. Refuse any filter " + "that is not part of that combination. A combination that names no filters wants only the " + "filters your original request implies: do not invent a date window it never asked for.\n" + ) + elif filters: filters_rule = ( f"5. Your alert needs exactly these filters and NOTHING else: {filters}. " "If the agent offers, proposes or asks about any further date/time window, " @@ -745,12 +832,45 @@ def generate_simulated_alert_response( " Repeat it if the agent proposes a different cadence.\n" + period_rule + title_rule + + _alternatives_rule(expected) + "Reply concisely and directly." ) return _ask_simulated_user(openai_client, system_prompt, conversation_history, agent_message) +def _states_threshold(option: CatalogMetricAlert) -> bool: + return option.threshold is not None or option.threshold_from is not None or option.threshold_to is not None + + +def _alternatives_rule(expected: CatalogMetricAlert) -> str: + """Rule 9: the combinations an `any_of` item accepts, by their threshold and filters. + + Options differ in nothing else (``_ANY_OF_FIELDS``), so rules 1-8 state every other field. + """ + if not expected.alternatives: + return "" + combinations: list[str] = [] + for letter, option in zip(_COMBINATION_LABELS, expected.alternatives, strict=False): + parts: list[str] = [] + if option.threshold is not None: + parts.append(f"threshold={option.threshold}") + if option.threshold_from is not None or option.threshold_to is not None: + parts.append(f"threshold=between {option.threshold_from} and {option.threshold_to}") + if option.filters is not None: + parts.append(f"filters={json.dumps(option.filters)}" if option.filters else "filters=no filter at all") + # Never empty: options differ only in threshold and filters, and never repeat one another. + combinations.append(f"{letter}) " + ", ".join(parts)) + return ( + "9. Your goal can be met by any ONE of these combinations: " + + " | ".join(combinations) + + ". A value a combination names replaces the value rules 1-8 give for that field. " + "Accept a proposal only when it matches one combination in full. If it mixes fields of " + "different combinations or matches none, do NOT confirm: ask for the combination closest to " + "what the agent proposed. Never name a value yourself unless the agent asks for one.\n" + ) + + def _ask_simulated_user(openai_client: Any, system_prompt: str, conversation_history: list, agent_message: str) -> str: messages: list = [{"role": "system", "content": system_prompt}] messages.extend(conversation_history) @@ -815,6 +935,9 @@ class AlertEvaluation: alert_sensitivity_correct: bool | None = None alert_trigger_interval_correct: bool | None = None alert_date_dataset_correct: bool | None = None + # Scored on the first proposal, before the simulated user could correct it. + alert_initial_filters_correct: bool | None = None + alert_initial_recipients_correct: bool | None = None # Set only for an item that expects no alert; then it is the run's one check. alert_not_proposed: bool | None = None @@ -1038,8 +1161,118 @@ def _normalize_setup_turns(expected: dict) -> tuple[str, ...]: return tuple(turns) +def _normalize_check_first_proposal( + expected: dict, *, filters_stated: bool, recipients_stated: bool +) -> tuple[str, ...]: + """Fields also scored on the first proposal; each must be one the item states.""" + value = _case_insensitive_get(expected, *_CHECK_FIRST_PROPOSAL_KEYS) + if value is None: + return () + if not isinstance(value, list) or not value or any(field not in _FIRST_PROPOSAL_FIELDS for field in value): + raise ValueError( + f"check_first_proposal must be a non-empty list of {list(_FIRST_PROPOSAL_FIELDS)}, got {value!r}" + ) + if "filters" in value and not filters_stated: + raise ValueError("check_first_proposal names filters, but the item states no filter expectation") + if "recipients" in value and not recipients_stated: + raise ValueError("check_first_proposal names recipients, but the item states no recipients") + return tuple(dict.fromkeys(value)) + + +_ANY_OF_KEYS = ("any_of", "any of") +_CHECK_FIRST_PROPOSAL_KEYS = ("check_first_proposal", "check first proposal") +# Keys that describe the item, not one possible alert, so an `any_of` option may not state them. +_ITEM_LEVEL_KEYS = ( + *_ANY_OF_KEYS, + *_CHECK_FIRST_PROPOSAL_KEYS, + "setup_turns", + "setup turns", + "expect_alert", + "expect alert", +) +# Spellings of the filter expectation; an option stating one replaces all of them. +_FILTER_KEYS = ("filters", "time window/filters", "time_window") +# Every group of spellings the normalizer reads as one field; an option stating one spelling +# replaces the whole group, or the item's other spelling would be read instead of the option's. +_KEY_SPELLINGS = ( + _FILTER_KEYS, + ("recipients", "recipient(s)"), + ("metric_id", "metric"), + ("dashboard_id", "dashboard"), + ("granularity", "detection granularity"), + ("trigger_interval", "trigger interval"), + ("date_dataset_id", "date dataset"), +) + + +def _merge_option(base: dict, option: dict) -> dict: + """``base`` with ``option``'s keys replacing the same keys, compared case-insensitively.""" + replaced = {str(k).strip().lower() for k in option} + for spellings in _KEY_SPELLINGS: + if replaced & set(spellings): + replaced |= set(spellings) + return {**{k: v for k, v in base.items() if str(k).strip().lower() not in replaced}, **option} + + def _normalize_expected_output(expected: dict) -> CatalogMetricAlert: - """Parse expected_output dict into CatalogMetricAlert, accepting display-format or internal-format keys.""" + """Parse expected_output into CatalogMetricAlert; ``any_of`` lists complete alternatives. + + Each ``any_of`` option replaces some fields of the item (``Threshold``, ``Filters``, ...) and + keeps the rest, so fields that must agree (a threshold and the period it is measured over) + are matched together, never one from each option. + """ + options = _case_insensitive_get(expected, *_ANY_OF_KEYS) + if options is None: + return _normalize_single_expectation(expected) + if not isinstance(options, list) or len(options) < 2 or not all(isinstance(o, dict) and o for o in options): + raise ValueError(f"any_of must list at least two non-empty objects, got {options!r}") + if len(options) > len(_COMBINATION_LABELS): + raise ValueError(f"any_of lists {len(options)} options; the simulated user is told at most 26") + for option in options: + item_level = sorted(k for k in option if str(k).strip().lower() in _ITEM_LEVEL_KEYS) + if item_level: + raise ValueError(f"an any_of option may not state {item_level}; state them once for the item") + base = {k: v for k, v in expected.items() if str(k).strip().lower() not in _ANY_OF_KEYS} + if _normalize_expect_alert(base) is False: + raise ValueError("expect_alert: false states no alert, so it cannot list alternatives") + alternatives = tuple(_normalize_single_expectation(_merge_option(base, option)) for option in options) + _validate_alternatives(alternatives) + # The item is what the options share, not the base parsed on its own: the base alone may be + # incomplete (a trigger interval whose trigger only the options give), and a value every + # option replaces is no longer the goal. A field the options disagree on keeps its default; + # rule 9 states it per combination. + shared = { + f.name: getattr(alternatives[0], f.name) + for f in dataclasses.fields(CatalogMetricAlert) + if all(getattr(a, f.name) == getattr(alternatives[0], f.name) for a in alternatives[1:]) + } + return dataclasses.replace(CatalogMetricAlert(**shared), alternatives=alternatives) + + +def _validate_alternatives(alternatives: tuple[CatalogMetricAlert, ...]) -> None: + """Reject `any_of` options the simulated user could not tell apart or that would accept anything.""" + exempt = (*_ANY_OF_FIELDS, "alternatives") + differing = sorted( + f.name + for f in dataclasses.fields(CatalogMetricAlert) + if f.name not in exempt and any(getattr(a, f.name) != getattr(alternatives[0], f.name) for a in alternatives) + ) + if differing: + # Rules 1-8 state each other field once; the simulated user never sees a proposal's + # group-by at all. A field that differs would be asked for one way and scored another. + raise ValueError(f"any_of options may differ only in threshold and filters, not in {differing}") + for name, states in (("Threshold", _states_threshold), ("Filters", lambda a: a.filters is not None)): + if len({states(a) for a in alternatives}) > 1: + # An option without it inherits an unstated base and accepts any value: the mix any_of blocks. + raise ValueError(f"every any_of option must state {name}, or none may (Filters: [] means no filter)") + for i, option in enumerate(alternatives): + # A misspelled key ("Thresold") is ignored, which often leaves two options the same. + if option in alternatives[:i]: + raise ValueError(f"any_of option {i + 1} repeats an earlier one; is one of its keys misspelled?") + + +def _normalize_single_expectation(expected: dict) -> CatalogMetricAlert: + """Parse one expectation, accepting display-format or internal-format keys.""" operator = _case_insensitive_get(expected, "operator") or "GREATER_THAN" threshold = _case_insensitive_get(expected, "threshold") threshold_from = _case_insensitive_get(expected, "threshold_from") @@ -1076,6 +1309,9 @@ def _normalize_expected_output(expected: dict) -> CatalogMetricAlert: trigger_interval = _normalize_expected_enum(expected, _TRIGGER_INTERVALS, "trigger_interval", "trigger interval") date_dataset_id = _normalize_expected_text(expected, "date_dataset_id", "date dataset") setup_turns = _normalize_setup_turns(expected) + check_first_proposal = _normalize_check_first_proposal( + expected, filters_stated=filters is not None, recipients_stated=bool(recipients) + ) if trigger_interval is not None and trigger != "ONCE_PER_INTERVAL": raise ValueError(f"trigger_interval applies only to the ONCE_PER_INTERVAL trigger, got trigger {trigger!r}") # Both are ANOMALY arguments. On any other operator the agent sends neither, so a stated @@ -1102,6 +1338,7 @@ def _normalize_expected_output(expected: dict) -> CatalogMetricAlert: trigger_interval=trigger_interval, date_dataset_id=date_dataset_id, setup_turns=setup_turns, + check_first_proposal=check_first_proposal, ) @@ -1208,18 +1445,99 @@ def render_turn_for_simulated_user(chat_result: ChatResult) -> str: return "\n\n".join(chunk for chunk in chunks if chunk) +def _first_proposal(turns: list[list[ToolCallEvent]]) -> tuple[dict, dict] | None: + """Arguments and result of the first proposal the user was shown, or None when none succeeded. + + ``turns`` holds the tool calls of each scored turn, in order. The user sees only the last + proposal of a turn, so a proposal the agent replaced within the same turn is not the answer. + + The result carries the proposal's filters in the AFM form ``create_metric_alert`` receives + (``afm_filters``, empty when left out), the same form the expectation is written in. + """ + for events in turns: + shown: tuple[dict, dict] | None = None + for tc in events: + if tc.function_name != _PROPOSAL_TOOL: + continue + result = tc.parsed_result() + # gen-ai's result always names the stored proposal; its filters default to none. + if isinstance(result, dict) and result.get("success") is not False and result.get("alert_proposal_ref"): + shown = _parsed_arguments(tc), result + if shown is not None: + return shown + return None + + +def _proposal_recipient_args(proposal_args: dict) -> dict: + """A proposal's recipient list as the ``create_metric_alert`` arguments `_check_recipients` reads. + + The list may arrive JSON-encoded as a string, as the create tool's recipient lists do. + """ + raw = proposal_args.get("recipients") + if isinstance(raw, str): + try: + raw = json.loads(raw) + except ValueError: + raw = None + recipients = [r for r in raw if isinstance(r, dict)] if isinstance(raw, list) else [] + return { + "internal_recipients": [r["id"] for r in recipients if r.get("id")], + "external_recipients": [r["email"] for r in recipients if not r.get("id") and r.get("email")], + } + + +def _evaluate_first_proposal( + expected: CatalogMetricAlert, + turns: list[list[ToolCallEvent]], + sdk: GoodDataSdk, + current_user_id: str | None, +) -> tuple[bool | None, bool | None]: + """(filters, recipients) of the first proposal; ``None`` for a field the item does not name. + + The final alert can be right only because the simulated user corrected the first proposal. + These checks keep that first answer visible. + """ + if not expected.check_first_proposal: + return None, None + proposal = _first_proposal(turns) + filters_ok: bool | None = None + recipients_ok: bool | None = None + if "filters" in expected.check_first_proposal: + filters_ok = proposal is not None and _check_filters( + expected, {"filters": proposal[1].get("afm_filters") or []} + ) + if "recipients" in expected.check_first_proposal: + recipients_ok = proposal is not None and _check_recipients( + expected, _proposal_recipient_args(proposal[0]), sdk=sdk, current_user_id=current_user_id + ) + return filters_ok, recipients_ok + + def _evaluate_alert( expected: CatalogMetricAlert, actual_args: dict, alert_created: bool, sdk: GoodDataSdk, current_user_id: str | None, + proposal_turns: list[list[ToolCallEvent]] | None = None, ) -> AlertEvaluation: - """Score the created alert's arguments; a conditional check is ``None`` when the item states no value.""" + """Score the created alert's arguments; a conditional check is ``None`` when the item states no value. + + ``proposal_turns`` holds the tool calls of each scored turn, read for the first-proposal checks. + """ + + if expected.alternatives: + # The option the run comes closest to; a full match on any option passes. + evaluations = [ + _evaluate_alert(option, actual_args, alert_created, sdk, current_user_id, proposal_turns) + for option in expected.alternatives + ] + return max(evaluations, key=lambda ev: (ev.strict_pass, sum(ev.checks().values()))) def conditional(check: bool | None) -> bool | None: return None if check is None else alert_created and check + initial_filters, initial_recipients = _evaluate_first_proposal(expected, proposal_turns or [], sdk, current_user_id) return AlertEvaluation( alert_created=alert_created, operator_correct=alert_created and expected.operator == actual_args.get("operator"), @@ -1243,6 +1561,8 @@ def conditional(check: bool | None) -> bool | None: ), alert_trigger_interval_correct=conditional(_check_trigger_interval(expected, actual_args)), alert_date_dataset_correct=conditional(_check_date_dataset(expected, actual_args)), + alert_initial_filters_correct=initial_filters, + alert_initial_recipients_correct=initial_recipients, ) @@ -1265,7 +1585,7 @@ def run_agentic_alert_skill( raise ValueError(f"max_iterations must be at least 1, got {max_iterations}") expected = _normalize_expected_output(expected_output) current_user_id: str | None = None - # A no-alert item scores no recipients, so it needs no current user. + # A no-alert item scores no recipients, so it needs no current user. `any_of` options share theirs. if expected.expect_alert and _CURRENT_USER_RECIPIENT in expected.recipients: current_user_id = _resolve_current_user_id(host, token) if current_user_id is None: @@ -1294,6 +1614,13 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: metric_ids_to_delete.extend(_extract_created_metric_ids(events)) return calls + # Taken before the run, so cleanup by a named id never touches an alert the run did not create. + existing_ids = _automation_ids(host, token, workspace_id) + all_tool_call_events: list[ToolCallEvent] = [] + # Tool calls of each question and follow-up turn; setup turns are not scored. + scored_turns: list[list[ToolCallEvent]] = [] + # Events of turns that broke mid-stream: not part of the run's record, but they can name an alert. + partial_events: list[ToolCallEvent] = [] run_started = time.monotonic() try: alert_id: str | None = None @@ -1301,7 +1628,6 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: alert_created = False reasoning_steps: list[str] = [] response_id: str | None = None - all_tool_call_events: list[ToolCallEvent] = [] all_reasoning_step_events: list[ReasoningStepEvent] = [] turn_offset = 0.0 # each turn's call_ts/ts restarts near 0 -- shift by prior turns' wall time tool_index_offset = 0 @@ -1337,6 +1663,7 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: reasoning_steps.extend(partial.reasoning_steps or []) response_id = partial.response_id or response_id _track_writes(partial.tool_call_events or []) + partial_events.extend(partial.tool_call_events or []) exit_reason = chat_failure_exit(exc) iterations = 0 break @@ -1371,6 +1698,7 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: reasoning_steps.extend(partial.reasoning_steps or []) response_id = partial.response_id or response_id _track_writes(partial.tool_call_events or []) + partial_events.extend(partial.tool_call_events or []) exit_reason = chat_failure_exit(exc) break # `turns_used` counts attempts (set above, so a failed send still shows the @@ -1387,6 +1715,7 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: reasoning_index_offset=reasoning_index_offset, ) all_tool_call_events.extend(chat_result.tool_call_events or []) + scored_turns.append(list(chat_result.tool_call_events or [])) all_reasoning_step_events.extend(chat_result.reasoning_step_events or []) calls = _track_writes(chat_result.tool_call_events or []) tools_called = {tc.function_name for tc in chat_result.tool_call_events or []} @@ -1464,7 +1793,7 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: alert_not_proposed=declined, ) else: - ev = _evaluate_alert(expected, actual_args, alert_created, sdk, current_user_id) + ev = _evaluate_alert(expected, actual_args, alert_created, sdk, current_user_id, scored_turns) return AlertRunResult( conversation_id=conv_id, alert_id=alert_id, @@ -1483,6 +1812,12 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: finally: for created_id in dict.fromkeys(alert_ids_to_delete): _delete_alert(sdk, workspace_id, created_id) + unseen = _named_automation_ids(all_tool_call_events + partial_events) - set(alert_ids_to_delete) + if unseen and existing_ids is not None: + now = _automation_ids(host, token, workspace_id) or set() + for leaked_id in sorted((unseen & now) - existing_ids): + print(f"[CLEANUP] Deleting alert {leaked_id}: created, but its create call was not in the turn") + _delete_alert(sdk, workspace_id, leaked_id) # After the alerts: an alert on a created metric would keep that metric in use. for metric_id in dict.fromkeys(metric_ids_to_delete): _delete_metric(sdk, workspace_id, metric_id) @@ -1506,9 +1841,11 @@ def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: pass_at_k = any(r.eval.strict_pass for r in run_results) pass_power_k = all(r.eval.strict_pass for r in run_results) + # A passing run first: with `any_of`, a failing run scored against a larger option can have + # more true checks than a passing one. best = max( run_results, - key=lambda r: sum(r.eval.checks().values()), + key=lambda r: (r.eval.strict_pass, sum(r.eval.checks().values())), ) return AgenticAlertSummary( run_results=run_results, diff --git a/packages/gooddata-eval/tests/conftest.py b/packages/gooddata-eval/tests/conftest.py index 0f77eaa8a..5ced9a2c2 100644 --- a/packages/gooddata-eval/tests/conftest.py +++ b/packages/gooddata-eval/tests/conftest.py @@ -53,3 +53,10 @@ def _no_metric_delete_recheck(monkeypatch: pytest.MonkeyPatch) -> None: """A MagicMock SDK reports every metric as present, so the recheck would sleep and delete again. Tests of the recheck itself set the delays they need.""" monkeypatch.setattr("gooddata_eval.core.agentic.metric_skill._DELETE_RECHECK_DELAYS_S", ()) + + +@pytest.fixture(autouse=True) +def _no_alert_automation_listing(monkeypatch: pytest.MonkeyPatch) -> None: + """The alert runner lists the workspace's automations over HTTP before and after a run. + Tests have no server: an unlistable workspace skips the cleanup by named id.""" + monkeypatch.setattr("gooddata_eval.core.agentic.alert_skill._automation_ids", lambda *_args: None) diff --git a/packages/gooddata-eval/tests/test_agentic_alert_skill_checks.py b/packages/gooddata-eval/tests/test_agentic_alert_skill_checks.py index aa1aeef3f..cad23d501 100644 --- a/packages/gooddata-eval/tests/test_agentic_alert_skill_checks.py +++ b/packages/gooddata-eval/tests/test_agentic_alert_skill_checks.py @@ -13,10 +13,16 @@ AlertRunResult, _check_comparison, _evaluate_alert, + _named_automation_ids, _normalize_expected_output, + _proposal_recipient_args, generate_simulated_alert_response, run_agentic_alert_skill, ) +from gooddata_eval.core.agentic.alert_skill import ( + # Bound at import, before conftest replaces the module attribute for every test. + _automation_ids as _list_automation_ids, +) from gooddata_eval.core.chat.sse_client import ChatError from gooddata_eval.core.models import ChatResult, LoopExit @@ -531,3 +537,595 @@ def test_a_run_needs_at_least_one_turn(max_iterations: int) -> None: expected_output={"Expect alert": False}, max_iterations=max_iterations, ) + + +# --- any-of expectations and the first proposal ------------------------------------------------ + +_LAST_MONTH = { + "relativeDateFilter": { + "dataset": {"identifier": {"id": "date", "type": "dataset"}}, + "granularity": "MONTH", + "from": -1, + "to": -1, + } +} +_YEAR_2025 = { + "absoluteDateFilter": { + "dataset": {"identifier": {"id": "date", "type": "dataset"}}, + "from": "2025-01-01", + "to": "2025-12-31", + } +} +_US = { + "positiveAttributeFilter": { + "label": {"identifier": {"id": "customer_country", "type": "label"}}, + "in": {"values": ["United States"]}, + } +} +_BASE = { + "Metric": "Net Sales (net_sales)", + "Operator": "LESS_THAN", + "Threshold": "1000", + "Recipient(s)": "admin@gooddata.com", +} +# A chart by month: the UI's default (sum of the chart over its range) or the last completed month. +_DATE_CHART = { + **_BASE, + "any_of": [ + {"Threshold": 47136608.82, "Filters": [_YEAR_2025]}, + {"Threshold": 5428565.6, "Filters": [_LAST_MONTH]}, + ], +} +_CREATE_ARGS = { + "metric_id": "net_sales", + "operator": "LESS_THAN", + "threshold": 1000, + "external_recipients": ["admin@gooddata.com"], +} + + +@pytest.mark.parametrize( + ("threshold", "filters", "passes"), + [ + (47136608.82, [_YEAR_2025], True), + (5428565.6, [_LAST_MONTH], True), + # Each field matches an option, but not the same one: the sum of a year against one month. + (47136608.82, [_LAST_MONTH], False), + (5428565.6, [_YEAR_2025], False), + ], +) +def test_any_of_matches_fields_of_one_option_together(threshold: float, filters: list, passes: bool) -> None: + ev = _evaluate(_DATE_CHART, {**_CREATE_ARGS, "threshold": threshold, "filters": filters}) + assert ev.strict_pass is passes + + +def test_any_of_option_keeps_the_fields_it_does_not_state() -> None: + expected = {**_BASE, "Filters": [_US], "any_of": [{"Threshold": 1}, {"Threshold": 2}]} + ev = _evaluate(expected, {**_CREATE_ARGS, "threshold": 2, "filters": [_US], "metric_id": "other"}) + assert ev.checks()["threshold_correct"] is True + assert ev.checks()["filters_correct"] is True + assert ev.checks()["metric_correct"] is False + + +def test_any_of_option_replaces_every_spelling_of_the_filters() -> None: + expected = {**_BASE, "Time window/Filters": "None (All time)", "any_of": [{"Filters": [_US]}, {"Filters": []}]} + assert _evaluate(expected, {**_CREATE_ARGS, "filters": [_US]}).strict_pass + assert _evaluate(expected, {**_CREATE_ARGS, "filters": []}).strict_pass + assert not _evaluate(expected, {**_CREATE_ARGS, "filters": [_LAST_MONTH]}).strict_pass + + +@pytest.mark.parametrize( + "any_of", + [ + [{"Threshold": 1}], + [{"Threshold": 1}, {}], + [{"Threshold": 1}, "2"], + [{"Threshold": 1}, {"setup_turns": ["x"]}], + [{"Threshold": 1}, {"Sensitivity": "EXTREME"}], + [{"Threshold": 1}, {"Threshold": 2, "check_first_proposal": ["recipients"]}], + ], +) +def test_malformed_any_of_fails_early(any_of: list) -> None: + with pytest.raises(ValueError): + _normalize_expected_output({**_BASE, "any_of": any_of}) + + +def test_any_of_options_may_not_differ_in_group_by() -> None: + # The simulated user never sees a proposal's group-by, so it could not tell such options apart. + region = {"localIdentifier": "a0", "label": {"identifier": {"id": "region", "type": "label"}}} + with pytest.raises(ValueError, match=r"only in threshold and filters, not in \['attributes'\]"): + _normalize_expected_output( + {**_BASE, "any_of": [{"Threshold": 1, "Attributes": []}, {"Threshold": 2, "Attributes": [region]}]} + ) + # A group-by the options share stays allowed. + shared = _normalize_expected_output( + {**_BASE, "Attributes": [region], "any_of": [{"Threshold": 1}, {"Threshold": 2}]} + ) + assert shared.attributes is not None and len(shared.attributes) == 1 + + +@pytest.mark.parametrize( + "any_of", + [ + # Option 2 states no filters over a base that states none, so it would accept the year filter + # of option 1 with its own threshold. + [{"Threshold": 47136608.82, "Filters": [_YEAR_2025]}, {"Threshold": 5428565.6}], + [{"Threshold": 1, "Filters": []}, {"Filters": [_US]}], + ], +) +def test_every_any_of_option_states_threshold_and_filters_or_none_does(any_of: list) -> None: + base = {k: v for k, v in _BASE.items() if k != "Threshold"} + with pytest.raises(ValueError, match="every any_of option must state"): + _normalize_expected_output({**base, "any_of": any_of}) + + +def test_any_of_option_that_repeats_another_fails_early() -> None: + with pytest.raises(ValueError, match="repeats an earlier one"): + _normalize_expected_output({**_BASE, "any_of": [{"Threshold": 5}, {"Threshold": 6}, {"Threshold": 5}]}) + + +def test_any_of_options_with_a_misspelled_key_fail_early() -> None: + # "Thresold" is ignored, so each such option is the item itself. + with pytest.raises(ValueError, match="repeats an earlier one"): + _normalize_expected_output({**_BASE, "any_of": [{"Thresold": 5}, {"Threshold": 6}, {"Thresold": 7}]}) + # Over a base without a threshold, the misspelled option is the one that states none. + base = {k: v for k, v in _BASE.items() if k != "Threshold"} + with pytest.raises(ValueError, match="must state Threshold"): + _normalize_expected_output({**base, "any_of": [{"Thresold": 5}, {"Threshold": 6}]}) + + +def test_any_of_item_is_what_its_options_share() -> None: + # The base alone states an interval without its trigger; only the merged options are complete. + expected = _normalize_expected_output( + { + **_BASE, + "Trigger interval": "MONTH", + "any_of": [ + {"Trigger": "ONCE_PER_INTERVAL", "Threshold": 1}, + {"Trigger": "ONCE_PER_INTERVAL", "Threshold": 2}, + ], + } + ) + assert (expected.trigger, expected.trigger_interval) == ("ONCE_PER_INTERVAL", "MONTH") + # The options disagree on the threshold, so the item states none; rule 9 gives it per combination. + assert expected.threshold is None + assert [a.threshold for a in expected.alternatives] == [1, 2] + + +def test_any_of_option_with_a_trigger_that_breaks_the_interval_still_fails() -> None: + with pytest.raises(ValueError, match="ONCE_PER_INTERVAL"): + _normalize_expected_output( + { + **_BASE, + "Trigger interval": "MONTH", + "any_of": [{"Trigger": "ONCE_PER_INTERVAL", "Threshold": 1}, {"Trigger": "Every time", "Threshold": 2}], + } + ) + + +def test_no_alert_item_cannot_list_alternatives() -> None: + with pytest.raises(ValueError): + _normalize_expected_output({"Expect alert": False, "any_of": [{"Threshold": 1}, {"Threshold": 2}]}) + + +@pytest.mark.parametrize("extra", [{"check_first_proposal": ["metric"]}, {"check_first_proposal": []}]) +def test_malformed_first_proposal_key_fails_early(extra: dict) -> None: + with pytest.raises(ValueError): + _normalize_expected_output({**_BASE, "Filters": [_US], **extra}) + + +def test_first_proposal_check_needs_a_stated_expectation() -> None: + with pytest.raises(ValueError, match="no filter expectation"): + _normalize_expected_output({"Operator": "LESS_THAN", "check_first_proposal": ["filters"]}) + with pytest.raises(ValueError, match="no recipients"): + _normalize_expected_output({"Operator": "LESS_THAN", "Filters": [], "check_first_proposal": ["recipients"]}) + # Each option of an any_of item states the filters, so the check is valid there. + expected = {**_BASE, "check_first_proposal": ["filters"], "any_of": [{"Filters": [_US]}, {"Filters": []}]} + assert _normalize_expected_output(expected).check_first_proposal == ("filters",) + + +def _proposal_event(filters: list, recipients: list, *, success: bool = True, automation_id: str = "") -> dict: + return { + "functionName": "prepare_metric_alert_proposal", + "functionArguments": json.dumps({"recipients": recipients, "automation_id": automation_id or None}), + "result": json.dumps( + {"success": success, "alert_proposal_ref": "p1", "afm_filters": filters, "afm_attributes": []} + if success + else {"success": False, "error": "bad label"} + ), + } + + +def _events(*raw: dict) -> list: + return ChatResult.model_validate({"toolCallEvents": list(raw)}).tool_call_events + + +def test_first_proposal_is_scored_although_the_final_alert_is_right() -> None: + expected = {**_BASE, "Filters": [_LAST_MONTH, _US], "check_first_proposal": ["filters", "recipients"]} + turns = [ + _events(_proposal_event([_US], [{"label": "You", "id": "someone", "email": None}])), + _events(_proposal_event([_LAST_MONTH, _US], [{"label": "admin", "id": None, "email": "admin@gooddata.com"}])), + ] + ev = _evaluate_alert( + _normalize_expected_output(expected), + {**_CREATE_ARGS, "filters": [_LAST_MONTH, _US]}, + True, + MagicMock(), + None, + turns, + ) + checks = ev.checks() + assert checks["filters_correct"] is True + assert checks["recipients_correct"] is True + assert checks["alert_initial_filters_correct"] is False + assert checks["alert_initial_recipients_correct"] is False + assert not ev.strict_pass + + +def test_first_proposal_is_the_last_one_of_its_turn() -> None: + # The user is shown only the turn's last proposal; one the agent replaced before replying is not its answer. + expected = {**_BASE, "Filters": [_US], "check_first_proposal": ["filters"]} + turn = _events(_proposal_event([], []), _proposal_event([_US], [])) + ev = _evaluate_alert( + _normalize_expected_output(expected), {**_CREATE_ARGS, "filters": [_US]}, True, MagicMock(), None, [turn] + ) + assert ev.checks()["alert_initial_filters_correct"] is True + + +def test_first_proposal_uses_the_option_the_run_matches() -> None: + expected = {**_BASE, "check_first_proposal": ["filters"], "any_of": [{"Filters": [_US]}, {"Filters": []}]} + events = _events(_proposal_event([], [{"label": "admin", "id": None, "email": "admin@gooddata.com"}])) + ev = _evaluate_alert( + _normalize_expected_output(expected), {**_CREATE_ARGS, "filters": []}, True, MagicMock(), None, [events] + ) + assert ev.strict_pass + + +def test_first_proposal_ignores_the_other_tool_calls_of_its_turn() -> None: + expected = {**_BASE, "Filters": [_US], "check_first_proposal": ["filters"]} + turn = _events( + {"functionName": "search_objects", "functionArguments": "{}", "result": "{}"}, _proposal_event([_US], []) + ) + ev = _evaluate_alert( + _normalize_expected_output(expected), {**_CREATE_ARGS, "filters": [_US]}, True, MagicMock(), None, [turn] + ) + assert ev.checks()["alert_initial_filters_correct"] is True + + +def test_first_proposal_skips_a_rejected_call() -> None: + expected = {**_BASE, "Filters": [_US], "check_first_proposal": ["filters"]} + events = _events( + _proposal_event([], [], success=False), + _proposal_event([_US], [{"label": "admin", "id": None, "email": "admin@gooddata.com"}]), + ) + ev = _evaluate_alert( + _normalize_expected_output(expected), {**_CREATE_ARGS, "filters": [_US]}, True, MagicMock(), None, [events] + ) + assert ev.checks()["alert_initial_filters_correct"] is True + + +def test_first_proposal_reads_recipients_sent_as_a_json_string() -> None: + recipients = json.dumps([{"label": "admin", "id": None, "email": "admin@gooddata.com"}]) + assert _proposal_recipient_args({"recipients": recipients}) == { + "internal_recipients": [], + "external_recipients": ["admin@gooddata.com"], + } + assert _proposal_recipient_args({"recipients": "not json"}) == { + "internal_recipients": [], + "external_recipients": [], + } + + +def test_first_proposal_check_fails_without_any_proposal() -> None: + expected = {**_BASE, "Filters": [_US], "check_first_proposal": ["filters"]} + ev = _evaluate_alert( + _normalize_expected_output(expected), {**_CREATE_ARGS, "filters": [_US]}, True, MagicMock(), None, [] + ) + assert ev.checks()["alert_initial_filters_correct"] is False + + +def test_first_proposal_checks_are_absent_unless_requested() -> None: + checks = _evaluate({**_BASE, "Filters": [_US]}, {**_CREATE_ARGS, "filters": [_US]}).checks() + assert "alert_initial_filters_correct" not in checks + assert "alert_initial_recipients_correct" not in checks + assert {"alert_initial_filters_correct", "alert_initial_recipients_correct"} <= set(ALERT_CONDITIONAL_SCORES) + + +def test_simulated_user_is_told_every_combination() -> None: + fake_openai = MagicMock() + fake_openai.return_value.chat.completions.create.return_value = MagicMock( + choices=[MagicMock(message=MagicMock(content="ok"))] + ) + with ( + patch("gooddata_eval.core.agentic.alert_skill._OpenAI", fake_openai), + patch.dict("os.environ", {"OPENAI_API_KEY": "test-key"}), + ): + generate_simulated_alert_response("Confirm?", _normalize_expected_output(_DATE_CHART), [], question="q") + prompt = fake_openai.return_value.chat.completions.create.call_args.kwargs["messages"][0]["content"] + assert "any ONE of these combinations" in prompt + assert "A) threshold=47136608.82" in prompt + assert "B) threshold=5428565.6" in prompt + assert "absoluteDateFilter" in prompt + assert "matches one combination in full" in prompt + + +def _simulated_user_prompt(expected_output: dict) -> str: + fake_openai = MagicMock() + fake_openai.return_value.chat.completions.create.return_value = MagicMock( + choices=[MagicMock(message=MagicMock(content="ok"))] + ) + with ( + patch("gooddata_eval.core.agentic.alert_skill._OpenAI", fake_openai), + patch.dict("os.environ", {"OPENAI_API_KEY": "test-key"}), + ): + generate_simulated_alert_response("Confirm?", _normalize_expected_output(expected_output), [], question="q") + return fake_openai.return_value.chat.completions.create.call_args.kwargs["messages"][0]["content"] + + +@pytest.mark.parametrize( + "fields", + [ + {"Operator": "GREATER_THAN"}, + {"Recipient(s)": "me"}, + {"Trigger": "ONCE"}, + {"Metric": "Other (other)"}, + {"Dashboard": "D (d2)"}, + {"Title": "Low sales"}, + ], +) +def test_any_of_options_may_differ_only_in_threshold_and_filters(fields: dict) -> None: + # Rules 1-8 of the simulated user state every other field once, so a differing one would be + # asked for one way and scored another. + with pytest.raises(ValueError, match="only in threshold and filters"): + _normalize_expected_output({**_BASE, "any_of": [{"Threshold": 1}, {"Threshold": 2, **fields}]}) + + +def test_simulated_user_is_told_the_range_of_each_between_combination() -> None: + prompt = _simulated_user_prompt( + { + "Metric": "Net Sales (net_sales)", + "Operator": "BETWEEN", + "any_of": [{"threshold_from": 1, "threshold_to": 9}, {"threshold_from": 2, "threshold_to": 8}], + } + ) + assert "threshold=one of the combinations in rule 9" in prompt + assert "A) threshold=between 1 and 9 | B) threshold=between 2 and 8" in prompt + + +def test_simulated_user_goal_uses_a_value_every_combination_replaces() -> None: + prompt = _simulated_user_prompt( + { + **_BASE, + "Recipient(s)": "base@example.com", + "any_of": [ + {"Threshold": 1, "Recipient(s)": "alt@example.com"}, + {"Threshold": 2, "Recipient(s)": "alt@example.com"}, + ], + } + ) + assert "alt@example.com" in prompt + assert "base@example.com" not in prompt + + +def test_simulated_user_goal_omits_the_filters_the_combinations_replace() -> None: + prompt = _simulated_user_prompt({**_BASE, "Filters": [_US], "any_of": [{"Filters": []}, {"Threshold": 3}]}) + goal = next(line for line in prompt.splitlines() if line.startswith("1. ")) + assert "filters=" not in goal + assert "A) threshold=1000, filters=no filter at all" in prompt + + +def test_any_of_option_replaces_every_spelling_of_a_field() -> None: + expected = _normalize_expected_output( + { + "recipients": ["a@x.com"], + "metric_id": "net_sales", + "dashboard_id": "d1", + "any_of": [ + {"Recipient(s)": "b@x.com", "Metric": "Other (other)", "Dashboard": "D (d2)", "Threshold": n} + for n in (1, 2) + ], + } + ) + for option in expected.alternatives: + assert (option.recipients, option.metric_id, option.dashboard_id) == (["b@x.com"], "other", "d2") + + +def test_any_of_display_spelling_is_read() -> None: + expected = _normalize_expected_output({**_BASE, "Any of": [{"Threshold": 1}, {"Threshold": 2}]}) + assert [a.threshold for a in expected.alternatives] == [1, 2] + + +def test_any_of_option_for_the_current_user_resolves_it() -> None: + created = ChatResult.model_validate( + { + "text_response": "", + "toolCallEvents": [ + { + "functionName": "create_metric_alert", + "functionArguments": json.dumps( + {**_CREATE_ARGS, "external_recipients": [], "internal_recipients": ["u1"]} + ), + "result": '{"id": "alert-1"}', + } + ], + } + ) + client = MagicMock() + client.send_message.return_value = created + expected = {**_BASE, "Filters": [], "Recipient(s)": "me", "any_of": [{"Threshold": 1000}, {"Threshold": 5}]} + with patch("gooddata_eval.core.agentic.alert_skill._resolve_current_user_id", return_value="u1") as mock_resolve: + run, _, _ = _run(client, expected) + mock_resolve.assert_called_once() + assert run.eval.strict_pass + + +def test_too_many_any_of_options_fail_early() -> None: + with pytest.raises(ValueError, match="at most 26"): + _normalize_expected_output({**_BASE, "any_of": [{"Threshold": n} for n in range(27)]}) + + +def test_no_alert_item_may_not_check_a_first_proposal() -> None: + with pytest.raises(ValueError, match="expect_alert: false"): + _normalize_expected_output( + {"Expect alert": False, "Recipient(s)": "a@x.com", "check_first_proposal": ["recipients"]} + ) + + +def test_catalog_from_dict_reads_any_of_and_first_proposal_fields() -> None: + alert = CatalogMetricAlert.from_dict( + {"filters": [], "check_first_proposal": ["filters"], "any_of": [{"threshold": 1}, {"threshold": 2}]} + ) + assert alert.check_first_proposal == ("filters",) + assert [a.threshold for a in alert.alternatives] == [1, 2] + with pytest.raises(ValueError): + CatalogMetricAlert.from_dict({"filters": [], "check_first_proposal": "filters"}) + + +# --- cleanup of an alert whose create event never reached the harness --------------------------- + + +def test_named_ids_ignore_arguments_that_are_not_an_object() -> None: + events = _events({"functionName": "prepare_metric_alert_proposal", "functionArguments": "[1]", "result": "{}"}) + assert _named_automation_ids(events) == set() + + +def test_named_ids_skip_an_id_that_is_not_a_string() -> None: + # A list is unhashable; it must not stop the cleanup of the ids that are valid. + events = _events( + {"functionName": "prepare_metric_alert_proposal", "functionArguments": '{"automation_id": []}', "result": "{}"}, + {"functionName": "create_metric_alert", "functionArguments": '{"automation_id": 7}', "result": "{}"}, + { + "functionName": "create_metric_alert", + "functionArguments": '{"automation_id": "net-sales-below"}', + "result": "{}", + }, + ) + assert _named_automation_ids(events) == {"net-sales-below"} + + +@pytest.mark.parametrize( + "body", + [ + {}, + [], + {"data": None}, + {"data": "a1"}, + {"data": [{"id": "a1"}, {"name": "x"}]}, + {"data": [{"id": 5}]}, + {"data": [7]}, + ], +) +def test_a_malformed_automation_listing_lists_nothing(body: object) -> None: + # Read as an empty workspace, it would make an alert that existed before the run look new. + page = MagicMock(json=MagicMock(return_value=body)) + with patch("gooddata_eval.core.agentic.alert_skill.httpx.get", return_value=page): + assert _list_automation_ids("http://host", "tok", "ws1") is None + + +def test_an_empty_automation_listing_is_an_empty_workspace() -> None: + page = MagicMock(json=MagicMock(return_value={"data": []})) + with patch("gooddata_eval.core.agentic.alert_skill.httpx.get", return_value=page): + assert _list_automation_ids("http://host", "tok", "ws1") == set() + + +def test_best_run_is_a_passing_run() -> None: + expected = {**_BASE, "any_of": [{"Threshold": 1}, {"Threshold": 2}]} + + def created(args: dict) -> ChatResult: + return ChatResult.model_validate( + { + "toolCallEvents": [ + { + "functionName": "create_metric_alert", + "functionArguments": json.dumps(args), + "result": '{"id": "a"}', + } + ] + } + ) + + client = MagicMock() + client.create_conversation.return_value = "conv-2" + client.send_message.side_effect = [ + created({**_CREATE_ARGS, "threshold": 3}), + created({**_CREATE_ARGS, "threshold": 2}), + ] + with ( + patch("gooddata_eval.core.agentic.alert_skill.ChatClient", return_value=client), + patch("gooddata_eval.core.agentic.alert_skill._delete_alert"), + ): + summary = run_agentic_alert_skill( + host="http://host", + token="tok", + workspace_id="ws1", + question="q", + expected_output=expected, + k=2, + initial_conversation_id="conv-1", + ) + first, second = summary.run_results + assert not first.eval.strict_pass and second.eval.strict_pass + assert summary.best is second + + +def test_automation_listing_reads_every_page() -> None: + pages = [[{"id": f"a{n}"} for n in range(500)], [{"id": "last"}]] + responses = [MagicMock(json=MagicMock(return_value={"data": page})) for page in pages] + with patch("gooddata_eval.core.agentic.alert_skill.httpx.get", side_effect=responses) as mock_get: + ids = _list_automation_ids("http://host", "tok", "ws1") + assert ids is not None and len(ids) == 501 and "last" in ids + assert [c.kwargs["params"]["page"] for c in mock_get.call_args_list] == [0, 1] + + +def test_automation_listing_that_cannot_finish_lists_nothing() -> None: + # A server that ignores the page number returns the first page forever: a partial list is no list. + page = MagicMock(json=MagicMock(return_value={"data": [{"id": f"a{n}"} for n in range(500)]})) + with patch("gooddata_eval.core.agentic.alert_skill.httpx.get", return_value=page): + assert _list_automation_ids("http://host", "tok", "ws1") is None + + +def test_alert_named_in_a_proposal_is_deleted_when_its_create_event_is_missing() -> None: + proposal = ChatResult.model_validate( + { + "text_response": "", + "alertProposals": [{"cta": "Should I create this alert?"}], + "toolCallEvents": [_proposal_event([], [], automation_id="net-sales-below")], + } + ) + client = MagicMock() + client.send_message.side_effect = [proposal] + [_turn("Done.")] * 5 + with patch( + "gooddata_eval.core.agentic.alert_skill._automation_ids", + side_effect=[{"other-alert", "net-sales-below-old"}, {"other-alert", "net-sales-below"}], + ): + _, _, mock_delete = _run(client, {**_BASE, "Filters": []}) + assert [c.args[2] for c in mock_delete.call_args_list] == ["net-sales-below"] + + +def test_named_alert_that_existed_before_the_run_is_kept() -> None: + proposal = ChatResult.model_validate( + { + "text_response": "", + "alertProposals": [{"cta": "Should I create this alert?"}], + "toolCallEvents": [_proposal_event([], [], automation_id="shared-alert")], + } + ) + client = MagicMock() + client.send_message.side_effect = [proposal] + [_turn("Done.")] * 5 + with patch("gooddata_eval.core.agentic.alert_skill._automation_ids", return_value={"shared-alert"}): + _, _, mock_delete = _run(client, {**_BASE, "Filters": []}) + assert mock_delete.call_args_list == [] + + +def test_first_proposal_without_a_filters_key_reads_as_no_filter() -> None: + expected = {**_BASE, "Time window/Filters": "None (All time)", "check_first_proposal": ["filters", "recipients"]} + event = { + "functionName": "prepare_metric_alert_proposal", + "functionArguments": json.dumps({"recipients": [{"id": None, "email": "admin@gooddata.com"}]}), + "result": json.dumps({"success": True, "alert_proposal_ref": "p1"}), + } + ev = _evaluate_alert( + _normalize_expected_output(expected), {**_CREATE_ARGS, "filters": []}, True, MagicMock(), None, [_events(event)] + ) + assert ev.checks()["alert_initial_filters_correct"] is True + assert ev.checks()["alert_initial_recipients_correct"] is True