#!/usr/bin/env python3 """Run both RE-1 executions and write the capsule metrics file. This is the single published top-level command for the experiment: python3 scripts/run_capsule.py --output-dir /path/to/out It shells out to scripts/run_one.sh twice (fresh containers, same commands), then runs harness/analyze.py and reduces the analysis to metrics.json. """ from __future__ import annotations import argparse import json import os import shutil import subprocess import sys from pathlib import Path HERE = Path(__file__).resolve().parent.parent def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--output-dir", required=True, type=Path) parser.add_argument( "--evidence-dir", type=Path, default=Path(os.environ.get("RE1_EVIDENCE_DIR", str(Path.cwd() / "evidence"))), ) args = parser.parse_args() out = args.output_dir.resolve() out.mkdir(parents=True, exist_ok=True) env = {**os.environ, "RE1_EVIDENCE_DIR": str(args.evidence_dir)} for run_id in ("r1", "r2"): print(f"== {run_id} ==", flush=True) subprocess.run(["bash", str(HERE / "scripts" / "run_one.sh"), run_id], check=True, env=env) runs = args.evidence_dir / "runs" proc = subprocess.run( [ sys.executable, str(HERE / "harness" / "analyze.py"), "--run1", str(runs / "r1"), "--run2", str(runs / "r2"), "--out", str(out / "analysis.json"), "--md", str(out / "analysis.md"), ] ) analysis = json.loads((out / "analysis.json").read_text()) for run_id in ("r1", "r2"): dest = out / run_id dest.mkdir(parents=True, exist_ok=True) shutil.copy2(runs / run_id / "results.json", dest / "results.json") shutil.copy2(runs / run_id / "run_status.json", dest / "run_status.json") def repro(key: str) -> float: return analysis["quantities"].get(key, {}).get("rel_diff", 1.0) metrics = { "decision": analysis["decision"], "max_relative_deviation": max( (row["rel_diff"] for row in analysis["quantities"].values()), default=1.0 ) if not analysis["missing"] else 1.0, "missing_quantities": len(analysis["missing"]), "failing_quantities": len(analysis["failing"]), "quantities_checked": len(analysis["quantities"]), "qwen_ppl_delta_repro": repro("qwen2.5-0.5b-instruct.ppl_delta"), "qwen_hellaswag_delta_repro": repro("qwen2.5-0.5b-instruct.hellaswag_delta_pct"), "smollm_ppl_delta_repro": repro("smollm2-360m-instruct.ppl_delta"), "smollm_hellaswag_delta_repro": repro("smollm2-360m-instruct.hellaswag_delta_pct"), } (out / "metrics.json").write_text(json.dumps(metrics, indent=2, sort_keys=True) + "\n") print(json.dumps(metrics, indent=2)) return proc.returncode if __name__ == "__main__": raise SystemExit(main())