Datasets:
Download source/src/simready/generation/contracts.py from AndyZ2001/mingru-mojoco-runs: direct link, hf CLI and curl.
- Browser
- Download file 15.4 kB
-
https://huggingface.co/datasets/AndyZ2001/mingru-mojoco-runs/resolve/main/source/src/simready/generation/contracts.py
- Command line
-
hf download hf://datasets/AndyZ2001/mingru-mojoco-runs/source/src/simready/generation/contracts.py
-
curl -L -o contracts.py https://huggingface.co/datasets/AndyZ2001/mingru-mojoco-runs/resolve/main/source/src/simready/generation/contracts.py
15.4 kB
| """One agent contract per track and engine: what the agent is given, what it never sees, what it hands back, under which | |
| budget, image and engine, and the exact words it is told (the files under prompts/). | |
| A contract has a name and no version. Its identity is the sha256 of its content: this record, every prompt file it | |
| renders and every file it stages into the container (self-check, checker tools). Any change to any of them is a new | |
| hash, recorded in each run's run.json. docs/generation.md has the table. | |
| """ | |
| from functools import cache | |
| import hashlib | |
| import json | |
| from pathlib import Path | |
| import re | |
| import subprocess | |
| from ..common.io import BENCHMARK | |
| HERE = Path(__file__).resolve().parent | |
| PROMPTS, RUNTIME = HERE / "prompts", HERE / "runtime" | |
| EVALUATION = HERE.parent / "evaluation" # simready.evaluation; its checker files are staged into the container as they are | |
| ENGINES = {"mujoco": "MuJoCo", "isaac": "Isaac Sim"} | |
| ENGINE_VERSIONS = {"mujoco": "4.0.0", "isaac": "6.2.0"} | |
| # Generation images (runtime/Dockerfile, runtime/Dockerfile.isaac): the engine plus headless Blender, pinned by version and sha256 | |
| # (runtime/install_blender.sh), the pinned OpenCode CLI and, for MuJoCo, the packages of runtime/requirements-tools.txt. | |
| IMAGES = {"mujoco": "simready-generation:latest", "isaac": "simready-isaac-generation:latest"} | |
| BLENDER_VERSION = "5.2.3" | |
| # The only hosts a generation container reaches, through the egress proxy: the CLIs' model API and login, and OpenCode's two | |
| # web-search services (Claude's and Codex's web search runs on the provider's side). Found by logging every tunnel of a Claude, | |
| # a Codex and an OpenCode session, web search included. Left out: telemetry, Codex's content hosts and the npm registry | |
| # OpenCode tries to install a plugin package from (it runs without it). | |
| OPENCODE_HOSTS = ("opencode.ai", "mcp.exa.ai", "search.parallel.ai") | |
| EGRESS_HOSTS = ("api.anthropic.com", "platform.claude.com", "chatgpt.com", "auth.openai.com") + OPENCODE_HOSTS | |
| # Claude Code is mounted from the host. Codex and OpenCode are installed in both images, | |
| # pinned by version and sha256 (runtime/install_codex.sh and runtime/install_opencode.sh). | |
| CLIS = { | |
| "claude": {"name": "Claude Code", "binary": "host ~/.local/bin/claude, mounted read-only", | |
| "login": "<config dir>/.credentials.json (default ~/.claude; --claude-config-dir)", | |
| "tools": "every tool but WebFetch (--disallowedTools WebFetch); WebSearch runs on Anthropic's side", | |
| "hosts": ["api.anthropic.com", "platform.claude.com"]}, | |
| "codex": {"name": "Codex CLI", "version": "0.167.0", "binary": "/opt/codex/bin/codex in both generation images", | |
| "binary_sha256": "cdcb45aaa01d0f14e298985e09465aebccd47f64ecd298aea44814af7b62bbda", "login": "~/.codex/auth.json", | |
| "tools": 'every tool, web_search="live", browser features off', "hosts": ["chatgpt.com", "auth.openai.com"]}, | |
| "opencode": {"name": "OpenCode", "version": "1.19.5", "binary": "/opt/opencode/bin/opencode in both generation images", | |
| "binary_sha256": "7df4e4136301f16589b0f30bf91a61380fca5f8ef22dd36570f5ebe1d4ec802c", | |
| "login": "host OpenCode V2 auth export/import API, or legacy ~/.local/share/opencode/auth.json", "billing": "subscription (OpenCode Go)", | |
| "tools": "every tool but webfetch (denied by its permission config, so not offered); websearch on (Exa or Parallel)", | |
| "hosts": list(OPENCODE_HOSTS)}, | |
| } | |
| # Whether an OpenCode model's read tool passes images to it, measured with a probe PNG (described correctly by both). | |
| # A model not listed is recorded as not measured (None); the prompt is the same either way. | |
| OPENCODE_IMAGE_INPUT = {"opencode-go/kimi-k3.1-pro": True, "opencode-go/deepseek-v4.1-flash": True} | |
| ISOLATION = {"container": "Docker: read-only input (and tools), writable output and scratch, temporary credentials; no repository, " | |
| "benchmark ground truth or evaluator files", | |
| "network": "an internal network whose only exit is an HTTPS CONNECT proxy (runtime/egress_proxy.py)", "egress_hosts": list(EGRESS_HOSTS)} | |
| BUDGET = {"told": "the first of the session's cumulative budgets, in wall-clock seconds from container start", | |
| "resume": "each later budget resumes the same conversation for the rest of its time; each budget is its own run", | |
| "enforcement": "the runner kills the container at the deadline; the output as it stands is the result"} | |
| PROTOCOL = "the frozen protocol of the track and engine (data/benchmark/frozen/<track>/<engine>.json)" | |
| SCENE_INPUTS = ["video.mp4", "scale_anchor.json", "anchor_reference.png", "anchor_marked.png", "prompt.txt", "generation_spec.json", "self_check.py"] | |
| DEMO_WITHHELD = ["groundtruth/: the wrist video and its calibration, the stereo videos, the robot log (trajectory.h5), the end-state label, " | |
| "the measured ground truth", "camera serials and provenance/", PROTOCOL, "other runs"] | |
| DEMO_OUTPUTS = {"mujoco": ["scene.xml (bodies moved, target, table; only target when the table is the target)", "assets/", "objects.json", "README.md"], | |
| "isaac": ["scene.usda (/World/moved, /World/target, /World/table)", "objects.json", "README.md"]} | |
| DEMO_TOOLS = {"mujoco": {"check_package.py": "demos/check_mujoco.py", "mjcf_facts.py": "demos/mjcf_facts.py", "rules.py": "demos/rules.py"}, | |
| "isaac": {"check_package.py": "demos/check_isaac.py", "usd_facts.py": "demos/usd_facts.py", "rules.py": "demos/rules.py"}} | |
| RIGID_OUTPUTS = {"mujoco": ["scene.xml (body object with a freejoint, ground plane)", "assets/", "objects.json", "README.md"], | |
| "isaac": ["scene.usda (object, PhysX scene, box ground)", "objects.json", "README.md"]} | |
| DEFORMABLE_OUTPUTS = {"mujoco": ["scene.xml (one flexcomp, dof full)", "assets/ (only if the flexcomp reads a mesh)", "README.md"], | |
| "isaac": ["scene.usda (/World/Object/Material)", "meta.json"]} | |
| DEFORMABLE_TOOLS = {"mujoco": {"check_package.py": "engines/mujoco/deformable_admission.py"}, | |
| "isaac": {"check_package.py": "engines/isaac/container/deformable_check.py", | |
| "deformable_inspect.py": "engines/isaac/container/deformable_inspect.py"}} | |
| SCENE_OUTPUTS = {"mujoco": ["scene.xml", "assets/", "objects.json", "README.md", "refinement_log.json", "previews/"], | |
| "isaac": ["scene.usda", "build_scene.py", "objects.json", "render_preview.py", "preview.mp4", "interaction.mp4", "README.md", "refinement_log.json"]} | |
| SCENE_CHECK = {"mujoco": "python /work/input/self_check.py /work/output", | |
| "isaac": "/isaac-sim/python.sh /work/input/self_check.py --scene /work/output --out /tmp/self_check"} | |
| REFINEMENT = {"minimum_completed_rounds": 3, "minimum_reference_views": 6, "minimum_inspected_video_frames": 12, "final_validation_seconds": 90} | |
| def _contracts(): | |
| found = {} | |
| for engine in ENGINES: | |
| found[f"scenes-{engine}"] = { | |
| "track": "scenes", "engine": engine, "inputs": SCENE_INPUTS + (["sdk_smoke.py"] if engine == "isaac" else []), | |
| "withheld": ["groundtruth/ (objects, cameras, task catalogs, object dynamics, interaction test) and its source tag", | |
| "provenance/ (source annotation, anchor selection)", PROTOCOL, "other runs"], | |
| "outputs": SCENE_OUTPUTS[engine], "self_check": SCENE_CHECK[engine], "refinement": REFINEMENT, | |
| "prompt": ["scenes/about_" + engine + ".md", "scenes/task.md", "scenes/self_check.md", "scenes/output_" + engine + ".md"], | |
| "runtime_note": "scenes/runtime_mujoco.md" if engine == "mujoco" else None, | |
| # The Isaac room self-check is the evaluator's passive-physics stage (it checks the stated requirements only). | |
| "files": {"input/self_check.py": "runtime/self_check_mujoco.py" if engine == "mujoco" else "evaluation:engines/isaac/container/isaac_physics.py", | |
| **({"input/sdk_smoke.py": "runtime/isaac_smoke.py"} if engine == "isaac" else {})}} | |
| found[f"objects-rigid-{engine}"] = { | |
| "track": "objects", "material": "rigid", "engine": engine, "inputs": ["object.json (name, category)", "photos/"], | |
| "withheld": ["groundtruth/ (size, mass, description, the reference scan)", "provenance/", PROTOCOL, "other reconstructions"], | |
| "outputs": RIGID_OUTPUTS[engine], "self_check": None, "refinement": None, "prompt": [f"objects_rigid/{engine}.md"], "files": {}} | |
| found[f"objects-deformable-{engine}"] = { | |
| "track": "objects", "material": "deformable", "engine": engine, "inputs": ["case.txt", "object_facts.json", "media_manifest.json", "media/"], | |
| "withheld": ["groundtruth/ (reference size and shape)", "provenance/", PROTOCOL + " (probe, workcell, fixtures, limits)", "other reconstructions"], | |
| "outputs": DEFORMABLE_OUTPUTS[engine], "refinement": None, "prompt": [f"objects_deformable/{engine}.md"], "words": "objects_deformable/words.json", | |
| "self_check": ("python /work/tools/check_package.py /work/output <2d|3d>" if engine == "mujoco" else | |
| "/isaac-sim/python.sh /work/tools/check_package.py"), | |
| "files": {f"tools/{k}": "evaluation:" + v for k, v in DEFORMABLE_TOOLS[engine].items()}} | |
| found[f"demos-{engine}"] = { | |
| "track": "demos", "engine": engine, "inputs": ["instruction.txt", "ext1.mp4", "ext2.mp4", "timestamps.json", | |
| "cameras.json (both exterior poses and intrinsics)"], | |
| "withheld": DEMO_WITHHELD, "outputs": DEMO_OUTPUTS[engine], "refinement": None, "prompt": ["demos/task.md"], | |
| "parts": {"format": f"demos/format_{engine}.md"}, | |
| "self_check": "python /work/tools/check_package.py" if engine == "mujoco" else "/isaac-sim/python.sh /work/tools/check_package.py", | |
| "files": {f"tools/{k}": "evaluation:" + v for k, v in DEMO_TOOLS[engine].items()}} | |
| for name, entry in found.items(): | |
| entry.update(name=name, engine_version=ENGINE_VERSIONS[entry["engine"]], image=IMAGES[entry["engine"]], blender_version=BLENDER_VERSION, | |
| budget=BUDGET, isolation=ISOLATION, protocol=f"data/benchmark/frozen/{entry['track']}/{entry['engine']}.json") | |
| return found | |
| CONTRACTS = _contracts() | |
| def contract_name(track, engine, material=None): | |
| name = f"{track}-{material}-{engine}" if track == "objects" else f"{track}-{engine}" | |
| if name not in CONTRACTS: | |
| raise KeyError(f"No contract for {track} {material or ''} {engine}") | |
| return name | |
| def source(reference): | |
| """The file a contract's `files` entry names: runtime/... in this package, evaluation:... in simready.evaluation.""" | |
| return EVALUATION / reference.removeprefix("evaluation:") if reference.startswith("evaluation:") else HERE / reference | |
| def missing_files(name): | |
| return [ref for ref in CONTRACTS[name]["files"].values() if not source(ref).is_file()] | |
| def prompt_files(name): | |
| entry = CONTRACTS[name] | |
| return sorted({*entry["prompt"], *entry.get("parts", {}).values(), *filter(None, [entry.get("runtime_note"), entry.get("words")]), | |
| "resume.md", "image_tools.json"}) | |
| def _digest(name, stamp): | |
| h = hashlib.sha256(json.dumps(CONTRACTS[name], sort_keys=True).encode()) | |
| for rel in prompt_files(name): | |
| h.update(f"\0prompts/{rel}\0".encode() + (PROMPTS / rel).read_bytes()) | |
| for target, ref in sorted(CONTRACTS[name]["files"].items()): | |
| path = source(ref) | |
| h.update(f"\0{target}\0".encode() + (path.read_bytes() if path.is_file() else b"<missing>")) | |
| return h.hexdigest() | |
| def sha256(name): | |
| """The contract's identity: its record, its prompt files and its staged files.""" | |
| files = [PROMPTS / rel for rel in prompt_files(name)] + [source(ref) for ref in CONTRACTS[name]["files"].values()] | |
| return _digest(name, tuple(p.stat().st_mtime_ns if p.is_file() else 0 for p in files)) | |
| def record(name): | |
| """What run.json records about the contract a run was generated under.""" | |
| entry = CONTRACTS[name] | |
| protocol = BENCHMARK / "frozen" / entry["track"] / f"{entry['engine']}.json" | |
| keep = ("track", "material", "engine", "engine_version", "image", "blender_version", "inputs", "withheld", "outputs", "self_check", | |
| "budget", "refinement", "isolation") | |
| return {"name": name, "sha256": sha256(name), **{k: entry[k] for k in keep if k in entry}, | |
| "protocol": {"file": entry["protocol"], "sha256": _file_sha(protocol) if protocol.is_file() else None}} | |
| def _file_sha(path): | |
| return hashlib.sha256(Path(path).read_bytes()).hexdigest() | |
| # ---------------------------------------------------------------- the words | |
| PLACEHOLDER = re.compile(r"\{\{([a-z_]+)\}\}") | |
| def fill(text, values): | |
| """Replace every {{name}}; a name with no value is an error, so no placeholder reaches an agent.""" | |
| def one(match): | |
| if match.group(1) not in values: | |
| raise KeyError(f"prompt placeholder {{{{{match.group(1)}}}}} has no value") | |
| return str(values[match.group(1)]) | |
| return PLACEHOLDER.sub(one, text) | |
| def read_prompt(rel): | |
| return (PROMPTS / rel).read_text() | |
| def tool_wording(text, cli): | |
| """The prompts name Claude's image tool ("the Read tool"); Codex has none of that name, and OpenCode's is `read`.""" | |
| for old, new in json.loads(read_prompt("image_tools.json")).get(cli, []): | |
| text = text.replace(old, new) | |
| return text | |
| def render(contract, cli="claude", **values): | |
| """Exactly what the agent is told under a contract: (prompt.txt text, submitted text).""" | |
| entry = CONTRACTS[contract] | |
| if entry.get("words"): | |
| values = {**json.loads(read_prompt(entry["words"]))[values.pop("dimension")], **values} | |
| parts = {key: fill(read_prompt(rel), values) for key, rel in entry.get("parts", {}).items()} | |
| text = tool_wording("".join(fill(read_prompt(rel), {**values, **parts}) for rel in entry["prompt"]), cli) | |
| note = read_prompt(entry["runtime_note"]) if entry.get("runtime_note") else "" | |
| return text, text + note | |
| def resume_text(done_seconds, left_seconds): | |
| left = round(left_seconds) | |
| return fill(read_prompt("resume.md"), {"done": round(done_seconds / 60), "left": round(left / 60), "seconds": f"{left:,}"}) | |
| def cli_version(cli): | |
| """The installed version of a host-mounted CLI, or the pinned one.""" | |
| if "version" in CLIS[cli]: | |
| return CLIS[cli]["version"] | |
| binary = str(Path.home() / ".local/bin/claude") | |
| try: | |
| return subprocess.run([binary, "--version"], capture_output=True, text=True, timeout=30).stdout.strip() or None | |
| except (OSError, TypeError, subprocess.SubprocessError): | |
| return None | |
| def cli_record(cli, model, config_dir=None): | |
| """What run.json records about the agent CLI: name, version, binary, tools, hosts, the login folder and, for OpenCode, | |
| whether the model was measured to see images through its read tool (None: not measured).""" | |
| entry = {"cli": cli, **CLIS[cli], "version": cli_version(cli), "model": model} | |
| if cli == "claude": | |
| entry["config_dir"] = str(config_dir) if config_dir else "~/.claude" | |
| if cli == "opencode": | |
| entry["image_input"] = OPENCODE_IMAGE_INPUT.get(model) | |
| return entry | |