|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Deterministic CLI regression gate for ref-verify. |
| 3 | +
|
| 4 | +Runs the labeled corpus in ``cli_regression.jsonl`` through ``check-file`` and |
| 5 | +classifies every row into one of: |
| 6 | +
|
| 7 | +- SAFETY pass/fail — invariants that must hold on every commit: |
| 8 | + * ``must_accept`` rows must end ACCEPT (the supported happy path stays green) |
| 9 | + * ``must_not_accept`` rows must NOT end ACCEPT (no fabricated/relational/ |
| 10 | + unreachable/over-accepting claim is ever waved through) |
| 11 | + A SAFETY failure exits non-zero and should block release. |
| 12 | +
|
| 13 | +- PROGRESS — gated rows whose ``expected_verdict`` is not yet reached because a |
| 14 | + named issue (``gated_on``) has not landed. These are reported, not failed; they |
| 15 | + flip to PASS as their fixes land. |
| 16 | +
|
| 17 | +Stdlib only. Usage: |
| 18 | + PYTHONPATH=src python3 evals/run_cli_regression.py |
| 19 | +""" |
| 20 | + |
| 21 | +from __future__ import annotations |
| 22 | + |
| 23 | +import json |
| 24 | +import subprocess |
| 25 | +import sys |
| 26 | +import tempfile |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +CORPUS = Path(__file__).with_name("cli_regression.jsonl") |
| 30 | + |
| 31 | + |
| 32 | +def _load_corpus() -> list[dict]: |
| 33 | + rows = [] |
| 34 | + for line in CORPUS.read_text(encoding="utf-8").splitlines(): |
| 35 | + line = line.strip() |
| 36 | + if line: |
| 37 | + rows.append(json.loads(line)) |
| 38 | + return rows |
| 39 | + |
| 40 | + |
| 41 | +def _run_cli(rows: list[dict]) -> dict[str, dict]: |
| 42 | + with tempfile.NamedTemporaryFile("w", suffix=".jsonl", delete=False, encoding="utf-8") as handle: |
| 43 | + for row in rows: |
| 44 | + handle.write(json.dumps({"id": row["id"], "doi": row["doi"], "claim": row["claim"]}) + "\n") |
| 45 | + claims_path = handle.name |
| 46 | + try: |
| 47 | + proc = subprocess.run( |
| 48 | + [sys.executable, "-m", "ref_verify.cli", "check-file", claims_path, "--json"], |
| 49 | + capture_output=True, |
| 50 | + text=True, |
| 51 | + ) |
| 52 | + finally: |
| 53 | + Path(claims_path).unlink(missing_ok=True) |
| 54 | + if not proc.stdout.strip(): |
| 55 | + raise SystemExit(f"check-file produced no JSON. stderr:\n{proc.stderr}") |
| 56 | + payload = json.loads(proc.stdout) |
| 57 | + return {r["id"]: r for r in payload["results"]} |
| 58 | + |
| 59 | + |
| 60 | +def main() -> int: |
| 61 | + rows = _load_corpus() |
| 62 | + results = _run_cli(rows) |
| 63 | + |
| 64 | + safety_failures: list[str] = [] |
| 65 | + progress_pending: list[str] = [] |
| 66 | + print(f"{'id':26}{'verdict':20}{'expected':14}{'class':13}note") |
| 67 | + print("-" * 92) |
| 68 | + for row in rows: |
| 69 | + res = results.get(row["id"], {}) |
| 70 | + verdict = res.get("verdict", "MISSING") |
| 71 | + status = res.get("status", "") |
| 72 | + accepted = verdict == "ACCEPT" |
| 73 | + klass, note = "PASS", "" |
| 74 | + |
| 75 | + if row.get("must_accept") and not accepted: |
| 76 | + klass, note = "SAFETY-FAIL", "must ACCEPT but did not" |
| 77 | + safety_failures.append(row["id"]) |
| 78 | + elif row.get("must_not_accept") and accepted: |
| 79 | + klass, note = "SAFETY-FAIL", "must NOT ACCEPT but did" |
| 80 | + safety_failures.append(row["id"]) |
| 81 | + elif row.get("must_not_accept"): |
| 82 | + # Control row: the only invariant is "never ACCEPT". The exact non-ACCEPT |
| 83 | + # verdict (UNVERIFIABLE vs PARTIAL) can vary with source availability, so it |
| 84 | + # is not pinned. |
| 85 | + klass = "PASS" |
| 86 | + elif verdict != row["expected_verdict"] and status != row["expected_verdict"]: |
| 87 | + gated = ",".join(row.get("gated_on") or []) or "?" |
| 88 | + klass, note = "PENDING", f"want {row['expected_verdict']} after {gated}" |
| 89 | + progress_pending.append(row["id"]) |
| 90 | + |
| 91 | + shown = verdict if verdict != "WARN" else f"{verdict}/{status}" |
| 92 | + print(f"{row['id']:26}{shown:20}{row['expected_verdict']:14}{klass:13}{note}") |
| 93 | + |
| 94 | + print("-" * 92) |
| 95 | + print( |
| 96 | + f"SAFETY: {len(rows) - len(safety_failures)}/{len(rows)} ok" |
| 97 | + f" | PROGRESS pending: {len(progress_pending)}" |
| 98 | + ) |
| 99 | + if safety_failures: |
| 100 | + print("SAFETY FAILURES (release blockers):", ", ".join(safety_failures)) |
| 101 | + return 1 |
| 102 | + if progress_pending: |
| 103 | + print("Pending (informational, not a failure):", ", ".join(progress_pending)) |
| 104 | + return 0 |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + raise SystemExit(main()) |
0 commit comments