|
| 1 | +# (C) 2026 GoodData Corporation |
| 2 | +"""The head of an agentic kind's failure message: the gate result, then every failing run. |
| 3 | +
|
| 4 | +Each kind's assertion message describes its BEST run, which on a partial gate failure (some |
| 5 | +of the K runs passed) is a passing run: every check reads True and the reason the item |
| 6 | +failed is nowhere in the message. This module renders the part that is missing -- for each |
| 7 | +failing run its index, why its loop stopped and the checks it failed, with expected/actual |
| 8 | +where the kind knows them -- from the same ``failed_runs`` records the item reports, so the |
| 9 | +message and the triage data cannot disagree about which runs failed or why. |
| 10 | +
|
| 11 | +One renderer for every kind on purpose: the readers of these messages (people and the |
| 12 | +triage tooling) look for one shape, whatever the capability under test. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import json |
| 18 | +from collections.abc import Iterable, Mapping, Sequence |
| 19 | +from dataclasses import dataclass |
| 20 | +from enum import Enum |
| 21 | +from typing import Any |
| 22 | + |
| 23 | +__all__ = ["FailedCheck", "cap_failed_checks", "failed_check_records", "failing_runs_note", "failure_head"] |
| 24 | + |
| 25 | +# A value longer than this is cut: the message is one line per run, and the whole value is in |
| 26 | +# the run's detail anyway. |
| 27 | +_MAX_VALUE_CHARS = 160 |
| 28 | + |
| 29 | + |
| 30 | +class _Unset(Enum): |
| 31 | + UNSET = "unset" |
| 32 | + |
| 33 | + |
| 34 | +_UNSET = _Unset.UNSET |
| 35 | + |
| 36 | + |
| 37 | +@dataclass(frozen=True) |
| 38 | +class FailedCheck: |
| 39 | + """One check a run failed, with the expected/actual pair when the kind knows it. |
| 40 | +
|
| 41 | + ``label`` narrows the check when one name covers several parts, e.g. the ``date`` part of |
| 42 | + ``filters_correct``. |
| 43 | + """ |
| 44 | + |
| 45 | + name: str |
| 46 | + expected: Any = _UNSET |
| 47 | + actual: Any = _UNSET |
| 48 | + label: str | None = None |
| 49 | + |
| 50 | + def as_record(self) -> dict[str, Any]: |
| 51 | + """The JSON-ready form written into a failing run's ``failed_checks``.""" |
| 52 | + record: dict[str, Any] = {"name": self.name} |
| 53 | + if self.label is not None: |
| 54 | + record["label"] = self.label |
| 55 | + if self.expected is not _UNSET: |
| 56 | + record["expected"] = self.expected |
| 57 | + if self.actual is not _UNSET: |
| 58 | + record["actual"] = self.actual |
| 59 | + return record |
| 60 | + |
| 61 | + |
| 62 | +def failed_check_records(checks: Iterable[FailedCheck]) -> list[dict[str, Any]]: |
| 63 | + """``checks`` as the records ``failed_runs[].failed_checks`` holds.""" |
| 64 | + return [check.as_record() for check in checks] |
| 65 | + |
| 66 | + |
| 67 | +def _text(value: Any) -> str: |
| 68 | + text = value if isinstance(value, str) else json.dumps(value, default=str, ensure_ascii=False, sort_keys=True) |
| 69 | + return " ".join(text.split()) |
| 70 | + |
| 71 | + |
| 72 | +def _value(value: Any) -> str: |
| 73 | + if value is None or value == [] or value == {} or value == "": |
| 74 | + return "none" |
| 75 | + text = _text(value) |
| 76 | + return text if len(text) <= _MAX_VALUE_CHARS else text[: _MAX_VALUE_CHARS - 3] + "..." |
| 77 | + |
| 78 | + |
| 79 | +def cap_failed_checks(records: Sequence[Mapping[str, Any]]) -> list[dict[str, Any]]: |
| 80 | + """``records`` with every expected/actual value cut to what the message shows of it. |
| 81 | +
|
| 82 | + A check's values can be whole filter or bucket structures, repeated for every failing run |
| 83 | + of every item; the record keeps the same ``_MAX_VALUE_CHARS`` the message does. A value |
| 84 | + that fits stays as it is, JSON and all; a longer one becomes its cut text. The run's |
| 85 | + ``detail`` holds the full values. |
| 86 | + """ |
| 87 | + capped: list[dict[str, Any]] = [] |
| 88 | + for record in records: |
| 89 | + entry = dict(record) |
| 90 | + for key in ("expected", "actual"): |
| 91 | + if key in entry and len(_text(entry[key])) > _MAX_VALUE_CHARS: |
| 92 | + entry[key] = _value(entry[key]) |
| 93 | + capped.append(entry) |
| 94 | + return capped |
| 95 | + |
| 96 | + |
| 97 | +def _render_check(record: Mapping[str, Any]) -> str: |
| 98 | + name = str(record.get("name", "?")) |
| 99 | + parts: list[str] = [] |
| 100 | + if "expected" in record: |
| 101 | + parts.append(f"expected {_value(record['expected'])}") |
| 102 | + if "actual" in record: |
| 103 | + parts.append(f"actual {_value(record['actual'])}") |
| 104 | + label = record.get("label") |
| 105 | + if not parts and label is None: |
| 106 | + return name |
| 107 | + inner = ", ".join(parts) |
| 108 | + if label is not None: |
| 109 | + inner = f"{label}: {inner}" if inner else str(label) |
| 110 | + return f"{name}[{inner}]" |
| 111 | + |
| 112 | + |
| 113 | +def _render_run(record: Mapping[str, Any], runs_total: int) -> str: |
| 114 | + outcome = record.get("outcome") |
| 115 | + head = f"run {record.get('run_index', '?')}/{runs_total}" |
| 116 | + exit_reason = record.get("exit_reason") |
| 117 | + if isinstance(outcome, Mapping) and outcome.get("status") == "error": |
| 118 | + # A harness error: the reason (simulated_user_failed, judge_failed) is the diagnosis. |
| 119 | + # Its checks are not listed: they failed because the harness did, not the agent. |
| 120 | + return f"{head} ERROR ({outcome.get('reason') or 'unknown'})" |
| 121 | + head += " FAIL" + (f" (loop exit: {exit_reason})" if exit_reason else "") |
| 122 | + checks = record.get("failed_checks") |
| 123 | + if checks: |
| 124 | + return f"{head}: {', '.join(_render_check(c) for c in checks)}" |
| 125 | + if checks is not None: |
| 126 | + return f"{head}: no failing check recorded" |
| 127 | + return head |
| 128 | + |
| 129 | + |
| 130 | +def failing_runs_note(failed_runs: Sequence[Mapping[str, Any]], runs_total: int) -> str: |
| 131 | + """Every failing run on one line each, joined with ``; ``. Empty when no run failed.""" |
| 132 | + return "; ".join(_render_run(record, runs_total) for record in failed_runs) |
| 133 | + |
| 134 | + |
| 135 | +def failure_head(gate_note: str, failed_runs: Sequence[Mapping[str, Any]], runs_total: int) -> str: |
| 136 | + """``gate_note`` followed by the failing runs, ready to start a kind's assertion message. |
| 137 | +
|
| 138 | + Returns ``gate_note`` unchanged when there is no failing run to list, so a kind that |
| 139 | + builds no ``failed_runs`` keeps its message exactly as before. |
| 140 | + """ |
| 141 | + note = failing_runs_note(failed_runs, runs_total) |
| 142 | + return f"{gate_note} Failing runs: {note}." if note else gate_note |
0 commit comments