"""Analyze the full unbundle C3 run vs baseline. Reports: - aggregate catch rate across all rows vs baseline 64.7% - T1-slot flips (same trie position as baseline): fixed / regressed / net - tail-turn catch rate, broken down by tail length and by position - the 512 untouched passthrough rows as a sanity control Writes RESULT_all.md. Run: python -u temp/story_remediation/unbundle/analyze_all.py """ from __future__ import annotations import json from collections import defaultdict, Counter from pathlib import Path HERE = Path(__file__).resolve().parent ROOT = HERE.parents[2] OUT = HERE / "out" RES = OUT / "C3_all.jsonl" BASELINE = ROOT / "datasetreview" / "results" / "new" / "C3.jsonl" N100 = ROOT / "distractor_generation_2" / "datasets" / "n100" / "trajectories.jsonl" MD = HERE / "RESULT_all.md" def caught_of(d): if "caught" in d and d["caught"] is not None: return d["caught"] return d.get("guess") == d.get("answer_key") def main(): base = {} for l in open(BASELINE, encoding="utf-8"): if l.strip(): d = json.loads(l); base[d["item_id"]] = d base_caught = {k: caught_of(v) for k, v in base.items()} nb = len(base); bc = sum(base_caught.values()) res = [json.loads(l) for l in open(RES, encoding="utf-8") if l.strip()] ok = [r for r in res if r.get("error") is None and "caught" in r] errs = [r for r in res if r not in ok] # tail length per orig from n100 orig = {json.loads(l)["example_id"]: json.loads(l) for l in open(N100, encoding="utf-8") if l.strip()} tail_len = {e: len(r["calls"]) - (r["metadata"]["anchor_depth"] + 1) for e, r in orig.items()} n = len(ok); c = sum(1 for r in ok if r["caught"]) t1 = [r for r in ok if r.get("role") == "turn1"] tail = [r for r in ok if r.get("role") and r.get("role") != "turn1"] passth = [r for r in ok if not r.get("role")] # T1 flips vs baseline (same eid, same trie position) fixed = regress = kept_c = kept_f = 0 for r in t1: bcaught = base_caught.get(r["item_id"]) if bcaught is None: continue if bcaught and not r["caught"]: fixed += 1 elif not bcaught and r["caught"]: regress += 1 elif bcaught and r["caught"]: kept_c += 1 else: kept_f += 1 t1_caught = sum(1 for r in t1 if r["caught"]) # tail by length and position tail_by_len = defaultdict(lambda: [0, 0]) tail_by_pos = defaultdict(lambda: [0, 0]) for r in tail: oe = r.get("orig_eid"); tl = tail_len.get(oe, 0) tail_by_len[tl][0] += 1; tail_by_len[tl][1] += int(r["caught"]) pos = int(r["role"][4:]) tail_by_pos[pos][0] += 1; tail_by_pos[pos][1] += int(r["caught"]) pc = sum(1 for r in passth if r["caught"]) L = [] def p(s=""): L.append(s); print(s) p("# Full Unbundle: C3 Results\n") p(f"Baseline (n100, single-turn): **{bc}/{nb} = {bc/nb:.1%}** caught\n") p(f"Unbundled dataset: {n} rows judged ({len(errs)} errors)\n") p(f"Overall caught: **{c}/{n} = {c/n:.1%}**\n") p("## Row composition") p(f"- turn1 (preserved trie divergence): {len(t1)}") p(f"- tail turns (one action each): {len(tail)}") p(f"- untouched passthrough: {len(passth)}\n") p("## T1 slots vs baseline (same 283 trie positions)") denom = fixed + regress + kept_c + kept_f p(f"- baseline caught here: {fixed+kept_c}/{denom}") p(f"- unbundled caught here: {t1_caught}/{len(t1)} = {t1_caught/max(len(t1),1):.1%}") p(f"- **fixed (caught -> fooled): {fixed}**") p(f"- regressed (fooled -> caught): {regress}") p(f"- net catch reduction on T1: {fixed-regress}\n") p("## Tail turns (the re-rooted follow-ups)") tc = sum(1 for r in tail if r["caught"]) p(f"- tail caught: {tc}/{len(tail)} = {tc/max(len(tail),1):.1%}") p("- by original tail length:") for tl in sorted(tail_by_len): tot, cc = tail_by_len[tl] p(f" tail_len={tl}: {cc}/{tot} = {cc/tot:.0%}") p("- by turn position:") for pos in sorted(tail_by_pos): tot, cc = tail_by_pos[pos] p(f" turn{pos}: {cc}/{tot} = {cc/tot:.0%}") p("") p("## Passthrough control (should track baseline)") p(f"- caught: {pc}/{len(passth)} = {pc/max(len(passth),1):.1%}") bpc = sum(base_caught.get(r["item_id"], False) for r in passth) p(f"- same rows in baseline: {bpc}/{len(passth)} = {bpc/max(len(passth),1):.1%}\n") # effective per-scenario: does the ORIGINAL confuser (now split) get caught # in ANY of its turns? (a scenario is "detected" if any split turn is caught) by_orig = defaultdict(list) for r in t1 + tail: by_orig[r.get("orig_eid")].append(r["caught"]) scen_any = sum(1 for e, v in by_orig.items() if any(v)) scen_t1only = sum(1 for r in t1 if r["caught"]) p("## Per-scenario view (283 split confusers)") p(f"- caught in >=1 of its turns: {scen_any}/{len(by_orig)} = {scen_any/len(by_orig):.1%}") p(f"- caught at the T1 (trie) slot: {scen_t1only}/{len(by_orig)} = {scen_t1only/len(by_orig):.1%}") baseline_on_split = sum(base_caught.get(e, False) for e in by_orig) p(f"- baseline caught these same confusers: {baseline_on_split}/{len(by_orig)} = {baseline_on_split/len(by_orig):.1%}") MD.write_text("\n".join(L) + "\n", encoding="utf-8") print(f"\nwrote {MD.relative_to(ROOT)}") if __name__ == "__main__": main()