"""Run the Python script from each agent answer once, with a 45-second limit, and save what it printed.
Usage: python3 grade.py <run dir>/<setup>   e.g. agent-r1/playwright-mcp"""
import json, pathlib, re, subprocess, sys

d = pathlib.Path(sys.argv[1])
work = pathlib.Path("scripts") / d.parent.name / d.name
work.mkdir(parents=True, exist_ok=True)
for f in sorted(d.glob("*.json")):
    log = json.loads(f.read_text())
    blocks = re.findall(r"```python\n(.*?)```", log.get("final") or "", re.S)
    out = {"task": log["task"], "has_script": bool(blocks)}
    if blocks:
        script = work / f"{log['task']}.py"
        script.write_text(blocks[-1])
        try:
            p = subprocess.run(["uv", "run", "-q", "--no-project", "--with", "httpx", "python", "-I", script.name],
                               cwd=work, capture_output=True, text=True, timeout=45)
            out.update(exit=p.returncode, stdout=p.stdout[-1500:], stderr=p.stderr[-600:])
        except subprocess.TimeoutExpired:
            out.update(exit="timeout", stdout="", stderr="")
    (work / f"{log['task']}.result.json").write_text(json.dumps(out, indent=1))
    print(f"{log['task']:<18} exit={out.get('exit')!s:<7} {out.get('stdout', '')[:110]!r}")
