"""Verification after drafting, from the raw traces. For each final run: first_write = seq of the first write to the draft (edit/write tool on draft.md, or a shell command that writes draft.md) post_checks = web searches + web page reads (websearch, webfetch, pdftext/doctext of a URL, browser fetch) after first_write dated = web searches whose query names a date or currency word (2026, November, current, still, latest, today, now, recent) Permutation test shuffles model labels within each (task, repeat).""" import json, re, sys, random, statistics as st, collections from pathlib import Path D = Path(sys.argv[1]); OUT = Path(sys.argv[2]) runs = {json.loads(l)["run"]: json.loads(l) for l in open(D / "runs.jsonl")} final = {k: v for k, v in runs.items() if v.get("score")} SEARCH = re.compile(r"^⚙ websearch|^\$ .*\b(pplx|browser search)\b") READ = re.compile(r"^⚙ webfetch|^\$ .*\b(pdftext|doctext)\s+[\"']?https?://|^\$ .*\bbrowser (fetch|asset|session)\b|^\$ .*\bcurl\b.*https?://") DATA = re.compile(r"^\$ .*\b(sec-filings|trade|funding|fda|biotech)\s+\w") RECENT_DATA = re.compile(r"^\$ .*\bsec-filings\b.*--form\s+['\"]?8-K") # a write to the draft, detected from the tool's own result line, which every write style produces WRITE = re.compile(r"^→ (Wrote \d+ bytes to|Edited) \S*draft\.md|^\$ .*(sed -i\b.*draft\.md|>\s*\S*draft\.md|tee \S*draft\.md)") DATED = re.compile(r"(?i)\b(2026|november|nov\b|current|currently|still|latest|today|now|recent|as of)\b") def run_key(r): return f'{r["task"]}-{r["model"]}-{r["repeat"]}' + (f'-{r["step"]}' if r.get("step") else "") rows = {} for name, r in final.items(): tf = D / "traces" / f"{name}.jsonl" if not tf.exists(): continue lines = [json.loads(l) for l in open(tf)] said = [(e["seq"], e["line"]) for e in lines if e.get("event") == "harness_said" and e.get("stream") != "timing"] fw = next((s for s, l in said if WRITE.search(l)), None) searches = [(s, l) for s, l in said if SEARCH.search(l)] reads = [(s, l) for s, l in said if READ.search(l)] datas = [(s, l) for s, l in said if DATA.search(l)] post = [x for x in searches + reads + datas if fw is not None and x[0] > fw] dated_post = [x for x in searches if fw is not None and x[0] > fw and DATED.search(x[1])] + \ [x for x in datas if fw is not None and x[0] > fw and RECENT_DATA.search(x[1])] rows[name] = dict(task=r["task"], model=r["model"], repeat=r["repeat"], acc=r["score"]["accuracy"], first_write=fw, searches=len(searches), reads=len(reads), data=len(datas), post_checks=len(post), post_dated_searches=len(dated_post), verified=1 if post else 0) json.dump(rows, open(OUT / "verify_runs.json", "w"), indent=0) models = ["P1", "DS", "GLM"] def by(feat, rs): return {m: st.mean([r[feat] for r in rs if r["model"] == m]) for m in models} def spread(feat, rs): v = by(feat, rs); return max(v.values()) - min(v.values()) def perm(feat, rs, n=5000, seed=1): rng = random.Random(seed); obs = spread(feat, rs); g = collections.defaultdict(list) for r in rs: g[(r["task"], r["repeat"])].append(r) hit = 0 for _ in range(n): sh = [] for grp in g.values(): labs = [r["model"] for r in grp]; rng.shuffle(labs); sh += [dict(r, model=l) for r, l in zip(grp, labs)] hit += spread(feat, sh) >= obs - 1e-12 return (hit + 1) / (n + 1) rs = list(rows.values()) print("runs", len(rs), "with a draft write:", sum(r["first_write"] is not None for r in rs)) for f in ["searches", "reads", "data", "post_checks", "post_dated_searches", "verified"]: print(f"{f:22s}", {m: round(x, 2) for m, x in by(f, rs).items()}, "perm p =", round(perm(f, rs), 4)) print("\nper task: share of runs that checked the web after drafting (P1|DS|GLM), mean post checks") for t in sorted({r["task"] for r in rs}): sub = [r for r in rs if r["task"] == t] v = by("verified", sub); c = by("post_checks", sub) print(t, "|".join(f"{v[m]:.1f}" for m in models), " checks", "|".join(f"{c[m]:.1f}" for m in models)) print("\nT10 runs") for k in sorted(k for k in rows if k.startswith("T10")): r = rows[k]; print(k, "acc", round(r["acc"], 3), "fw", r["first_write"], "searches", r["searches"], "reads", r["reads"], "data", r["data"], "post", r["post_checks"], "recency", r["post_dated_searches"])