File size: 2,882 Bytes
49aa045
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Corpus reference validation with Python actions delegated to an isolated worker."""

import argparse
import json
import logging
import time
from collections import Counter
from pathlib import Path

from .adapter import ROOT, task_id
from .oracle import solve
from .runtime.service import Episode, upstream


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--worker-url", required=True)
    parser.add_argument("--report", type=Path, default=ROOT / "reports/corpus.json")
    args = parser.parse_args()
    logging.disable(logging.CRITICAL)
    rows = []
    started = time.monotonic()
    for source in sorted(
        (ROOT / "data/questions").glob("*/narrative/*.json"), key=task_id
    ):
        scenario = json.loads(source.read_text())
        episode = Episode(source, 500, worker_url=args.worker_url)
        visited_templates = set()

        def action(payload):
            response = episode.dispatch(payload)
            if payload["action"] == "tool_call" and response.get("ok"):
                node = next(
                    n for n in scenario["nodes"] if n["id"] == payload["node_id"]
                )
                visited_templates.add(node["template_id"])
            return response

        row = {"task_id": task_id(source), "difficulty": int(source.parent.parent.name)}
        try:
            row.update(solve(scenario, action, episode.initial))
            row["templates_executed"] = sorted(visited_templates)
        except Exception as exc:
            row.update(won=False, error=str(exc), actions=episode.count)
        rows.append(row)
        for mapping in (
            upstream.SCENARIOS,
            upstream.SIMULATORS,
            upstream.RAW_NODES_DATA,
            upstream.FILE_METADATA,
        ):
            mapping.pop(episode.case_id, None)
        if len(rows) % 30 == 0:
            print(
                f"Validated {len(rows)}/270, failures={sum(not r['won'] for r in rows)}",
                flush=True,
            )
    report = {
        "scope": "In-process upstream facade and public action oracle; Python execution uses isolated Docker worker. Not 270 Harbor container trials.",
        "tasks": len(rows),
        "successes": sum(r["won"] for r in rows),
        "difficulty_counts": dict(Counter(row["difficulty"] for row in rows)),
        "templates_executed": sorted(
            {t for row in rows for t in row.get("templates_executed", [])}
        ),
        "seconds": round(time.monotonic() - started, 2),
        "results": rows,
    }
    args.report.parent.mkdir(parents=True, exist_ok=True)
    args.report.write_text(json.dumps(report, indent=2) + "\n")
    print(json.dumps({k: v for k, v in report.items() if k != "results"}))
    raise SystemExit(0 if report["tasks"] == report["successes"] == 270 else 1)


if __name__ == "__main__":
    main()