"""Verify the local research evidence and run the shipped auditor example.""" import argparse from collections import Counter import hashlib import json from pathlib import Path import subprocess import sys from tempfile import TemporaryDirectory from metric_audit import run_snippet HERE = Path(__file__).resolve().parent WORK = HERE.parent / "work" AUDIT = WORK / "table2_full/audit" def check(valid, message): if not valid: raise SystemExit(message) def read(path): return json.loads(path.read_text(encoding="utf-8-sig")) def verify_demo(evidence): check([evidence["cohorts"][key]["n"] for key in ("human", "agent", "public")] == [473, 423, 646], "Table 2 cohort counts changed") check([len(row) for row in evidence["matrix"]] == [6] * 5, "Expected 25 matrix cells") outcomes = {(row["checkpoint"], row["suite"]): row["cases"] for row in evidence["test_outcomes"]} check(len(outcomes) == 25 and sum(map(len, outcomes.values())) == 1345, "Test outcomes are incomplete") for checkpoint, *suites in evidence["matrix"]: for suite, cell in enumerate(suites, 1): cases = outcomes[int(checkpoint), suite] check(cell == f"{sum(status == 'passed' for status in cases.values())}/{len(cases)}", f"Matrix count differs for checkpoint {checkpoint}, suite {suite}") check(len(evidence["diagnoses"]) == 50 and len({row[0] for row in evidence["diagnoses"]}) == 50, "Persistent failure IDs are incomplete") check(Counter(row[3] for row in evidence["diagnoses"]) == {True: 39, False: 4, None: 7}, "Rendering classifications differ") check(sorted(evidence["cause_counts"].values()) == [2, 2, 3, 43], "Cause counts differ") patches = {(row["checkpoint"], row["suite"], row["patch"]): row for row in evidence["ablation"]} check(len(patches) == 120, "Patch matrix is incomplete") for checkpoint in (3, 4, 5): first = patches[checkpoint, 3, "A"]["regressed"] second = patches[checkpoint, 3, "B"]["regressed"] combined = patches[checkpoint, 3, "AB"]["regressed"] check(len(first) == 4 and set(first) == set(second) and not combined, f"Unexpected patch interaction at checkpoint {checkpoint}") check(patches[checkpoint, 3, "ABC"]["passed"] == 49, "Joint repair result changed") result = run_snippet((HERE / "example.py").read_text(encoding="utf-8")) check([r["rule_count"] for r in result["bundles"].values()] == [197, 137], "Scorer did not load both pinned bundles") for mode, bundle in result["bundles"].items(): check(bundle["score"]["total_loc"] > 0 and len(bundle["counted_lines"]) == bundle["score"]["verbosity_flagged_loc"], f"Line-level output disagrees with the {mode} score") check(result["rule_hits_only_in"]["current"] or result["rule_hits_only_in"]["legacy"], "The example should exercise a rule-level difference") print("Verified the evidence, checkpoint and patch results, and live dual-rule scoring.") def verify_research(evidence): for name, expected in evidence["source_sha256"].items(): check(hashlib.sha256((WORK / name).read_bytes()).hexdigest() == expected, f"Evidence hash changed for {name}") for name, expected in read(AUDIT / "evidence-sha256.json").items(): check(hashlib.sha256((AUDIT / name).read_bytes()).hexdigest() == expected, f"Frozen audit hash changed for {name}") subprocess.run([sys.executable, "-B", str(HERE / "export_evidence.py"), "--check"], check=True) with TemporaryDirectory(prefix="bpr-replay-") as directory: replay = Path(directory) / "audit.json" subprocess.run([sys.executable, "-B", str(AUDIT / "analyze.py"), "--output", str(replay)], check=True, stdout=subprocess.DEVNULL) check(read(replay) == read(AUDIT / "table2-audit.json"), "Table 2 recomputation differs") print("Verified frozen hashes and recomputed the full Table 2 summary from the saved scores.") def verify_public_data(evidence): from replay_scores import recompute scores = read(HERE / "study_scores.json") replay = recompute(scores) check(replay["ratios"] == evidence["ratios"], "Published ratios differ") check(replay["human_tiers"] == evidence["human_tiers"], "Published star tiers differ") check(all(group == evidence["cohorts"][key] for key, group in replay["cohorts"].items()), "Published cohort means differ") check(len(scores["agent_failures"]) == 2, "Expected both agent parsing failures") check(sum(source["n"] for source in scores["published_sources"]) == 646, "Published score source counts differ") trace = [json.loads(line) for line in (HERE / "agent-trace.jsonl").read_text().splitlines()] check(len(trace) == 149 and sum(row["event"] == "source" for row in trace) == 5, "Trace event coverage differs") print("Verified the public score replay and redacted trace coverage.") def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--research", action="store_true", help="Also check the full local research checkout") args = parser.parse_args() evidence = read(HERE / "evidence.json") verify_demo(evidence) if (HERE / "study_scores.json").exists(): verify_public_data(evidence) if args.research: verify_research(evidence) if __name__ == "__main__": main()