Official Codebase of 3DCodeVerse
Python
bench: the compute-matched best-of-k baseline, and the script that reads it
ziyao0140662f8263
diff --git a/harness/bench/bestofk_report.py b/harness/bench/bestofk_report.pynew file mode 100644index 00000000..ad48774a--- /dev/null+++ b/harness/bench/bestofk_report.py@@ -0,0 +1,128 @@+"""Is the harness better than sampling k times for the same money?++The reviewer question a harness paper has to answer: a loop that plans, builds, gates and+refines costs ~28x one raw generation on `compare_v4` (median generation cost $0.9221+against $0.0327). Spend that money on 28 independent one-shot samples and keep the best —+does the harness still win?++ python bench/bestofk_report.py bench/out/bestofk --against bench/out/compare_v4++`bestofk/rep*/results.jsonl` are K independent one-shot batteries over the same prompts+and the same fixed judge; the best-of-k curve is computed here rather than by a new arm+kind, so no code path the comparison depends on is new. The harness column is READ from+the recorded battery — its generator (`api-agent`) was deleted from the tree on 2026-08-28,+so those rows are the arm the paper reports.++**The winner's curse is reported, not hidden.** max() over k noisy scores overestimates+the true best by roughly `sigma * sqrt(2 ln k)`; with the pro judge's sigma = 0.030+(PAPER_WRITING §2) that is +0.078 at k = 28. The bias runs AGAINST the harness, so a+harness win that survives it is the conservative reading; a harness loss inside that band+is not a loss.+"""++from __future__ import annotations++import argparse+import json+import math+import statistics+import sys+from collections import defaultdict+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parent.parent))++#: judge repeatability on the calibration set (docs/PAPER_WRITING.md §2), used only to+#: state how much of a best-of-k maximum is selection noise+JUDGE_SIGMA = 0.030+++def _rows(path: Path) -> list[dict]:+ return [json.loads(x) for x in path.read_text().splitlines() if x.strip()]+++def one_shot_samples(root: Path) -> dict[str, list[float]]:+ """``{prompt: [score per rep]}`` — one entry per rep that scored that prompt."""+ out: dict[str, list[float]] = defaultdict(list)+ for rep in sorted(root.glob("rep*/results.jsonl")):+ seen: dict[str, float] = {}+ for r in _rows(rep):+ if r.get("score") is None:+ continue+ seen[r["prompt_id"]] = float(r["score"]) # last row wins, as paired_compare does+ for pid, sc in seen.items():+ out[pid].append(sc)+ return dict(out)+++def harness_scores(root: Path, *, arm_prefix: str = "harness:") -> dict[str, float]:+ """``{prompt: score}`` for the recorded harness arm, last row per prompt."""+ out: dict[str, float] = {}+ for r in _rows(root / "results.jsonl"):+ if not str(r.get("arm", "")).startswith(arm_prefix) or r.get("score") is None:+ continue+ out[r["prompt_id"]] = float(r["score"])+ return out+++def best_of(samples: list[float], k: int) -> float | None:+ """Best of the FIRST k samples — the reps are independent and unordered, so this is+ one draw of best-of-k rather than the best over everything available."""+ return max(samples[:k]) if len(samples) >= k else None+++def curve(one: dict[str, list[float]], harness: dict[str, float], ks: list[int]) -> list[dict]:+ out = []+ for k in ks:+ pairs = [(p, best_of(s, k), harness[p]) for p, s in one.items() if p in harness]+ pairs = [(p, b, h) for p, b, h in pairs if b is not None]+ if not pairs:+ continue+ deltas = [h - b for _, b, h in pairs]+ wins = sum(1 for d in deltas if d > 0)+ losses = sum(1 for d in deltas if d < 0)+ mean = statistics.mean(deltas)+ se = statistics.stdev(deltas) / math.sqrt(len(deltas)) if len(deltas) > 1 else 0.0+ out.append({"k": k, "n": len(pairs), "best_of_k": statistics.mean(b for _, b, _ in pairs),+ "harness": statistics.mean(h for _, _, h in pairs), "delta": mean,+ "ci": 1.96 * se, "wins": wins, "losses": losses,+ "curse": JUDGE_SIGMA * math.sqrt(2 * math.log(k)) if k > 1 else 0.0})+ return out+++def report(root: Path, against: Path) -> str:+ one = one_shot_samples(root)+ harness = harness_scores(against)+ if not one or not harness:+ return f"nothing to compare: {len(one)} prompts sampled, {len(harness)} harness rows"+ reps = max(len(v) for v in one.values())+ ks = [k for k in (1, 2, 4, 8, 16, 28) if k <= reps]+ lines = [f"# harness vs best-of-k one-shot — {len(one)} prompts, up to {reps} reps", "",+ f"Winner's-curse bound: judge sigma {JUDGE_SIGMA:.3f}, so a best-of-k maximum carries",+ "about `sigma*sqrt(2 ln k)` of selection noise. It inflates the BASELINE, never the harness.",+ "", "| k | n | best-of-k | harness | harness − best-of-k | 95 % CI | W/L | curse bound |",+ "|--:|--:|--:|--:|--:|---|---|--:|"]+ for row in curve(one, harness, ks):+ lines.append(f"| {row['k']} | {row['n']} | {row['best_of_k']:.3f} | {row['harness']:.3f} | "+ f"{row['delta']:+.3f} | [{row['delta'] - row['ci']:+.3f}, {row['delta'] + row['ci']:+.3f}] | "+ f"{row['wins']}/{row['losses']} | {row['curse']:+.3f} |")+ counts = sorted({len(v) for v in one.values()})+ lines += ["", f"reps per prompt: {counts[0]}–{counts[-1]}"]+ return "\n".join(lines)+++def main(argv: list[str] | None = None) -> int:+ ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])+ ap.add_argument("root", type=Path, help="directory holding rep*/results.jsonl")+ ap.add_argument("--against", type=Path, required=True, help="recorded battery with the harness arm")+ ap.add_argument("--out", type=Path)+ ns = ap.parse_args(argv)+ text = report(ns.root, ns.against)+ print(text)+ if ns.out:+ ns.out.write_text(text)+ return 0+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/harness/tests/compare_bench/test_bestofk_report.py b/harness/tests/compare_bench/test_bestofk_report.pynew file mode 100644index 00000000..526e049c--- /dev/null+++ b/harness/tests/compare_bench/test_bestofk_report.py@@ -0,0 +1,92 @@+"""``bench/bestofk_report.py``: the compute-matched baseline a harness paper has to answer.++A loop that plans, builds, gates and refines costs ~28x one raw generation on `compare_v4`+($0.9221 against $0.0327 median generation cost). Spend that on 28 one-shot samples and+keep the best — does the harness still win? These pin the three things the answer depends+on: which sample the curve takes, how a maximum over noisy scores is reported, and which+direction the difference is measured in.+"""++from __future__ import annotations++import json+import math+from pathlib import Path++import pytest++from bench.bestofk_report import (+ JUDGE_SIGMA,+ best_of,+ curve,+ harness_scores,+ one_shot_samples,+ report,+)+++def _rep(root: Path, name: str, scores: dict[str, float | None], *, extra: list[dict] | None = None) -> None:+ d = root / name+ d.mkdir(parents=True, exist_ok=True)+ rows = [{"prompt_id": p, "arm": "oneshot:x", "score": s} for p, s in scores.items()]+ rows += extra or []+ (d / "results.jsonl").write_text("\n".join(json.dumps(r) for r in rows))+++def test_best_of_takes_the_first_k_and_refuses_to_pad():+ """The reps are independent and unordered, so best-of-k is the best of the FIRST k —+ taking the best of everything available would report best-of-28 as best-of-4."""+ assert best_of([0.1, 0.9, 0.5], 2) == 0.9+ assert best_of([0.1, 0.9, 0.5], 3) == 0.9+ assert best_of([0.1, 0.9], 3) is None, "fewer samples than k is not a best-of-k"+ # order matters: the same multiset with the high draw late is a different best-of-2+ assert best_of([0.1, 0.5, 0.9], 2) == 0.5+++def test_a_rep_that_scored_a_prompt_twice_counts_once(tmp_path: Path):+ """`results.jsonl` is append-only and a resumed cell writes a second row; the last one+ wins, exactly as `paired_compare` dedupes."""+ _rep(tmp_path, "rep01", {"a": 0.2}, extra=[{"prompt_id": "a", "arm": "oneshot:x", "score": 0.7}])+ assert one_shot_samples(tmp_path) == {"a": [0.7]}+++def test_an_unscored_cell_is_dropped_not_zeroed(tmp_path: Path):+ """A cell with no score tests nothing about the model; scoring it 0 would make+ best-of-k look worse for free."""+ _rep(tmp_path, "rep01", {"a": None, "b": 0.4})+ assert one_shot_samples(tmp_path) == {"b": [0.4]}+++def test_only_the_harness_arm_is_read_from_the_recorded_battery(tmp_path: Path):+ (tmp_path / "results.jsonl").write_text("\n".join(json.dumps(r) for r in [+ {"prompt_id": "a", "arm": "oneshot:gemini:x", "score": 0.1},+ {"prompt_id": "a", "arm": "harness:api-agent:gemini:x", "score": 0.6},+ {"prompt_id": "b", "arm": "oneshot+repair:gemini:x", "score": 0.3},+ ]))+ assert harness_scores(tmp_path) == {"a": 0.6}+++def test_the_difference_is_harness_minus_baseline_and_the_curse_is_reported():+ one = {"a": [0.1, 0.9], "b": [0.2, 0.3]}+ harness = {"a": 0.5, "b": 0.5}+ rows = {r["k"]: r for r in curve(one, harness, [1, 2])}++ # k=1 takes the first sample of each: 0.1 and 0.2, so the harness leads by 0.35+ assert rows[1]["best_of_k"] == pytest.approx(0.15) and rows[1]["delta"] == pytest.approx(0.35)+ assert rows[1]["wins"] == 2 and rows[1]["losses"] == 0+ # k=2 lets `a` draw its 0.9 and the harness LOSES that prompt+ assert rows[2]["best_of_k"] == pytest.approx(0.6) and rows[2]["delta"] == pytest.approx(-0.10)+ assert rows[2]["wins"] == 1 and rows[2]["losses"] == 1+ # the curse bound grows with k and is stated so a reader can discount the baseline+ assert rows[1]["curse"] == 0.0+ assert rows[2]["curse"] == pytest.approx(JUDGE_SIGMA * math.sqrt(2 * math.log(2)))+++def test_a_prompt_the_harness_never_ran_is_not_paired(tmp_path: Path):+ _rep(tmp_path / "reps", "rep01", {"a": 0.2, "orphan": 0.9})+ (tmp_path / "results.jsonl").write_text(json.dumps(+ {"prompt_id": "a", "arm": "harness:x", "score": 0.6}))+ rows = curve(one_shot_samples(tmp_path / "reps"), harness_scores(tmp_path), [1])+ assert rows[0]["n"] == 1, "only prompts both arms ran are a pair"+ text = report(tmp_path / "reps", tmp_path)+ assert "| 1 | 1 |" in text