"""Replay episodes against a running server over WebSocket, the way the arena plays them. For every task: reset, run `ls`, then play the given terminal action and check done=true with a reward in [0, 1]. Exits non-zero on the first failure. Usage: python tools/replay.py http://localhost:8000 [--tasks all] [--actions '[{"action_type":"exec","command":"ls -la"},{"action_type":"evaluate"}]'] """ import argparse import asyncio import json import sys import websockets sys.path.insert(0, ".") from terminal_desk.families import all_task_ids # noqa: E402 async def episode(url: str, task_id: str, actions: list[dict]) -> dict: async with websockets.connect(url.replace("http", "ws", 1) + "/ws", max_size=2**24) as ws: await ws.send(json.dumps({"type": "reset", "data": {"task_id": task_id}})) reset = json.loads(await ws.recv()) if reset.get("type") != "observation": raise SystemExit(f"{task_id}: reset failed: {reset}") obs = reset["data"] assert not obs["done"], obs result = None for action in actions: await ws.send(json.dumps({"type": "step", "data": action})) msg = json.loads(await ws.recv()) if msg.get("type") != "observation": raise SystemExit(f"{task_id}: step {action} failed: {msg}") result = msg["data"] if result["done"]: break if not (result and result["done"] and 0.0 <= float(result["reward"]) <= 1.0): raise SystemExit(f"{task_id}: no terminal reward in [0,1]: {result}") return {"task_id": task_id, "instruction_chars": len(obs["observation"]["instruction"]), "reward": result["reward"], "last_output": result["observation"]["output"][:80]} async def main() -> None: p = argparse.ArgumentParser() p.add_argument("url") p.add_argument("--tasks", default="all") p.add_argument("--actions", default='[{"action_type": "exec", "command": "ls -la"}, {"action_type": "evaluate"}]') args = p.parse_args() tasks = all_task_ids() if args.tasks == "all" else args.tasks.split(",") actions = json.loads(args.actions) for t in tasks: r = await episode(args.url, t, actions) print(json.dumps(r)) print(f"OK {len(tasks)} episodes") if __name__ == "__main__": asyncio.run(main())