"""Verify a submission request against the built image it names, before anything is pushed. Checks: task ids equal manifest/training_tasks.txt in order, none reserved, every task carries the standard limits; the request schema equals the running container's GET /schema; every training task resets in the container and serves exactly the files and instruction of its frozen fixed instance (tests/fixtures/training_fingerprints.json, sha256 of each file compared); the example actions end an episode with a reward; the finish action evaluates. Prints a JSON report; exits 1 on any failure. Usage: PYTHONPATH=. .venv/bin/python tools/check_package.py --url http://localhost:18001 \ --request ../submission.skills-v1.draft.json [--image-id sha256:...] """ import argparse import asyncio import hashlib import json import random import sys import httpx import websockets sys.path.insert(0, ".") sys.path.insert(0, "tools") from make_submission import LIMITS # noqa: E402 from terminal_desk.families import ALL_FAMILIES, instance_seed, is_reserved, parse_task_id # noqa: E402 LIST_FILES = "find . -type f | sort | while read f; do printf '%s %s\\n' \"$(sha256sum < \"$f\" | cut -c1-64)\" \"${f#./}\"; done" async def served(url: str, task_id: str) -> tuple[str, 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 RuntimeError(f"{task_id}: reset failed: {reset}") await ws.send(json.dumps({"type": "step", "data": {"action_type": "exec", "command": LIST_FILES}})) out = json.loads(await ws.recv())["data"]["observation"]["output"] await ws.send(json.dumps({"type": "step", "data": {"action_type": "close"}})) await ws.recv() files = {} for line in out.splitlines(): digest, _, name = line.partition(" ") files[name] = digest return reset["data"]["observation"]["instruction"], files async def plays(url: str, task_id: str, actions: list) -> 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}})) await ws.recv() last = None for act in actions: await ws.send(json.dumps({"type": "step", "data": act})) last = json.loads(await ws.recv())["data"] return last def expected(task_id: str) -> tuple[str, dict]: fid, level, _ = parse_task_id(task_id) b = ALL_FAMILIES[fid].build(level, random.Random(instance_seed(task_id))) files = {n: hashlib.sha256(c if isinstance(c, bytes) else c.encode()).hexdigest() for n, c in b.files.items()} return b.instruction, files async def main() -> None: p = argparse.ArgumentParser() p.add_argument("--url", required=True) p.add_argument("--request", required=True) p.add_argument("--manifest", default="manifest/training_tasks.txt") p.add_argument("--image-id", default="") a = p.parse_args() req = json.load(open(a.request)) manifest = [ln.strip() for ln in open(a.manifest) if ln.strip() and not ln.startswith("#")] problems, report = [], {} ids = [t["task_id"] for t in req["tasks"]] if ids != manifest: problems.append("task ids differ from the manifest") if any(is_reserved(t) for t in ids): problems.append("reserved task in the request") for t in req["tasks"]: if {k: v for k, v in t.items() if k != "task_id"} != LIMITS: problems.append(f"{t['task_id']}: limits differ from the standard set") schema = httpx.get(f"{a.url}/schema", timeout=10).json() if req["schema"] != {"action": schema["action"], "observation": schema["observation"]}: problems.append("request schema differs from the container's /schema") if req.get("finish_action") != {"action_type": "evaluate"}: problems.append("finish_action is not evaluate") mismatched = [] for t in ids: got_instr, got_files = await served(a.url, t) want_instr, want_files = expected(t) if got_instr != want_instr or got_files != want_files: mismatched.append(t) if mismatched: problems.append(f"container serves different instances for {mismatched}") last = await plays(a.url, ids[0], req["example_actions"]) if not (last and last["done"] and last["reward"] in (0.0, 1.0)): problems.append("example actions do not end the episode with a reward") last = await plays(a.url, ids[-1], [req["finish_action"]]) if not (last and last["done"] and last["reward"] in (0.0, 1.0)): problems.append("finish action does not end the episode with a reward") report.update({ "request": a.request, "request_sha256": hashlib.sha256(open(a.request, "rb").read()).hexdigest(), "manifest_sha256": hashlib.sha256(open(a.manifest, "rb").read()).hexdigest(), "image": req["image"], "local_image_id": a.image_id, "tasks": len(ids), "schema_matches_container": "request schema differs from the container's /schema" not in problems, "tasks_serving_frozen_instances": len(ids) - len(mismatched), "problems": problems, }) print(json.dumps(report, indent=1)) if problems: raise SystemExit(1) if __name__ == "__main__": asyncio.run(main())