"""The fix the alignment diagnostic licenses: present the SAME candidate set in both paths. `tool_align.py` measured it: the benchmark's `candidate_tags` returns **40** rows (its helper's default `limit=40`), the shipped tool returns **the full set** (65, 107, 56, 45, ... in the sample; the eval's own characterisation is median 43, p90 73, max 132). So the 0.767 benchmark and the 0.483 product were measured on *different tasks*, and the product's is harder. This ranks the shipped candidate set and caps it at the benchmark's width, so both paths present the same set - and then re-measures V1. If V1 moves toward 0.767, the divergence was causal; if it does not, the divergence was incidental and the gap is elsewhere. Either answer is a result. """ import json, os, pathlib, subprocess, sys, time W = pathlib.Path("/home/marimo/dragon_fsds") OUT = W/"tool_fix.json" N = int(os.environ.get("DRAGON_CAND_CAP", "40")) # 1. measure the shipped width vs the benchmark's width, from the artefact the diagnostic wrote ta = json.loads((W/"tool_align.json").read_text()) widths = [d for r in ta["rows"] for d in r["diffs"] if d["call"] == "candidate_tags"] harness_w = [int(d["dragon_harness"]) for d in widths if str(d["dragon_harness"]).isdigit()] bench_w = [int(d["bench_agent"]) for d in widths if str(d["bench_agent"]).isdigit()] res = {"benchmark_cap": N, "benchmark_widths": bench_w[:8], "shipped_widths": harness_w[:8], "shipped_median": (sorted(harness_w)[len(harness_w)//2] if harness_w else None), "verdict": ("the shipped path presents a WIDER candidate set than the benchmark measured against - " "the two numbers are not like-for-like") if harness_w and max(harness_w) > N else "widths agree; look elsewhere"} # 2. the fix, as an instruction the shipped harness can honour without a code change: cap via env res["fix"] = {"env": {"DRAGON_CAND_MAX": str(N)}, "where": "dragon_harness.candidate_tags (cap + rank by |value| relevance)", "then": "re-run j1 (python v1_test.py 60) and compare against 0.483"} # 3. re-measure V1 with the cap in the environment, if the harness honours it if res["verdict"].startswith("the shipped path"): t0 = time.time() p = subprocess.run(["python", "v1_test.py", "20"], cwd=str(W), env={**os.environ, "DRAGON_FACTS_DB": str(W/"facts.duckdb"), "DRAGON_CAND_MAX": str(N)}, capture_output=True, text=True, timeout=3600) try: v = json.loads((W/"v1_test.json").read_text()) res["v1_with_cap"] = {"n": v["n"], "answer_ok": v["answer_ok"], "fabricated": v["total_fabricated"], "seconds": round(time.time()-t0, 1)} except Exception as e: res["v1_with_cap"] = f"unreadable: {type(e).__name__}" OUT.write_text(json.dumps(res, indent=1, default=str)) print("TOOL FIX", json.dumps(res)[:700], flush=True)