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 71f6c0a81..fcd94b43a 100644 --- a/packages/gooddata-eval/src/gooddata_eval/core/agentic/_catalog.py +++ b/packages/gooddata-eval/src/gooddata_eval/core/agentic/_catalog.py @@ -73,27 +73,21 @@ class CatalogMetricAlert: """Alert name the user asked for; ``None`` means unasserted.""" dashboard_id: str | None = None """Dashboard the alert must be bound to; ``None`` means unasserted.""" + expect_alert: bool = True + """``False``: the request names nothing the alert can monitor, so no alert may be proposed or created.""" + sensitivity: str | None = None + """ANOMALY sensitivity (LOW/MEDIUM/HIGH); ``None`` means unasserted.""" + trigger_interval: str | None = None + """Interval of a ONCE_PER_INTERVAL trigger (DAY/WEEK/MONTH/QUARTER/YEAR); ``None`` means unasserted.""" + date_dataset_id: str | None = None + """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.""" @classmethod def from_dict(cls, d: dict) -> CatalogMetricAlert: - """Build a CatalogMetricAlert from a canonical (lowercase-key) dict.""" - recipients_raw = d.get("recipients") or [] - if isinstance(recipients_raw, str): - recipients = [r.strip() for r in recipients_raw.replace(";", ",").split(",") if r.strip()] - else: - recipients = list(recipients_raw) - return cls( - operator=d.get("operator", "GREATER_THAN"), - threshold=d.get("threshold"), - threshold_from=d.get("threshold_from"), - threshold_to=d.get("threshold_to"), - trigger=d.get("trigger", "not specified"), - metric_id=d.get("metric_id"), - recipients=recipients, - filters=d.get("filters"), - attributes=d.get("attributes"), - granularity=AnomalyDetectionGranularity.parse(d.get("granularity")), - comparison=d.get("comparison"), - title=d.get("title"), - dashboard_id=d.get("dashboard_id"), - ) + """Build a CatalogMetricAlert with the parsing and validation a dataset item's expected_output gets.""" + # Imported here: alert_skill imports this module. + from gooddata_eval.core.agentic.alert_skill import _normalize_expected_output # noqa: PLC0415 + + return _normalize_expected_output(d) 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 c6fc75542..16d314e3f 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 @@ -41,6 +41,7 @@ submit_trace_scoring, utc_now, ) +from gooddata_eval.core.agentic.metric_skill import _delete_metric, _extract_created_metric_ids from gooddata_eval.core.chat.render import render_answer_text from gooddata_eval.core.chat.sse_client import ChatClient, ChatError from gooddata_eval.core.config import ReasoningEffort @@ -75,9 +76,10 @@ "date_dataset_id": "comparison_date_dataset_id", "date_attribute_id": "comparison_date_attribute_id", "arithmetic_operator": "arithmetic_operator", + "compare_metric_id": "compare_metric_id", } # Compared verbatim: object ids are case-sensitive, unlike the enum-valued properties. -_COMPARISON_ID_ARGS = ("comparison_date_dataset_id", "comparison_date_attribute_id") +_COMPARISON_ID_ARGS = ("comparison_date_dataset_id", "comparison_date_attribute_id", "compare_metric_id") # What create_metric_alert applies when the argument is left out, per its own contract. A # metric-to-metric comparison has no periods, so periods_ago defaults only for a period kind. _COMPARISON_ARG_DEFAULTS: dict[str, object] = {"comparison_periods_ago": 1, "arithmetic_operator": "DIFFERENCE"} @@ -97,7 +99,55 @@ "attributes_correct", "granularity_correct", ) -_CONDITIONAL_ALERT_CHECKS = ("comparison_correct", "title_correct", "dashboard_correct") +_CONDITIONAL_ALERT_CHECKS = ( + "comparison_correct", + "title_correct", + "dashboard_correct", + "alert_sensitivity_correct", + "alert_trigger_interval_correct", + "alert_date_dataset_correct", +) +# 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). +ALERT_CONDITIONAL_SCORES = (*_CONDITIONAL_ALERT_CHECKS, _NO_ALERT_CHECK) + +_SENSITIVITIES = ("LOW", "MEDIUM", "HIGH") +# What create_metric_alert applies to an ANOMALY alert when `sensitivity` is left out, per its own contract. +_DEFAULT_SENSITIVITY = "MEDIUM" +_TRIGGER_INTERVALS = ("DAY", "WEEK", "MONTH", "QUARTER", "YEAR") +# The tool whose call is the agent proposing an alert, whether or not the call succeeded. +_PROPOSAL_TOOL = "prepare_metric_alert_proposal" +# The metric skill's write tool. The alert prompt lets the agent offer a metric the workspace lacks. +_CREATE_METRIC_TOOL = "create_metric" +# A no-alert item asserts nothing about an alert, so stating any of these is a fixture mistake. +_ALERT_ONLY_KEYS = ( + "operator", + "threshold", + "threshold_from", + "threshold_to", + "filters", + "time window/filters", + "time_window", + "attributes", + "granularity", + "detection granularity", + "comparison", + "title", + "dashboard", + "dashboard_id", + "sensitivity", + "trigger_interval", + "trigger interval", + "date_dataset_id", + "date dataset", +) +# 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 +# request, gpt-5.6-luna): every agent answered in turn 1, confirmed it would not create the alert +# in turn 2, and spent turns 3-6 on pleasantries without a tool call. Three turns cover that with +# one to spare. +_NO_ALERT_MAX_ITERATIONS = 3 # Expected-recipient spellings that mean the owner of the API token. _CURRENT_USER_RECIPIENT = "me" @@ -358,6 +408,42 @@ def _check_dashboard(expected: CatalogMetricAlert, actual_args: dict) -> bool: return isinstance(actual, str) and actual == expected.dashboard_id +def _check_enum_arg(expected_value: str | None, actual_args: dict, arg: str, default: str | None = None) -> bool | None: + """Case-folded compare of an enum-valued argument; ``None`` when the fixture states no value. + + ``default`` is the value ``create_metric_alert`` applies when the argument is left out, so an + omitted argument is compared as that value. + """ + if expected_value is None: + return None + actual = actual_args.get(arg) + if actual is None: + actual = default + return isinstance(actual, str) and actual.strip().upper() == expected_value + + +def _check_trigger_interval(expected: CatalogMetricAlert, actual_args: dict) -> bool | None: + """The stated interval, on an alert that fires once per interval; ``None`` when unasserted. + + The tool applies ``trigger_interval`` only to ONCE_PER_INTERVAL and defaults the trigger to + ALWAYS. ``_check_trigger`` skips ANOMALY alerts, so an anomaly alert sent without that trigger + would otherwise score an interval it never uses. + """ + if expected.trigger_interval is None: + return None + actual_trigger = actual_args.get("trigger") or actual_args.get("triggerMode") or "ALWAYS" + if _TRIGGER_DISPLAY_TO_API.get(actual_trigger, actual_trigger) != "ONCE_PER_INTERVAL": + return False + return _check_enum_arg(expected.trigger_interval, actual_args, "trigger_interval") + + +def _check_date_dataset(expected: CatalogMetricAlert, actual_args: dict) -> bool | None: + """The ANOMALY detection dataset, compared verbatim; ``None`` when unasserted.""" + if expected.date_dataset_id is None: + return None + return actual_args.get("date_dataset_id") == expected.date_dataset_id + + def _check_metric(expected: CatalogMetricAlert, actual_args: dict) -> bool: if not expected.metric_id: return True @@ -483,6 +569,10 @@ def generate_simulated_alert_response( raise OSError("OPENAI_API_KEY environment variable is not set") openai_client = _OpenAI(api_key=api_key) + if not expected.expect_alert: + return _ask_simulated_user( + openai_client, _no_alert_sim_user_prompt(question), conversation_history, agent_message + ) metric = expected.metric_id or "not specified" operator = expected.operator @@ -533,6 +623,8 @@ def generate_simulated_alert_response( # ONCE/ONCE_PER_INTERVAL proposal that the assertion then failed. trigger_key = "ALWAYS" if trigger in _ALWAYS_TRIGGER_VALUES else trigger trigger_request = _TRIGGER_INSTRUCTIONS.get(trigger_key, f"set the trigger to {trigger}") + if expected.trigger_interval: + trigger_request += f", with a trigger interval of {expected.trigger_interval}" # Three branches, matching the three states of `expected.filters`. `[]` and `None` must not # share one: telling the sim-user "you want NO filters" on an unstated expectation makes it @@ -571,7 +663,10 @@ def generate_simulated_alert_response( "7. This is an ANOMALY alert. Anomaly detection REQUIRES a time granularity, and that " f"granularity is NOT a date filter. State it in your first reply and repeat it whenever " f"asked: use {granularity} granularity. Rule 5 constrains filters on the metric only — it " - "never applies to this detection interval, so never refuse to give one.\n" + "never applies to this detection interval, so never refuse to give one." + + (f" Use {expected.sensitivity} sensitivity." if expected.sensitivity else "") + + (f" Detect on the date dataset {expected.date_dataset_id}." if expected.date_dataset_id else "") + + "\n" ) elif operator in _RELATIVE_OPERATORS: # The item names no comparison date or period; delegating is an answer the agent accepts, @@ -582,7 +677,9 @@ def generate_simulated_alert_response( "year), and those are NOT a date filter. Rule 5 constrains filters on the metric only — " "it never applies to the comparison. If the agent asks which date or which period the " "comparison should use, do not refuse and do not name one yourself: hand the choice back, " - "for example 'Pick whichever date and period fit best.'\n" + "for example 'Pick whichever date and period fit best.' A proposal's filters may list one " + "relative date filter for that comparison date: it is the comparison period, not a time " + "window, so never refuse it.\n" ) else: period_rule = ( @@ -591,6 +688,9 @@ def generate_simulated_alert_response( ) original_request = f'Your original request to the agent was: "{question}"\n' if question else "" + if expected.setup_turns: + earlier = " / ".join(f'"{turn}"' for turn in expected.setup_turns) + original_request = f"Earlier in this conversation you asked: {earlier}.\n" + original_request # The agent may offer several metrics and describe one as ignoring filters or counting all # time. That reads like rule 5's "no filters ... all time", which made the simulated user @@ -624,6 +724,9 @@ def generate_simulated_alert_response( f"1. Your goal: metric={metric}, operator={operator}, threshold={threshold}, " f"recipients={recipients}, trigger={trigger_key}" + (f", filters={filters}" if filters else "") + + (f", sensitivity={expected.sensitivity}" if expected.sensitivity else "") + + (f", trigger interval={expected.trigger_interval}" if expected.trigger_interval else "") + + (f", date dataset={expected.date_dataset_id}" if expected.date_dataset_id else "") + ".\n" + metric_choice_rule + "2. Never revert or change a decision that was already confirmed in a previous turn.\n" @@ -645,6 +748,10 @@ def generate_simulated_alert_response( + "Reply concisely and directly." ) + return _ask_simulated_user(openai_client, system_prompt, conversation_history, agent_message) + + +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) messages.append( @@ -658,6 +765,24 @@ def generate_simulated_alert_response( return response.choices[0].message.content or "" +def _no_alert_sim_user_prompt(question: str) -> str: + """The simulated user of an item whose request names nothing the workspace can monitor. + + It never accepts a substitute: the check is whether the agent proposes one anyway. + """ + return ( + "You are a user who asked an AI agent to create an alert.\n" + f'Your original request to the agent was: "{question}"\n' + "The workspace may not have what you asked for. Rules you MUST follow:\n" + "1. You want an alert only on exactly what you asked for. Never accept a different or similar metric, " + "never ask the agent to create a new metric, and never confirm an alert proposal.\n" + "2. If the agent offers alternatives or asks which one to use, decline them all and say you only wanted " + "what you asked for.\n" + "3. If the agent says it cannot create the alert, accept that and end politely.\n" + "Reply concisely and directly." + ) + + def _delete_alert(sdk: GoodDataSdk, workspace_id: str, alert_id: str) -> None: """Best-effort delete of an alert (automation) created during evaluation. @@ -687,6 +812,11 @@ class AlertEvaluation: comparison_correct: bool | None = None title_correct: bool | None = None dashboard_correct: bool | None = None + alert_sensitivity_correct: bool | None = None + alert_trigger_interval_correct: bool | None = None + alert_date_dataset_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 def checks(self) -> dict[str, bool]: """Every check that ran, by name; strict_pass requires all of them. @@ -695,6 +825,8 @@ def checks(self) -> dict[str, bool]: not report a ``title_correct`` it was never scored on, and a refusal on it is not counted as a title failure. """ + if self.alert_not_proposed is not None: + return {_NO_ALERT_CHECK: self.alert_not_proposed} ran: dict[str, bool] = {name: getattr(self, name) for name in _ALERT_CHECKS} for name in _CONDITIONAL_ALERT_CHECKS: value = getattr(self, name) @@ -829,6 +961,9 @@ def _normalize_expected_comparison(expected: dict) -> dict[str, str | int | floa if arg is None: # Validated here so a misspelled property fails before the run spends an API call. raise ValueError(f"Unrecognised comparison property {key!r}; expected one of {sorted(_COMPARISON_ARGS)}") + if arg in _COMPARISON_ID_ARGS and value is not None and not isinstance(value, str): + # str() would turn ["net_sales_goal"] into an id no tool argument can ever equal. + raise ValueError(f"Comparison {key} must be an object id string, got {value!r}") canonical = _canonical_comparison_value(arg, value) if canonical is None and arg == "comparison_periods_ago" and str(value or "").strip(): raise ValueError(f"Comparison periods_ago must be a number, got {value!r}") @@ -868,6 +1003,41 @@ def _normalize_expected_dashboard(expected: dict) -> str | None: return parsed +def _normalize_expected_enum(expected: dict, allowed: tuple[str, ...], *keys: str) -> str | None: + """A stated enum value, upper-cased; raises on a value outside ``allowed`` so a typo never reads green.""" + value = _normalize_expected_text(expected, *keys) + if value is None: + return None + canonical = value.upper() + if canonical not in allowed: + raise ValueError(f"{keys[0]} expectation must be one of {list(allowed)}, got {value!r}") + return canonical + + +def _normalize_expect_alert(expected: dict) -> bool: + """``Expect alert: false`` marks an item whose request must end without any alert.""" + value = _case_insensitive_get(expected, "expect_alert", "expect alert") + if value is None: + return True + if not isinstance(value, bool): + raise ValueError(f"expect_alert must be true or false, got {value!r}") + if not value: + stated = sorted(k for k in expected if str(k).strip().lower() in _ALERT_ONLY_KEYS) + if stated: + raise ValueError(f"expect_alert: false states no alert, but the item also states {stated}") + return value + + +def _normalize_setup_turns(expected: dict) -> tuple[str, ...]: + """Messages sent before the question; each one a non-empty string.""" + turns = _case_insensitive_get(expected, "setup_turns", "setup turns") + if turns is None: + return () + if not isinstance(turns, list) or not all(isinstance(t, str) and t.strip() for t in turns): + raise ValueError(f"setup_turns must be a list of non-empty strings, got {turns!r}") + return tuple(turns) + + def _normalize_expected_output(expected: dict) -> CatalogMetricAlert: """Parse expected_output dict into CatalogMetricAlert, accepting display-format or internal-format keys.""" operator = _case_insensitive_get(expected, "operator") or "GREATER_THAN" @@ -901,6 +1071,17 @@ def _normalize_expected_output(expected: dict) -> CatalogMetricAlert: comparison = _normalize_expected_comparison(expected) title = _normalize_expected_text(expected, "title") dashboard_id = _normalize_expected_dashboard(expected) + expect_alert = _normalize_expect_alert(expected) + sensitivity = _normalize_expected_enum(expected, _SENSITIVITIES, "sensitivity") + 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) + 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 + # MEDIUM would always match the default and only raise the quality score. + if (sensitivity is not None or date_dataset_id is not None) and operator != "ANOMALY": + raise ValueError(f"sensitivity and date_dataset_id apply only to the ANOMALY operator, got {operator!r}") return CatalogMetricAlert( operator=operator, @@ -916,6 +1097,11 @@ def _normalize_expected_output(expected: dict) -> CatalogMetricAlert: comparison=comparison, title=title, dashboard_id=dashboard_id, + expect_alert=expect_alert, + sensitivity=sensitivity, + trigger_interval=trigger_interval, + date_dataset_id=date_dataset_id, + setup_turns=setup_turns, ) @@ -958,12 +1144,11 @@ class _AlertCalls: def _extract_alert_calls(tool_call_events: list[ToolCallEvent]) -> _AlertCalls: - """Read every ``create_metric_alert`` call, not only the first. + """Read every ``create_metric_alert`` call of a turn. - The agent may retry after an error. Reading only the first call scored the arguments of a - call that failed and missed the id of the retry that succeeded, so that alert leaked into - the workspace (QA-29886). The run is scored on the first call that did not fail, which is - the first call whenever none failed, as before. + The agent may retry after an error. A call that failed created nothing, so the run is scored + on the first call that did not fail, and the id of every alert a call created is collected so + that alert is deleted after the run. """ calls = _AlertCalls() for tc in tool_call_events: @@ -998,7 +1183,7 @@ def render_alert_proposal(proposal: dict) -> str: summary = {k: v for k, v in proposal.items() if k != "cta"} alert = dict(summary.get("alert") or {}) # `execution` is the only place a proposal carries its filters, which rule 3 of the simulated - # user checks. Hoisting them keeps them visible (QA-29886). The rest stays out: the measures + # user checks. Hoisting them keeps them visible. The rest stays out: the measures # repeat `condition`, and the simulated user is told nothing about group-bys, so a date # group-by would only read as a time window rule 5 tells it to refuse. execution = alert.pop("execution", None) @@ -1023,6 +1208,44 @@ def render_turn_for_simulated_user(chat_result: ChatResult) -> str: return "\n\n".join(chunk for chunk in chunks if chunk) +def _evaluate_alert( + expected: CatalogMetricAlert, + actual_args: dict, + alert_created: bool, + sdk: GoodDataSdk, + current_user_id: str | None, +) -> AlertEvaluation: + """Score the created alert's arguments; a conditional check is ``None`` when the item states no value.""" + + def conditional(check: bool | None) -> bool | None: + return None if check is None else alert_created and check + + return AlertEvaluation( + alert_created=alert_created, + operator_correct=alert_created and expected.operator == actual_args.get("operator"), + threshold_correct=alert_created and _check_threshold(expected, actual_args), + trigger_correct=alert_created and _check_trigger(expected, actual_args), + filters_correct=alert_created and _check_filters(expected, actual_args), + metric_correct=alert_created and _check_metric(expected, actual_args), + recipients_correct=alert_created + and _check_recipients(expected, actual_args, sdk=sdk, current_user_id=current_user_id), + attributes_correct=alert_created and _check_attributes(expected, actual_args), + granularity_correct=alert_created and _check_granularity(expected, actual_args), + comparison_correct=None + if expected.comparison is None + else alert_created and _check_comparison(expected, actual_args), + title_correct=None if expected.title is None else alert_created and _check_title(expected, actual_args), + dashboard_correct=None + if expected.dashboard_id is None + else alert_created and _check_dashboard(expected, actual_args), + alert_sensitivity_correct=conditional( + _check_enum_arg(expected.sensitivity, actual_args, "sensitivity", default=_DEFAULT_SENSITIVITY) + ), + alert_trigger_interval_correct=conditional(_check_trigger_interval(expected, actual_args)), + alert_date_dataset_correct=conditional(_check_date_dataset(expected, actual_args)), + ) + + def run_agentic_alert_skill( host: str, token: str, @@ -1037,9 +1260,13 @@ def run_agentic_alert_skill( user_context: dict | None = None, ) -> AgenticAlertSummary: """Run the alert-skill agentic evaluation K times and return a summary.""" + if max_iterations < 1: + # With no turn the agent never gets the question, and a no-alert item would pass unasked. + 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 - if _CURRENT_USER_RECIPIENT in expected.recipients: + # A no-alert item scores no recipients, so it needs no current user. + 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: # A harness fault: report it as an item error, not as the agent's recipient mistake. @@ -1057,6 +1284,16 @@ def run_agentic_alert_skill( def _run_once(conv_id: str) -> AlertRunResult: alert_ids_to_delete: list[str] = [] + # A metric the agent created stays in the shared workspace, and the item's next run would find it. + metric_ids_to_delete: list[str] = [] + + def _track_writes(events: list[ToolCallEvent]) -> _AlertCalls: + """The turn's alert calls; records every alert and metric it created for cleanup.""" + calls = _extract_alert_calls(events) + alert_ids_to_delete.extend(calls.created_ids) + metric_ids_to_delete.extend(_extract_created_metric_ids(events)) + return calls + run_started = time.monotonic() try: alert_id: str | None = None @@ -1080,8 +1317,43 @@ def _run_once(conv_id: str) -> AlertRunResult: # that simply runs out of range() is correctly labelled without a trailing else. exit_reason = LoopExit.BUDGET_EXHAUSTED turns_used = 0 + no_alert = not expected.expect_alert + # A no-alert run ends when the agent proposes anything; the simulated user only declines. + iterations = min(max_iterations, _NO_ALERT_MAX_ITERATIONS) if no_alert else max_iterations + proposed = False + # Whether the agent wrote anything in a scored turn; a no-alert run may end quiet after declining. + answered = False run_started = time.monotonic() - for _iteration in range(max_iterations): + # Setup turns set the scene (a chart to alert on) and are not scored. A failed one + # ends the run as the agent's failure, like a failed question turn. + for number, turn in enumerate(expected.setup_turns, start=1): + try: + setup = client.send_message(conv_id, turn) + except ChatError as exc: + print(f"[CHAT] setup turn {number} failed for conversation {conv_id}: {exc}") + # A broken stream can still have created an alert server-side. + partial = exc.partial_result + if partial is not None: + reasoning_steps.extend(partial.reasoning_steps or []) + response_id = partial.response_id or response_id + _track_writes(partial.tool_call_events or []) + exit_reason = chat_failure_exit(exc) + iterations = 0 + break + steps += setup.reasoning_step_count + reasoning_steps.extend(setup.reasoning_steps or []) + turn_offset, tool_index_offset, reasoning_index_offset = shift_and_index_events( + setup, + turn_offset=turn_offset, + tool_index_offset=tool_index_offset, + reasoning_index_offset=reasoning_index_offset, + ) + all_tool_call_events.extend(setup.tool_call_events or []) + all_reasoning_step_events.extend(setup.reasoning_step_events or []) + _track_writes(setup.tool_call_events or []) + conversation_history.append({"role": "user", "content": turn}) + conversation_history.append({"role": "assistant", "content": render_answer_text(setup)}) + for _iteration in range(iterations): turns_used = _iteration + 1 try: chat_result = client.send_message(conv_id, current_question) @@ -1098,7 +1370,7 @@ def _run_once(conv_id: str) -> AlertRunResult: if partial is not None: reasoning_steps.extend(partial.reasoning_steps or []) response_id = partial.response_id or response_id - alert_ids_to_delete.extend(_extract_alert_calls(partial.tool_call_events or []).created_ids) + _track_writes(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 @@ -1116,8 +1388,18 @@ def _run_once(conv_id: str) -> AlertRunResult: ) all_tool_call_events.extend(chat_result.tool_call_events or []) all_reasoning_step_events.extend(chat_result.reasoning_step_events or []) - calls = _extract_alert_calls(chat_result.tool_call_events or []) - alert_ids_to_delete.extend(calls.created_ids) + calls = _track_writes(chat_result.tool_call_events or []) + tools_called = {tc.function_name for tc in chat_result.tool_call_events or []} + substitute = _PROPOSAL_TOOL in tools_called or _CREATE_METRIC_TOOL in tools_called + if no_alert and (calls.called or substitute or chat_result.alert_proposals): + # Nothing in the request can be monitored, so any proposal is a substitute the + # user never agreed to. A proposal call the tool rejected leaves no proposal + # part, but the agent still tried to propose one. A metric the agent creates + # is a substitute too: the simulated user never asks for one. + proposed = True + actual_args = calls.args + exit_reason = LoopExit.SUCCESS + break if calls.created: alert_id, actual_args = calls.alert_id, calls.args alert_created = True @@ -1136,8 +1418,9 @@ def _run_once(conv_id: str) -> AlertRunResult: if not response_text and (calls.called or not chat_result.tool_call_events): exit_reason = LoopExit.AGENT_SILENT break + answered = answered or bool(response_text) # Stop before generating a follow-up for the last iteration - if _iteration >= max_iterations - 1: + if _iteration >= iterations - 1: break # Recorded rather than raised, matching metric_skill and kda_skill. Letting it # propagate did keep a harness fault from being scored as a content failure, @@ -1161,25 +1444,27 @@ def _run_once(conv_id: str) -> AlertRunResult: conversation_history.append({"role": "user", "content": follow_up}) current_question = follow_up - ev = AlertEvaluation( - alert_created=alert_created, - operator_correct=alert_created and expected.operator == actual_args.get("operator"), - threshold_correct=alert_created and _check_threshold(expected, actual_args), - trigger_correct=alert_created and _check_trigger(expected, actual_args), - filters_correct=alert_created and _check_filters(expected, actual_args), - metric_correct=alert_created and _check_metric(expected, actual_args), - recipients_correct=alert_created - and _check_recipients(expected, actual_args, sdk=sdk, current_user_id=current_user_id), - attributes_correct=alert_created and _check_attributes(expected, actual_args), - granularity_correct=alert_created and _check_granularity(expected, actual_args), - comparison_correct=None - if expected.comparison is None - else alert_created and _check_comparison(expected, actual_args), - title_correct=None if expected.title is None else alert_created and _check_title(expected, actual_args), - dashboard_correct=None - if expected.dashboard_id is None - else alert_created and _check_dashboard(expected, actual_args), - ) + if no_alert: + # Only a run that spent its whole budget without a proposal shows the agent + # declining. A run cut short by a chat failure or a failed simulated user never + # gave the agent the turns to propose one. An agent that declined and then had + # nothing to add to the user's "OK, thanks" ends silent, and that is a decline + # too; one silent from its first turn never answered the request. + declined = not proposed and ( + exit_reason == LoopExit.BUDGET_EXHAUSTED or (exit_reason == LoopExit.AGENT_SILENT and answered) + ) + ev = AlertEvaluation( + alert_created=False, + operator_correct=False, + threshold_correct=False, + trigger_correct=False, + filters_correct=False, + metric_correct=False, + recipients_correct=False, + alert_not_proposed=declined, + ) + else: + ev = _evaluate_alert(expected, actual_args, alert_created, sdk, current_user_id) return AlertRunResult( conversation_id=conv_id, alert_id=alert_id, @@ -1198,6 +1483,9 @@ def _run_once(conv_id: str) -> AlertRunResult: finally: for created_id in dict.fromkeys(alert_ids_to_delete): _delete_alert(sdk, workspace_id, created_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) try: conv_id_0 = initial_conversation_id if initial_conversation_id is not None else client.create_conversation() @@ -1364,18 +1652,13 @@ def _write_scores(ctx: RunTraceContext) -> None: best = summary.best ev = best.eval + # The checks that ran, not a fixed field list: quality_score is the share of true booleans + # here, so a no-alert item must not carry the alert checks it was never scored on, and a + # stated conditional check must count. detail = agentic_detail( best.tool_call_events, best.reasoning_step_events, - alert_created=ev.alert_created, - operator_correct=ev.operator_correct, - threshold_correct=ev.threshold_correct, - trigger_correct=ev.trigger_correct, - filters_correct=ev.filters_correct, - metric_correct=ev.metric_correct, - recipients_correct=ev.recipients_correct, - attributes_correct=ev.attributes_correct, - granularity_correct=ev.granularity_correct, + **ev.checks(), actual_alert_arguments=best.actual_alert_arguments, # Why the loop stopped. alert_created=False alone cannot tell a refusal from a run # that hit max_iterations while still on track -- see LoopExit. @@ -1397,12 +1680,7 @@ def _write_scores(ctx: RunTraceContext) -> None: raise_agentic_failure( AlertSkillAssertionError, f"Alert skill assertion failed. {gate_note} strict_pass={ev.strict_pass}. " - f"alert_created={ev.alert_created}, operator_correct={ev.operator_correct}, " - f"threshold_correct={ev.threshold_correct}, trigger_correct={ev.trigger_correct}, " - f"filters_correct={ev.filters_correct}, metric_correct={ev.metric_correct}, " - f"recipients_correct={ev.recipients_correct}, " - f"attributes_correct={ev.attributes_correct}, " - f"granularity_correct={ev.granularity_correct}. " + f"Checks: {ev.checks()}. " f"Actual args: {best.actual_alert_arguments}", harness_error=run_labels.harness_error_record, shadow=run_labels.shadow(gate), diff --git a/packages/gooddata-eval/tests/test_agentic_alert_skill.py b/packages/gooddata-eval/tests/test_agentic_alert_skill.py index c85620c9c..d98866185 100644 --- a/packages/gooddata-eval/tests/test_agentic_alert_skill.py +++ b/packages/gooddata-eval/tests/test_agentic_alert_skill.py @@ -890,12 +890,15 @@ def _run_turns(*turns: ChatResult | ChatError, max_iterations: int = 1) -> tuple max_iterations=max_iterations, initial_conversation_id="conv-1", ) + sent = [c.args for c in client.send_message.call_args_list] + question = "Notify me whenever the number of orders goes above 500" + assert sent == [("conv-1", question)] + [("conv-1", "Please try again.")] * (len(sent) - 1) return summary.best, [c.args[2] for c in mock_delete.call_args_list] @pytest.mark.parametrize("error", [_MCP_ERROR, _REGISTRY_ERROR], ids=["mcp_error", "registry_error"]) def test_retry_after_failed_create_scores_and_deletes_the_alert_it_created(error: str) -> None: - # The first call errored; reading only it leaked the retry's alert and scored failed arguments. + # The first call errored: the retry's alert is the one scored and deleted. run, deleted = _run_turns(_turn(_create_call(error, threshold=1), _create_call('{"id": "alert-2"}'))) assert run.eval.alert_created is True assert run.eval.threshold_correct is True @@ -911,7 +914,7 @@ def test_a_failed_call_after_a_created_alert_does_not_replace_its_arguments() -> def test_the_first_created_alert_is_scored_and_every_one_is_deleted() -> None: - # As before QA-29886: when no call failed, the run is scored on the first one. + # When no call failed, the run is scored on the first one. run, deleted = _run_turns( _turn(_create_call('{"id": "alert-1"}'), _create_call('{"data": {"id": "alert-2"}}', threshold=7)) ) @@ -960,7 +963,7 @@ def test_the_run_ends_when_the_agent_says_nothing_after_every_create_call_failed @pytest.mark.parametrize("result", [None, "Alert created."], ids=["no_result", "not_json"]) def test_a_create_call_whose_result_cannot_be_read_still_counts_as_created(result: str | None) -> None: - # Kept as before QA-29886: an unusual result shape must not turn into a false failure. + # An unusual result shape must not turn into a false failure. # Such a result carries no id, so there is nothing to delete. run, deleted = _run_turns(_turn(_create_call(result))) assert run.eval.alert_created is True @@ -970,7 +973,7 @@ def test_a_create_call_whose_result_cannot_be_read_still_counts_as_created(resul def test_a_broken_stream_deletes_only_the_alerts_its_create_calls_created() -> None: - # The cleanup of the chat-error path reads every call too, not only the first: a failed call + # The cleanup of the chat-error path reads every call too: a failed call # created nothing to delete, and its retry's alert must not leak. partial = _turn(_create_call(_REGISTRY_ERROR, threshold=1), _create_call('{"id": "alert-2"}'), text="") run, deleted = _run_turns(ChatError("stream died after create", partial_result=partial)) diff --git a/packages/gooddata-eval/tests/test_agentic_alert_skill_checks.py b/packages/gooddata-eval/tests/test_agentic_alert_skill_checks.py new file mode 100644 index 000000000..aa1aeef3f --- /dev/null +++ b/packages/gooddata-eval/tests/test_agentic_alert_skill_checks.py @@ -0,0 +1,533 @@ +# (C) 2026 GoodData Corporation. All rights reserved. +# SPDX-License-Identifier: LicenseRef-GoodData-Enterprise +"""Opt-in alert-skill checks: no-alert items, anomaly sensitivity/interval/dataset, metric comparison, setup turns.""" + +import json +from unittest.mock import MagicMock, patch + +import pytest +from gooddata_eval.core.agentic._catalog import CatalogMetricAlert +from gooddata_eval.core.agentic.alert_skill import ( + ALERT_CONDITIONAL_SCORES, + AlertEvaluation, + AlertRunResult, + _check_comparison, + _evaluate_alert, + _normalize_expected_output, + generate_simulated_alert_response, + run_agentic_alert_skill, +) +from gooddata_eval.core.chat.sse_client import ChatError +from gooddata_eval.core.models import ChatResult, LoopExit + +_ANOMALY = { + "Metric": "Total Sales (total_sales)", + "Operator": "ANOMALY", + "Trigger": "ONCE_PER_INTERVAL", + "Granularity": "MONTH", + "Recipient(s)": "admin@gooddata.com", +} +# A create_metric_alert argument set as the agent sends it for the anomaly item above. +_ANOMALY_ARGS = { + "metric_id": "total_sales", + "operator": "ANOMALY", + "granularity": "MONTH", + "sensitivity": "HIGH", + "date_dataset_id": "order_date", + "trigger": "ONCE_PER_INTERVAL", + "trigger_interval": "MONTH", + "external_recipients": ["admin@gooddata.com"], +} + + +def _evaluate(expected_output: dict, args: dict) -> AlertEvaluation: + return _evaluate_alert(_normalize_expected_output(expected_output), args, True, MagicMock(), None) + + +# --- parsing --------------------------------------------------------------------------------- + + +def test_new_keys_are_parsed_and_canonicalised() -> None: + expected = _normalize_expected_output( + { + **_ANOMALY, + "Sensitivity": "high", + "Trigger interval": "month", + "Date dataset": "order_date", + "setup_turns": ["Show me Net Sales as a headline."], + } + ) + assert expected.sensitivity == "HIGH" + assert expected.trigger_interval == "MONTH" + assert expected.date_dataset_id == "order_date" + assert expected.setup_turns == ("Show me Net Sales as a headline.",) + assert expected.expect_alert is True + + +def test_items_without_the_new_keys_parse_as_before() -> None: + expected = _normalize_expected_output({"Metric": "Net Sales (net_sales)", "Operator": "LESS_THAN"}) + assert (expected.sensitivity, expected.trigger_interval, expected.date_dataset_id) == (None, None, None) + assert expected.setup_turns == () + assert expected.expect_alert is True + + +@pytest.mark.parametrize( + "extra", + [ + {"Sensitivity": "VERY_HIGH"}, + {"Trigger interval": "HOUR"}, + {"Date dataset": 5}, + {"setup_turns": "Show me Net Sales."}, + {"setup_turns": [" "]}, + {"Expect alert": "no"}, + ], +) +def test_malformed_new_keys_fail_before_any_request(extra: dict) -> None: + with pytest.raises(ValueError): + _normalize_expected_output({**_ANOMALY, **extra}) + + +def test_trigger_interval_requires_the_once_per_interval_trigger() -> None: + with pytest.raises(ValueError, match="ONCE_PER_INTERVAL"): + _normalize_expected_output({"Operator": "GREATER_THAN", "Trigger": "Every time", "Trigger interval": "WEEK"}) + + +@pytest.mark.parametrize("extra", [{"Sensitivity": "MEDIUM"}, {"Date dataset": "order_date"}]) +def test_anomaly_settings_require_the_anomaly_operator(extra: dict) -> None: + # A GREATER_THAN alert sends neither argument, so a stated MEDIUM would always match the default. + with pytest.raises(ValueError, match="only to the ANOMALY operator"): + _normalize_expected_output({"Operator": "GREATER_THAN", "Threshold": "100", **extra}) + + +def test_no_alert_item_may_not_state_alert_fields() -> None: + with pytest.raises(ValueError, match="expect_alert: false"): + _normalize_expected_output({"Expect alert": False, "Operator": "GREATER_THAN", "Threshold": "0.05"}) + + +def test_no_alert_item_parses() -> None: + assert _normalize_expected_output({"Expect alert": False}).expect_alert is False + + +# --- checks ---------------------------------------------------------------------------------- + + +def test_anomaly_settings_are_scored_when_stated() -> None: + ev = _evaluate( + {**_ANOMALY, "Sensitivity": "HIGH", "Trigger interval": "MONTH", "Date dataset": "order_date"}, _ANOMALY_ARGS + ) + checks = ev.checks() + assert checks["alert_sensitivity_correct"] is True + assert checks["alert_trigger_interval_correct"] is True + assert checks["alert_date_dataset_correct"] is True + assert ev.strict_pass + + +def test_wrong_anomaly_settings_fail_their_own_check() -> None: + ev = _evaluate( + {**_ANOMALY, "Sensitivity": "HIGH", "Trigger interval": "MONTH", "Date dataset": "order_date"}, + {**_ANOMALY_ARGS, "sensitivity": "MEDIUM", "trigger_interval": "DAY", "date_dataset_id": "date"}, + ) + checks = ev.checks() + assert checks["alert_sensitivity_correct"] is False + assert checks["alert_trigger_interval_correct"] is False + assert checks["alert_date_dataset_correct"] is False + assert not ev.strict_pass + + +@pytest.mark.parametrize("trigger", [{"trigger": "ALWAYS"}, {"trigger": None}, {}], ids=["always", "null", "missing"]) +def test_the_interval_needs_the_once_per_interval_trigger(trigger: dict) -> None: + # The tool defaults the trigger to ALWAYS, and an ALWAYS alert never uses its trigger_interval. + args = {k: v for k, v in _ANOMALY_ARGS.items() if k != "trigger"} | trigger + checks = _evaluate({**_ANOMALY, "Trigger interval": "MONTH"}, args).checks() + assert checks["alert_trigger_interval_correct"] is False + + +def test_unstated_anomaly_settings_are_not_scored() -> None: + # An argument the item states nothing about adds no check, so such an item keeps the same score names. + checks = _evaluate(_ANOMALY, {**_ANOMALY_ARGS, "sensitivity": "LOW"}).checks() + for name in ("alert_sensitivity_correct", "alert_trigger_interval_correct", "alert_date_dataset_correct"): + assert name not in checks + + +def test_conditional_score_names_are_published() -> None: + assert set(ALERT_CONDITIONAL_SCORES) >= { + "alert_sensitivity_correct", + "alert_trigger_interval_correct", + "alert_date_dataset_correct", + "alert_not_proposed", + "comparison_correct", + "title_correct", + "dashboard_correct", + } + + +def test_metric_to_metric_comparison_checks_the_compared_metric() -> None: + expected = _normalize_expected_output( + {"Operator": "LESS_THAN", "Comparison": {"compare_metric_id": "net_sales_goal"}} + ) + assert expected.comparison == {"compare_metric_id": "net_sales_goal"} + assert _check_comparison(expected, {"compare_metric_id": "net_sales_goal"}) + assert not _check_comparison(expected, {"compare_metric_id": "total_sales"}) + assert not _check_comparison(expected, {}) + + +@pytest.mark.parametrize("key", ["compare_metric_id", "date_dataset_id", "date_attribute_id"]) +@pytest.mark.parametrize("value", [["net_sales_goal"], {"id": "net_sales_goal"}, 7]) +def test_a_comparison_id_must_be_a_string(key: str, value: object) -> None: + with pytest.raises(ValueError, match=f"Comparison {key} must be an object id string"): + _normalize_expected_output({"Operator": "LESS_THAN", "Comparison": {key: value}}) + + +def test_a_null_comparison_id_stays_unasserted() -> None: + expected = _normalize_expected_output( + {"Operator": "LESS_THAN", "Comparison": {"kind": "METRIC", "compare_metric_id": None}} + ) + assert expected.comparison == {"comparison_kind": "METRIC"} + + +# --- run loop -------------------------------------------------------------------------------- + + +def _turn(text: str = "", *, proposal: bool = False, create_result: str | None = None) -> ChatResult: + events = [] + if create_result is not None: + events.append( + { + "functionName": "create_metric_alert", + "functionArguments": json.dumps(_ANOMALY_ARGS), + "result": create_result, + } + ) + payload: dict = {"text_response": text, "toolCallEvents": events} + if proposal: + payload["alertProposals"] = [{"cta": "Should I create this alert?", "alert": {"trigger": "ALWAYS"}}] + return ChatResult.model_validate(payload) + + +def _metric_turn(text: str = "", *, create_result: str | None = None) -> ChatResult: + """A turn whose agent created the churn-rate metric, optionally also creating an alert.""" + events = [ + { + "functionName": "create_metric", + "functionArguments": "{}", + "result": json.dumps({"metric_id": "churn_rate", "created_new": True}), + } + ] + if create_result is not None: + events.append( + { + "functionName": "create_metric_alert", + "functionArguments": json.dumps(_ANOMALY_ARGS), + "result": create_result, + } + ) + return ChatResult.model_validate({"text_response": text, "toolCallEvents": events}) + + +def _run( + client: MagicMock, expected_output: dict, simulated_reply: str = "No, thanks.", max_iterations: int = 6 +) -> tuple[AlertRunResult, MagicMock, MagicMock]: + with ( + patch("gooddata_eval.core.agentic.alert_skill.ChatClient", return_value=client), + patch( + "gooddata_eval.core.agentic.alert_skill.generate_simulated_alert_response", return_value=simulated_reply + ) as mock_sim, + patch("gooddata_eval.core.agentic.alert_skill._delete_alert") as mock_delete, + ): + summary = run_agentic_alert_skill( + host="http://host", + token="tok", + workspace_id="ws1", + question="Notify me if customer churn rate goes above 5%.", + expected_output=expected_output, + k=1, + max_iterations=max_iterations, + initial_conversation_id="conv-1", + ) + # Every send stays in the run's conversation: setup turns, the question, then the simulated replies. + sent = [c.args for c in client.send_message.call_args_list] + opening = [*expected_output.get("setup_turns", []), "Notify me if customer churn rate goes above 5%."] + # A run cut short (a broken setup turn) sends a prefix of this sequence. + expected_sends = (opening + [simulated_reply] * len(sent))[: len(sent)] + assert sent == [("conv-1", message) for message in expected_sends] + return summary.best, mock_sim, mock_delete + + +def test_no_alert_item_passes_when_the_agent_never_proposes() -> None: + client = MagicMock() + client.send_message.return_value = _turn("There is no churn rate metric. Should I use Return Customers?") + run, _, _ = _run(client, {"Expect alert": False}) + assert run.eval.checks() == {"alert_not_proposed": True} + assert run.eval.strict_pass + # The simulated user only declines, so the run stops early instead of using the whole budget. + assert client.send_message.call_count == 3 + + +def test_no_alert_item_fails_on_a_proposal() -> None: + client = MagicMock() + client.send_message.side_effect = [_turn("Which metric?"), _turn(proposal=True)] + run, _, _ = _run(client, {"Expect alert": False}) + assert run.eval.checks() == {"alert_not_proposed": False} + assert not run.eval.strict_pass + + +def test_no_alert_item_fails_and_cleans_up_when_an_alert_is_created() -> None: + client = MagicMock() + client.send_message.return_value = _turn(create_result='{"id": "alert-1"}') + run, _, mock_delete = _run(client, {"Expect alert": False}) + assert run.eval.checks() == {"alert_not_proposed": False} + assert [c.args[2] for c in mock_delete.call_args_list] == ["alert-1"] + + +def test_no_alert_item_fails_and_cleans_up_when_the_agent_creates_a_metric() -> None: + # The created metric would make the next run of this item find one and propose an alert. + client = MagicMock() + client.send_message.return_value = _metric_turn("I created a Churn Rate metric for you.") + with patch("gooddata_eval.core.agentic.alert_skill._delete_metric") as mock_delete_metric: + run, _, _ = _run(client, {"Expect alert": False}) + assert run.eval.checks() == {"alert_not_proposed": False} + assert run.exit_reason == LoopExit.SUCCESS + assert [c.args[1:] for c in mock_delete_metric.call_args_list] == [("ws1", "churn_rate")] + + +def test_a_metric_the_agent_created_is_deleted_after_its_alert() -> None: + client = MagicMock() + client.send_message.return_value = _metric_turn(create_result='{"id": "alert-1"}') + order = MagicMock() + with ( + patch("gooddata_eval.core.agentic.alert_skill._delete_metric", order.delete_metric), + patch("gooddata_eval.core.agentic.alert_skill._delete_alert", order.delete_alert), + patch("gooddata_eval.core.agentic.alert_skill.ChatClient", return_value=client), + ): + run_agentic_alert_skill( + host="http://host", + token="tok", + workspace_id="ws1", + question="Notify me if customer churn rate goes above 5%.", + expected_output=_ANOMALY, + k=1, + initial_conversation_id="conv-1", + ) + assert [(c[0], c.args[1:]) for c in order.mock_calls] == [ + ("delete_alert", ("ws1", "alert-1")), + ("delete_metric", ("ws1", "churn_rate")), + ] + + +def test_no_alert_item_uses_the_declining_simulated_user() -> None: + fake_openai = MagicMock() + fake_openai.return_value.chat.completions.create.return_value = MagicMock( + choices=[MagicMock(message=MagicMock(content="No."))] + ) + with ( + patch("gooddata_eval.core.agentic.alert_skill._OpenAI", fake_openai), + patch.dict("os.environ", {"OPENAI_API_KEY": "test-key"}), + ): + generate_simulated_alert_response( + "Should I use Return Customers instead?", + _normalize_expected_output({"Expect alert": False}), + [], + question="Notify me if customer churn rate goes above 5%.", + ) + prompt = fake_openai.return_value.chat.completions.create.call_args.kwargs["messages"][0]["content"] + assert "Never accept a different or similar metric" in prompt + assert "churn rate" in prompt + + +def test_setup_turns_are_sent_first_in_the_same_conversation_and_not_scored() -> None: + setup = ChatResult.model_validate( + { + "text_response": "Here is Net Sales: $105.2 M.", + "toolCallEvents": [ + {"functionName": "create_adhoc_visualization", "functionArguments": "{}", "result": "{}"} + ], + } + ) + created = _turn(create_result='{"id": "alert-1"}') + client = MagicMock() + client.send_message.side_effect = [setup, created] + expected = {**_ANOMALY, "setup_turns": ["Show me Net Sales as a headline."]} + run, _, _ = _run(client, expected) + sent = [c.args for c in client.send_message.call_args_list] + assert sent[0] == ("conv-1", "Show me Net Sales as a headline.") + assert sent[1] == ("conv-1", "Notify me if customer churn rate goes above 5%.") + assert run.eval.alert_created is True + # The question turn is the first scored turn. + assert run.turns_used == 1 + + +def test_setup_turns_reach_the_simulated_user_history() -> None: + setup = ChatResult.model_validate({"text_response": "Here is Net Sales: $105.2 M."}) + client = MagicMock() + client.send_message.side_effect = [setup, _turn("Which channel?"), _turn(create_result='{"id": "a"}')] + expected = {**_ANOMALY, "setup_turns": ["Show me Net Sales as a headline."]} + _, mock_sim, _ = _run(client, expected) + history = mock_sim.call_args.args[2] + assert history[0] == {"role": "user", "content": "Show me Net Sales as a headline."} + assert "$105.2 M" in history[1]["content"] + + +def test_simulated_user_is_told_the_stated_anomaly_settings() -> 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( + "Which sensitivity?", + _normalize_expected_output( + {**_ANOMALY, "Sensitivity": "HIGH", "Trigger interval": "MONTH", "Date dataset": "order_date"} + ), + [], + question="Watch Total Sales for anomalies by order month with high sensitivity.", + ) + prompt = fake_openai.return_value.chat.completions.create.call_args.kwargs["messages"][0]["content"] + assert "Use HIGH sensitivity" in prompt + assert "date dataset order_date" in prompt + assert "trigger interval of MONTH" in prompt + + +def test_simulated_user_is_told_its_setup_turns_came_first() -> 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( + "Which channel?", + _normalize_expected_output( + {**_ANOMALY, "setup_turns": ["Show me Net Sales as a headline.", "Now break it down by month."]} + ), + [], + question="Watch Total Sales for anomalies by order month.", + ) + prompt = fake_openai.return_value.chat.completions.create.call_args.kwargs["messages"][0]["content"] + assert ( + 'Earlier in this conversation you asked: "Show me Net Sales as a headline." / "Now break it down by month.".\n' + 'Your original request to the agent was: "Watch Total Sales for anomalies by order month."' + ) in prompt + + +def test_omitted_sensitivity_is_scored_as_the_tool_default() -> None: + # create_metric_alert applies MEDIUM when sensitivity is left out. + args = {k: v for k, v in _ANOMALY_ARGS.items() if k != "sensitivity"} + assert _evaluate({**_ANOMALY, "Sensitivity": "MEDIUM"}, args).checks()["alert_sensitivity_correct"] is True + assert _evaluate({**_ANOMALY, "Sensitivity": "HIGH"}, args).checks()["alert_sensitivity_correct"] is False + + +def test_no_alert_item_fails_when_the_question_turn_errors() -> None: + # A run the chat cut short never showed the agent declining. + client = MagicMock() + client.send_message.side_effect = ChatError("stream died") + run, _, _ = _run(client, {"Expect alert": False}) + assert run.exit_reason == LoopExit.CHAT_ERROR + assert run.eval.checks() == {"alert_not_proposed": False} + + +def test_no_alert_item_passes_when_the_agent_goes_quiet_after_declining() -> None: + # "I can't create this alert." -> "OK, thanks." -> an empty turn: nothing was ever proposed. + client = MagicMock() + client.send_message.side_effect = [_turn("I can't create this alert."), _turn("")] + run, _, _ = _run(client, {"Expect alert": False}, simulated_reply="OK, thanks.") + assert run.exit_reason == LoopExit.AGENT_SILENT + assert run.turns_used == 2 + assert run.eval.checks() == {"alert_not_proposed": True} + + +def test_no_alert_item_fails_when_the_agent_goes_silent() -> None: + client = MagicMock() + client.send_message.return_value = _turn("") + run, _, _ = _run(client, {"Expect alert": False}) + assert run.exit_reason == LoopExit.AGENT_SILENT + assert run.eval.checks() == {"alert_not_proposed": False} + + +def test_no_alert_item_fails_on_a_rejected_proposal_call() -> None: + # A rejected proposal call leaves no proposal part, but the agent still tried to propose a substitute. + rejected = ChatResult.model_validate( + { + "text_response": "I could not prepare that alert.", + "toolCallEvents": [ + { + "functionName": "prepare_metric_alert_proposal", + "functionArguments": "{}", + "result": '{"success": false, "error": "rejected"}', + } + ], + } + ) + client = MagicMock() + client.send_message.return_value = rejected + run, _, _ = _run(client, {"Expect alert": False}) + assert run.eval.checks() == {"alert_not_proposed": False} + + +def test_no_alert_item_needs_no_current_user() -> None: + client = MagicMock() + client.send_message.return_value = _turn("There is no churn rate metric.") + with patch("gooddata_eval.core.agentic.alert_skill._resolve_current_user_id") as mock_resolve: + run, _, _ = _run(client, {"Expect alert": False, "Recipient(s)": "me"}) + mock_resolve.assert_not_called() + assert run.eval.strict_pass + + +def test_a_broken_setup_turn_deletes_the_alert_it_created() -> None: + partial = _turn(create_result='{"id": "alert-1"}') + client = MagicMock() + client.send_message.side_effect = [ChatError("stream died", partial_result=partial)] + run, _, mock_delete = _run(client, {**_ANOMALY, "setup_turns": ["Show me Net Sales as a headline."]}) + assert run.exit_reason == LoopExit.CHAT_ERROR + assert run.eval.alert_created is False + assert [c.args[2] for c in mock_delete.call_args_list] == ["alert-1"] + + +def test_catalog_from_dict_parses_the_new_fields_as_expected_output_does() -> None: + alert = CatalogMetricAlert.from_dict( + { + "operator": "ANOMALY", + "trigger": "ONCE_PER_INTERVAL", + "sensitivity": "high", + "trigger_interval": "month", + "setup_turns": ["Show me Net Sales."], + } + ) + assert (alert.sensitivity, alert.trigger_interval) == ("HIGH", "MONTH") + assert alert.setup_turns == ("Show me Net Sales.",) + assert alert.expect_alert is True + assert CatalogMetricAlert.from_dict({"expect_alert": None}).expect_alert is True + assert CatalogMetricAlert.from_dict({"expect_alert": False}).expect_alert is False + + +@pytest.mark.parametrize( + "fields", + [ + {"expect_alert": "false"}, + {"setup_turns": "Show me Net Sales."}, + {"setup_turns": [" "]}, + {"operator": "ANOMALY", "sensitivity": "extreme"}, + ], +) +def test_catalog_from_dict_rejects_what_expected_output_rejects(fields: dict) -> None: + with pytest.raises(ValueError): + _normalize_expected_output(fields) + with pytest.raises(ValueError): + CatalogMetricAlert.from_dict(fields) + + +@pytest.mark.parametrize("max_iterations", [0, -1]) +def test_a_run_needs_at_least_one_turn(max_iterations: int) -> None: + with pytest.raises(ValueError, match="max_iterations"): + run_agentic_alert_skill( + host="http://host", + token="tok", + workspace_id="ws1", + question="q", + expected_output={"Expect alert": False}, + max_iterations=max_iterations, + )