Download source/scripts/replay.py from rca-umb/five-desks-control: direct link, hf CLI and curl.
- Browser
- Download file 5.16 kB
-
https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/scripts/replay.py
- Command line
-
hf download hf://datasets/rca-umb/five-desks-control/source/scripts/replay.py
-
curl -L -o replay.py https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/scripts/replay.py
5.16 kB
| """Replay every task against a running server: admission examples, oracle, wrong and budget paths. | |
| Usage: python scripts/replay.py --url http://localhost:8000 [--seeds 3] [--out reports/replay.json] | |
| """ | |
| import argparse | |
| import json | |
| import sys | |
| import time | |
| from pathlib import Path | |
| from urllib.request import urlopen | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from five_desks.tasks import TASK_IDS, build # noqa: E402 | |
| from wsclient import Session # noqa: E402 | |
| EXAMPLE_ACTIONS = [{"tool": "list"}, {"tool": "submit", "answer": {}}] | |
| def wrong(value): | |
| if isinstance(value, bool): | |
| return not value | |
| if isinstance(value, (int, float)): | |
| return value * 1.5 + 7 | |
| if isinstance(value, list): | |
| return value[:-1] + ["P999"] | |
| return str(value) + "-wrong" | |
| def run(url: str, seeds: int) -> dict: | |
| with urlopen(f"{url}/schema", timeout=15) as response: | |
| schema = json.load(response) | |
| report = {"url": url, "episodes": 0, "failures": [], "schema": {k: schema[k] for k in ("action", "observation")}} | |
| started = time.monotonic() | |
| def fail(msg): | |
| report["failures"].append(msg) | |
| for task_id in TASK_IDS: | |
| # 1. Admission: the example actions without a seed must end with a terminal reward in [0, 1]. | |
| with Session(url) as s: | |
| s.reset(task_id=task_id) | |
| data = None | |
| for action in EXAMPLE_ACTIONS: | |
| data = s.step(action) | |
| if data["done"]: | |
| break | |
| if not data["done"] or not 0 <= data["reward"] <= 1: | |
| fail(f"{task_id}: example actions did not terminate: {data}") | |
| report["episodes"] += 1 | |
| for seed in range(seeds): | |
| inst = build(task_id, seed) | |
| answer = {p.key: p.expected for p in inst.parts} | |
| # 2. Oracle path: read every file, use calc, submit the right answer -> 1.0. | |
| with Session(url) as s: | |
| obs = s.reset(task_id=task_id, seed=seed) | |
| if obs["done"] or "Submit exactly" not in obs["observation"]["output"]: | |
| fail(f"{task_id}/{seed}: bad reset {obs}") | |
| for name in inst.files: | |
| out = s.step({"tool": "read", "path": name})["observation"]["output"] | |
| if out.strip() != inst.files[name].strip(): | |
| fail(f"{task_id}/{seed}: read {name} mismatch") | |
| calc = s.step({"tool": "calc", "expr": "(2+3)*4"})["observation"]["output"] | |
| if calc != "= 20": | |
| fail(f"{task_id}/{seed}: calc returned {calc!r}") | |
| data = s.step({"tool": "submit", "answer": answer}) | |
| if not data["done"] or data["reward"] != 1.0: | |
| fail(f"{task_id}/{seed}: oracle reward {data.get('reward')}") | |
| after = s.step({"tool": "list"}) | |
| if not after["done"]: | |
| fail(f"{task_id}/{seed}: step after submit did not stay done") | |
| # 3. Wrong answers -> < 1; one right part -> partial credit. | |
| with Session(url) as s: | |
| s.reset(task_id=task_id, seed=seed) | |
| bad = {k: wrong(v) for k, v in answer.items()} | |
| data = s.step({"tool": "submit", "answer": bad}) | |
| if not data["done"] or data["reward"] >= 0.5: | |
| fail(f"{task_id}/{seed}: wrong answer reward {data['reward']}") | |
| with Session(url) as s: | |
| s.reset(task_id=task_id, seed=seed) | |
| first = inst.parts[0].key | |
| partial = {k: (v if k == first else wrong(v)) for k, v in answer.items()} | |
| data = s.step({"tool": "submit", "answer": partial}) | |
| if not 0 < data["reward"] < 1: | |
| fail(f"{task_id}/{seed}: partial reward {data['reward']}") | |
| # 4. Budget: never submitting ends the episode at reward 0. | |
| with Session(url) as s: | |
| s.reset(task_id=task_id, seed=seed) | |
| data = None | |
| for _ in range(inst.max_steps): | |
| data = s.step({"tool": "calc", "expr": "1+1"}) | |
| if not data["done"] or data["reward"] != 0: | |
| fail(f"{task_id}/{seed}: budget end {data}") | |
| report["episodes"] += 5 | |
| report["seconds"] = round(time.monotonic() - started, 1) | |
| report["passed"] = not report["failures"] | |
| return report | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--url", default="http://localhost:8000") | |
| parser.add_argument("--seeds", type=int, default=2) | |
| parser.add_argument("--out", default="") | |
| args = parser.parse_args() | |
| report = run(args.url.rstrip("/"), args.seeds) | |
| if args.out: | |
| Path(args.out).parent.mkdir(parents=True, exist_ok=True) | |
| Path(args.out).write_text(json.dumps(report, indent=2) + "\n") | |
| print(json.dumps({k: report[k] for k in ("passed", "episodes", "seconds")}), "failures:", report["failures"][:10]) | |
| sys.exit(0 if report["passed"] else 1) | |
| if __name__ == "__main__": | |
| main() | |