File size: 2,504 Bytes
90cf45f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | """Extract the per-task fields of our Legion Pi run that build.py needs into source/legion_pi_per_task.jsonl.
Input: a Legion sweep root (its rows.jsonl plus the Harbor Trial folders of each cell's selected attempt). The
output keeps only task-level numbers and the submitted answer: no trajectories, hosts, paths or tokens.
Usage: python scripts/extract_legion_run.py SWEEP_ROOT [OUT] (OUT defaults to source/legion_pi_per_task.jsonl)
"""
import json
import sys
from pathlib import Path
root = Path(sys.argv[1])
out = Path(sys.argv[2]) if len(sys.argv) > 2 else Path(__file__).resolve().parent.parent / "source" / "legion_pi_per_task.jsonl"
def load_traj(path):
raw = path.read_text()
try:
return json.loads(raw)
except ValueError:
# Harbor's scrubber can leave "\[REDACTED]" inside a JSON escape; re-escape it to parse.
return json.loads(raw.replace("\\[REDACTED]", "\\\\[REDACTED]"))
rows = []
for line in (root / "rows.jsonl").read_text().splitlines():
cell = json.loads(line)
assert cell["status"] == "completed", cell["selector"]
att = Path(cell["selected_attempt"])
trials = sorted((root / att / "results" / att.name / "agent-001" / "trials").iterdir())
assert len(trials) == 1, (cell["selector"], trials)
t = trials[0]
res = json.loads((t / "result.json").read_text())
grade = json.loads((t / "verifier" / "grade.json").read_text())
reward = res["verifier_result"]["rewards"]["reward"]
assert bool(reward) == bool(grade["passed"]), cell["selector"]
sub = grade.get("submission")
steps = load_traj(t / "agent" / "trajectory.json")["steps"]
agent = [s for s in steps if s.get("source") == "agent" and (s.get("extra") or {}).get("stop_reason")]
rows.append({
"task": cell["selector"],
"submission": sub.strip() if isinstance(sub, str) else None,
"correct": bool(reward),
"grade_why": grade.get("why"),
"output_tokens": res["agent_result"]["n_output_tokens"],
"llm_calls": len(agent),
"tool_calls": sum(len(s.get("tool_calls") or []) for s in steps if s.get("source") == "agent"),
"length_stops": sum(s["extra"]["stop_reason"] == "length" for s in agent),
})
rows.sort(key=lambda r: r["task"])
assert len(rows) == len({r["task"] for r in rows}) == 600, len(rows)
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in rows))
print(f"wrote {len(rows)} rows to {out}")
|