"""Replay admission-style episodes against a running server over the OpenEnv WebSocket. For every task: the example actions end with done=true and a reward in [0, 1]; the reference answer scores 1; a wrong answer scores below 1; the python tool cannot read the task sources; and an episode that never submits ends at the action budget with reward 0. """ import argparse import json import sys import time from pathlib import Path from urllib.request import urlopen from websockets.sync.client import connect sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from workbench.models import MAX_STEPS # noqa: E402 from workbench.tasks import ALL_TASK_IDS, build_task # noqa: E402 # every family at levels 1 and 2, plus the Repo Repair levels that get submitted TASK_IDS = ALL_TASK_IDS + [f"{t.split('@')[0]}@{lv}" for t in ALL_TASK_IDS if t.startswith("swe-repo-") and t.endswith("@1") for lv in (3, 4)] EXAMPLE_ACTIONS = [{"tool": "ls"}, {"tool": "submit", "answer": ""}] def exchange(ws, message): ws.send(json.dumps(message)) reply = json.loads(ws.recv(timeout=60)) assert reply.get("type") == "observation", reply data = reply["data"] assert isinstance(data["done"], bool) and 0 <= data["reward"] <= 1, data return data def episode(url, task_id, seed, actions): with connect(url, open_timeout=15) as ws: first = exchange(ws, {"type": "reset", "data": {"task_id": task_id, "seed": seed}}) assert not first["done"] result = first for action in actions: result = exchange(ws, {"type": "step", "data": action}) return first, result def main(): parser = argparse.ArgumentParser() parser.add_argument("--url", default="http://127.0.0.1:8000") parser.add_argument("--seeds", type=int, default=3) parser.add_argument("--out", default="artifacts/replay.json") args = parser.parse_args() base = args.url.rstrip("/") ws_url = base.replace("http", "ws", 1) + "/ws" schema = json.load(urlopen(f"{base}/schema", timeout=15)) Path(args.out).parent.mkdir(parents=True, exist_ok=True) Path(args.out).with_name("schema.json").write_text( json.dumps({k: schema[k] for k in ("action", "observation")}, indent=2) + "\n") report, started = [], time.monotonic() for task_id in TASK_IDS: for seed in range(args.seeds): task = build_task(task_id, seed) first, ex = episode(ws_url, task_id, seed, EXAMPLE_ACTIONS) assert ex["done"] and ex["reward"] == 0.0, (task_id, ex) if task.meta.get("grades_workspace"): # Repo Repair: write the fixed files, then submit fixes = [{"tool": "write", "path": m["path"], "content": task.meta["pristine"][m["path"]]} for m in task.meta["mutants"]] _, good = episode(ws_url, task_id, seed, fixes + [{"tool": "submit", "answer": ""}]) assert good["done"] and good["reward"] == 1.0, (task_id, seed, good) bad = ex # untouched repository already scored 0 above else: assert task.oracle not in json.dumps(first) or len(task.oracle) < 8, task_id _, good = episode(ws_url, task_id, seed, [{"tool": "submit", "answer": task.oracle}]) assert good["done"] and good["reward"] == 1.0, (task_id, seed, good) _, bad = episode(ws_url, task_id, seed, [{"tool": "submit", "answer": task.wrong[0]}]) assert bad["done"] and bad["reward"] < 1.0, (task_id, seed, bad) report.append({"task_id": task_id, "seed": seed, "example": ex["reward"], "oracle": good["reward"], "wrong": bad["reward"], "reset_chars": len(first["observation"]["output"])}) probe = "import glob\ntry:\n open('/app/workbench/grading.py')\n print('READABLE')\nexcept OSError as e:\n print('blocked', type(e).__name__)\n" _, sandbox = episode(ws_url, TASK_IDS[0], 0, [{"tool": "python", "code": probe}]) _, budget = episode(ws_url, TASK_IDS[0], 0, [{"tool": "ls"}] * MAX_STEPS) assert budget["done"] and budget["reward"] == 0.0, budget summary = {"passed": True, "episodes": len(report) * 3 + 2, "tasks": len(TASK_IDS), "sandbox_probe": sandbox["observation"]["output"].strip(), "max_reset_chars": max(r["reset_chars"] for r in report), "duration_s": round(time.monotonic() - started, 1), "results": report} Path(args.out).write_text(json.dumps(summary, indent=2) + "\n") print(json.dumps({k: v for k, v in summary.items() if k != "results"})) if __name__ == "__main__": main()