"""Assemble the Hugging Face dataset folder for Five Desks v5: the finance specialist or the collapse test. Usage: .venv/bin/python scripts/build_dataset_v5.py --mode finance|control (writes dataset_v5_/) """ import argparse import glob import json import shutil import sys from pathlib import Path from statistics import mean ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) from diligence.environment import CONTROL_PREFIX, DiligenceEnvironment, build # noqa: E402 def calibration(pattern): rewards, lengths = {}, {} for f in glob.glob(str(ROOT / pattern)): for line in open(f): ep = json.loads(line) rewards.setdefault(ep["task_id"], []).append(ep["reward"]) lengths.setdefault(ep["task_id"], []).append(len(ep["turns"])) return rewards, lengths def main(): ap = argparse.ArgumentParser() ap.add_argument("--mode", choices=["finance", "control"], required=True) args = ap.parse_args() request = json.loads((ROOT / f"submission_v5_{args.mode}_candidate.json").read_text()) out = ROOT / f"dataset_v5_{args.mode}" shutil.rmtree(out, ignore_errors=True) (out / "calibration").mkdir(parents=True) a_rewards, a_lengths = calibration("reports/calibration-v4a/*/episodes.jsonl") b_rewards, _ = calibration("reports/calibration-code/*/episodes.jsonl") with open(out / "tasks.jsonl", "w") as f: for task in request["tasks"]: tid = task["task_id"] row = {"task_id": tid, "image": task.get("image", request["image"]), "submitted_for_training": True} base = tid.removeprefix(CONTROL_PREFIX) if tid.startswith("bugfix-"): r = b_rewards.get(tid, []) row.update(qwen3_8_27b_mean_reward=round(mean(r), 3) if r else None, qwen3_8_27b_rewards=r) else: t = build(base, 0) row.update(family=t.family, domain=t.domain, max_tool_calls=t.max_steps, example_prompt=DiligenceEnvironment().reset(task_id=tid, seed=0).output) if tid.startswith(CONTROL_PREFIX): # what the control reward would have paid the untrained model calls = a_lengths.get(base, []) row.update(untrained_calls_per_episode=calls, untrained_control_reward=round(mean((t.max_steps - c + 1) / t.max_steps for c in calls), 3) if calls else None) else: r = a_rewards.get(tid, []) row.update(qwen3_8_27b_mean_reward=round(mean(r), 3) if r else None, qwen3_8_27b_rewards=r) f.write(json.dumps(row) + "\n") for run in sorted(glob.glob(str(ROOT / "reports/calibration-v4a/*"))) + ( sorted(glob.glob(str(ROOT / "reports/calibration-code/*"))) if args.mode == "finance" else []): tag = Path(run).parent.name.replace("calibration-", "") + "_" + Path(run).name for name in ["summary.json", "episodes.jsonl"]: if (Path(run) / name).exists(): shutil.copy(Path(run) / name, out / "calibration" / f"{tag}_{name}") shutil.copy(ROOT / "reports/lottery-analysis-2026-10-12.md", out / "calibration/lottery-analysis.md") shutil.copy(ROOT / "reports/v4-arena-gap.md", out / "calibration/v4-arena-gap.md") src = out / "source" skip = shutil.ignore_patterns("__pycache__", "*.pyc", "data", "out", "sdists", "*.log") for item in ["diligence", "codedesk", "server", "scripts", "tests", "proxy_eval", "openenv.yaml", "requirements.in", "requirements.lock", ".dockerignore", "CHANGELOG.md"]: path = ROOT / item if path.is_dir(): shutil.copytree(path, src / item, ignore=skip) elif path.exists(): shutil.copy(path, src / item) shutil.copy(ROOT / f"card_v5_{args.mode}.md", out / "README.md") if args.mode == "control": # so anyone can run the same control from their own account shutil.copy(ROOT / "submission_v5_control_candidate.json", out / "submission_request.json") print("dataset at", out, sorted(p.name for p in out.iterdir())) if __name__ == "__main__": main()