File size: 2,606 Bytes
dcef129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
"""Replay Five Desks v2 tasks over the live WebSocket API: example actions, oracle, naive and finish paths.

Usage: .venv/bin/python scripts/replay_v2.py --url http://localhost:8001 --seeds 5 --out reports/replay-v2.json
"""

import argparse
import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(Path(__file__).resolve().parent))

from diligence.environment import TASK_IDS, build  # noqa: E402
from wsclient import Session  # noqa: E402

EXAMPLE = [{"tool": "list_files", "args": {}}, {"tool": "submit", "args": {}}]
FINISH = {"tool": "submit", "args": {}}


def run(url, task_id, seed, actions):
    with Session(url) as s:
        data = s.reset(task_id=task_id, seed=seed)
        assert not data["done"]
        for action in actions:
            data = s.step(action)
            if data["done"]:
                break
        return data


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--url", default="http://localhost:8001")
    ap.add_argument("--seeds", type=int, default=5)
    ap.add_argument("--out", default="reports/replay-v2.json")
    args = ap.parse_args()
    results, failures = [], 0
    for task_id in TASK_IDS:
        for seed in range(args.seeds):
            task = build(task_id, seed)
            ex = run(args.url, task_id, seed, EXAMPLE)
            oracle = run(args.url, task_id, seed, task.oracle())
            naive = run(args.url, task_id, seed, task.naive())
            fin = run(args.url, task_id, seed, [FINISH])
            # unseeded example run, as the arena plays it
            unseeded = run(args.url, task_id, None, EXAMPLE) if seed == 0 else ex
            ok = (ex["done"] and 0 <= ex["reward"] <= 1 and oracle["done"] and oracle["reward"] == 1.0
                  and naive["done"] and naive["reward"] < 1.0 and fin["done"] and fin["reward"] == 0.0
                  and unseeded["done"])
            failures += not ok
            results.append({"task_id": task_id, "seed": seed, "ok": ok, "example": ex["reward"],
                            "oracle": oracle["reward"], "naive": naive["reward"], "finish": fin["reward"]})
            if not ok:
                print("FAIL", results[-1])
    Path(args.out).write_text(json.dumps({"url": args.url, "episodes": len(results) * 5, "failures": failures,
                                          "results": results}, indent=1) + "\n")
    print(f"{len(results)} task/seed pairs, {len(results) * 5} episodes, failures: {failures}")
    sys.exit(1 if failures else 0)


if __name__ == "__main__":
    main()