"""Phrase-level probes: quantify the habits that only show up in prose. Counting tool calls says how the agent worked. These probes read what it *said* -- in its final report and in the short narration between tool calls -- to test claims that would otherwise rest on a handful of cherry-picked examples: did it measure a baseline before changing anything, did it keep a development set separate from the benchmark, did it mention sampling noise, did it admit a negative result. Each probe is a conservative regex over one text surface. They are evidence of a habit being *present*, never proof it is absent -- an agent can hold out a dev set without using the word. """ from __future__ import annotations import re from collections import Counter from .parse import Run # Probes over the final report the agent signs off with. REPORT_PROBES: dict[str, re.Pattern[str]] = { "reports_baseline_number": re.compile(r"baseline[^\n.]{0,60}?\d+(?:\.\d+)?\s*%|\bbaseline\b[^\n]{0,40}\b0\.\d+"), "reports_version_table": re.compile(r"\|\s*(?:v\d|model|run|stage)\b.*\|", re.I), "names_dev_set": re.compile(r"\bdev\b|held[- ]out|validation set|\bval\b set|never (?:saw|used)", re.I), "quantifies_uncertainty": re.compile(r"stderr|standard error|\bn\s*=\s*\d+|\b±\b|noise|within (?:the )?margin|significan", re.I), "admits_negative_result": re.compile( r"did not (?:beat|improve|work|help)|no (?:measurable |significant )?(?:improvement|gain)|regress|" r"worse than|failed to|honest (?:outcome|limitation)|I did not", re.I, ), "states_limitation": re.compile(r"limitation|caveat|bottleneck|ceiling|cannot|the honest", re.I), "compliance_statement": re.compile(r"contamination|decontaminat|no .{0,20}test data|compliance|untouched|unmodified", re.I), "explains_mechanism": re.compile(r"because|the reason|which is why|driven by|root cause|diagnos", re.I), "mentions_time_budget": re.compile(r"time (?:budget|left|ran out)|remaining (?:time|hours)|with .{0,15}hours? left", re.I), "describes_scorer_behaviour": re.compile( r"scorer|extraction|the grader|first (?:```|code block)|parser|how (?:it|the eval) (?:scores|extracts)", re.I ), "reports_ablation": re.compile(r"ablat|with(?:out)? (?:the )?\w+ (?:it|this) (?:dropped|rose|fell)|A/B|compared .{0,30}variant", re.I), } # Probes over the short narration the agent emits between tool calls. For # Claude this is the only natural-language rationale in the whole trace -- # its 5,596 extended-thinking blocks are present but encrypted, zero readable # characters. Codex exposes its reasoning in full, so for Codex this surface # is the tip rather than the whole. NARRATION_PROBES: dict[str, re.Pattern[str]] = { "abandons_a_branch": re.compile(r"not worth|abandon|drop (?:this|that|the)|give up on|revert to|roll back|scrap", re.I), "reasons_about_time": re.compile(r"hours? (?:left|remaining)|time budget|won'?t finish|too slow to|enough time", re.I), "reasons_about_noise": re.compile(r"noise|stderr|too few samples|not significant|could be chance|small n", re.I), "plans_fallback": re.compile(r"fall ?back|if (?:this|that) fails|safety|keep (?:the|a) (?:best|working)|insurance", re.I), "questions_own_measurement": re.compile(r"might be measuring|is that real|double[- ]check|verify (?:that|the) (?:number|score)|sanity", re.I), } def narration_text(run: Run) -> str: """Every assistant text block: what the agent chose to say out loud.""" out = [] for ev in run.events: if ev.kind != "assistant": continue for c in ev.raw.get("message", {}).get("content", []) or []: if isinstance(c, dict) and c.get("type") == "text": out.append(c.get("text", "") or "") return "\n".join(out) def reasoning_text(run: Run) -> str: """Readable extended-thinking text, where the SDK exposes any. Empty for every Claude trace -- those blocks are present but encrypted. """ out = [] for ev in run.events: if ev.kind != "assistant": continue for c in ev.raw.get("message", {}).get("content", []) or []: if isinstance(c, dict) and c.get("type") == "thinking": out.append(c.get("thinking", "") or "") return "\n".join(out) def probe_run(run: Run) -> dict[str, bool]: """Each habit on its natural surface, plus an `anywhere_` variant. The two scaffolds put the same content in different places, so one surface cannot serve every comparison: * plain the final report only. Comparable within an agent. Claude signs off in 2.4k characters, Codex in 340, so this understates Codex badly. * `said_` report + between-turn narration -- everything the agent chose to say out loud. **Both SDKs expose this, so this is the surface to compare across agents.** * `anywhere_` the above plus extended-thinking text. Only Codex has any, so a cross-agent gap here measures what the SDK exposes, not what the agent did. """ report = run.final_message or "" narration = narration_text(run) said = report + "\n" + narration everything = said + "\n" + reasoning_text(run) res = {k: bool(p.search(report)) for k, p in REPORT_PROBES.items()} res.update({f"said_{k}": bool(p.search(said)) for k, p in REPORT_PROBES.items()}) res.update({f"anywhere_{k}": bool(p.search(everything)) for k, p in REPORT_PROBES.items()}) res.update({k: bool(p.search(narration)) for k, p in NARRATION_PROBES.items()}) return res def probe_cohort(runs: list[Run]) -> dict[str, dict[str, object]]: hits: Counter[str] = Counter() n_report = sum(1 for r in runs if r.final_message) for r in runs: for k, v in probe_run(r).items(): if v: hits[k] += 1 rows = {} variants = ( list(REPORT_PROBES) + [f"said_{k}" for k in REPORT_PROBES] + [f"anywhere_{k}" for k in REPORT_PROBES] + list(NARRATION_PROBES) ) for k in variants: denom = n_report if k in REPORT_PROBES else len(runs) rows[k] = { "runs_with_evidence": hits[k], "of": denom, "pct": round(100 * hits[k] / denom, 1) if denom else None, "surface": ( "final report" if k in REPORT_PROBES else "report + narration (cross-agent comparable)" if k.startswith("said_") else "incl. extended thinking (Codex only)" if k.startswith("anywhere_") else "narration" ), } return rows