"""Turn every measured arm into a readable failure list, per family. A score of 0.0 on the arithmetic family says nothing about WHAT the arm did. This reads the per-question detail the benchmark records (question, gold, the model's final turn) and writes both a JSON and a Markdown digest, so the next data change is chosen from evidence. """ import glob, json, time from pathlib import Path W = Path("/home/marimo/dragon_fsds") arms = {} for f in sorted(glob.glob(str(W / "agent_benchmark_v4*_results.json"))): d = json.loads(Path(f).read_text()) for key in ("base", "adapter"): r = d.get(key) if not isinstance(r, dict) or "details" not in r: continue label = "base" if key == "base" else d.get("adapter_path", Path(f).stem) label = Path(str(label)).parent.name.replace("dragon-lora-", "") if key == "adapter" else "base" arms[label] = {"file": Path(f).name, "n": r["n"], "answer_ok": r["answer_ok"], "args_ok": r["args_ok"], "by_family": r.get("by_family"), "details": r["details"]} best = max(arms, key=lambda k: arms[k]["answer_ok"]) if arms else None lines = ["# Failure analysis - per arm, per family", ""] for label, r in sorted(arms.items(), key=lambda kv: -kv[1]["answer_ok"]): lines += [f"## {label} (answer_ok {r['answer_ok']:.3f}, args_ok {r['args_ok']:.3f}, n={r['n']})", "", "| family | n | answer_ok | args_ok |", "|---|---:|---:|---:|"] for fam, v in sorted((r["by_family"] or {}).items()): lines.append(f"| `{fam}` | {v['n']} | {v['answer_ok']:.3f} | {v['args_ok']:.3f} |") lines.append("") if label == best: lines += [f"### Every failure of the best arm (`{label}`)", ""] for d in r["details"]: if d["answer_ok"]: continue lines += [f"- **{d['family']}** (qtrs={d['qtrs']}, tag `{d['tag']}`, gold {d['gold']})", f" - Q: {d['question'][:170]}", f" - final: {(d['final'] or '')[:200].replace(chr(10), ' ')}"] result = {"at": time.strftime("%Y-%m-%d %H:%M:%S"), "best_arm": best, "arms": {k: {kk: vv for kk, vv in v.items() if kk != "details"} for k, v in arms.items()}} (W / "failure_analysis.json").write_text(json.dumps(result, indent=2)) (W / "failure_analysis.md").write_text("\n".join(lines) + "\n") print(f"analysed {len(arms)} arms, best={best}; wrote failure_analysis.md")