The first exam for code agents in the physical world: frozen robot tasks from many benchmark suites, each answered with a program and verified in a separate sandbox. Runs on Harbor.
Python ai-agentsbenchmarkcoding-agentsembodied-aievaluationharborllm-agentsrobot-learningroboticssimulation
HumanoidBench: realtime mode -- 10 tasks <base>-realtime (eai-standard/2.3) where time does not stop (+2 more) (#83, #88, #90)
Kangrui Wangdf02469d53e9
diff --git a/AGENTS.md b/AGENTS.mdindex 3c4258e54d..7ba0307309 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -34,6 +34,9 @@ Read `README.md`, `docs/PROTOCOL.md` (the contract, v1.1) and `CONTRIBUTING.md`MolmoSpaces standard tasks: its layer (`rcb_limited.py` with crash recovery, `rcb_client.py`, `rcb1.py`, `run.sh`) has one source,`scripts/eai_standard/` (D9), copied unchanged into every 2.2 task (`check-tooling-identity.sh`); each benchmark addsonly its adapter `environment/sim/server.py` (`scripts/eai_standard/toy_server.py` is the smallest one).+- `eai-standard/2.4` (docs/STANDARD_MODE_2_4.md, RoboTangle): 2.3 + the operations capability. Its layer is+ `scripts/eai_standard/v2_4/rcb_limited.py` + `rcb_client.py` with 2.2's `rcb1.py` and `run.sh`, copied unchanged+ into every 2.4 task (`check-tooling-identity.sh`).`python3 scripts/eai_standard/selftest.py` tests the crash recovery on the CPU in about two minutes.- Benchmark-level rules (templates, generators, budgets, tooling file names, the world-frame and tooling declarations):`docs/benchmarks/<bench>.md`.diff --git a/ci_checks/check-protocol-v1.py b/ci_checks/check-protocol-v1.pyindex ce22ee98bd..75d206d5b0 100755--- a/ci_checks/check-protocol-v1.py+++ b/ci_checks/check-protocol-v1.py@@ -18,7 +18,7 @@ from pathlib import PathROOT = Path(__file__).resolve().parent.parentREGISTRY = "ghcr.io/mll-lab-nu/eai-"-PROTOCOLS = {"eai-standard/2.1", "eai-standard/2.2"} # standard mode+PROTOCOLS = {"eai-standard/2.1", "eai-standard/2.2", "eai-standard/2.4"} # standard mode (2.4: the operations capability, issue #89)REALTIME_PROTOCOLS = {"eai-standard/2.3"} # realtime mode (PROTOCOL §4b): 2.3 is the realtime capabilityLEGACY_PROTOCOLS = {"rcb-limited/2.1"}PERSONAL_PATH = re.compile(rb"/(home|Users)/[A-Za-z0-9_.-]+/")diff --git a/ci_checks/check-standard-client.sh b/ci_checks/check-standard-client.shindex ddf20a11ec..078d9c002b 100755--- a/ci_checks/check-standard-client.sh+++ b/ci_checks/check-standard-client.sh@@ -6,8 +6,8 @@# - every FROM is CLIENT_IMAGE;# - every `pip install` installs the pins: `-r <file>` with a byte-identical copy of client-requirements.txt in the# build context, or the full pin list inline (name==version for every pin, nothing else).-# A failure for the eai-standard/2.2 tasks, whose generators copy client.env and the pins (BEHAVIOR-1K, RoboLab,-# RoboWits, RoboPaint). For every other task it only reports (a warning, never a failure) whether all standard tasks+# A failure for the eai-standard/2.2 and 2.4 tasks, whose generators copy client.env and the pins (BEHAVIOR-1K, RoboLab,+# RoboWits, RoboPaint; RoboTangle on 2.4). For every other task it only reports (a warning, never a failure) whether all standard tasks# share the one client image of client.env (their own pip pins are their benchmark's business; PROTOCOL §7, open).# Usage: check-standard-client.sh [tasks dir] (repo-level; run_all.sh calls it once)set -uo pipefail@@ -83,7 +83,7 @@ for t in sorted(os.listdir(tasks)):if os.path.isfile(df):probs += [f"{sub}/Dockerfile: {p}" for p in pip_problems(df, os.path.join(tasks, t, sub))]froms += re.findall(r"^\s*FROM\s+(\S+)", re.sub(r"\\\n", " ", open(df).read()), re.M | re.I)- is22 = bool(re.fullmatch(r"(eai-standard|rcb-limited)/2\.2", str(meta.get("protocol", ""))))+ is22 = bool(re.fullmatch(r"(eai-standard|rcb-limited)/2\.2|eai-standard/2\.4", str(meta.get("protocol", "")))) # 2.2 and 2.4 layersn22 += is22v1 += 1if any(f != IMAGE for f in froms):@@ -101,6 +101,6 @@ if v1_off:else:print(f"ok one client image: all {v1} standard task dirs FROM {IMAGE}")if not bad:- print(f"ok standard client: {n22} eai-standard/2.2 task dirs on the client image with the shared pins")+ print(f"ok standard client: {n22} eai-standard/2.2 / 2.4 task dirs on the client image with the shared pins")sys.exit(1 if bad else 0)PYdiff --git a/ci_checks/check-task-fields.sh b/ci_checks/check-task-fields.shindex 74a068fa90..04c316b455 100755--- a/ci_checks/check-task-fields.sh+++ b/ci_checks/check-task-fields.sh@@ -1,5 +1,5 @@#!/usr/bin/env bash-# task.toml: name matches folder, required metadata (docs/PROTOCOL.md §2.3, docs/TAXONOMY.md), deliverable dir, agent timeout cap.+# task.toml: name matches folder, required metadata (docs/PROTOCOL.md §2.3, docs/TAXONOMY.md), deliverable dir, agent timeout cap (with per-benchmark exceptions).t="$1"; python3 - "$t" <<'PY'import sys, tomllib, pathlibt = pathlib.Path(sys.argv[1]); d = tomllib.load(open(t / "task.toml", "rb")); rc = 0@@ -20,6 +20,11 @@ elif m["mode"] != suffix: fail(f"[metadata].mode = {m['mode']!r} but the directoif "tier" in m: fail("[metadata].tier was removed in protocol v1.0 (use mode)")if "/app/output/" not in d.get("artifacts", []): fail('top-level artifacts must include "/app/output/"')if not d.get("agent", {}).get("timeout_sec"): fail("[agent].timeout_sec required")-elif d["agent"]["timeout_sec"] > 14400: fail("[agent].timeout_sec > 4h cap")+else:+ # PROTOCOL §2.3: 14400 s cap; per-benchmark exceptions by owner decision, recorded in PROTOCOL §2.3 (task-directory prefix -> cap)+ BUDGET_EXCEPTIONS = {"robotangle-": 21600} # RoboTangle, owner 2026-10-08 (issue #87)+ cap = next((c for p, c in BUDGET_EXCEPTIONS.items() if t.name.startswith(p)), 14400)+ if d["agent"]["timeout_sec"] > cap:+ fail(f"[agent].timeout_sec > {cap} s cap" + (" (4h, PROTOCOL §2.3)" if cap == 14400 else " (documented exception, PROTOCOL §2.3)"))sys.exit(rc)PYdiff --git a/ci_checks/check-tooling-identity.sh b/ci_checks/check-tooling-identity.shindex 5d8082e06c..a9a8aad2f1 100755--- a/ci_checks/check-tooling-identity.sh+++ b/ci_checks/check-tooling-identity.sh@@ -12,6 +12,8 @@# run.sh are byte-identical to their source in scripts/eai_standard/ (docs/STANDARD_MODE_2_2.md), and the sidecar is# set up for crash recovery: `command: sh /srv/run.sh` (run.sh exits non-zero when the service crashes) and# `restart: on-failure:N` with N >= 3 (PROTOCOL §4.1, §4.7).+# The eai-standard/2.4 layer (metadata.protocol 2.4: RoboTangle) is held to the same rules, with rcb_limited.py and+# rcb_client.py from scripts/eai_standard/v2_4/ (docs/STANDARD_MODE_2_4.md) and 2.2's rcb1.py and run.sh.# Usage: check-tooling-identity.sh [tasks dir] (repo-level; run_all.sh calls it once)set -uo pipefailroot="$(cd "$(dirname "$0")/.." && pwd)"@@ -27,6 +29,9 @@ LAYER = {"environment/rcb_client.py": "rcb_client.py", "environment/rcb1.py": "r"environment/sim/rcb1.py": "rcb1.py", "environment/sim/run.sh": "run.sh"}sha = lambda p: hashlib.sha256(open(p, "rb").read()).hexdigest()[:16] # noqa: E731canon = {f: sha(os.path.join(src, f)) for f in set(LAYER.values()) if os.path.isfile(os.path.join(src, f))}+V24 = {"rcb_limited.py", "rcb_client.py"} # the 2.4 layer's own files (scripts/eai_standard/v2_4/); the rest is 2.2's+canon24 = {f: sha(os.path.join(src, "v2_4" if f in V24 else "", f)) for f in set(LAYER.values())+ if os.path.isfile(os.path.join(src, "v2_4" if f in V24 else "", f))}seen = collections.defaultdict(lambda: collections.defaultdict(dict)) # benchmark -> role -> {sha: first copy}dirs = collections.Counter(); bad = []; n_layer = 0@@ -51,14 +56,17 @@ for t in sorted(os.listdir(tasks)):continuebench = t.split("-")[0]proto = str(tomllib.load(open(tt, "rb")).get("metadata", {}).get("protocol", ""))- if re.fullmatch(r"(eai-standard|rcb-limited)/2\.2", proto):+ v24 = proto == "eai-standard/2.4"+ if v24 or re.fullmatch(r"(eai-standard|rcb-limited)/2\.2", proto):n_layer += 1+ ver, ref = ("2.4", canon24) if v24 else ("2.2", canon)for rel, f in sorted(LAYER.items()):p = os.path.join(tasks, t, rel)+ srcf = f"scripts/eai_standard/{'v2_4/' if v24 and f in V24 else ''}{f}"if not os.path.isfile(p):- bad.append(f"{t}: {rel} missing (eai-standard/2.2 layer, scripts/eai_standard/{f})")- elif sha(p) != canon.get(f):- bad.append(f"{t}: {rel} differs from scripts/eai_standard/{f} (regenerate the task)")+ bad.append(f"{t}: {rel} missing (eai-standard/{ver} layer, {srcf})")+ elif sha(p) != ref.get(f):+ bad.append(f"{t}: {rel} differs from {srcf} (regenerate the task)")cf = os.path.join(tasks, t, "environment/docker-compose.yaml")blk = sim_service(cf) if os.path.isfile(cf) else ""m = re.search(r"^\s*restart:\s*[\"']?on-failure:(\d+)", blk, re.M)@@ -80,12 +88,12 @@ for t in sorted(os.listdir(tasks)):bad += [f"{b}: {len(v)} versions of {role}: " + ", ".join(f"{h} ({p})" for h, p in sorted(v.items()))for b, roles in sorted(seen.items()) for role, v in sorted(roles.items()) if len(v) > 1]if bad:- print("FAIL shared sources (byte-identical copies; the 2.2 layer as in scripts/eai_standard/):")+ print("FAIL shared sources (byte-identical copies; the 2.2 / 2.4 layers as in scripts/eai_standard/):")for b in bad[:60]:print(" -", b)if len(bad) > 60:print(f" ... {len(bad) - 60} more")sys.exit(1)print("ok shared sources: " + ("; ".join(f"{b} {dirs[b]} task dirs, {len(seen[b])} shared files" for b in sorted(seen)) or "no BEHAVIOR / RoboLab / RoboWits / ManiGuard / MolmoSpaces / MyoSuite / DexJoCo tasks")- + f"; the eai-standard/2.2 layer in {n_layer} task dirs")+ + f"; the eai-standard/2.2 / 2.4 layers in {n_layer} task dirs")PYdiff --git a/docs/LIMITED_MODE_PROTOCOL.md b/docs/LIMITED_MODE_PROTOCOL.mdindex 4c1208a461..aca6f3a799 100644--- a/docs/LIMITED_MODE_PROTOCOL.md+++ b/docs/LIMITED_MODE_PROTOCOL.md@@ -202,4 +202,4 @@ agent latency counts, the agent's own program may act) and uses its `slowdown` n`/act`, `/wait`, `/ready`, seats, reviews or metering proxy; native agents only (PROTOCOL D11).## Versioning-`protocol` in `/spec`. 1.0 (`/step` with one action and base64 images) is retired; 2.0 = `/submit` + `/observe` + RCB1; 2.1 = 2.0 + measured proprioception (82 columns) + optional depth noise + crash recovery; `eai-standard/2.1` is the v1.0 name of 2.1. Minor versions only add; the 2.x client accepts every 2.x version under both spellings. Accepted, pending: `eai-standard/2.2` (PROTOCOL §6). `eai-standard/2.3` (2026-10-08) = 2.2 + the realtime capability, the realtime mode's protocol (above).+`protocol` in `/spec`. 1.0 (`/step` with one action and base64 images) is retired; 2.0 = `/submit` + `/observe` + RCB1; 2.1 = 2.0 + measured proprioception (82 columns) + optional depth noise + crash recovery; `eai-standard/2.1` is the v1.0 name of 2.1. Minor versions only add; the 2.x client accepts every 2.x version under both spellings. Accepted, pending: `eai-standard/2.2` (PROTOCOL §6). `eai-standard/2.3` (2026-10-08) = 2.2 + the realtime capability, the realtime mode's protocol (above). `eai-standard/2.4` (2026-10-08) = 2.3 + the operations capability (operation ids, request deadlines, `GET /operations/<id>`, call watchdog, tick ledger; [STANDARD_MODE_2_4.md](STANDARD_MODE_2_4.md)), for standard tasks.diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.mdindex c1fe44e6eb..a6d567f8c6 100644--- a/docs/PROTOCOL.md+++ b/docs/PROTOCOL.md@@ -4,7 +4,8 @@ Status: **final** (2026-09-30; v1.0.1 2026-10-01: both modes carry a suffix, notask README; **v1.1 2026-10-08: a third mode, realtime (§4b), served by `eai-standard/2.3`; protocol-change issue #86**).The tasks in the repository follow it except for the open items in §7. The wire format of standard mode is specified in[`LIMITED_MODE_PROTOCOL.md`](LIMITED_MODE_PROTOCOL.md) (`eai-standard/2.1`, and the realtime mode's `eai-standard/2.3`);-its 2.2 extension in [`STANDARD_MODE_2_2.md`](STANDARD_MODE_2_2.md).+its 2.2 extension in [`STANDARD_MODE_2_2.md`](STANDARD_MODE_2_2.md), and the 2.4 operations capability in+[`STANDARD_MODE_2_4.md`](STANDARD_MODE_2_4.md) (2026-10-08, issue #89).> **Names.** v1.0 renamed the two modes (unlimited → **privileged**, limited → **standard**) and the standard-mode> protocol (`rcb-limited/2.x` → **`eai-standard/2.x`**); task directories end in `-privileged` or `-standard`. Base images@@ -44,14 +45,14 @@ Unchanged: the task name prefix `robot-coding-bench/`, the client and tool file| # | Topic | Decision ||---|---|---|-| D1 | Standard-mode protocol | Only `eai-standard/2.x`. Additive extensions are accepted through a protocol-change PR (2.2 is accepted once its services implement crash recovery). Reset is only an optional capability (`budget.reset = N`, default 0; results with N > 0 are reported separately). Documented per-benchmark exception (owner, 2026-10-07): HumanoidBench standard defaults to N = 50 (§4.5). The realtime mode's `eai-standard/2.3` (§4b) is 2.2 plus the realtime capability. No step caps. |+| D1 | Standard-mode protocol | Only `eai-standard/2.x`. Additive extensions are accepted through a protocol-change PR (2.2 is accepted once its services implement crash recovery). Reset is only an optional capability (`budget.reset = N`, default 0; results with N > 0 are reported separately). Documented per-benchmark exception (owner, 2026-10-07): HumanoidBench standard defaults to N = 50 (§4.5). The realtime mode's `eai-standard/2.3` (§4b) is 2.2 plus the realtime capability. `eai-standard/2.4` (issue #89) is 2.3 plus the operations capability; standard tasks may use it. No step caps. || D2 | Tasks without an oracle | Allowed, with mandatory human review (§2.7). || D3 | Results | Not in the repository: agent results (per-task READMEs, videos, timelines, batch tables) are published on the results site and the Hugging Face dataset; a task directory has no `results/`. || D4 | Run id | `<harness>-<model>-<effort>`, model token with underscores (`codex-gpt6_luna-xhigh`); provider, tier and settings go in metadata. || D5 | Images | One registry (EAI organization), images named `eai-<name>` and versioned in `images/<name>/VERSION`; every image that may be redistributed is public, the rest are `BUILD_ONLY`. One image per **benchmark**, not per simulator: benchmarks on the same simulator may need different versions of it and of its dependencies (e.g. RoboCasa, RoboCasa365 and RoboCasa-GR1 on robosuite), so each may have its own image; an image is shared only where the dependency set is identical. || D6 | PR granularity | One benchmark per PR; changes to shared files go in a separate PR reviewed by a maintainer. || D7 | World-frame information in instructions | Decided per benchmark, consistent within it, documented in the benchmark's doc. |-| D8 | Agent time budget | 3600 s by default; longer budgets are justified, uniform within a benchmark, and results are grouped by budget. |+| D8 | Agent time budget | 3600 s by default, at most 14400 s; longer budgets are justified, uniform within a benchmark, and results are grouped by budget. Documented per-benchmark exceptions to the cap (§2.3): RoboTangle 21600 s (owner, 2026-10-08). || D9 | Shared code | Shared sources are copied into tasks by the generator; nothing at run time imports repository code outside the task directory. || D10 | Token budget | Per trial: **100M input tokens, 10M output tokens** by default; exceeding either fails the trial. || D11 | Harness | Official results use Harbor's native agents only; no agent adapters, and no gateway that holds credentials or changes requests. A transparent usage gateway that forwards requests unchanged and only records token usage (`scripts/usage_gateway/`) is allowed. |@@ -62,7 +63,7 @@ Unchanged: the task name prefix `robot-coding-bench/`, the client and tool file1. `task.toml`, `instruction.md`, `README.md`, `environment/Dockerfile`, `tests/test.sh` (executable), `tests/Dockerfile`. [CI] `solution/solve.sh` depending on the oracle level (§2.7). [new]2. `[task].name = "robot-coding-bench/<folder>"`; required metadata present and valid (§2.3). [CI]-3. Top-level `artifacts` includes `"/app/output/"`; `[agent].timeout_sec` set and ≤ 14400. [CI]+3. Top-level `artifacts` includes `"/app/output/"`; `[agent].timeout_sec` set and ≤ 14400, or ≤ the benchmark's documented exception (§2.3). [CI]4. Every image reference pinned to a tag or digest, never `:latest`. [CI]5. `[verifier] environment_mode = "separate"`; `tests/Dockerfile` copies `test.sh`. [CI]6. `instruction.md` names the deliverable under `/app/output/` and never mentions `/tests/`. [CI]@@ -106,16 +107,26 @@ Unchanged: the task name prefix `robot-coding-bench/`, the client and tool file| `[task].description`, `authors`, `keywords` | SHOULD | [current] || `[metadata]`: `author_name`, `task_type ∈ {trajectory, policy}`, `simulator`, `robot`, `family`, `mode ∈ {privileged, standard, realtime}`, `expert_time_estimate_hours` | MUST | [CI] || `tier` | MUST NOT | [CI] (`privileged` → `mode = "privileged"`, `perception` → `mode = "standard"`) |-| Standard tasks: `protocol = "eai-standard/2.1"` or `"eai-standard/2.2"`; realtime tasks: `protocol = "eai-standard/2.3"`; privileged tasks: no `protocol` | MUST | [CI] |+| Standard tasks: `protocol = "eai-standard/2.1"`, `"eai-standard/2.2"` or `"eai-standard/2.4"`; realtime tasks: `protocol = "eai-standard/2.3"`; privileged tasks: no `protocol` | MUST | [CI] || `oracle ∈ {full, partial, none}`; `oracle_evidence` when not `full` | MUST | [CI] || `simulator`, `robot` from the vocabularies in [`TAXONOMY.md`](TAXONOMY.md) (extend by PR) | MUST | [current]; CI check [new] || `instance_seed`; benchmark-specific fields registered in TAXONOMY | MAY | [current] || Top-level `artifacts` includes `"/app/output/"` | MUST | [CI] |-| `[agent].timeout_sec` set, ≤ 14400; 3600 unless justified (uniform within a benchmark) | MUST / SHOULD | [CI] / [new] |+| `[agent].timeout_sec` set, ≤ 14400 (per-benchmark exceptions below); 3600 unless justified (uniform within a benchmark) | MUST / SHOULD | [CI] / [new] || `[verifier].environment_mode = "separate"` | MUST | [CI] || `[verifier].timeout_sec`, `[environment]` `cpus` / `memory_mb` / `build_timeout_sec` | SHOULD | [current] || `[environment].gpus` (unsupported by Harbor's local Docker sandbox; see §2.4) | MUST NOT | [current] |+**Per-benchmark agent budget exceptions.** A benchmark's tasks may set `[agent].timeout_sec` above 14400 s only by owner+decision. The decision is recorded here, in `docs/benchmarks/<bench>.md`, and in the exception table of+`ci_checks/check-task-fields.sh`. The budget is uniform within the benchmark, in both modes. A standard sidecar's wall+clock (`RCB_WALL_CLOCK_S`, `/spec.budget.wall_clock_s`) equals it. That benchmark's results state the budget and are+compared only with results under the same budget (D8). The token budget (D10) is unchanged.+- **RoboTangle** (owner, 2026-10-08; issue #87): 21600 s (6 h) for every task. The topological puzzles are much+ harder than typical robotics tasks (reference executions take 2–185 min of simulated work), and the benchmark's+ owner sets the budget to probe the capability ceiling. Simulator speed-ups add interaction within it; they never+ shorten it (`docs/benchmarks/robotangle.md`, added with the tasks in #81).+### 2.4 Images, registry, GPUA base image is defined by `images/<name>/Dockerfile` and tagged `<registry>/eai-<name>:<images/<name>/VERSION>`; `<name>` is@@ -224,8 +235,8 @@ example → nop result → predicate summary*. The reviewer checks:## 4. Standard mode (`mode = "standard"`): `eai-standard/2.x`Wire details (endpoints, RCB1 frames, `meta`, error codes, crash recovery) are in-[`LIMITED_MODE_PROTOCOL.md`](LIMITED_MODE_PROTOCOL.md) (2.1) and [`STANDARD_MODE_2_2.md`](STANDARD_MODE_2_2.md) (2.2). This-section lists what is required.+[`LIMITED_MODE_PROTOCOL.md`](LIMITED_MODE_PROTOCOL.md) (2.1), [`STANDARD_MODE_2_2.md`](STANDARD_MODE_2_2.md) (2.2) and+[`STANDARD_MODE_2_4.md`](STANDARD_MODE_2_4.md) (2.4). This section lists what is required.### 4.0 Versions| Version | Date | Content |@@ -236,8 +247,9 @@ section lists what is required.| **eai-standard/2.1** | 2026-09-30 | rename only; identical to rcb-limited/2.1 including crash recovery. **Current.** || eai-standard/2.2 | 2026-10-01 | additive extension (§6, [`STANDARD_MODE_2_2.md`](STANDARD_MODE_2_2.md)) with crash recovery; served by BEHAVIOR-1K, RoboLab, RoboWits, RoboPaint, MolmoSpaces, MyoSuite and DexJoCo || eai-standard/2.3 | 2026-10-08 | 2.2 + the realtime capability (§4b; [`LIMITED_MODE_PROTOCOL.md`](LIMITED_MODE_PROTOCOL.md), "Realtime mode"); every 2.2 addition stays a MAY. Served by realtime-mode tasks only (HumanoidBench); standard tasks stay on 2.1 / 2.2. Issue #86 |+| eai-standard/2.4 | 2026-10-08 | 2.3 + the operations capability ([`STANDARD_MODE_2_4.md`](STANDARD_MODE_2_4.md)): operation ids and request deadlines (408 / 409), `GET /operations/<id>`, a service-owned call watchdog, a physics-tick ledger, fsynced journals; every earlier addition, the realtime capability included, stays a MAY. Served by standard tasks (RoboTangle); reference implementation `scripts/eai_standard/v2_4/`. Issue #89 |-Standard services report `eai-standard/2.1` or `eai-standard/2.2`, realtime services `eai-standard/2.3`; the shipped clients+Standard services report `eai-standard/2.1`, `eai-standard/2.2` or `eai-standard/2.4`, realtime services `eai-standard/2.3`; the shipped clientsaccept every 2.x version and the former `rcb-limited/2.x` spelling as well.**Core rules.** The simulator runs in a sidecar the agent cannot open; four endpoints; RCB1 binary responses; no@@ -403,6 +415,16 @@ submit), and the evidence of §4b. Every 2.2 addition stays a MAY capability, sois a 2.3 service. Only realtime-mode tasks serve it (§4b); 2.x clients connect to it unchanged, and the timing semanticsare declared in `/spec.timing` and the task's instruction.+**`eai-standard/2.4`** (2026-10-08, issue #89; [`STANDARD_MODE_2_4.md`](STANDARD_MODE_2_4.md)). 2.3 plus the operations+capability: `meta.operation_id` and `meta.request_deadline_unix_s` on `/submit` and `/observe` (408 for an expired request,+409 for an id already admitted: a call is never executed twice), `GET /operations/<id>` (`admitted`, `completed`, `error`,+`interrupted` with `execution_uncertain` after a crash), a service-owned watchdog that ends a hung native call as an infra+error, a physics-tick ledger that resumes replay with the ticks each row took, and fsynced journals. Every earlier addition+(2.2's, and 2.3's realtime capability) stays a MAY, so a 2.2 service with the operations capability is a 2.4 service;+standard tasks may declare it, realtime tasks stay on 2.3. The reference implementation is `scripts/eai_standard/v2_4/`+(`rcb_limited.py`, `rcb_client.py`; `rcb1.py` and `run.sh` are 2.2's), copied unchanged into every 2.4 task. First+served by RoboTangle, whose release named it "2.3" before that number went to the realtime capability.+**Not accepted:** step caps; single-control `step()` calls.## 7. Migration statusdiff --git a/docs/STANDARD_MODE_2_4.md b/docs/STANDARD_MODE_2_4.mdnew file mode 100644index 0000000000..bf8d0bf548--- /dev/null+++ b/docs/STANDARD_MODE_2_4.md@@ -0,0 +1,80 @@+# eai-standard/2.4 — the operations capability++Status: accepted 2026-10-08 (owner), protocol-change issue #89. It was first served by RoboTangle's standard tasks; the+benchmark's own release called it "2.3", but that number had already gone to the realtime capability (#86). Reference+implementation: [`scripts/eai_standard/v2_4/`](../scripts/eai_standard/v2_4/), with `rcb_limited.py` and+`rcb_client.py`. `rcb1.py`, `run.sh`, the client image and the client pins are the 2.2 ones in `scripts/eai_standard/`.++## 1. Version++`eai-standard/2.4` = 2.3 + the **operations capability** below.+- Every earlier addition stays a MAY capability. That covers 2.2's depth, `frame_time_s`, extrinsics, `POST /kinematics`+ and the per-call wall bound, and 2.3's realtime capability ([`LIMITED_MODE_PROTOCOL.md`](LIMITED_MODE_PROTOCOL.md),+ "Realtime mode"). So a 2.2 service that adds the operations capability is a 2.4 service.+- Standard-mode tasks may declare `protocol = "eai-standard/2.4"` (PROTOCOL §2.3). Realtime-mode tasks stay on 2.3+ (PROTOCOL §4b).+- Everything in PROTOCOL §4 holds unchanged: four endpoints (plus `/kinematics` and the one below), RCB1, no reset,+ crash recovery that resumes the same episode, and grading by a fresh replay.+- `/spec.protocol` is `eai-standard/2.4`.++Why: a native simulator call can run for minutes (IPC and PBD deformables). Under 2.2, a client whose HTTP read timed+out cannot tell whether the call ran, and a retry can execute the same actions twice. The capability makes every call+identifiable, bounded, and queryable after the fact.++## 2. Operation ids and request deadlines++| Item | Level |+|---|---|+| `/submit` and `/observe` accept `meta.operation_id` (string, `[A-Za-z0-9_-]{1,80}`) and `meta.request_deadline_unix_s` (finite number, Unix seconds); a malformed value is 400 | MUST |+| A request whose deadline has passed before execution starts gets **408**; no physics runs | MUST |+| An `operation_id` that the service has already admitted gets **409**, with `operation_id`, the operation record and a pointer to `/operations/<id>`; it is never executed again | MUST |+| The response `meta` echoes `operation_id` | MUST |+| A request without these fields is served as in 2.2 | MUST |+| The client sends a fresh id and a deadline (now + `max_call_wall_s`) on every call to a 2.4 service. It keeps the last id (`Robot.last_operation_id`), and its read timeout is at least the service budget + 120 s | SHOULD (reference client: always) |+| After a timeout the client queries the operation instead of re-sending the actions. A new id for the same actions is a new operation | SHOULD |++## 3. `GET /operations/<id>`++Returns the operation record as JSON: 200, or 404 for an unknown id, or 400 for a malformed id. Fields:+- `operation_id`;+- `status`: `admitted` | `completed` | `error` | `interrupted`;+- `rows_before`: control rows executed before the call;+- `committed_rows`: rows executed when the call ended;+- `stopped`: the call's stop reason, as in 2.2's `meta.stopped`.++After a crash recovery, an operation that was `admitted` becomes `interrupted`, with `execution_uncertain: true` and+`restored_committed_rows` (the rows the resumed episode replayed). `completed` means the call ended. Rows it executed are+in `committed_rows`. The service keeps no copy of the response video. The endpoint is read-only, never steps physics, and+is the only GET this capability adds.++## 4. Call watchdog++| Item | Level |+|---|---|+| A call's budget is `min(max_call_wall_s, request_deadline_unix_s − now)`. The service checks it, and a client disconnect, at control-step boundaries | MUST |+| A native step cannot be pre-empted safely, so the service also owns a hard watchdog. A call that has not finished 60 s past its budget ends the process. The restart then ends the episode as an infra error, `call_watchdog` (`infra_error = 1`), and does not replay into the call that hung | MUST |++## 5. Physics-tick ledger and durability++| Item | Level |+|---|---|+| The journal can record, for each executed row, the physics ticks it actually took (`journal.ticks.i64`, int64, one per row). A step that ends early inside its control period records fewer ticks, and `sim_time_s` and `frame_time_s` follow the actual physics clock | MAY (MUST for a service whose steps can end mid-period) |+| A resume replays each row with its recorded tick count. A missing or incomplete ledger ends the episode as an infra error, `resume_failed` | MUST if the ledger is kept |+| Journal rows, the tick ledger, checkpoints and operation records are fsynced, including their directory, before the call that counts them is reported complete | MUST |+| `dt` must be an integer multiple of the control period within float tolerance; otherwise 400 (2.1 rule, restated) | MUST |++## 6. Evidence++As in 2.2 (`/data/episode.jsonl`, `trajectory.npz`, `final.json`, `calls/`), plus:+- `/data/operations/<id>.json`;+- `journal.ticks.i64`, when kept;+- `call-watchdog.json`, while a call is running.++The verifier still grades `final.json` only (PROTOCOL §4.8).++## 7. Compatibility++- 2.x clients connect to a 2.4 service unchanged. They send no operation ids and get 2.2 behaviour.+- The 2.4 client sends ids only when `/spec.protocol` is 2.4, so it also works against 2.1–2.3 services.+- A benchmark adopts 2.4 by copying `scripts/eai_standard/v2_4/` unchanged; `ci_checks/check-tooling-identity.sh`+ checks the copies.diff --git a/docs/TAXONOMY.md b/docs/TAXONOMY.mdindex f1981be734..9b86349bb9 100644--- a/docs/TAXONOMY.md+++ b/docs/TAXONOMY.md@@ -7,7 +7,7 @@ Known values; a new value is added by pull request (CI validation of these vocab| `author_name` | the author's name (an organization may go in `author_org`) || `task_type` | `trajectory` · `policy` || `mode` | `privileged` · `standard` (formerly `unlimited` / `limited`) · `realtime` (protocol v1.1, PROTOCOL §4b) |-| `protocol` | standard and realtime mode only. Realtime: `eai-standard/2.3`. Standard: `eai-standard/2.1` · `eai-standard/2.2` (BEHAVIOR-1K, RoboLab, RoboWits, RoboPaint, ManiGuard, MolmoSpaces, MyoSuite, DexJoCo, MuJoCo Playground's quadruped tasks; [STANDARD_MODE_2_2.md](STANDARD_MODE_2_2.md)) |+| `protocol` | standard and realtime mode only. Realtime: `eai-standard/2.3`. Standard: `eai-standard/2.1` · `eai-standard/2.2` (BEHAVIOR-1K, RoboLab, RoboWits, RoboPaint, ManiGuard, MolmoSpaces, MyoSuite, DexJoCo, MuJoCo Playground's quadruped tasks; [STANDARD_MODE_2_2.md](STANDARD_MODE_2_2.md)) · `eai-standard/2.4` ([STANDARD_MODE_2_4.md](STANDARD_MODE_2_4.md), the operations capability) || `simulator` | `robosuite` · `robotwin` (RoboTwin 2.0 = SAPIEN 3 + CuRobo) · `mujoco` · `kinder` (KinDER: MuJoCo and PyBullet) · `humanoidbench` (HumanoidBench: MuJoCo + dm_control) · `robowits` (RoboWits on Genesis) · `robolab` (NVIDIA RoboLab on Isaac Lab) · `behavior` (BEHAVIOR-1K: OmniGibson on Isaac Sim) · `maniskill` (ManiSkill 3 / SAPIEN: RoboPaint) · `vlabench` (VLABench: MuJoCo + dm_control) · `maniguard` (ManiGuard: BEHAVIOR-1K 3.7.2, OmniGibson on Isaac Sim 4.5) · `molmospaces` (Ai2 MolmoSpaces: MuJoCo) · `myosuite` (MyoSuite: musculoskeletal models on MuJoCo) · `dexjoco` (DexJoCo: MuJoCo 3.4 + its gymnasium envs) — the simulator, not the image: benchmarks on one simulator may use different images and versions (PROTOCOL D5) || `robot` | `franka-panda` · `aloha-agilex` · `aloha-2` · `kuka-iiwa14-sharpa-ha4` · `tidybot-plus-plus` · `unitree-h1` · `unitree-h1hand` (H1 with two Shadow hands) · `sawyer` (Rethink Robotics Sawyer, as in Meta-World) · `marvin-bimanual` (RoboWits) · `franka-robotiq-2f85` (a Franka arm with a Robotiq 2F-85, the DROID setup: RoboLab, MolmoSpaces' FR3) · `r1pro` (BEHAVIOR-1K) · `franka-panda-omron` (RoboCasa's PandaOmron: Franka Panda on an Omron mobile base with a lift torso) · `fourier-gr1` (Fourier GR1 humanoid: two arms, waist and two Fourier dexterous hands, fixed base; RoboCasa-GR1) · `franka-panda-longfinger` (ManiGuard: Franka Panda with long fin-ray fingers) · `rainbow-rby1` (Rainbow Robotics RB-Y1: holonomic base, 6-joint torso, two 7-joint arms with parallel grippers; MolmoSpaces) · `myohand` (MyoSuite's MyoHand: a musculoskeletal human forearm and hand, 39 muscles) · `myoarm` (MyoSuite's MyoArm: a musculoskeletal human shoulder, arm and hand, 34 or 63 muscles) · `myoarm-torso` (MyoSuite's table-tennis player: torso, right arm and hand on a sliding pelvis) · `myoarm-mpl` (MyoSuite's MyoArm next to the Modular Prosthetic Limb, a motorized arm) · `myolegs` (MyoSuite's MyoLegs: a free-standing musculoskeletal torso and both legs) · `myoleg-osl` (MyoSuite's MyoLeg with an Open-Source Leg knee-ankle prosthesis) · `unitree-go1` (Unitree Go1 quadruped; MuJoCo Playground) · `boston-dynamics-spot` (Boston Dynamics Spot quadruped; MuJoCo Playground) · `google-barkour-vb` (Google Barkour vB quadruped; MuJoCo Playground) · `franka-allegro` (Franka Panda with a 16-DoF Allegro hand; DexJoCo) · `franka-allegro-bimanual` (two of them side by side; DexJoCo) || `family` | free-form slug naming the scene/goal, shared by its instances (e.g. `stack_blocks_two`, `dextoolbench/brush/blue_brush/sweep_forward`) |diff --git a/docs/benchmarks/humanoidbench.md b/docs/benchmarks/humanoidbench.mdindex 8fcfc97b11..af66abc603 100644--- a/docs/benchmarks/humanoidbench.md+++ b/docs/benchmarks/humanoidbench.md@@ -8,9 +8,9 @@ Whole-body humanoid control: locomotion and manipulation on a Unitree H1, scored| simulator | MuJoCo 3.1.6 + dm_control, CPU || robots | `h1`: Unitree H1, 19 actuators · `h1hand`: H1 + two Shadow hands, 61 actuators || score | an episode's summed reward (≤ 1000 steps); pass = at least upstream's success bar |-| tasks | 10 × 2 modes: `tasks/humanoidbench-<category>-<task>-i00-privileged` and `-standard` (`eai-standard/2.1`) |+| tasks | 10 × 3 modes: `tasks/humanoidbench-<category>-<task>-i00-privileged`, `-standard` (`eai-standard/2.1`) and `-realtime` (the realtime mode, `eai-standard/2.3`: standard mode on a clock that does not stop, below) || image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` (`images/humanoidbench/`) |-| oracle | `sit_simple` (the fixture): full, both modes; the 9 scored tasks: none, human review (`oracle_evidence` in each task.toml) |+| oracle | `sit_simple` (the fixture): full, all three modes; the 9 scored tasks: none, human review (`oracle_evidence` in each task.toml; realtime: approved by the owner in PR #83, 2026-10-08) |## Tasks: upstream's 32, ours ✅ (9 scored + the `sit_simple` fixture)@@ -62,6 +62,7 @@ Why these (no model's results enter the choice):|---|---|---|---|| privileged | the simulator and its full state; no reward | unlimited | its `trajectory.npz`, replayed twice in a fresh process || standard | head cameras, joint angles/velocities, a pelvis IMU, a fixed room camera; no reward | 50 (below; `RCB_RESET_BUDGET=0`: none) | the last episode, replayed in a fresh simulator |+| realtime | the same as standard, but time does not stop: the robot runs at 50 Hz in real time while the agent thinks (below) | 50 (the realtime mode's default, PROTOCOL §4b) | the last episode: the targets the robot executed at each control step, replayed in a fresh simulator |### Standard mode's reset budget: 50@@ -92,6 +93,48 @@ D1's default of 0 (§4.5); results state the reset budget they ran under. What agrants N. Results under a budget other than 50 are reported separately, and HumanoidBench standard-mode resultsrecorded before this change (budget 0) are a different configuration from today's.+### Realtime mode: time does not stop++In standard mode the robot waits, frozen, between the agent's calls: a model that thinks for a minute between two+chunks loses nothing. A real H1 does not wait. The realtime mode (PROTOCOL §4b, protocol v1.1;+`tasks/humanoidbench-*-i00-realtime`, 10 tasks, one per family, `mode = "realtime"`) is standard mode with the same+instance, sensors, success rule, reset budget (50, the realtime mode's default), wall clock (1 h) and client, on a+clock that does not stop (`scripts/humanoidbench/shared/realtime/rcb_realtime.py`, which extends `rcb_service.py`; the+wire protocol is `eai-standard/2.3`, 2.2 plus the realtime capability, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"):++- **Clock.** An episode starts paused at the frozen start, its servos holding their starting targets. Its clock starts+ at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 20 ms of wall time+ (`RCB_SLOWDOWN` = S: every S × 20 ms; 1 by default, other values reported separately) until the episode ends. Each+ episode after a reset starts paused again; so does an episode resumed after a crash.+- **Actions.** The service keeps a plan, one target row per coming control step. `submit` expands its chunk exactly as+ in 2.1 (rows `dt` apart, `linear` or `hold`) and writes it into the plan from control step `at` (default: the next+ one), replacing what was planned from there, and answers at once with the robot as it is now. A row whose step has+ already run when the request arrives is dropped (`late_steps`). When the plan runs out, the last target is held+ (zero-order hold): a slow agent's robot keeps its last servo targets, as a position-controlled H1 would.+- **Observations.** `get_obs()` is the robot now; `get_obs(seconds=s)` watches the next s seconds and returns what the+ cameras saw, without touching the plan. `cameras=[]` returns proprioception only (the controller's call).+- **Grading.** Unchanged: the journal (`trajectory_live.f64`) holds the sim row the simulator applied at every control+ step, whatever was submitted, and the finalize replays the last episode's rows in a fresh simulator. The replay+ therefore equals the live run however late or early the agent was; `deterministic` checks it. Evidence beside it:+ `ticks.f64` (each step's wall time and the call that planned it; -1 = held), `calls.jsonl`, `submitted.f64` (every+ row as sent), and in `final.json` `max_lag_s`, `stalls`, `held_steps`, `late_steps`.+- **Timing guarantees.** The simulator thread runs a due control step before any waiting request; HumanoidBench's+ physics takes 0.3–2.2 ms per control step (facts.json, `episode_seconds`) and a head-camera image about 0.5 ms on the+ sidecar's GPU, against a 20 ms step. A service that falls behind runs the missed steps back to back; more than+ 0.5 s behind, it drops the debt (a stall) instead of fast-forwarding the robot.+- **Crashes.** A simulator crash is an infrastructure failure, not the agent's (owner, 2026-10-08): past+ `RCB_MAX_RESUMES` restarts the trial ends `sim_crashed`, a failed resume `resume_failed`, and a simulator error that+ ends the graded episode (`reason = "error"`) counts too; `final.json` and `reward.json` carry `infra_error = 1`.++**What makes it a meaningful task.** A model's turn takes seconds; the H1 falls within 1-3 s of robot time without+active balance; a whole episode is 20 s (cube: 10 s). An agent that answers each observation with a model call cannot+keep the robot up, so the instruction says so and tells it to write a controller: a Python loop in its own container+that reads proprioception and streams targets at the control rate through the client (a one-row call with+`cameras=[]` costs a few ms over the shared network namespace), run in the background, and improved between episodes.+The agent's code still runs only in its own container, never next to the simulator (standard mode's isolation is+kept); the 50 resets are what make that loop of writing, running and revising a controller possible at all, which is+why the realtime mode defaults to 50 resets rather than D1's 0.+World-frame information (PROTOCOL D7): privileged mode gives the full state; standard mode gives none -- no root pose,no object or target pose; the room camera's pose is given in the robot's starting frame.@@ -120,6 +163,8 @@ its model-API key in its container, so it could call a search-enabled model thro```bashharbor run -c scripts/humanoidbench/configs/codex-openrouter.yaml --env-file .env -p tasks/humanoidbench-loco-hurdle-i00-standardharbor run -p tasks/humanoidbench-loco-sit-simple-i00-standard -a oracle # or -a nop: scores 0+harbor run -c scripts/humanoidbench/configs/codex-openrouter.yaml --env-file .env -p tasks/humanoidbench-loco-walk-i00-realtime+harbor run -p tasks/humanoidbench-loco-sit-simple-i00-realtime -a oracle # streams the same 999 rows````codex-openrouter.yaml` is Harbor's own Codex agent with OpenRouter as its model provider (run id`codex-gpt6_luna-medium`). Standard tasks run with 50 resets by default; `RCB_RESET_BUDGET=0 harbor run ...` runs them@@ -133,4 +178,6 @@ Agent results are not kept in this repository (PROTOCOL D3): per-run READMEs (ve`reward.json`, the replay's last frame and videos are published on the results site, keyed by run id. Standard-moderesults state their reset budget: the standard configuration is 50 resets (since 2026-10-07); runs with 0 -- everyHumanoidBench standard-mode run recorded before that date -- are reported separately, and runs recorded before-protocol v1.0 are marked as such.+protocol v1.0 are marked as such. Realtime results (`-realtime`) are their own mode and are never pooled with+standard ones; they state the reset budget and `slowdown`. Official runs use `slowdown` 1 (the default); results+with `RCB_SLOWDOWN` ≠ 1 are reported separately from them (PROTOCOL §4b).diff --git a/scripts/eai_standard/v2_4/rcb_client.py b/scripts/eai_standard/v2_4/rcb_client.pynew file mode 100644index 0000000000..648425e3b4--- /dev/null+++ b/scripts/eai_standard/v2_4/rcb_client.py@@ -0,0 +1,227 @@+"""eai-standard/2.4 client (the 2.2 client, formerly rcb-limited/2.x, plus operation ids; docs/STANDARD_MODE_2_4.md) — the+only thing a standard-mode agent gets (installed at /opt/tools/rcb_client.py). Against a 2.4 service every submit /+get_obs carries an operation id and a request deadline: after a timeout, `r.operation_status()` (or+`r.operation_status(r.last_operation_id)`) says whether the call ran; never re-send it blindly.++ from rcb_client import Robot+ r = Robot() # finds the service (sim:8000, then localhost:8000), waits for warm-up+ spec = r.spec() # actions, cameras (+ intrinsics, mounting), robot_spec, world, budget, kinematics+ obs = r.get_obs() # current frames: obs.last[cam] -> uint8 (H,W,3); obs.meta["proprio"]+ obs = r.get_obs(depth=True) # + obs.last_depth[cam] -> float32 (H,W) z-depth in metres, 0 = no reading+ res = r.submit("joint_position", A, dt=0.1) # A: (T, dim) rows dt apart; returns the execution video+ res = r.submit("ee_pose", P) # (T, dim) end-effector targets (see spec()["action_types"])+ res.video[cam] -> mp4 bytes res.frames[cam] -> uint8 (N,H,W,3) if video="frames"/"both" res.last[cam]+ res.proprio -> float32 (N, D), one row per frame (names: spec()["proprio_layout"]; res.group("tcp_pose"))+ res.frame_step -> int64 (N,) control step of each frame res.frame_time_s -> float64 (N,) simulated time+ res.extrinsic_cv[cam] -> float64 (N,3,4) world->camera per frame (res.meta["cameras"]: intrinsics + the last frame's pose)+ res = r.submit(..., depth="frames") # + res.depth[cam] -> float32 (N,H,W) metres, one per frame (1 mm steps)+ r.fk(q) # {"ok", "poses": {frame: [x,y,z, quaternion]}}: forward kinematics, no physics+ r.ik({frame: pose}, seed=q) # {"ok", "q", "error": {frame: {"pos_m", "rot_rad"}}}: inverse kinematics+ res.save("/app/runs/003") # meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, proprio.npy, depth, frame_meta.npz+ obs = r.get_obs(seconds=3, video="both") # hold the last command, let physics run 3 s, return the video+ r.status() # calls, control steps, done, reason, elapsed time++Nothing is put into your context automatically: what you look at, and where you store it, is your code's decision.++If a call fails with a connection error or a 5xx, the service may have crashed; it restarts and resumes the SAME episode+(never a reset) in the state after the last call it completed, which you may not have received. r.wait_ready() waits+until it answers again (resuming replays the episode so far: long episodes can take minutes); then check r.status()+before you continue (spec()["on_service_error"]).+"""+from __future__ import annotations+import json, os, pathlib, sys, time, zlib+import numpy as np+import requests+import rcb1++PROTOCOL = "eai-standard/2.2" # also talks to 2.0 / 2.1 services (they ignore depth and send no frame metadata)+ACCEPTED = ("eai-standard/2.", "rcb-limited/2.") # every 2.x service, under the protocol's current and former name+++class EpisodeOver(RuntimeError):+ pass+++def _decode_depth_frames(p: dict) -> np.ndarray:+ a = np.frombuffer(zlib.decompress(p["data"]), dtype="<u2").reshape(p["shape"])+ return a.astype(np.float32) / 1000.0+++class Result:+ """Decoded RCB1 response: meta (dict), video (cam -> mp4 bytes), frames (cam -> uint8 (N,H,W,3)), last (cam -> uint8+ (H,W,3)), last_depth (cam -> float32 (H,W) m), depth (cam -> float32 (N,H,W) m, one per frame), proprio (float32 (N,D)),+ frame_step (int64 (N,)), frame_time_s (float64 (N,)), extrinsic_cv (cam -> float64 (N,3,4)). save(dir) writes it out;+ show(cam, k) returns one frame; group(name) the proprio columns of one spec()["proprio_groups"] group. stopped: None,+ or why the service stopped the call early ("max_call_wall_s": it ran longer than spec()["max_call_wall_s"]); then+ meta["executed_rows"] < the rows sent, and meta["proprio"]["now"] is where the robot is."""+ def __init__(self, meta: dict, parts: dict, spec: dict | None = None):+ self.meta = meta; self.raw = parts; self._spec = spec or {}; self.stopped = meta.get("stopped")+ self.video, self.frames, self.last, self.last_depth, self.depth, self.extrinsic_cv = {}, {}, {}, {}, {}, {}+ self.proprio = None; self.frame_step = None; self.frame_time_s = None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4": self.video[name[6:]] = data+ elif name.startswith("frames/") and kind == "array": self.frames[name[7:]] = data+ elif name.startswith("last/") and kind == "array": self.last[name[5:]] = data+ elif name.startswith("last_depth/") and kind == "array": self.last_depth[name[11:]] = np.asarray(data, dtype=np.float32)+ elif name.startswith("depth_frames/") and p.get("codec") == "zlib": self.depth[name[13:]] = _decode_depth_frames(p)+ elif name.startswith("depth/") and kind == "array": self.depth[name[6:]] = np.asarray(data, dtype=np.float32) # 2.1 form+ elif name.startswith("extrinsic_cv/") and kind == "array": self.extrinsic_cv[name[13:]] = data+ elif name == "proprio": self.proprio = data+ elif name == "frame_step": self.frame_step = data+ elif name == "frame_time_s": self.frame_time_s = data++ @property+ def done(self) -> bool: return bool(self.meta.get("done"))++ def show(self, camera: str, k: int = -1) -> np.ndarray:+ if camera in self.frames: return self.frames[camera][k]+ return self.last[camera]++ def group(self, name: str, now: bool = False) -> np.ndarray:+ """Columns of proprio group `name` (spec()["proprio_groups"]): (N, n) over the frames, or (n,) of meta["proprio"]["now"]."""+ first, n = self._spec["proprio_groups"][name][:2]+ if now: return np.asarray(self.meta["proprio"]["now"], dtype=np.float64)[first:first + n]+ return self.proprio[:, first:first + n]++ def save(self, out_dir: str) -> str:+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items(): (out / f"{cam}.mp4").write_bytes(b)+ try:+ from PIL import Image+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True); Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam; d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr): Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ except ImportError:+ for cam, img in self.last.items(): np.save(out / f"last_{cam}.npy", img)+ for cam, arr in self.frames.items(): np.save(out / f"frames_{cam}.npy", arr)+ for cam, arr in self.last_depth.items(): np.save(out / f"last_depth_{cam}.npy", arr)+ for cam, arr in self.depth.items(): np.savez_compressed(out / f"depth_{cam}.npz", depth_mm=np.rint(arr * 1000).astype(np.uint16))+ if self.proprio is not None: np.save(out / "proprio.npy", self.proprio)+ if self.frame_step is not None:+ np.savez(out / "frame_meta.npz", frame_step=self.frame_step, frame_time_s=self.frame_time_s,+ **{f"extrinsic_cv/{c}": e for c, e in self.extrinsic_cv.items()})+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ cands = [base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000"]+ t0 = time.time(); last = None+ while time.time() - t0 < timeout:+ for b in [c for c in cands if c]:+ try:+ r = requests.get(b + "/spec", timeout=(5, 30))+ if r.status_code == 503: last = "warming up"; break+ r.raise_for_status(); self.base = b; self._spec = r.json()+ if not str(self._spec.get("protocol", "")).startswith(ACCEPTED):+ raise RuntimeError(f"not a 2.x service: protocol {self._spec.get('protocol')!r}")+ return+ except requests.exceptions.RequestException as e:+ last = e+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ import uuid+ budget = float(self._spec.get("max_call_wall_s", 900))+ timeout = max(timeout, budget + 120)+ meta = dict(meta)+ if self._spec.get("protocol") == "eai-standard/2.4":+ meta.setdefault("operation_id", uuid.uuid4().hex)+ meta.setdefault("request_deadline_unix_s", time.time() + budget)+ self.last_operation_id = meta["operation_id"]+ body = rcb1.encode(meta, parts)+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"}, timeout=(5, timeout))+ if r.status_code == 409:+ raise EpisodeOver(r.json().get("reason") if r.headers.get("Content-Type", "").startswith("application/json") else "episode finished")+ if r.status_code >= 400:+ try: err = r.json()+ except Exception: err = {"error": r.text[:300]}+ raise ValueError(f"{r.status_code}: {err.get('error')} {err.get('detail', '')}".strip())+ m, p = rcb1.decode(r.content); return Result(m, p, self._spec)++ def _common(self, cameras, video_fps, depth, include_depth) -> dict:+ cams = list(cameras) if cameras is not None else self._spec.get("default_cameras")+ if depth is None or depth is False: depth = "frames" if include_depth else "none"+ elif depth is True: depth = "last"+ m = {"cameras": cams, "video_fps": None if video_fps is None else int(video_fps), "depth": depth}+ return {k: v for k, v in m.items() if v is not None}++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear", cameras=None,+ video_fps: int | None = None, video: str = "mp4", depth=None, include_depth: bool = False, note: str = "") -> Result:+ """Execute a chunk of low-level actions (T, dim); see spec()['action_types'] for dims, layouts, limits and semantics.+ dt defaults to one control period, cameras to spec()['default_cameras']; depth: None | True (= "last") | "frames"."""+ a = np.asarray(actions, dtype=np.float32)+ if a.ndim == 1: a = a[None, :]+ if dt is None and self._spec.get("control_hz"): dt = 1.0 / float(self._spec["control_hz"])+ meta = {"type": action_type, "interpolate": interpolate, "video": video, "note": note,+ **({} if dt is None else {"dt": float(dt)}), **self._common(cameras, video_fps, depth, include_depth)}+ res = self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)+ if res.stopped:+ print(f"[rcb_client] the service stopped this call early ({res.stopped}): {res.meta.get('executed_rows')} of {len(a)} rows "+ "executed; meta['proprio']['now'] is where the robot is", file=sys.stderr)+ return res++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int | None = None, depth=None, include_depth: bool = False,+ video: str = "frames") -> Result:+ """Current frames (seconds=0, free) or hold the last command and watch for `seconds` (returns the video).+ depth: None | True (= "last": the last frame's depth) | "frames" (one depth map per frame)."""+ meta = {"seconds": float(seconds), "video": video, **self._common(cameras, video_fps, depth, include_depth)}+ res = self._post("/observe", meta, [], timeout=600)+ if res.stopped:+ print(f"[rcb_client] the service stopped this watch early ({res.stopped}) after {res.meta.get('control_steps_watch')} control "+ "steps", file=sys.stderr)+ return res++ def operation_status(self, operation_id=None):+ operation_id = operation_id or self.last_operation_id+ r = requests.get(self.base + "/operations/" + operation_id, timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60)); r.raise_for_status(); return r.json()++ def wait_ready(self, timeout: float = 3600.0) -> dict:+ """After a connection error: wait until the service answers GET /status again (it resumes the same episode after a+ crash), then return the status. Raises RuntimeError after `timeout` seconds."""+ t0 = time.time(); last = None+ while time.time() - t0 < timeout:+ try:+ return self.status()+ except requests.exceptions.RequestException as e:+ last = e+ time.sleep(5)+ raise RuntimeError(f"sim service did not come back within {timeout:.0f} s: {last}")++ def kinematics(self, body: dict) -> dict:+ """POST /kinematics (stateless: never moves the robot, never advances time). Raises ValueError on a bad request."""+ r = requests.post(self.base + "/kinematics", json=body, timeout=(5, 120))+ if r.status_code >= 400:+ try: err = r.json()+ except Exception: err = {"error": r.text[:300]}+ raise ValueError(f"{r.status_code}: {err.get('error')} {err.get('detail', '')}".strip())+ return r.json()++ def fk(self, q, links=None, base_pose=None) -> dict:+ """Forward kinematics: q in spec()['kinematics']['joint_names'] order -> {"ok", "frame", "poses": {link: [x,y,z, quat]}}+ (quaternion order spec()['world']['quaternion_order']; a mobile robot answers in its base frame unless base_pose is given)."""+ body = {"op": "fk", "q": [float(v) for v in np.asarray(q).reshape(-1)]}+ if links is not None: body["links"] = list(links)+ if base_pose is not None: body["base_pose"] = [float(v) for v in base_pose]+ return self.kinematics(body)++ def ik(self, targets: dict, seed=None, base_pose=None, **opts) -> dict:+ """Inverse kinematics: {frame: [x,y,z, quat]} (frames from spec()['kinematics']['ik_targets']) and an optional seed q ->+ {"ok", "q", "error": {frame: {"pos_m", "rot_rad"}}}. opts: tol_pos_m, tol_rot_rad, max_iters."""+ body = {"op": "ik", "targets": {k: [float(v) for v in p] for k, p in targets.items()}, **opts}+ if seed is not None: body["seed"] = [float(v) for v in np.asarray(seed).reshape(-1)]+ if base_pose is not None: body["base_pose"] = [float(v) for v in base_pose]+ return self.kinematics(body)diff --git a/scripts/eai_standard/v2_4/rcb_limited.py b/scripts/eai_standard/v2_4/rcb_limited.pynew file mode 100644index 0000000000..60abadd9b8--- /dev/null+++ b/scripts/eai_standard/v2_4/rcb_limited.py@@ -0,0 +1,1043 @@+"""eai-standard/2.4: the 2.2 layer (the HTTP layer, the protocol helpers and the crash recovery; docs/STANDARD_MODE_2_2.md,+the 2.1 base is docs/LIMITED_MODE_PROTOCOL.md) plus the operations capability of docs/STANDARD_MODE_2_4.md: operation ids+and request deadlines (408 / 409), GET /operations/<id>, the service-owned call watchdog, the physics-tick ledger+(journal.ticks.i64) and fsynced journals. First served by RoboTangle (whose release called it 2.3).++The source is scripts/eai_standard/v2_4/rcb_limited.py; the generators copy it unchanged into every 2.4 task's+environment/sim/ (ci_checks/check-tooling-identity.sh). A service keeps its simulator to itself (scene, action+execution, cameras, kinematic model) and hands this module plain callables:++ import rcb_limited as L+ EP = L.Episode(DATA, dim=N, resume="exact") # before the scene is built; "approx" for a nondeterministic simulator+ EP.startup() # start count, back-off after failed starts, startup watchdog+ S = <build the scene>; EP.scene_built() # the watchdog stands down+ def ctrl_step(a, rec): # every executed control step+ S.step(a); stop = EP.journal(a); rec.tick(); return E["done"] or stop+ rec = L.Recorder(cams, every, grab, proprio_row, extrinsics, clock, depth=L.depth_mode(meta), clock_hz=CTRL_HZ)+ rec.tick(force=True) # first frame (before the chunk / watch)+ ...; rec.tick(); ... # after every control step: a frame every `every` steps+ rec.tick(force=True, last=True) # last frame (+ its depth)+ parts = rec.parts(video, fps) # RCB1 parts: video/ frames/ last/ extrinsic_cv/ last_depth/ depth_frames/ depth/ proprio frame_*+ S.record_start()+ EP.begin(state=E, step=S.step, state_sha=S.state_sha, proprio=proprio_row, spec=SPEC, start_event={...}, on_end=end_fn)+ L.serve(L.Service(spec=SPEC, lock=LOCK, state=E, submit=do_submit, observe=do_observe, kinematics=do_kinematics,+ status=status, on_submit_error=lambda exc: finish("error"), log=EP.log,+ episode=EP, state_sha=S.state_sha, proprio=proprio_row))++ grab(cams, want_depth) -> ({cam: uint8 (H,W,3)}, {cam: float32 (H,W) z-depth in metres, 0 = no reading})+ extrinsics(cams) -> {cam: (3,4) world -> camera, OpenCV axes (x right, y down, z forward)}+ proprio_row() -> float32 (len(spec["proprio_layout"]),)+ clock() -> int, control steps since the episode start (the frame's step index)+ do_kinematics(req) -> dict; `req` is parse_kinematics(body, spec["kinematics"]); it must never step physics++None of the callables may step physics. check_spec(SPEC) runs in serve(): a service whose /spec misses a 2.2 field, or+carries a field the protocol forbids (segmentation, object identities or sizes, physical parameters), does not start.+Crash recovery (class Episode; PROTOCOL §4.7): every executed control step is journaled, every completed call+checkpointed (rows, state hash, the robot's proprio row); after a crash compose restarts the container and the service+replays the journal and continues the SAME episode, never a reset. `python rcb_limited.py end <data dir>` is the shared+first half of a collect hook."""+from __future__ import annotations+import faulthandler, json, math, os, re, select, signal, socket, sys, tempfile, threading, time, traceback, zlib+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Any, Callable++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.4"+COMPAT = ["eai-standard/2.2", "rcb-limited/2.0", "rcb-limited/2.1", "eai-standard/2.1"] # requests written for these are served unchanged+MAX_BODY = 64 * 1024 * 1024+MAX_RESP = 256 * 1024 * 1024+DEPTH_MODES = ("none", "last", "frames")+DEPTH_MAX_M = 65.535 # uint16 millimetres; a farther surface reads 0 (no reading)+DEPTH_NOISE = os.environ.get("RCB_DEPTH_NOISE", "off").strip().lower() # off | realsense (as rcb-limited/2.1)+_RNG = np.random.default_rng(int(os.environ.get("RCB_DEPTH_NOISE_SEED", "0")))+PROPRIO_GROUPS_REQUIRED = ("joint_pos", "joint_vel", "gripper_opening", "tcp_pose", "joint_effort")+PART_PREFIXES = ("video/", "frames/", "last/", "last_depth/", "depth_frames/", "depth/", "extrinsic_cv/")+PART_NAMES = ("proprio", "frame_step", "frame_time_s")+++def control_periods(dt, hz):+ """Validate duration without silently changing the requested control clock."""+ dt, hz = float(dt), float(hz)+ if not math.isfinite(dt) or not math.isfinite(hz) or hz <= 0 or dt < 1 / hz - 1e-9 or dt > .5 + 1e-9:+ raise ValueError("dt must be a finite duration between one control period and 0.5 s")+ count = round(dt * hz)+ if count < 1 or not math.isclose(dt * hz, count, rel_tol=0, abs_tol=1e-7):+ raise ValueError("dt must be an integer multiple of the control period")+ return int(count)+++# ---------------------------------------------------------------- depth+def depth_mode(meta: dict) -> str:+ """The request's depth mode: "none" | "last" | "frames" (2.2 field `depth`), or "legacy" for a 2.0/2.1 request that+ sets `include_depth: true` and no `depth` (answered as 2.1 did: `depth/<cam>` float16 (N,H,W) metres)."""+ d = meta.get("depth")+ if d is None or d is False:+ return "legacy" if meta.get("include_depth") is True else "none"+ if d is True:+ return "last"+ if isinstance(d, str) and d in DEPTH_MODES:+ return d+ raise ValueError(f"depth must be one of {'|'.join(DEPTH_MODES)} (got {d!r})")+++def clean_depth(z) -> np.ndarray:+ """float32 z-depth in metres: non-finite, negative and beyond-range pixels read 0 (no reading); the optional+ RCB_DEPTH_NOISE=realsense model (sigma = 2 mm + 2.5 mm * z^2, 1 % dropouts) is applied here, after rendering, so it+ never touches physics."""+ z = np.asarray(z, dtype=np.float32).copy()+ if z.ndim == 3 and z.shape[-1] == 1:+ z = z[..., 0]+ bad = ~np.isfinite(z) | (z <= 0) | (z > DEPTH_MAX_M)+ z[bad] = 0.0+ if DEPTH_NOISE == "realsense":+ valid = z > 0+ z = z + _RNG.normal(0.0, 1.0, z.shape).astype(np.float32) * (0.002 + 0.0025 * z * z)+ z[_RNG.random(z.shape) < 0.01] = 0.0+ z[~valid] = 0.0+ z[(z < 0) | (z > DEPTH_MAX_M)] = 0.0+ return z.astype(np.float32)+++def depth_frames_part(name: str, stack: np.ndarray) -> tuple[str, str, bytes, dict]:+ """(N,H,W) metres -> an RCB1 "bytes" part: zlib of C-order uint16 millimetres (0 = no reading). Clients that do not+ know it keep the raw bytes and ignore it; rcb_client decodes it back to float32 metres."""+ mm = np.rint(np.asarray(stack, dtype=np.float64) * 1000.0)+ mm[(mm < 0) | (mm > 65535)] = 0+ a = np.ascontiguousarray(mm.astype("<u2"))+ return (name, "bytes", zlib.compress(a.tobytes(), 1), {"codec": "zlib", "dtype": "uint16", "shape": list(a.shape), "unit": "mm"})+++def encode_mp4(frames: np.ndarray, fps: int) -> bytes:+ """H.264 mp4 (yuv420p, faststart) of uint8 (N,H,W,3) frames: the same encoding every 2.0 service used."""+ import imageio.v2 as imageio+ with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tf:+ path = tf.name+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ return open(path, "rb").read()+ finally:+ try:+ os.unlink(path)+ except OSError:+ pass+++# ---------------------------------------------------------------- recording what the cameras see during one call+class Recorder:+ """Frames of the requested cameras every `every` clock ticks, plus forced first/last frames; for every frame the+ proprio row, the clock (step index), each camera's extrinsics and, if asked, its depth. All arrays share one frame+ axis N: frames/<cam>, the mp4's frames, proprio, frame_step, frame_time_s, extrinsic_cv/<cam>, depth_frames/<cam>."""++ def __init__(self, cameras, every: int, grab: Callable, proprio_row: Callable, extrinsics: Callable, clock: Callable,+ depth: str = "none", clock_hz: float = 1.0, time_clock=None, fps=None):+ if depth not in DEPTH_MODES + ("legacy",):+ raise ValueError(f"bad depth mode {depth!r}")+ self.cams = list(cameras); self.every = max(1, int(every)); self.mode = depth; self.hz = float(clock_hz)+ self._grab, self._prop, self._ext, self._clock = grab, proprio_row, extrinsics, clock+ self._time_clock = time_clock or (lambda: self._clock() / self.hz)+ self.fps = float(self.hz / self.every if fps is None else fps)+ if not math.isfinite(self.fps) or not 0 < self.fps <= self.hz:+ raise ValueError("video fps must be positive and no greater than control_hz")+ self.times = []; self._next_time = None+ self.frames: dict = {c: [] for c in self.cams}; self.depth: dict = {c: [] for c in self.cams}+ self.ext: dict = {c: [] for c in self.cams}; self.last_depth: dict = {}+ self.prop: list = []; self.steps: list = []; self.n = 0; self.missing = 0++ def tick(self, force: bool = False, last: bool = False) -> None:+ now = float(self._time_clock())+ if self._next_time is None:+ self._next_time = now+ if force or last or now + 1e-10 >= self._next_time:+ if not self.times or now > self.times[-1]:+ self._capture(last)+ while self._next_time <= now + 1e-10:+ self._next_time += 1 / self.fps+ self.n += 1++ def _capture(self, last: bool) -> None:+ want = self.mode in ("frames", "legacy") or (last and self.mode == "last")+ rgb, dep = self._grab(self.cams, want)+ ext = self._ext(self.cams)+ for c in self.cams:+ img = rgb.get(c)+ if img is not None:+ img = np.ascontiguousarray(np.asarray(img)[..., :3].astype(np.uint8))+ else: # a camera that failed to render: a black frame (sized in parts()) keeps one frame axis+ self.missing += 1+ self.frames[c].append(img)+ e = ext.get(c)+ self.ext[c].append(np.full((3, 4), np.nan) if e is None else np.asarray(e, dtype=np.float64).reshape(3, 4))+ if want:+ d = dep.get(c)+ if d is None:+ d = np.zeros(img.shape[:2] if img is not None else (1, 1), np.float32)+ else:+ d = clean_depth(d)+ if img is not None and d.shape != img.shape[:2]:+ raise RuntimeError(f"depth of {c} is {d.shape}, its RGB {img.shape[:2]}: they must be pixel-aligned")+ if self.mode in ("frames", "legacy"):+ self.depth[c].append(d)+ if last or self.mode == "frames":+ self.last_depth[c] = d+ self.prop.append(np.asarray(self._prop(), dtype=np.float32).reshape(-1))+ self.steps.append(int(self._clock()))+ self.times.append(float(self._time_clock()))++ def parts(self, video: str, fps: int) -> list:+ if self.mode == "last" and not self.last_depth and self.prop: # no last=True tick: the state has not moved since+ _, dep = self._grab(self.cams, True)+ self.last_depth = {c: clean_depth(dep[c]) for c in self.cams if c in dep}+ out = []+ for c in self.cams:+ good = [f for f in self.frames[c] if f is not None]+ hw = good[-1].shape[:2] if good else (1, 1)+ if len(good) < len(self.frames[c]):+ self.frames[c] = [f if f is not None else np.zeros(hw + (3,), np.uint8) for f in self.frames[c]]+ self.depth[c] = [d if d.shape == hw else np.zeros(hw, np.float32) for d in self.depth[c]]+ if c in self.last_depth and self.last_depth[c].shape != hw:+ self.last_depth[c] = np.zeros(hw, np.float32)+ fr = np.stack(self.frames[c]) if self.frames[c] else np.zeros((0, 1, 1, 3), np.uint8)+ if video in ("mp4", "both") and len(fr):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, fps), {"fps": fps, "n": int(len(fr))}))+ if video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ if len(fr):+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ out.append(rcb1.array_part(f"extrinsic_cv/{c}", np.stack(self.ext[c]) if self.ext[c] else np.zeros((0, 3, 4))))+ if self.mode in ("last", "frames") and c in self.last_depth:+ out.append(rcb1.array_part(f"last_depth/{c}", self.last_depth[c]))+ if self.mode == "frames" and self.depth[c]:+ out.append(depth_frames_part(f"depth_frames/{c}", np.stack(self.depth[c])))+ if self.mode == "legacy" and self.depth[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.depth[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop) if self.prop else np.zeros((0, 0), np.float32)))+ st = np.asarray(self.steps, dtype=np.int64)+ out.append(rcb1.array_part("frame_step", st))+ out.append(rcb1.array_part("frame_time_s", np.asarray(self.times, dtype=np.float64)))+ return out+++def encode_response(meta: dict, parts: list, max_bytes: int = MAX_RESP) -> bytes:+ """The RCB1 body; over `max_bytes` the per-frame depth goes first, then the raw frames (mp4, last frames, last+ depth, proprio and the per-frame arrays always stay), and meta["degraded"] says what was dropped."""+ body = rcb1.encode(meta, parts)+ dropped = []+ for prefixes in (("depth_frames/", "depth/"), ("frames/",)):+ if len(body) <= max_bytes:+ break+ keep = [p for p in parts if not p[0].startswith(prefixes)]+ if len(keep) < len(parts):+ parts = keep; dropped += [x.rstrip("/") for x in prefixes]+ body = rcb1.encode({**meta, "degraded": f"response over {max_bytes} bytes: {', '.join(dropped)} dropped"}, parts)+ return body+++# ---------------------------------------------------------------- kinematics+def _vec(x, n: int, what: str) -> list:+ a = np.asarray(x, dtype=np.float64).reshape(-1) if x is not None else None+ if a is None or a.size != n or not np.all(np.isfinite(a)):+ raise ValueError(f"{what} must be {n} finite numbers")+ return a.tolist()+++def _pose(x, what: str) -> list:+ p = _vec(x, 7, what)+ nq = math.sqrt(sum(v * v for v in p[3:]))+ if nq < 1e-6:+ raise ValueError(f"{what}: quaternion has zero norm")+ return p[:3] + [v / nq for v in p[3:]]+++def parse_kinematics(body: dict, kin: dict) -> dict:+ """Validate a /kinematics request against spec["kinematics"] (ValueError -> 400). Returns+ {"op": "fk", "q", "links", "base_pose"} or {"op": "ik", "targets": {frame: pose7}, "seed", "base_pose", "tol_pos_m",+ "tol_rot_rad", "max_iters"}; poses are x y z + a unit quaternion in the spec's order; q follows kin["joint_names"]."""+ if not isinstance(body, dict):+ raise ValueError("body must be a JSON object")+ op = body.get("op"); n = len(kin["joint_names"])+ base = None+ if body.get("base_pose") is not None:+ if kin.get("frame") != "base":+ raise ValueError("base_pose is only accepted by a mobile robot (spec kinematics.frame == 'base')")+ base = _pose(body["base_pose"], "base_pose")+ if op == "fk":+ links = body.get("links") or list(kin["links"])+ if not isinstance(links, list) or any(l not in kin["links"] for l in links):+ raise ValueError(f"links must be a list of names from spec kinematics.links: {kin['links']}")+ return {"op": "fk", "q": _vec(body.get("q"), n, "q"), "links": links, "base_pose": base}+ if op == "ik":+ tg = body.get("targets")+ if not isinstance(tg, dict) or not tg or any(k not in kin["ik_targets"] for k in tg):+ raise ValueError(f"targets must map frames from spec kinematics.ik_targets {kin['ik_targets']} to poses")+ ik = kin.get("ik", {})+ seed = None if body.get("seed") is None else _vec(body["seed"], n, "seed")+ tol_p = float(body.get("tol_pos_m", ik.get("tol_pos_m", 0.005)))+ tol_r = float(body.get("tol_rot_rad", ik.get("tol_rot_rad", 0.05)))+ it = int(body.get("max_iters", ik.get("max_iters", 200)))+ if not (1e-5 <= tol_p <= 0.5 and 1e-4 <= tol_r <= 3.2 and 1 <= it <= 5000):+ raise ValueError("tol_pos_m in [1e-5, 0.5], tol_rot_rad in [1e-4, 3.2], max_iters in [1, 5000]")+ return {"op": "ik", "targets": {k: _pose(v, f"targets[{k}]") for k, v in tg.items()}, "seed": seed, "base_pose": base,+ "tol_pos_m": tol_p, "tol_rot_rad": tol_r, "max_iters": it}+ raise ValueError("op must be 'fk' or 'ik'")+++def fk_response(poses: dict, frame: str) -> dict:+ return {"ok": True, "op": "fk", "frame": frame, "poses": {k: [float(v) for v in np.asarray(p, dtype=np.float64).reshape(7)] for k, p in poses.items()}}+++def ik_response(q, ok: bool, errors: dict, frame: str, note: str | None = None) -> dict:+ """errors: {frame: (position error m, rotation error rad)} of the returned q (None / non-finite -> null: no solution)."""+ fin = lambda v: float(v) if v is not None and math.isfinite(float(v)) else None # noqa: E731+ out = {"ok": bool(ok), "op": "ik", "frame": frame, "q": None if q is None else [float(v) for v in np.asarray(q).reshape(-1)],+ "error": {k: {"pos_m": fin(e[0]), "rot_rad": fin(e[1])} for k, e in errors.items()}}+ if note:+ out["note"] = note+ return out+++def quat_angle(q1, q2) -> float:+ """Rotation angle (rad) between two unit quaternions of the same order."""+ d = abs(float(np.dot(np.asarray(q1, dtype=np.float64), np.asarray(q2, dtype=np.float64))))+ return 2.0 * math.acos(min(1.0, d))+++# ---------------------------------------------------------------- /spec self-check+FORBIDDEN_KEY = re.compile(r"mask|segment|semantic(?!s$)|instance_?id|bbox|bounding|categor|object|\bobj_|label|mass|friction|"+ r"restitution|inertia|density|damping|stiffness", re.I)+GAIN_PATHS = ("robot_spec.controller",) # joint drive gains (stiffness, damping) are the robot's own, and allowed there+POSITION_KEY = re.compile(r"(?:^|_)(?:pos|position|positions|pose|poses|origin|location|coords?|coordinates|world|translation)(?:_|$)|"+ r"(?:^|_)cent(?:er|re)s?(?:_|$)|(?:^|_)xyz?$|(?:^|_)(?:top|surface|floor|table|plane|height|elevation)?_?z$", re.I)+WORLD_TEXT = re.compile(r"\bworld\s+[xyz]\s*=", re.I)+COORDS = re.compile(r"\(\s*-?\d+(?:\.\d+)?\s*,\s*-?\d+(?:\.\d+)?\s*(?:,\s*-?\d+(?:\.\d+)?\s*)?\)")+++def world_text(s: str) -> bool:+ """A text that gives a world coordinate: a pixel-to-world formula, or a coordinate tuple with a decimal point that is+ not the robot's own pose ("robot base at (0, 0, 1.08)" stays)."""+ return bool(WORLD_TEXT.search(s)) or any("." in m.group(0) and not re.search(r"base|robot|origin", s[max(0, m.start() - 40):m.start()], re.I)+ for m in COORDS.finditer(s))+CONVENTIONS = {"static.floor_z": 0.0} # BEHAVIOR: the floor is the plane z = 0 (the world frame's definition)+++def static_positions(obj: Any, path: str = "static") -> list[str]:+ """Paths in /spec static that say WHERE something is (keys naming positions, heights, centres; texts with world+ coordinates). Names, inventories, sizes and colours may stay in static; positions never (docs § Never provided)."""+ out = []+ if isinstance(obj, dict):+ for k, v in obj.items():+ p = f"{path}.{k}"+ if POSITION_KEY.search(str(k)) and CONVENTIONS.get(p, object()) != v:+ out.append(p)+ else:+ out += static_positions(v, p)+ elif isinstance(obj, list):+ for i, v in enumerate(obj):+ out += static_positions(v, f"{path}[{i}]")+ elif isinstance(obj, str) and world_text(obj):+ out.append(path)+ return out+++SPEC_REQUIRED = ("protocol", "protocol_compat", "simulator", "robot", "task_text", "control_hz", "cameras", "default_cameras",+ "action_types", "proprio_layout", "proprio_groups", "static", "world", "budget", "robot_spec", "kinematics", "depth")+++def forbidden_keys(obj: Any, path: str = "", skip: tuple = ()) -> list[str]:+ """Key paths that name something the protocol never provides (key paths in `skip` are not searched)."""+ out = []+ if isinstance(obj, dict):+ for k, v in obj.items():+ p = f"{path}.{k}" if path else str(k)+ if p in skip:+ continue+ if FORBIDDEN_KEY.search(str(k)) and not (re.search(r"damping|stiffness", str(k), re.I) and p.startswith(GAIN_PATHS)):+ out.append(p)+ out += forbidden_keys(v, p, skip)+ elif isinstance(obj, list):+ for i, v in enumerate(obj):+ out += forbidden_keys(v, f"{path}[{i}]", skip)+ return out+++def check_spec(spec: dict) -> None:+ """Raise if /spec is not a complete 2.2 spec or names anything forbidden (run at startup, before binding :8000)."""+ miss = [k for k in SPEC_REQUIRED if k not in spec]+ if miss:+ raise RuntimeError(f"/spec misses {miss}")+ if spec["protocol"] != PROTOCOL:+ raise RuntimeError(f"/spec protocol {spec['protocol']!r} != {PROTOCOL}")+ bad = forbidden_keys(spec, skip=())+ if bad:+ raise RuntimeError(f"/spec names forbidden fields: {bad}")+ pos = static_positions(spec["static"])+ if pos:+ raise RuntimeError(f"/spec static says where things are (never provided): {pos}")+ lay = spec["proprio_layout"]+ if len(set(lay)) != len(lay):+ raise RuntimeError("proprio_layout names are not unique")+ for g in PROPRIO_GROUPS_REQUIRED:+ if g not in spec["proprio_groups"]:+ raise RuntimeError(f"proprio_groups misses {g}")+ for g, (first, n, _meaning) in spec["proprio_groups"].items():+ if not (0 <= first and first + n <= len(lay)):+ raise RuntimeError(f"proprio group {g} [{first}, +{n}) outside the {len(lay)} columns")+ kin = spec["kinematics"]+ for k in ("endpoint", "joint_names", "frame", "quaternion_order", "links", "tcp_frames", "ee_frames", "ik_targets"):+ if k not in kin:+ raise RuntimeError(f"kinematics misses {k}")+ if any(j not in lay for j in kin["joint_names"]):+ raise RuntimeError("kinematics.joint_names must be proprio_layout columns (the measured joint positions)")+ if any(f not in kin["links"] for f in list(kin["tcp_frames"]) + list(kin["ee_frames"]) + list(kin["ik_targets"])):+ raise RuntimeError("kinematics tcp_frames / ee_frames / ik_targets must be listed in kinematics.links")+ if spec["proprio_groups"]["tcp_pose"][1] != 7 * len(kin["tcp_frames"]):+ raise RuntimeError("proprio group tcp_pose must hold 7 columns per kinematics.tcp_frames entry")+ for c, cam in spec["cameras"].items():+ for k in ("rgb", "depth", "mounted", "resolution", "intrinsic_cv", "mount"):+ if k not in cam:+ raise RuntimeError(f"cameras.{c} misses {k}")+ if cam["mount"].get("type") not in ("static", "link"):+ raise RuntimeError(f"cameras.{c}.mount.type must be static|link")+ if any(c not in spec["cameras"] for c in spec["default_cameras"]):+ raise RuntimeError("default_cameras must be cameras")+++# ---------------------------------------------------------------- crash recovery (PROTOCOL §4.7; docs/STANDARD_MODE_2_2.md)+MAX_RESUMES = int(os.environ.get("RCB_MAX_RESUMES", "2"))+STARTUP_TIMEOUT_S = float(os.environ.get("RCB_STARTUP_TIMEOUT_S", "900")) # a task with a slower scene build sets more+MAX_CALL_WALL_S = float(os.environ.get("RCB_MAX_CALL_WALL_S", "900")) # < the client's 1800 s /submit read timeout+RESUME_MAX_DEV_M = float(os.environ.get("RCB_RESUME_MAX_DEV_M", "0.05")) # approx resumes: the gross-divergence guard+ON_SERVICE_ERROR = ("a connection error means the service crashed: it restarts and resumes the SAME episode (never a reset) "+ "in the state after the last call it completed, which you may not have received. Wait until GET /status "+ "answers again (rcb_client: Robot.wait_ready(); resuming replays the episode so far, which can take "+ "minutes in a long episode, and the wall clock keeps running), then check calls and control_steps "+ "before you continue. A 500 means the simulator failed: the episode is over (reason \"error\"). A call "+ "that runs longer than max_call_wall_s seconds stops at the next control step and returns what it "+ "executed (meta.stopped, executed_rows).")+++def _jsonable(v: Any) -> Any:+ if isinstance(v, np.ndarray):+ return v.tolist()+ if isinstance(v, (np.floating, np.integer, np.bool_)):+ return v.item()+ if isinstance(v, (list, tuple)):+ return [_jsonable(x) for x in v]+ if isinstance(v, dict):+ return {str(k): _jsonable(x) for k, x in v.items()}+ return v+++def _peer_closed(sock) -> bool:+ """True when the client of an HTTP request has closed its connection (it gave up on the call): the socket reads as+ end-of-file. A client never sends anything while it waits for the response, so readable means closed."""+ try:+ r, _, _ = select.select([sock], [], [], 0)+ return bool(r) and sock.recv(1, socket.MSG_PEEK) == b""+ except (OSError, ValueError):+ return True+++def _read_json(path: str) -> dict:+ try:+ with open(path) as f:+ d = json.load(f)+ return d if isinstance(d, dict) else {}+ except (OSError, ValueError):+ return {}+++def _write_json(path: str, obj: dict) -> None:+ tmp = f"{path}.{os.getpid()}.tmp"+ with open(tmp, "w") as f:+ json.dump(obj, f)+ f.flush(); os.fsync(f.fileno())+ os.replace(tmp, path)+ directory = os.open(os.path.dirname(path) or ".", os.O_RDONLY | os.O_DIRECTORY)+ try:+ os.fsync(directory)+ finally:+ os.close(directory)+++def final_verdict(data: str) -> dict | None:+ """final.json if it already holds a verdict that nothing may overwrite: the grading result (finalized, with+ `deterministic`) or the end of an episode the service gave up after crashes (`gave_up`)."""+ fin = _read_json(os.path.join(data, "final.json"))+ if fin.get("finalized") and (fin.get("gave_up") or fin.get("deterministic") is not None):+ return fin+ return None+++class Episode:+ """Crash-safe episode bookkeeping, the same rules as the RoboTwin and MuJoCo services (PROTOCOL §4.7):++ - startup(): before the scene is built. One line in startups.jsonl; a back-off of min(120, 20 (n - 1)) s when the+ earlier starts never began an episode (they crashed while building, e.g. GPU memory taken by other trials); the+ startup watchdog (faulthandler, a C thread: it fires even when a native scene build holds the GIL) dumps every+ thread's traceback and exits 1 after RCB_STARTUP_TIMEOUT_S, so compose restarts the build. Until begin() a SIGUSR1+ (the collect hook's "the agent phase is over") is only remembered.+ - journal(row): every executed control step (the exact row given to the simulator's step) goes to journal.f64.+ - checkpoint(): after every completed call (the HTTP layer calls it): the journal is flushed, then the call's+ checkpoint is logged in episode.jsonl (rows, control steps, state hash, the robot's proprio row, `persist` keys).+ - begin(): a fresh episode, or after a crash (run.sh exits non-zero, compose restarts the container, `restart:+ on-failure:N`) the SAME episode resumed: the journal is replayed up to the last checkpoint, the journal truncated+ there (the call that was in flight is lost as a whole: its caller got an error, not a result), the state hash+ compared, the service's counters restored; the wall clock keeps running from the first start.+ - Resume check, by the simulator's determinism class: `exact` (bit-exact in a fresh process): the hash must match,+ else the episode ends `resume_failed` (infra_error). `approx` (a simulator declared nondeterministic): a+ mismatch is recorded (resume_state_mismatch, and the robot's deviation from its checkpointed proprio: joint_pos and+ tcp_pose) and the episode continues, unless a TCP is more than RCB_RESUME_MAX_DEV_M from where it was+ (resume_failed, infra_error). The verdict never comes from the live state but from fresh replays of the complete+ recorded trajectory, so continuing cannot raise a score.+ - More than RCB_MAX_RESUMES crashes end the episode: `sim_crashed_on_call` if every crash happened while a call was+ being served (the agent's failure), else `sim_crashed` (infra_error = 1). A verdict already on disk is never+ overwritten; the service then idles with its PID file for the collect hook.+ - Per call, two bounds keep one call from holding the single-threaded service: a call that runs longer than+ max_call_wall_s, or whose client has hung up, stops at the next control step and returns what it executed+ (journal() returns True; meta.stopped says why). Neither bounds the episode: the wall clock is its only budget."""++ def __init__(self, data: str, dim: int, resume: str = "exact", persist: tuple = ()):+ if resume not in ("exact", "approx"):+ raise ValueError("resume must be 'exact' or 'approx'")+ self.data, self.dim, self.resume_class, self.persist = data, int(dim), resume, tuple(persist)+ os.makedirs(data, exist_ok=True)+ p = lambda n: os.path.join(data, n) # noqa: E731+ self.f_events, self.f_journal, self.f_final, self.f_pid, self.f_startups = (+ p("episode.jsonl"), p("journal.f64"), p("final.json"), p("server.pid"), p("startups.jsonl"))+ self.f_ticks = p("journal.ticks.i64")+ self.f_call_watchdog = p("call-watchdog.json")+ self._tf = None+ self.rows = 0 # control steps executed in this episode (journaled)+ self.max_call_wall_s = MAX_CALL_WALL_S+ self.n_startups = 0; self.resumes = 0; self.mismatches = 0; self.max_tcp_dev = 0.0; self.max_joint_dev = 0.0+ self._jf = None; self._call: dict | None = None; self._ids = 0; self._ck: tuple | None = None+ self._on_end: Callable | None = None; self._end_pending = False++ # ---- startup+ def startup(self) -> None:+ signal.signal(signal.SIGUSR1, self._usr1)+ try:+ os.remove(self.f_pid) # a PID file from before a crash would send the collect hook to a dead process+ except OSError:+ pass+ with open(self.f_startups, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(self.f_startups) as f:+ self.n_startups = sum(1 for line in f if line.strip())+ if self.n_startups > 1 and not any(e.get("ev") == "start" for e in self.events()):+ time.sleep(min(120.0, 20.0 * (self.n_startups - 1)))+ faulthandler.dump_traceback_later(STARTUP_TIMEOUT_S, exit=True)++ def scene_built(self) -> None:+ faulthandler.cancel_dump_traceback_later() # the scene exists: a resume replay may take long, it is not a hang++ def _usr1(self, *_):+ if self._on_end is None:+ self._end_pending = True+ else:+ threading.Thread(target=self._on_end, daemon=True).start()++ # ---- the record+ def log(self, ev: dict) -> None:+ with open(self.f_events, "a") as f:+ f.write(json.dumps(_jsonable({"ts": time.time(), **ev})) + "\n")+ f.flush(); os.fsync(f.fileno())++ def events(self) -> list:+ out = []+ try:+ with open(self.f_events) as f:+ for line in f:+ try:+ out.append(json.loads(line))+ except ValueError: # a line cut by a crash+ pass+ except OSError:+ pass+ return [e for e in out if isinstance(e, dict)]++ def journal(self, row, *, physics_steps=None) -> bool:+ """One executed control step. True: the call in progress must stop at this step boundary (meta.stopped)."""+ a = np.asarray(row, dtype=np.float64).reshape(-1)+ if a.size != self.dim:+ raise ValueError(f"journal rows have {self.dim} entries, got {a.size}")+ if self._jf is None:+ self._jf = open(self.f_journal, "ab")+ self._jf.write(a.tobytes())+ if physics_steps is not None:+ if self._tf is None:+ self._tf = open(self.f_ticks, "ab")+ self._tf.write(np.asarray([physics_steps], dtype=np.int64).tobytes())+ self.rows += 1+ return self.poll_stop()++ def poll_stop(self):+ """Check the call boundary before and after a non-preemptible physics step."""+ c = self._call+ if c is None:+ return False+ if c["stopped"] is None:+ now = time.monotonic()+ if now - c["t0"] > self.max_call_wall_s:+ c["stopped"] = "max_call_wall_s"+ elif c["sock"] is not None and now - c["polled"] >= 0.2:+ c["polled"] = now+ if _peer_closed(c["sock"]):+ c["stopped"] = "client_disconnected"+ return c["stopped"] is not None++ @property+ def stopping(self) -> bool:+ """The call in progress must stop (adapters whose loops test E["done"] test this too)."""+ return bool(self._call and self._call["stopped"])++ @property+ def stop_reason(self) -> str | None:+ return self._call["stopped"] if self._call else None++ def call_begin(self, path: str, sock=None, *, deadline=None) -> int:+ self._ids += 1+ if path != "/kinematics":+ budget = self.max_call_wall_s if deadline is None else min(self.max_call_wall_s, max(0, deadline - time.time()))+ self._call = {"id": self._ids, "t0": time.monotonic() - (self.max_call_wall_s - budget), "sock": sock, "polled": 0.0, "stopped": None}+ _write_json(self.f_call_watchdog, {"call": self._ids, "pid": os.getpid(), "deadline": time.time() + max(1, budget + 60)})+ faulthandler.dump_traceback_later(max(1, budget + 60), exit=True)+ self.log({"ev": "call_begin", "id": self._ids, "path": path})+ return self._ids++ def call_end(self, cid: int, path: str, **extra) -> None:+ self.log({"ev": "call_end", "id": cid, "path": path, **extra})+ if path != "/kinematics":+ faulthandler.cancel_dump_traceback_later()+ try:+ os.remove(self.f_call_watchdog)+ except FileNotFoundError:+ pass+ self._call = None++ def checkpoint(self, state: dict, state_sha: Callable | None, proprio: Callable | None) -> None:+ if self._jf is not None:+ self._jf.flush(); os.fsync(self._jf.fileno())+ if self._tf is not None:+ self._tf.flush(); os.fsync(self._tf.fileno())+ # the rows reach the file before the checkpoint that counts them+ if self._ck is not None and self._ck[0] == self.rows:+ sha, pr = self._ck[1], self._ck[2] # no physics since the last checkpoint+ else:+ try:+ sha = state_sha() if state_sha else None+ except Exception: # noqa: BLE001+ traceback.print_exc(); sha = None+ pr = self._proprio_row(proprio)+ self._ck = (self.rows, sha, pr)+ self.log({"ev": "checkpoint", "call": int(state.get("calls") or 0), "step": int(state.get("step") or 0),+ "control_steps": self.rows, "success": bool(state.get("success")), "done": bool(state.get("done")),+ "reason": state.get("reason"), "state_sha": sha, "proprio": pr,+ **({"persist": {k: state.get(k) for k in self.persist}} if self.persist else {})})++ @staticmethod+ def _proprio_row(proprio: Callable | None) -> list | None:+ try:+ return [round(float(v), 6) for v in np.asarray(proprio(), dtype=np.float64).reshape(-1)] if proprio else None+ except Exception: # noqa: BLE001+ traceback.print_exc(); return None++ def final_fields(self) -> dict:+ """Crash-recovery fields for final.json / episode.json (all numeric, for reward.json)."""+ return {"sim_startups": self.n_startups, "resumes": self.resumes, "resume_state_mismatch": int(self.mismatches > 0),+ "resume_max_tcp_dev_m": round(self.max_tcp_dev, 6), "resume_max_joint_dev": round(self.max_joint_dev, 6)}++ # ---- start or resume+ def begin(self, *, state: dict, step: Callable, state_sha: Callable, proprio: Callable, spec: dict,+ start_event: dict | None = None, on_end: Callable | None = None, step_ticks: Callable | None = None) -> bool:+ """Start the episode, or resume it after a crash. `step(row)` executes one journaled row on the simulator (the+ recorder on, no rendering needed); `on_end` is what the collect hook's SIGUSR1 runs (in a thread). Returns True+ if the episode was resumed."""+ try: # a line cut by the crash must not swallow the next record+ with open(self.f_events, "rb+") as f:+ f.seek(0, os.SEEK_END)+ if f.tell():+ f.seek(-1, os.SEEK_END)+ if f.read(1) != b"\n":+ f.write(b"\n")+ except OSError:+ pass+ ev = self.events()+ starts = [e for e in ev if e.get("ev") == "start"]+ watchdog = _read_json(self.f_call_watchdog)+ if starts and watchdog and time.time() >= float(watchdog["deadline"]):+ self.give_up("call_watchdog: unfinished native call exceeded its deadline", infra=True)+ if not starts:+ for path in (self.f_journal, self.f_ticks):+ try:+ os.remove(path)+ except FileNotFoundError:+ pass+ sha = state_sha()+ self._ck = (0, sha, self._proprio_row(proprio))+ self.log({**(start_event or {}), "ev": "start", "state_sha": sha, "protocol": PROTOCOL, "resume": self.resume_class,+ "journal_dim": self.dim})+ state["t0"] = time.time()+ self._arm(on_end)+ return False+ tries = [e for e in ev if e.get("ev") == "resuming"] # earlier resumes (a crash during one counts too)+ resumed = [e for e in ev if e.get("ev") == "resumed"]+ self.resumes = len(tries) # the record so far, also for a give-up below (final_fields)+ self.mismatches = sum(1 for e in resumed if e.get("exact") is False)+ self.max_tcp_dev = max([float(e.get("tcp_dev_m") or 0) for e in resumed] + [0.0])+ self.max_joint_dev = max([float(e.get("joint_dev") or 0) for e in resumed] + [0.0])+ if final_verdict(self.data):+ self.give_up("restarted after finalize", infra=False)+ k = max(i for i, e in enumerate(ev) if e.get("ev") in ("start", "resuming", "resumed"))+ tail = ev[k + 1:]+ in_call = bool({e.get("id") for e in tail if e.get("ev") == "call_begin"} - {e.get("id") for e in tail if e.get("ev") == "call_end"})+ if len(tries) >= MAX_RESUMES:+ agent = sum(1 for e in tries if e.get("crashed_in_call")) + int(in_call) == len(tries) + 1+ self.give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log({"ev": "resuming", "attempt": len(tries) + 1, "crashed_in_call": in_call,+ "downtime_s": round(time.time() - float(ev[-1].get("ts") or time.time()), 1)})+ self.resumes = len(tries) + 1 # this attempt counts, whether or not it succeeds+ cps = [e for e in ev if e.get("ev") == "checkpoint"]+ last = cps[-1] if cps else {"call": 0, "step": 0, "control_steps": 0, "success": False, "state_sha": starts[0].get("state_sha")}+ n = int(last.get("control_steps") or 0)+ operation_dir = os.path.join(self.data, "operations")+ if os.path.isdir(operation_dir):+ for name in os.listdir(operation_dir):+ path = os.path.join(operation_dir, name)+ record = _read_json(path)+ if record.get("status") == "admitted":+ _write_json(path, {**record, "status": "interrupted", "execution_uncertain": True,+ "restored_committed_rows": n})+ try:+ R = np.fromfile(self.f_journal, dtype=np.float64)+ except (OSError, ValueError):+ R = np.zeros(0)+ if R.size < n * self.dim:+ self.give_up("resume_failed: journal shorter than the log", infra=True)+ R = R[: n * self.dim].reshape(n, self.dim)+ t = time.time()+ ticks = None+ if step_ticks is not None and n:+ try:+ ticks = np.fromfile(self.f_ticks, dtype=np.int64)[:n]+ if len(ticks) != n or np.any(ticks <= 0):+ raise ValueError("incomplete tick journal")+ except (ValueError, OSError):+ self.give_up("resume_failed: actual tick journal missing", infra=True)+ for i, row in enumerate(R):+ if ticks is None:+ step(row)+ else:+ step_ticks(row, int(ticks[i]))+ if ticks is not None:+ ticks.tofile(self.f_ticks)+ replay_s = time.time() - t+ R.tofile(self.f_journal) # drop the rows of the call that was in flight+ self.rows = n+ sha = state_sha(); want = last.get("state_sha")+ exact = None if want is None else sha == want+ dev = self._deviation(last.get("proprio"), proprio, spec)+ if exact is False: # recorded first: a give-up below reports it too+ self.mismatches += 1+ self.max_tcp_dev = max(self.max_tcp_dev, dev.get("tcp_m") or 0.0)+ self.max_joint_dev = max(self.max_joint_dev, dev.get("joint") or 0.0)+ if self.resume_class == "exact":+ self.give_up("resume_failed: replayed state differs from the logged one", infra=True)+ if dev.get("tcp_m") is not None and dev["tcp_m"] > RESUME_MAX_DEV_M:+ self.give_up(f"resume_failed: a replayed TCP is {dev['tcp_m']:.3f} m from its checkpoint (> {RESUME_MAX_DEV_M} m)", infra=True)+ state["calls"] = int(last.get("call") or 0); state["step"] = int(last.get("step") or 0)+ if "success" in state:+ state["success"] = bool(last.get("success"))+ for key, v in (last.get("persist") or {}).items():+ state[key] = v+ state["t0"] = float(starts[0]["ts"])+ ended = [e for e in ev if e.get("ev") == "end" and int(e.get("control_steps") or 0) <= n] # not the lost call's+ if ended:+ state["done"] = True; state["reason"] = ended[-1].get("reason"); state["t_end"] = float(ended[-1]["ts"])+ state["episode_control_steps"] = int(ended[-1].get("control_steps") or n)+ self._ck = (n, sha, self._proprio_row(proprio)) # the replayed state's row (it may differ under approx)+ self.log({"ev": "resumed", "attempt": self.resumes, "control_steps": n, "calls": state["calls"], "state_sha": sha,+ "logged_sha": want, "exact": exact, "class": self.resume_class, "tcp_dev_m": dev.get("tcp_m"),+ "joint_dev": dev.get("joint"), "crashed_in_call": in_call, "replay_s": round(replay_s, 1),+ "downtime_s": round(time.time() - float(ev[-1].get("ts") or time.time()), 1)})+ self._arm(on_end)+ return True++ @staticmethod+ def _deviation(ref, proprio: Callable, spec: dict) -> dict:+ """The replayed robot against its checkpointed proprio row: max |joint_pos| difference (rad or m) and the largest+ TCP position error (m) over the tcp_pose frames (7 columns each: x y z + quaternion)."""+ if ref is None:+ return {}+ try:+ now = np.asarray(proprio(), dtype=np.float64).reshape(-1); ref = np.asarray(ref, dtype=np.float64).reshape(-1)+ except Exception: # noqa: BLE001+ return {}+ if now.shape != ref.shape:+ return {}+ g, out = spec.get("proprio_groups") or {}, {}+ if "joint_pos" in g:+ f, n = int(g["joint_pos"][0]), int(g["joint_pos"][1])+ out["joint"] = float(np.max(np.abs(now[f:f + n] - ref[f:f + n]))) if n else 0.0+ if "tcp_pose" in g:+ f, n = int(g["tcp_pose"][0]), int(g["tcp_pose"][1]); m = n // 7+ d = (now[f:f + 7 * m] - ref[f:f + 7 * m]).reshape(m, 7)[:, :3]+ out["tcp_m"] = float(np.max(np.linalg.norm(d, axis=1))) if m else 0.0+ return out++ def _arm(self, on_end: Callable | None) -> None:+ self._on_end = on_end+ signal.signal(signal.SIGUSR1, self._usr1)+ if on_end is not None and self._end_pending:+ self._end_pending = False+ threading.Thread(target=on_end, daemon=True).start()++ def give_up(self, reason: str, infra: bool) -> None:+ """End the trial's episode without a new one (never a reset): log it, write the verdict unless one is on disk,+ leave the PID file for the collect hook, and idle until the container is removed."""+ faulthandler.cancel_dump_traceback_later()+ self.log({"ev": "restart_refused", "reason": reason})+ if not final_verdict(self.data):+ fin = _read_json(self.f_final)+ fin.update({"success": False, "done": True, "reason": reason, "finalized": True, "gave_up": True, "protocol": PROTOCOL,+ **self.final_fields(), **({"infra_error": 1} if infra else {})})+ _write_json(self.f_final, fin)+ with open(self.f_pid, "w") as f:+ f.write(str(os.getpid()))+ signal.signal(signal.SIGUSR1, lambda *_: None)+ print(f"[{PROTOCOL}] restart refused: {reason}", flush=True)+ while True:+ time.sleep(3600)+++# ---------------------------------------------------------------- the HTTP layer+class Service:+ """What a sim service hands to serve(): spec (dict), lock, state (the service's E dict: done, reason, exiting, error),+ submit(meta, actions) and observe(meta) -> (meta_out, rcb1 parts), kinematics(req) -> dict, status() -> dict,+ on_submit_error(exc) (a simulator error in /submit or /observe: it ends the episode, reason "error"), log(event dict),+ and for crash recovery the Episode with the simulator's state_sha() and proprio row()."""++ def __init__(self, *, spec: dict, lock, state: dict, submit: Callable, observe: Callable, kinematics: Callable,+ status: Callable, on_submit_error: Callable, log: Callable, max_resp: int = MAX_RESP,+ episode: Episode | None = None, state_sha: Callable | None = None, proprio: Callable | None = None):+ self.spec, self.lock, self.state, self.submit, self.observe = spec, lock, state, submit, observe+ self.kinematics, self.status, self.on_submit_error, self.log, self.max_resp = kinematics, status, on_submit_error, log, max_resp+ self.episode, self.state_sha, self.proprio = episode, state_sha, proprio+ self.kin_calls = 0+++def make_handler(svc: Service):+ class H(BaseHTTPRequestHandler):+ def _send(self, code: int, ctype: str, body: bytes) -> None:+ try:+ self.send_response(code); self.send_header("Content-Type", ctype); self.send_header("Content-Length", str(len(body)))+ self.end_headers(); self.wfile.write(body)+ except (BrokenPipeError, ConnectionResetError): # the client hung up (its call was abandoned)+ pass++ def _json(self, code: int, obj: dict) -> None:+ self._send(code, "application/json", json.dumps(obj).encode())++ def _rcb1(self, meta: dict, parts: list) -> None:+ self._send(200, "application/x-rcb1", encode_response(meta, parts, svc.max_resp))++ def log_message(self, *a):+ pass++ def _body(self):+ try:+ n = int(self.headers.get("Content-Length", 0))+ except ValueError:+ raise ValueError("bad Content-Length")+ if n > MAX_BODY:+ raise ValueError("request too large")+ raw = self.rfile.read(n) if n else b""+ if raw[:4] == rcb1.MAGIC:+ return rcb1.decode(raw)+ body = json.loads(raw or b"{}")+ if not isinstance(body, dict):+ raise ValueError("body must be a JSON object or an RCB1 frame")+ return body, {}++ def do_GET(self):+ if self.path.startswith("/operations/") and svc.episode:+ op = self.path.removeprefix("/operations/")+ if not re.fullmatch(r"[A-Za-z0-9_-]{1,80}", op):+ return self._json(400, {"error": "invalid operation ID"})+ record = _read_json(os.path.join(svc.episode.data, "operations", op + ".json"))+ return self._json(200 if record else 404, record)+ if self.path == "/spec":+ return self._json(200, svc.spec)+ if self.path == "/status":+ st = svc.status()+ return self._json(200, {**st, "resumes": svc.episode.resumes} if svc.episode else st)+ return self._json(404, {"error": "no such endpoint"})++ def do_POST(self):+ if self.path not in ("/submit", "/observe", "/kinematics"):+ return self._json(404, {"error": "no such endpoint"})+ try:+ meta, parts = self._body()+ except Exception as exc: # noqa: BLE001+ return self._json(413 if "too large" in str(exc) else 400, {"error": str(exc)[:200]})+ E, ep = svc.state, svc.episode+ if self.path == "/kinematics": # stateless: never steps physics, never waits for the episode lock+ cid = ep.call_begin(self.path) if ep else 0+ try:+ req = parse_kinematics(meta, svc.spec["kinematics"])+ out = svc.kinematics(req); svc.kin_calls += 1+ except ValueError as exc:+ return self._json(400, {"error": str(exc)[:300]})+ except Exception as exc: # noqa: BLE001+ traceback.print_exc(); return self._json(500, {"error": "kinematics error", "detail": repr(exc)[:200]})+ finally:+ if ep:+ ep.call_end(cid, self.path)+ return self._json(200, out)+ operation = meta.get("operation_id")+ deadline = meta.get("request_deadline_unix_s")+ if operation is not None and (not isinstance(operation, str) or not re.fullmatch(r"[A-Za-z0-9_-]{1,80}", operation)):+ return self._json(400, {"error": "invalid operation ID"})+ if deadline is not None and (not isinstance(deadline, (int, float)) or not math.isfinite(deadline)):+ return self._json(400, {"error": "invalid request deadline"})+ with svc.lock:+ if deadline is not None and time.time() >= deadline:+ return self._json(408, {"error": "request expired before execution"})+ operation_path = None+ if operation and ep:+ directory = os.path.join(ep.data, "operations")+ os.makedirs(directory, exist_ok=True)+ operation_path = os.path.join(directory, operation + ".json")+ if os.path.exists(operation_path):+ return self._json(409, {"reason": "operation already admitted; query /operations/" + operation,+ "operation_id": operation, "operation": _read_json(operation_path)})+ if E.get("exiting") or (self.path == "/submit" and E.get("done")):+ return self._json(409, {"error": "episode finished", "reason": E.get("reason")})+ if operation_path:+ _write_json(operation_path, {"operation_id": operation, "status": "admitted", "rows_before": ep.rows})+ cid = ep.call_begin(self.path, self.connection, deadline=deadline) if ep else 0+ rows0 = ep.rows if ep else 0+ out = blobs = err = None; stopped = None+ try:+ if self.path == "/submit":+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions", []), dtype=np.float64)+ out, blobs = svc.submit(meta, A)+ else:+ out, blobs = svc.observe(meta)+ except ValueError as exc:+ err = (400, {"error": str(exc)})+ except Exception as exc: # noqa: BLE001+ traceback.print_exc(); E["error"] = repr(exc); svc.log({"ev": "error", "path": self.path, "error": repr(exc)})+ try:+ svc.on_submit_error(exc) # PROTOCOL §4.6: a simulator error ends the episode (reason "error")+ except Exception: # noqa: BLE001+ traceback.print_exc()+ err = (500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ if ep:+ stopped = ep.stop_reason+ if err is None or ep.rows != rows0:+ try:+ ep.checkpoint(E, svc.state_sha, svc.proprio)+ except Exception as exc: # failed durable commit is an infrastructure error+ traceback.print_exc()+ err = (500, {"error": "execution checkpoint could not be committed"})+ E["error"] = repr(exc)+ try:+ svc.on_submit_error(exc)+ except Exception:+ traceback.print_exc()+ ep.call_end(cid, self.path, **({"stopped": stopped} if stopped else {}))+ if operation_path:+ _write_json(operation_path, {"operation_id": operation, "status": "error" if err else "completed",+ "rows_before": rows0, "committed_rows": ep.rows, "stopped": stopped})+ if err is not None:+ return self._json(*err)+ return self._rcb1({**out, "stopped": stopped, "operation_id": operation}, blobs)+ return H+++def serve(svc: Service, port: int = 8000) -> None:+ """Check the spec, then serve forever (one request at a time, as every 2.x service)."""+ svc.spec.setdefault("on_service_error", ON_SERVICE_ERROR)+ if svc.episode is not None:+ svc.spec.setdefault("max_call_wall_s", svc.episode.max_call_wall_s)+ check_spec(svc.spec)+ HTTPServer(("0.0.0.0", port), make_handler(svc)).serve_forever()+++# ---------------------------------------------------------------- the collect hook's first half+def _alive(pid: int) -> bool:+ try:+ os.kill(pid, 0)+ except ProcessLookupError:+ return False+ except PermissionError:+ return True+ try: # a zombie (exited, not yet reaped) counts as gone+ with open(f"/proc/{pid}/stat") as f:+ return f.read().rsplit(")", 1)[-1].split()[0] != "Z"+ except OSError:+ return True+++def end_episode(data: str, timeout: float = 21600.0, wait_key: str | None = None, resend_s: float = 30.0) -> int:+ """Tell the service that the agent phase is over and wait until it has ended the episode. The service may be+ resuming after a crash (no PID file until it is ready again), so there is no fixed start-up wait, and a signal goes+ only to the process that wrote the newest line of startups.jsonl (its SIGUSR1 handler is installed by then).+ Returns 2: final.json already holds the verdict (the service gave up after crashes, or graded in-process; with+ `wait_key`, also waits for that final.json key, e.g. video_done): keep it, run no replays.+ Returns 0: the service dumped the episode (episode.json) and exited: the hook replays it next.+ Returns 1: timed out."""+ t0, sent = time.time(), 0.0+ f_pid, f_dump, f_starts = (os.path.join(data, n) for n in ("server.pid", "episode.json", "startups.jsonl"))+ while time.time() - t0 < timeout:+ fin = final_verdict(data)+ if fin is not None:+ if not wait_key or fin.get(wait_key) or fin.get("gave_up"):+ return 2+ time.sleep(2); continue+ try:+ with open(f_pid) as f:+ pid = int(f.read().strip())+ except (OSError, ValueError):+ pid = None+ try:+ with open(f_starts) as f:+ cur = json.loads([x for x in f if x.strip()][-1]).get("pid")+ except (OSError, ValueError, IndexError):+ cur = None # a service without Episode.startup(): signal what the PID file names+ if pid is not None and (cur is None or pid == cur):+ if not _alive(pid):+ if os.path.exists(f_dump):+ return 0+ elif time.time() - sent >= resend_s:+ try:+ os.kill(pid, signal.SIGUSR1); sent = time.time()+ except OSError:+ pass+ time.sleep(2)+ return 1+++if __name__ == "__main__":+ a = sys.argv[1:]+ if len(a) >= 2 and a[0] == "end":+ wk = a[a.index("--wait-key") + 1] if "--wait-key" in a else None+ to = float(a[a.index("--timeout") + 1]) if "--timeout" in a else 21600.0+ rc = end_episode(a[1], timeout=to, wait_key=wk)+ print({0: "[collect] episode dumped by the service", 1: "[collect] timed out waiting for the service",+ 2: "[collect] final.json already holds the verdict"}[rc], flush=True)+ sys.exit(rc)+ if len(a) == 2 and a[0] == "final-verdict":+ fin = final_verdict(a[1])+ print(json.dumps(fin) if fin else "no final verdict")+ sys.exit(0 if fin else 1)+ print("usage: rcb_limited.py end <data dir> [--wait-key KEY] [--timeout S] | final-verdict <data dir>")+ sys.exit(2)diff --git a/scripts/humanoidbench/README.md b/scripts/humanoidbench/README.mdindex 4246056f19..e6996d13c4 100644--- a/scripts/humanoidbench/README.md+++ b/scripts/humanoidbench/README.md@@ -15,10 +15,12 @@ results are published on the results site, not kept here (PROTOCOL D3).| `measure_baselines.py` | the `nop` / `hold` floors for all 32 upstream families, including the ones outside the subset — the evidence for which families deserve a slot || `templates/` | everything the tasks have in common, with `{{placeholders}}` || `templates/standard/`, `shared/standard/` | the same for each task's standard-mode twin (`tasks/*-standard/`, `eai-standard/2.1`): `rcb_service.py` (the simulator-agnostic service: endpoints, RCB1, episode, crash recovery, finalize), `server.py` (the HumanoidBench adapter), `rcb_client.py` and `rcb1.py` (the agent's client; its docstring is the API reference), `grade.py` (the verifier). Per-task prose is the task's `[task.standard]` table in `subset.toml` |+| `templates/realtime/`, `shared/realtime/` | each task's realtime-mode task (`tasks/*-realtime/`, `eai-standard/2.3`, PROTOCOL §4b): standard mode on a clock that does not stop. `rcb_realtime.py` (the service: extends `rcb_service.py` with the clock and the plan; the sidecar runs it instead of `server.py`, with the same adapter) and `rcb_client.py` (the realtime client: `submit(..., at=)`, `cameras=[]`); the rest is the standard twin's || `standard_probe.py` | what `measure-standard` runs against a twin's sim service: contract checks, the all-zero return against `facts.json`, a crash it must survive, the t=0 camera images, and the per-step cost the instruction quotes |-| `oracles/<family>/<mode>/` | a task's reference solution, where it has one (only the `sit_simple` fixture, both modes; files directly under `oracles/<family>/` go to both); `render` copies it to `solution/` |+| `oracles/<family>/<mode>/` | a task's reference solution, where it has one (only the `sit_simple` fixture: privileged, standard and realtime; files directly under `oracles/<family>/` go to all); `render` copies it to `solution/` || `configs/codex-openrouter.yaml` | the Harbor job config the recorded runs use: Harbor's own Codex agent on OpenRouter || `tests/test_standard_service.py` | the standard-mode service, client and verifier against a toy simulator (`python -m unittest discover -s scripts/humanoidbench/tests`; numpy, requests) |+| `tests/test_realtime_service.py` | the realtime service and client over real HTTP against the same toy simulator: the paused start, the clock, the plan (`at`, late rows, holds), watches, resets, the tick log, a restart || `gen_tasks.py` | the generator |```bash@@ -52,8 +54,8 @@ floor measured any other way would not be the number the verifier produces.## Changing something that every task shares-Edit `shared/` or `templates/`, then `./gen_tasks.py render`. All twenty directories (10 tasks, two modes)-move together — that is the whole point. `./gen_tasks.py check` fails when `tasks/` is not what `render` writes from+Edit `shared/` or `templates/`, then `./gen_tasks.py render`. All thirty directories (10 tasks: privileged, standard,+realtime) move together — that is the whole point. `./gen_tasks.py check` fails when `tasks/` is not what `render` writes fromtoday's sources, so a bar in `facts.json` or a sentence in `subset.toml` cannot change without the instructionsfollowing.diff --git a/scripts/humanoidbench/gen_tasks.py b/scripts/humanoidbench/gen_tasks.pyindex 9f21d2d2d5..b242635dcc 100755--- a/scripts/humanoidbench/gen_tasks.py+++ b/scripts/humanoidbench/gen_tasks.py@@ -18,6 +18,10 @@ everything the tasks have in common, `shared/` the one real copy of the tooling# behaves differently from the verifier's (needs mujoco)./gen_tasks.py measure-standard [family ...] # check + time the standard twins' sim service (needs a GPU)+Every task with a [task.standard] table is rendered three times, once per mode (docs/PROTOCOL.md D12):+`<base>-privileged`, `<base>-standard` and `<base>-realtime` (the realtime mode, §4b: standard mode on a clock that does+not stop, eai-standard/2.3; templates/realtime/, shared/realtime/).+`render` writes everything in tasks/ but the instances, which only `instance` draws, and the results/ of agent runs;it is idempotent:run it after editing a template or a prose field and every task picks the change up at once.@@ -455,6 +459,63 @@ def render_standard(t: dict, defaults: dict, facts: dict, tasks_dir: Path = TASKreturn out+# --------------------------------------------------------------------------- realtime mode+# The realtime task (`<base>-realtime`, mode = "realtime", eai-standard/2.3; docs/PROTOCOL.md §4b): the standard twin's+# sidecar, client and verifier, except that the service runs the episode on a clock that does not stop+# (shared/realtime/rcb_realtime.py, which extends rcb_service.py unchanged) and the agent gets the realtime client+# (`submit(..., at=)`, `cameras=[]`). Same instance, same success rule; reset budget `realtime_reset_budget` (50, the+# realtime mode's default); the instruction tells the agent to drive the robot with a controller program.+REALTIME_TEMPLATES = HERE / "templates" / "realtime"+REALTIME = HERE / "shared" / "realtime"+++def realtime_dir_name(t: dict, instance_tag: str) -> str:+ return base_dir_name(t, instance_tag) + "-realtime"+++# A recorded episode (a trajectory) is solvability evidence here too: planned from step 0 (`at`), its rows run at the+# same control steps.+REALTIME_EVIDENCE = ("; in realtime, the same rows planned into the episode from step 0 (submit's `at`) run at the "+ "same control steps and are graded the same")+++def realtime_context(t: dict, defaults: dict, facts: dict) -> dict:+ ctx = standard_context(t, defaults, facts)+ ev = t.get("oracle_evidence", "")+ ev += REALTIME_EVIDENCE if "trajectory" in ev else "" # a recorded episode, not "none known" or an RL score+ # the realtime tasks' own human review: the pull request that added them (realtime_review_pr; 0: not opened yet)+ review = {**defaults, "review_pr": defaults.get("realtime_review_pr", 0)}+ oracle = oracle_fields({**t, "oracle_evidence": ev}, review, "realtime")+ return ctx | {+ "base_dir": base_dir_name(t, defaults["instance_tag"]),+ "control_ms": f"{1000 / facts['standard']['control_hz']:g}",+ "reset_budget": int(defaults["realtime_reset_budget"]), # the compose default: spec()["budget"]["reset"]+ "reference_solution": t.get("realtime_reference_solution", NO_ORACLE),+ **oracle,+ }+++def render_realtime(t: dict, defaults: dict, facts: dict, tasks_dir: Path = TASKS_DIR) -> Path:+ ctx = realtime_context(t, defaults, facts)+ out = tasks_dir / realtime_dir_name(t, defaults["instance_tag"])+ clear(out)+ write_templates(out, REALTIME_TEMPLATES, [+ ("instruction.md", "instruction.md"), ("README.md", "README.md"), ("task.toml", "task.toml"),+ ("docker-compose.yaml", "environment/docker-compose.yaml")], ctx, md_width=110, items=True)+ write_templates(out, STANDARD_TEMPLATES, [+ ("Dockerfile", "environment/Dockerfile"), ("tests.Dockerfile", "tests/Dockerfile"),+ ("test.sh", "tests/test.sh")], ctx, md_width=110, items=True)+ sim = write_sim_dir(t, defaults, out / "environment" / "sim")+ shutil.copyfile(REALTIME / "rcb_realtime.py", sim / "rcb_realtime.py")+ for src, rel in [(REALTIME / "rcb_client.py", "environment/rcb_client.py"),+ (STANDARD / "rcb1.py", "environment/rcb1.py"), (STANDARD / "grade.py", "tests/grade.py")]:+ shutil.copyfile(src, out / rel)+ shutil.copyfile(CLIENT_REQUIREMENTS, out / "environment/client-requirements.txt")+ copy_oracle(t, "realtime", out)+ normalize(out)+ return out++# --------------------------------------------------------------------------- simulator-backed commandsdef unresolved_calls(family: str) -> list[str]:"""What the restored formula calls on self that the task class does not have. The all-zero episode cannot find@@ -628,6 +689,7 @@ def cmd_render(args) -> int:print("rendered", render(t, defaults, facts[t["family"]]).relative_to(ROOT))if "standard" in t:print("rendered", render_standard(t, defaults, facts[t["family"]]).relative_to(ROOT))+ print("rendered", render_realtime(t, defaults, facts[t["family"]]).relative_to(ROOT))return 0@@ -639,7 +701,8 @@ FORMULA = re.compile(r"\b(?:bounds|margin|value_at_margin)\s*=|\brewards\.toleradef leaks(task: Path, families: set[str]) -> list[str]:toml = tomllib.loads((task / "task.toml").read_text())- own, standard = toml["metadata"]["family"], toml["metadata"]["mode"] == "standard"+ # standard and realtime tasks score in the sim sidecar (environment/sim/), privileged ones in tests/+ own, standard = toml["metadata"]["family"], toml["metadata"]["mode"] in ("standard", "realtime")home = task / ("environment/sim" if standard else "tests") # where the formula has to beagent_side = [f for f in sorted((task / "environment").rglob("*"))if f.is_file() and not (standard and home in f.parents)]@@ -676,6 +739,7 @@ def cmd_check(args) -> int:rendered.add(render(t, defaults, facts[t["family"]], Path(tmp)).name)if "standard" in t:rendered.add(render_standard(t, defaults, facts[t["family"]], Path(tmp)).name)+ rendered.add(render_realtime(t, defaults, facts[t["family"]], Path(tmp)).name)for name in sorted(rendered):failed += _diff(filecmp.dircmp(TASKS_DIR / name, Path(tmp) / name, ignore=["results", "__pycache__"]), name)failed += [f"tasks/{p.name}: no longer rendered" for p in sorted(TASKS_DIR.glob("humanoidbench-*"))diff --git a/scripts/humanoidbench/oracles/sit_simple/realtime/oracle_stream.py b/scripts/humanoidbench/oracles/sit_simple/realtime/oracle_stream.pynew file mode 100644index 0000000000..339ee2c597--- /dev/null+++ b/scripts/humanoidbench/oracles/sit_simple/realtime/oracle_stream.py@@ -0,0 +1,24 @@+"""Oracle (realtime mode): the reference actions (sit_simple_actions.npy, the privileged oracle's 999 rows of+normalised servo targets) streamed through the client into the running episode's plan, one row per control step+(dt = control_dt, hold): the first chunk starts the clock at step 0, the second is planned from the step where the+first ends, long before that step comes, so every row runs at its own control step. The service ends the episode the+moment the return reaches the pass mark."""+import os+import time++import numpy as np+from rcb_client import Robot++A = np.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), "sit_simple_actions.npy"))+r = Robot()+dt = r.spec()["control_dt"]+rows = int(round(r.spec()["max_chunk_s"] / dt))+for i in range(0, len(A), rows):+ res = r.submit("joint_position", A[i:i + rows], dt=dt, interpolate="hold", at=i, cameras=[], note="oracle")+ print(f"rows {i}..{i + len(A[i:i + rows])} planned from step {res.meta['start_step']}, late {res.meta['late_steps']}",+ flush=True)+ if res.meta["late_steps"]:+ raise SystemExit("a chunk arrived late: the episode would not run the reference rows")+while not r.status()["done"]:+ time.sleep(0.5)+print(r.status())diff --git a/scripts/humanoidbench/oracles/sit_simple/realtime/solve.sh b/scripts/humanoidbench/oracles/sit_simple/realtime/solve.shnew file mode 100755index 0000000000..e2239bea1f--- /dev/null+++ b/scripts/humanoidbench/oracles/sit_simple/realtime/solve.sh@@ -0,0 +1,8 @@+#!/bin/bash+# Oracle (realtime mode): the privileged oracle's recorded actions (solution/sit_simple_actions.npy) streamed+# through the client into the episode's plan, one row per control step. No privileged information at run time: only+# the recorded actions.+set -e+mkdir -p /app/output+cp /solution/oracle_stream.py /app/output/gen.py+python /solution/oracle_stream.pydiff --git a/scripts/humanoidbench/shared/realtime/rcb_client.py b/scripts/humanoidbench/shared/realtime/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/scripts/humanoidbench/shared/realtime/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/scripts/humanoidbench/shared/realtime/rcb_realtime.py b/scripts/humanoidbench/shared/realtime/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/scripts/humanoidbench/shared/realtime/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/scripts/humanoidbench/subset.toml b/scripts/humanoidbench/subset.tomlindex fd61272bb1..eaa4de7142 100644--- a/scripts/humanoidbench/subset.toml+++ b/scripts/humanoidbench/subset.toml@@ -100,6 +100,10 @@ review_pr = 9 # the pull request whose human review covers the tas# own, so with no reset its whole run is one blind attempt. A per-benchmark exception to PROTOCOL D1's default of 0# (docs/benchmarks/humanoidbench.md); `RCB_RESET_BUDGET=0 harbor run ...` still runs the single-episode configuration.standard_reset_budget = 50+# Realtime mode's reset budget (docs/PROTOCOL.md §4b): 50 by default, the owner's decision of 2026-10-08 (the same as+# standard's); `RCB_RESET_BUDGET` overrides it. RCB_SLOWDOWN defaults to 1 (real time); other values are reported apart.+realtime_reset_budget = 50+realtime_review_pr = 83 # the pull request that added the realtime tasks; the owner approved them there (2026-10-08)# =====================================================================================================# C1 · flat-ground periodic gait@@ -610,6 +614,11 @@ CI fixture, not something an agent could reproduce from inside.'''standard_reference_solution = '''`solution/oracle_replay.py` sends the same 999 rows (`solution/sit_simple_actions.npy`)through the client, one per control step (`dt` = 0.02 s, `interpolate="hold"`), as an agent would send them; theservice ends the episode the moment the return reaches the pass mark. No privileged information at run time.'''+realtime_reference_solution = '''`solution/oracle_stream.py` plans the same 999 rows (`solution/sit_simple_actions.npy`)+into the running episode, one per control step (`dt` = 0.02 s, `interpolate="hold"`): the first 500 from step 0 (that+call starts the clock), the rest from step 500 (`at=500`), sent at once, so every row runs at its own control step+whatever the timing; the service ends the episode the moment the return reaches the pass mark. No privileged+information at run time.'''[task.standard]ends = "when the robot falls (its pelvis drops below about half its standing height, not far below its height when seated)"diff --git a/scripts/humanoidbench/templates/realtime/README.md b/scripts/humanoidbench/templates/realtime/README.mdnew file mode 100644index 0000000000..f0ce285d8d--- /dev/null+++ b/scripts/humanoidbench/templates/realtime/README.md@@ -0,0 +1,43 @@+# {{base_dir}}-realtime++Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of `{{task_dir}}`:+the same frozen instance and the same success rule as it and as `{{base_dir}}-standard`, but time does not stop. Once an+episode's clock starts (its first `submit`), the robot runs at {{control_hz}} Hz in real time and holds its last servo+targets while the agent thinks, so the agent drives it with a controller program of its own that streams targets+through the client. HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar+for rendering; `environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records+every control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; {{standard_tier}}, no reward; {{reset_budget}} resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every {{control_dt}} s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: {{nu}} normalised position targets, {{control_hz}} Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x {{control_dt}} s, results reported apart from S = 1), `RCB_RESET_BUDGET` ({{reset_budget}}: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |++Note: {{standard_camera_note}}++## Difficulty+See `{{task_dir}}`'s README; here the agent also has no state, no reward and no simulator of its own, and+{{reset_budget}} resets instead of unlimited ones, as in `{{base_dir}}-standard`. On top of that the robot does not wait:+a model's turn takes seconds and the H1 falls within a few seconds of robot time without active balance, so the agent+cannot steer it turn by turn. It has to write a controller that closes the loop at the control rate from+proprioception, run it in the background, and improve it between episodes from what it observed.++## Reference solution+{{reference_solution}}++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the fresh replay+ends in the live run's state), computed in the sidecar for the last episode. The replay re-executes the servo targets+the simulator applied at each control step (`trajectory_live.f64`), not the ones the agent submitted, so it equals the+live run whatever the timing was; `ticks.f64` records each step's wall time and the call that planned it, `calls.jsonl`+and `submitted.f64` every call and every row as sent, and `final.json` the clock's lag (`max_lag_s`, `stalls`) and the+held and late steps. `tests/grade.py` reads `/data/final.json` and copies the evidence to `/logs/verifier/`+(`replay.mp4`). A simulator crash or error is an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL+§4b). {{oracle_note}}++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`, `templates/standard/`,+`shared/standard/` and `shared/realtime/`.diff --git a/scripts/humanoidbench/templates/realtime/docker-compose.yaml b/scripts/humanoidbench/templates/realtime/docker-compose.yamlnew file mode 100644index 0000000000..041a66e5eb--- /dev/null+++ b/scripts/humanoidbench/templates/realtime/docker-compose.yaml@@ -0,0 +1,36 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: {{image}}+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: {{family}}+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: {{standard_task_text}}+ RCB_EPISODE: {{standard_episode_text}}+ RCB_WALL_CLOCK_S: "{{agent_timeout}}"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-{{reset_budget}}}" # realtime mode's default: {{reset_budget}} resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+{{room_camera_pose_line}}{{image_hw_line}} NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/scripts/humanoidbench/templates/realtime/instruction.md b/scripts/humanoidbench/templates/realtime/instruction.mdnew file mode 100644index 0000000000..6a6882f480--- /dev/null+++ b/scripts/humanoidbench/templates/realtime/instruction.md@@ -0,0 +1,62 @@+# {{family}} — instance {{instance_tag}} — REALTIME MODE (robot as a service; time does not stop)++You control a simulated {{robot_label}} (HumanoidBench / MuJoCo) **as if it were real hardware**: the+simulator runs in a service you cannot open, and you reach it only through a client. Like real hardware, it does not+wait for you: once an episode is running, the robot moves in real time while you think. **Task:** {{standard_task_statement}}++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the robot runs at {{control_hz}} Hz in real time, one control step every {{control_dt}} s of wall-clock time, whether or not you send anything, until the episode ends. Between your commands it executes what you planned, then holds the last servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at the control rate, reads the proprioception and sends targets, started in the background (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs. Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds{{standard_ends}}, after {{max_steps}} control steps ({{robot_seconds}} s after its clock started), when your time is up, or on a simulator error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on its side, sitting, face down) goes on until step {{max_steps}}, so an episode that is still running does not mean the robot is still up. You have **{{reset_budget}} resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says 0, there is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the same frozen starting state and starts a new episode, paused until your next `submit`, with its own {{max_steps}} control steps, either abandoning the running episode or after one has ended. Once an episode has ended, `submit` raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is graded**: an episode you reset away is never graded, a success ends the run at once (no reset after it), and when your time is up the episode then running is the one graded. The wall clock is the run's and never restarts; `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, {{image_hw}}), its own joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not {{standard_hidden}}, not the reward: the service judges the episode and only tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a simulator error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's range, each joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the IMU's frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for the record: save it early and keep it current.+- Wall-clock budget: {{agent_timeout_hours}} h. It ends without warning, and ending your session earlier ends the run the same way (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the plan from control step `at` (an episode step number; by default the next step), replacing whatever was planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held. `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending faster than {{control_hz}} Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of {{control_dt}}) and the next chunk before the plan runs out, with `at` = the step where it should take over; `at` may be up to `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control step at the earliest. A row whose step has already run when the request arrives is dropped (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**{{success_bar}}**: a total of rewards, not a number of steps (an episode is at most {{max_steps}} steps).+{{standard_scoring_note}} The service ends the episode the moment the pass mark is reached (`reason = "success"`), and+a fresh replay of the recorded episode, the servo targets the robot actually executed at each control step, must reach+it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference (`python -c "import rcb_client;+help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` ({{control_dt}} s), `max_control_steps` ({{max_steps}}), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt={{control_dt}}, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of {{nu}} targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most {{reset_budget}} in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # ({{nu}},) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about {{ms_single}} ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is {{control_ms}} ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order, the joints'+angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`, `imu_gravity_*`).++{{standard_actuator_layout}}diff --git a/scripts/humanoidbench/templates/realtime/task.toml b/scripts/humanoidbench/templates/realtime/task.tomlnew file mode 100644index 0000000000..3a2c544d5d--- /dev/null+++ b/scripts/humanoidbench/templates/realtime/task.toml@@ -0,0 +1,67 @@+# Realtime-mode task of {{task_dir}} (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/{{base_dir}}-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench {{family}} on the same frozen instance as {{task_dir}}. Time does not stop: once an episode starts, the robot runs at {{control_hz}} Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to {{reset_budget}} resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "{{robot}}", "{{paper_category_long}}", "realtime-mode", "trajectory", "{{family}}"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "{{robot_slug}}"+family = "{{family}}"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = {{seed}}+expert_time_estimate_hours = {{standard_expert_hours}}+{{oracle_lines}}++[verifier]+timeout_sec = {{verifier_timeout}}+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = {{agent_timeout}}+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = [{{allowed_hosts}}]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/scripts/humanoidbench/tests/test_realtime_service.py b/scripts/humanoidbench/tests/test_realtime_service.pynew file mode 100644index 0000000000..0ffb306f6f--- /dev/null+++ b/scripts/humanoidbench/tests/test_realtime_service.py@@ -0,0 +1,258 @@+"""Tests for the realtime service (shared/realtime/rcb_realtime.py) and its client, over real HTTP, against the toy+simulator of test_standard_service.py: no MuJoCo, no GPU, a few seconds.++ python -m unittest discover -s scripts/humanoidbench/tests # needs numpy and requests+"""+from __future__ import annotations++import importlib.util+import json+import sys+import tempfile+import threading+import time+import unittest+from pathlib import Path++import numpy as np++HERE = Path(__file__).resolve().parent+sys.path.insert(0, str(HERE))+from test_standard_service import FakeSim # noqa: E402 (puts shared/standard/ on sys.path)++REALTIME = HERE.parents[0] / "shared" / "realtime"+sys.path.append(str(REALTIME)) # appended: `import rcb_client` elsewhere stays the standard client+import rcb_realtime # noqa: E402++_spec = importlib.util.spec_from_file_location("rcb_client_realtime", REALTIME / "rcb_client.py")+rcb_client_realtime = importlib.util.module_from_spec(_spec)+_spec.loader.exec_module(rcb_client_realtime)+EpisodeOver, Robot = rcb_client_realtime.EpisodeOver, rcb_client_realtime.Robot+++class FastSim(FakeSim):+ """The toy point at 100 Hz, with a goal it never reaches unless a test wants it to."""+ control_dt, max_control_steps = 0.01, 300++ def __init__(self, goal: float = 1e9):+ super().__init__(goal)+++class Live(unittest.TestCase):+ """A realtime service on a free port: its simulator loop and its HTTP server each in a thread."""++ goal, reset_budget = 1e9, 2++ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.data = self.tmp.name+ self.start_service()++ def start_service(self):+ self.svc = rcb_realtime.RealtimeService(self.make_sim(), data=self.data, reset_budget=self.reset_budget,+ slowdown=1.0)+ self.svc.start()+ self.srv = self.svc.http_server(("127.0.0.1", 0))+ self.stop = threading.Event()+ self.threads = [threading.Thread(target=self.srv.serve_forever, daemon=True),+ threading.Thread(target=self.svc.loop, args=(self.stop,), daemon=True)]+ for t in self.threads:+ t.start()+ self.r = Robot(f"http://127.0.0.1:{self.srv.server_address[1]}", timeout=10)++ def make_sim(self):+ return FastSim(self.goal)++ def stop_service(self):+ self.stop.set()+ self.threads[1].join(5)+ self.srv.shutdown()+ self.srv.server_close()++ def tearDown(self):+ self.stop_service()+ self.tmp.cleanup()++ def finalize(self) -> dict:+ self.svc.on_sigusr1()+ for _ in range(100):+ fin = json.loads(Path(self.data, "final.json").read_text())+ if fin.get("finalized"):+ return fin+ time.sleep(0.05)+ self.fail("never finalized")++ def journal(self) -> np.ndarray:+ return np.fromfile(Path(self.data, "trajectory_live.f64"), dtype=np.float64).reshape(-1, 1)++ def ticks(self) -> np.ndarray:+ return np.fromfile(Path(self.data, "ticks.f64"), dtype=np.float64).reshape(-1, 2)+++class TestClock(Live):+ def test_spec_declares_the_realtime_timing(self):+ t = self.r.spec()["timing"]+ self.assertTrue(t["realtime"])+ self.assertEqual(t["slowdown"], 1.0)+ self.assertAlmostEqual(t["wall_s_per_control_step"], 0.01)+ self.assertEqual(self.r.spec()["protocol"], "eai-standard/2.3")++ def test_an_episode_waits_paused_until_its_first_submit(self):+ self.r.get_obs()+ time.sleep(0.15)+ st = self.r.status()+ self.assertEqual(st["control_steps"], 0)+ self.assertFalse(st["running"])++ def test_time_runs_after_the_first_submit_and_the_last_target_is_held(self):+ res = self.r.submit("move", [[0.5]] * 3, interpolate="hold", cameras=[])+ self.assertEqual(res.meta["start_step"], 0)+ self.assertTrue(res.meta["running"])+ self.assertEqual(res.last, {}) # cameras=[]: no images+ self.assertEqual(res.proprio.shape, (1, 2))+ time.sleep(0.3)+ st = self.r.status()+ self.assertTrue(20 <= st["control_steps"] <= 40, st) # about 30 steps at 100 Hz+ res = self.r.get_obs(cameras=[])+ self.assertAlmostEqual(res.meta["proprio"]["now"][1], 0.5) # the command still in force: held+ self.assertGreater(self.svc.held_steps, 10)++ def test_a_watch_returns_what_ran_meanwhile(self):+ self.r.submit("move", [[1.0]], cameras=[])+ res = self.r.get_obs(cameras=["cam"], seconds=0.1, video="frames", video_fps=30)+ self.assertEqual(res.meta["control_steps_watch"], 10)+ self.assertGreaterEqual(len(res.frames["cam"]), 3)++ def test_a_watch_starts_the_clock(self):+ res = self.r.get_obs(cameras=[], seconds=0.05)+ self.assertEqual(res.meta["control_steps_watch"], 5)+ self.assertTrue(self.r.status()["running"])+++class TestPlan(Live):+ def test_a_chunk_at_a_future_step_keeps_what_is_planned_before_it(self):+ self.r.submit("move", [[1.0]] * 30, interpolate="hold", cameras=[])+ res = self.r.submit("move", [[-1.0]] * 5, interpolate="hold", at=20, cameras=[])+ self.assertEqual(res.meta["start_step"], 20)+ self.assertEqual(res.meta["plan_end_step"], 25)+ time.sleep(0.4)+ fin = self.finalize()+ j = self.journal()[:, 0]+ np.testing.assert_array_equal(j[:20], 1.0)+ np.testing.assert_array_equal(j[20:25], -1.0)+ np.testing.assert_array_equal(j[25:], -1.0) # then held+ self.assertTrue(fin["deterministic"])+ self.assertEqual(fin["realtime"], 1)++ def test_a_late_chunk_drops_the_rows_whose_steps_are_gone(self):+ self.r.submit("move", [[0.0]], cameras=[])+ time.sleep(0.1)+ res = self.r.submit("move", np.linspace(0.01, 1.0, 100)[:, None], interpolate="hold", at=0, cameras=[])+ late = res.meta["late_steps"]+ self.assertGreater(late, 5)+ self.assertEqual(res.meta["start_step"], res.meta["control_steps"])+ time.sleep(0.05)+ self.finalize()+ j = self.journal()[:, 0]+ self.assertAlmostEqual(j[res.meta["control_steps"]], np.linspace(0.01, 1.0, 100)[late])++ def test_linear_starts_from_the_target_in_force_before_at(self):+ self.r.submit("move", [[0.0]] * 10, interpolate="hold", cameras=[])+ self.r.submit("move", [[1.0]], dt=0.04, interpolate="linear", at=8, cameras=[])+ time.sleep(0.2)+ self.finalize()+ np.testing.assert_allclose(self.journal()[8:12, 0], [0.25, 0.5, 0.75, 1.0])++ def test_at_too_far_ahead_or_malformed_is_400(self):+ for at in (10_000, -1, 1.5, "x"):+ with self.assertRaises(ValueError):+ self.r._post("/submit", {"type": "move", "actions": [[0.1]], "at": at}, [], timeout=10)+ self.assertEqual(self.r.status()["calls"], 0)++ def test_the_tick_log_lines_up_with_the_journal(self):+ self.r.submit("move", [[0.2]] * 5, cameras=[])+ time.sleep(0.2)+ self.finalize()+ ticks, journal = self.ticks(), self.journal()+ self.assertEqual(len(ticks), len(journal))+ np.testing.assert_array_equal(ticks[:5, 1], 1.0) # call 1's chunk+ self.assertTrue(np.all(ticks[5:, 1] == -1.0)) # then held+ self.assertTrue(np.all(np.diff(ticks[:, 0]) >= 0))+ self.assertTrue(Path(self.data, "calls.jsonl").read_text().strip())+ self.assertEqual(np.fromfile(Path(self.data, "submitted.f64")).reshape(-1, 2).tolist(), [[1.0, 0.2]] * 5)+++class TestEnd(Live):+ goal = 0.5++ def test_success_stops_the_clock_and_submit_is_409(self):+ self.r.submit("move", [[1.0]] * 10, interpolate="hold", cameras=[])+ time.sleep(0.15)+ st = self.r.status()+ self.assertTrue(st["done"])+ self.assertEqual(st["reason"], "success")+ self.assertFalse(st["running"])+ self.assertEqual(st["control_steps"], 5)+ with self.assertRaises(EpisodeOver):+ self.r.submit("move", [[1.0]], cameras=[])+ fin = self.finalize()+ self.assertTrue(fin["success"])+ self.assertEqual(fin["protocol"], "eai-standard/2.3")+ self.assertNotIn("infra_error", fin)++ def test_a_watch_ends_with_the_episode(self):+ self.r.submit("move", [[1.0]] * 10, interpolate="hold", cameras=[])+ res = self.r.get_obs(cameras=[], seconds=1.0)+ self.assertTrue(res.done)+ self.assertLess(res.meta["control_steps_watch"], 10)+++class TestSimulatorError(Live):+ """PROTOCOL §4b: a simulator error that ends the graded episode is infrastructure, not the agent's failure."""++ def make_sim(self):+ def boom(sim):+ if sim.t == 5:+ raise RuntimeError("simulator blew up")+ sim = FastSim(self.goal)+ sim.on_step = boom+ return sim++ def test_a_simulator_error_is_an_infra_error(self):+ self.r.submit("move", [[1.0]] * 10, interpolate="hold", cameras=[])+ time.sleep(0.15)+ st = self.r.status()+ self.assertTrue(st["done"])+ self.assertEqual(st["reason"], "error")+ fin = self.finalize()+ self.assertEqual(fin["infra_error"], 1)+ self.assertFalse(fin["success"])+++class TestResetAndResume(Live):+ def test_a_reset_starts_a_paused_episode(self):+ self.r.submit("move", [[0.3]], cameras=[])+ time.sleep(0.1)+ res = self.r.reset(cameras=["cam"])+ self.assertEqual(res.meta["control_steps"], 0)+ self.assertFalse(res.meta["running"])+ self.assertEqual(res.meta["episode"], 2)+ time.sleep(0.1)+ self.assertEqual(self.r.status()["control_steps"], 0)++ def test_a_restart_resumes_the_same_episode_paused(self):+ self.r.submit("move", [[0.1]] * 400, interpolate="hold", cameras=[])+ time.sleep(1.3) # past the first checkpoint (one per second of robot time)+ self.stop_service()+ n_ticks = len(self.ticks())+ self.start_service()+ st = self.r.status()+ self.assertFalse(st["running"])+ self.assertGreaterEqual(st["control_steps"], 100)+ self.assertEqual(st["control_steps"] % 100, 0) # back at the last checkpoint+ self.assertEqual(len(self.ticks()), self.svc.journal_rows)+ self.assertLessEqual(len(self.ticks()), n_ticks)+++if __name__ == "__main__":+ unittest.main()diff --git a/scripts/task_conventions.py b/scripts/task_conventions.pyindex 2a09687a44..6c97332109 100644--- a/scripts/task_conventions.py+++ b/scripts/task_conventions.py@@ -9,7 +9,7 @@ pre-v1.0 task (old field names, protocol spelling, README layout); renaming direWhat it does:- `task.toml`: `mode` from the directory suffix (`-privileged` / `-standard` / `-realtime`), no `tier`, `protocol = "eai-standard/2.1"`- for standard tasks (realtime tasks: `eai-standard/2.3`, set by their generator), `oracle` (+ `oracle_evidence`) for benchmarks whose oracle level is known from their files.+ for standard tasks (2.2 / 2.4 and realtime tasks' `eai-standard/2.3` are set by their generators), `oracle` (+ `oracle_evidence`) for benchmarks whose oracle level is known from their files.- protocol spelling `rcb-limited/2.x` -> `eai-standard/2.x` and "limited mode" -> "standard mode" in the task's text andservices; the client accepts both spellings.- `tests/test.sh` clears `/logs/verifier` first; graders write `success` (1/0) to `reward.txt` and only numbers to@@ -151,10 +151,11 @@ def normalize_toml(task: Path) -> None:def _shared_sources() -> set[bytes]:- """Contents of the files generators copy unchanged from scripts/eai_standard/ (the eai-standard/2.2 layer, D9): their copies- must stay byte-identical to the source (check-tooling-identity.sh), so normalize never rewrites them."""+ """Contents of the files generators copy unchanged from scripts/eai_standard/ (the eai-standard/2.2 layer, and the 2.4+ layer in v2_4/; D9): their copies must stay byte-identical to the source (check-tooling-identity.sh), so normalize never+ rewrites them."""src = Path(__file__).resolve().parent / "eai_standard"- return {p.read_bytes() for p in src.glob("*") if p.is_file()} if src.is_dir() else set()+ return {p.read_bytes() for p in src.rglob("*") if p.is_file() and "__pycache__" not in p.parts} if src.is_dir() else set()def normalize_text_files(task: Path) -> None:diff --git a/tasks/dataset.toml b/tasks/dataset.tomlindex fc2c7f2047..1e73ab38af 100644--- a/tasks/dataset.toml+++ b/tasks/dataset.toml@@ -4857,3 +4857,43 @@ digest = "sha256:bcd3454da1d025561cd49379c330a500c627553a5f8356022de9444ca47ad05name = "robot-coding-bench/behavior-wash-a-baseball-cap-i00-standard"digest = "sha256:8e41c22f5a7f3b0c1137874d2841fc3e074a3ef4645faa1182b110f919b4d795"+[[tasks]]+name = "robot-coding-bench/humanoidbench-loco-balance-hard-i00-realtime"+digest = "sha256:3bc849e4978c8d460622149681ef7de68acea397b865fb25349ecab14de2e2b6"++[[tasks]]+name = "robot-coding-bench/humanoidbench-loco-hurdle-i00-realtime"+digest = "sha256:07a0e363f302e169045cb1657b1d0aa8dbfe2fba7277cad7b385a753b9b516c2"++[[tasks]]+name = "robot-coding-bench/humanoidbench-loco-sit-simple-i00-realtime"+digest = "sha256:61e0d7446aeb1166c0e88a0169f40e37bae48d666af3a065bd6e6b0f0c20e5cd"++[[tasks]]+name = "robot-coding-bench/humanoidbench-loco-walk-i00-realtime"+digest = "sha256:64807537534bcc06e61d31883112af37db4b099ca815731fea99c1650fa79e9f"++[[tasks]]+name = "robot-coding-bench/humanoidbench-manip-bookshelf-simple-i00-realtime"+digest = "sha256:9d0079f945ab17e1fe913f25e6117a484e70af701ba12a436a9a89611c1d84e9"++[[tasks]]+name = "robot-coding-bench/humanoidbench-manip-cube-i00-realtime"+digest = "sha256:f029c3a894980d4f8dc0743de18db414cb5e08d45d3e73b26e8dba43c276f4b5"++[[tasks]]+name = "robot-coding-bench/humanoidbench-manip-door-i00-realtime"+digest = "sha256:5da640207ccf505e9a3e5f11bfbb16da506c366f17c8587c7ad59d1f3083dc8e"++[[tasks]]+name = "robot-coding-bench/humanoidbench-manip-powerlift-i00-realtime"+digest = "sha256:9d638d785822688ecbd508caeaca7813dcb2115e713a945d535b3358950fae16"++[[tasks]]+name = "robot-coding-bench/humanoidbench-manip-room-i00-realtime"+digest = "sha256:0f69d1f68d980bee70e3a3c386035aeb6ef60a8e185e2fc3f6b3f0870aae9979"++[[tasks]]+name = "robot-coding-bench/humanoidbench-manip-window-i00-realtime"+digest = "sha256:bb9f5d6ec8905c2e5192acf52ff935dd89214a0dda96d4a8b04543fdd1b2cf6c"+diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/README.md b/tasks/humanoidbench-loco-balance-hard-i00-realtime/README.mdnew file mode 100644index 0000000000..1477b7d421--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/README.md@@ -0,0 +1,63 @@+# humanoidbench-loco-balance-hard-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-loco-balance-hard-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1` |+| Family / instance | `balance_hard` / seed 0 |+| Oracle | `none` — none known: no run has passed it, and published RL reaches 31% of the bar (paper 57, FastTD3 244, bar 800); human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-loco-balance-hard-i00-privileged`: the same frozen instance and the same success rule as it and+as `humanoidbench-loco-balance-hard-i00-standard`, but time does not stop. Once an episode's clock starts (its+first `submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks,+so the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see floor and horizon only; the board and the roller under the feet never enter their+view. `room_camera` (the recording camera frozen at its starting pose) sees the robot on the board from+outside.++## Difficulty+See `humanoidbench-loco-balance-hard-i00-privileged`'s README; here the agent also has no state, no reward and+no simulator of its own, and 50 resets instead of unlimited ones, as in+`humanoidbench-loco-balance-hard-i00-standard`. On top of that the robot does not wait: a model's turn takes+seconds and the H1 falls within a few seconds of robot time without active balance, so the agent cannot steer+it turn by turn. It has to write a controller that closes the loop at the control rate from proprioception,+run it in the background, and improve it between episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no board or roller pose, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 19 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..e51aeb11e5--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,36 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: balance_hard+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Stand on the balance board and stay on it, upright and still, for the whole episode."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls, when anything but the roller under the board touches the ground or the roller touches anything but the ground and the board (see Scoring), after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..7561f11321Binary files /dev/null and b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..c56083962d--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "BalanceBase",+ "module": "humanoid_bench.envs.balance",+ "name": "get_reward",+ "source": "def get_reward(self):\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT + 0.37, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.9, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n horizontal_velocity = self.robot.center_of_mass_velocity()[[0, 1]]\n dont_move = rewards.tolerance(horizontal_velocity, margin=2).mean()\n return small_control * stand_reward * dont_move, {\n \"small_control\": small_control,\n \"stand_reward\": stand_reward,\n \"dont_move\": dont_move,\n \"standing\": standing,\n \"upright\": upright,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.balance": "_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.balance.BalanceHard"+}diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/instruction.md b/tasks/humanoidbench-loco-balance-hard-i00-realtime/instruction.mdnew file mode 100644index 0000000000..01927eef3a--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/instruction.md@@ -0,0 +1,120 @@+# balance_hard — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid (HumanoidBench / MuJoCo) **as if it were real hardware**: the+simulator runs in a service you cannot open, and you reach it only through a client. Like real hardware, it+does not wait for you: once an episode is running, the robot moves in real time while you think. **Task:**+stand on the balance board and stay on it, upright and still, for the whole episode.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls, when anything but the+ roller under the board touches the ground or the roller touches anything but the ground and the board (see+ Scoring), after 1000 control steps (20 s after its clock started), when your time is up, or on a simulator+ error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on its side,+ sitting, face down) goes on until step 1000, so an episode that is still running does not mean the robot is+ still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says 0, there+ is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the same+ frozen starting state and starts a new episode, paused until your next `submit`, with its own 1000 control+ steps, either abandoning the running episode or after one has ended. Once an episode has ended, `submit`+ raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is graded**:+ an episode you reset away is never graded, a success ends the run at once (no reset after it), and when your+ time is up the episode then running is the one graded. The wall clock is the run's and never restarts;+ `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not the board's or the roller's pose, not the reward: the service judges the episode+ and only tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a+ simulator error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's+ range, each joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the+ IMU's frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**800**: a total of rewards, not a number of steps (an episode is at most 1000 steps). A good step asks for+only three things, multiplied together — low actuator force, staying upright (head at full standing height on+the board, torso vertical), and near-zero horizontal velocity — so scoring well for one step is not the+difficulty. The difficulty is that the episode ends the moment the pelvis drops too low, anything but the+rolling sphere touches the ground — the board included — or the sphere touches anything other than the ground+and the board, and every step after that is worth nothing. The service ends the episode the moment the pass+mark is reached (`reason = "success"`), and a fresh replay of the recorded episode, the servo targets the+robot actually executed at each control step, must reach it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 19 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (19,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 4.2 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 19 actuators are position servos, in this order: `left_hip_yaw`, `left_hip_roll`, `left_hip_pitch`,+`left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`, `right_hip_pitch`, `right_knee`, `right_ankle`,+`torso`, `left_shoulder_pitch`, `left_shoulder_roll`, `left_shoulder_yaw`, `left_elbow`,+`right_shoulder_pitch`, `right_shoulder_roll`, `right_shoulder_yaw`, `right_elbow`+(`spec()["action_types"]["joint_position"]["layout"]`; the proprioception measures the same joints in the same+order). A target of −1 or +1 commands one end of that servo's range,+`spec()["action_types"]["joint_position"]["range_rad"]` in radians, and the scale is linear, so 0 is+mid-range, not 0 rad. A constant action commands joint *targets*, not a pose: whether the robot stays there+depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/task.toml b/tasks/humanoidbench-loco-balance-hard-i00-realtime/task.tomlnew file mode 100644index 0000000000..03a9fdc40c--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-loco-balance-hard-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-loco-balance-hard-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench balance_hard on the same frozen instance as humanoidbench-loco-balance-hard-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1", "locomotion", "realtime-mode", "trajectory", "balance_hard"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1"+family = "balance_hard"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 8.0+oracle = "none"+oracle_evidence = "none known: no run has passed it, and published RL reaches 31% of the bar (paper 57, FastTD3 244, bar 800); human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/grade.py b/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/test.sh b/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-loco-balance-hard-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/README.md b/tasks/humanoidbench-loco-hurdle-i00-realtime/README.mdnew file mode 100644index 0000000000..7da1de4f79--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/README.md@@ -0,0 +1,63 @@+# humanoidbench-loco-hurdle-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-loco-hurdle-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1` |+| Family / instance | `hurdle` / seed 0 |+| Oracle | `none` — no rollout of this instance; published RL passes the bar (FastTD3 887 / 700); human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-loco-hurdle-i00-privileged`: the same frozen instance and the same success rule as it and as+`humanoidbench-loco-hurdle-i00-standard`, but time does not stop. Once an episode's clock starts (its first+`submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks, so+the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see the walled track and the line of hurdles ahead. `room_camera` stands behind and+above the start and looks down the course (`RCB_ROOM_CAMERA_POSE` in the compose file): the robot stays in+frame from x = 0 to 20 m.++## Difficulty+See `humanoidbench-loco-hurdle-i00-privileged`'s README; here the agent also has no state, no reward and no+simulator of its own, and 50 resets instead of unlimited ones, as in `humanoidbench-loco-hurdle-i00-standard`.+On top of that the robot does not wait: a model's turn takes seconds and the H1 falls within a few seconds of+robot time without active balance, so the agent cannot steer it turn by turn. It has to write a controller+that closes the loop at the control rate from proprioception, run it in the background, and improve it between+episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no hurdle or wall positions, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 19 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..f602bcec1c--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,38 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: hurdle+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Run the robot forward down the walled track and over the hurdles, without touching the side or back walls and without falling."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops near the ground), after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ RCB_ROOM_CAMERA_POSE: "4,0,0.11,10,0,-26" # room_camera behind and above the start, looking down+ # the course: the robot stays in frame from x=0 to 20 m+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..fb4a172d6eBinary files /dev/null and b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..ccce10acf7--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Hurdle",+ "module": "humanoid_bench.envs.basic_locomotion_envs",+ "name": "get_reward",+ "source": "def get_reward(self):\n self.wall_collision_ids = [\n self._env.named.data.geom_xpos.axes.row.names.index(wall_name)\n for wall_name in [\n \"left_barrier_collision\",\n \"right_barrier_collision\",\n \"behind_barrier_collision\",\n ]\n ]\n\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.8, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n com_velocity = self.robot.center_of_mass_velocity()[0]\n move = rewards.tolerance(\n com_velocity,\n bounds=(self._move_speed, float(\"inf\")),\n margin=self._move_speed,\n value_at_margin=0,\n sigmoid=\"linear\",\n )\n move = (5 * move + 1) / 6\n wall_collision_discount = 1\n\n for pair in self._env.data.contact.geom:\n if any(\n [\n wall_collision_id in pair\n for wall_collision_id in self.wall_collision_ids\n ]\n ): # for no hand. if for hand, > 155\n wall_collision_discount = 0.1\n # print(pair)\n break\n\n reward = small_control * stand_reward * move * wall_collision_discount\n\n return reward, {\n \"stand_reward\": stand_reward,\n \"small_control\": small_control,\n \"move\": move,\n \"standing\": standing,\n \"upright\": upright,\n \"wall_collision_discount\": wall_collision_discount,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.basic_locomotion_envs": "_CRAWL_HEIGHT = 0.8\n_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.basic_locomotion_envs.Hurdle"+}diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/instruction.md b/tasks/humanoidbench-loco-hurdle-i00-realtime/instruction.mdnew file mode 100644index 0000000000..076291fea8--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/instruction.md@@ -0,0 +1,121 @@+# hurdle — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid (HumanoidBench / MuJoCo) **as if it were real hardware**: the+simulator runs in a service you cannot open, and you reach it only through a client. Like real hardware, it+does not wait for you: once an episode is running, the robot moves in real time while you think. **Task:** run+the robot forward down the walled track and over the hurdles, without touching the side or back walls and+without falling.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops near+ the ground), after 1000 control steps (20 s after its clock started), when your time is up, or on a+ simulator error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on its+ side, sitting, face down) goes on until step 1000, so an episode that is still running does not mean the+ robot is still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says+ 0, there is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the+ same frozen starting state and starts a new episode, paused until your next `submit`, with its own 1000+ control steps, either abandoning the running episode or after one has ended. Once an episode has ended,+ `submit` raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is+ graded**: an episode you reset away is never graded, a success ends the run at once (no reset after it), and+ when your time is up the episode then running is the one graded. The wall clock is the run's and never+ restarts; `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is on the track, not where the hurdles and walls are, not the reward: the service judges the episode+ and only tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a+ simulator error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's+ range, each joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the+ IMU's frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**700**: a total of rewards, not a number of steps (an episode is at most 1000 steps). Forward speed, staying+upright (head at standing height, torso vertical) and low actuator force, multiplied together — but the speed+target is a sprint rather than a walk, on the order of 5 m/s. On top of that, touching any barrier's collision+geometry multiplies that entire step down to a small fraction of its value: contact is a gate to be respected,+not a penalty to be traded off. Only the walls that line the track on both sides and behind the start count as+barriers here; the hurdles themselves carry no such penalty, but they are solid and have to be cleared. The+service ends the episode the moment the pass mark is reached (`reason = "success"`), and a fresh replay of the+recorded episode, the servo targets the robot actually executed at each control step, must reach it as well+and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 19 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (19,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 5.0 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 19 actuators are position servos, in this order: `left_hip_yaw`, `left_hip_roll`, `left_hip_pitch`,+`left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`, `right_hip_pitch`, `right_knee`, `right_ankle`,+`torso`, `left_shoulder_pitch`, `left_shoulder_roll`, `left_shoulder_yaw`, `left_elbow`,+`right_shoulder_pitch`, `right_shoulder_roll`, `right_shoulder_yaw`, `right_elbow`+(`spec()["action_types"]["joint_position"]["layout"]`; the proprioception measures the same joints in the same+order). A target of −1 or +1 commands one end of that servo's range,+`spec()["action_types"]["joint_position"]["range_rad"]` in radians, and the scale is linear, so 0 is+mid-range, not 0 rad. A constant action commands joint *targets*, not a pose: whether the robot stays there+depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/task.toml b/tasks/humanoidbench-loco-hurdle-i00-realtime/task.tomlnew file mode 100644index 0000000000..74206360cd--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-loco-hurdle-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-loco-hurdle-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench hurdle on the same frozen instance as humanoidbench-loco-hurdle-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1", "locomotion", "realtime-mode", "trajectory", "hurdle"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1"+family = "hurdle"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 6.0+oracle = "none"+oracle_evidence = "no rollout of this instance; published RL passes the bar (FastTD3 887 / 700); human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/grade.py b/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/test.sh b/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-loco-hurdle-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/README.md b/tasks/humanoidbench-loco-sit-simple-i00-realtime/README.mdnew file mode 100644index 0000000000..b4bd6088d5--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/README.md@@ -0,0 +1,64 @@+# humanoidbench-loco-sit-simple-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-loco-sit-simple-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1` |+| Family / instance | `sit_simple` / seed 0 |+| Oracle | `full` |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-loco-sit-simple-i00-privileged`: the same frozen instance and the same success rule as it and+as `humanoidbench-loco-sit-simple-i00-standard`, but time does not stop. Once an episode's clock starts (its+first `submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks,+so the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the chair is behind the robot and never enters the head cameras' view (floor and horizon only);+`room_camera` (the recording camera frozen at its starting pose) sees both.++## Difficulty+See `humanoidbench-loco-sit-simple-i00-privileged`'s README; here the agent also has no state, no reward and+no simulator of its own, and 50 resets instead of unlimited ones, as in+`humanoidbench-loco-sit-simple-i00-standard`. On top of that the robot does not wait: a model's turn takes+seconds and the H1 falls within a few seconds of robot time without active balance, so the agent cannot steer+it turn by turn. It has to write a controller that closes the loop at the control rate from proprioception,+run it in the background, and improve it between episodes from what it observed.++## Reference solution+`solution/oracle_stream.py` plans the same 999 rows (`solution/sit_simple_actions.npy`) into the running+episode, one per control step (`dt` = 0.02 s, `interpolate="hold"`): the first 500 from step 0 (that call+starts the clock), the rest from step 500 (`at=500`), sent at once, so every row runs at its own control step+whatever the timing; the service ends the episode the moment the return reaches the pass mark. No privileged+information at run time.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). The oracle+(`solution/solve.sh`) scores 1; `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no chair pose, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 19 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..dfb70fcd83--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,36 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: sit_simple+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Sit the robot down on the chair and keep it seated, upright and still."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops below about half its standing height, not far below its height when seated), after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..de74738f66Binary files /dev/null and b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..ae3ced7c67--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Sit",+ "module": "humanoid_bench.envs.basic_locomotion_envs",+ "name": "get_reward",+ "source": "def get_reward(self):\n sitting = rewards.tolerance(\n self._env.data.qpos[2], bounds=(0.68, 0.72), margin=0.2\n )\n chair_location = self._env.named.data.xpos[\"chair\"]\n on_chair = rewards.tolerance(\n self._env.data.qpos[0] - chair_location[0], bounds=(-0.19, 0.19), margin=0.2\n ) * rewards.tolerance(self._env.data.qpos[1] - chair_location[1], margin=0.1)\n sitting_posture = rewards.tolerance(\n self.robot.head_height() - self._env.named.data.site_xpos[\"imu\", \"z\"],\n bounds=(0.35, 0.45),\n margin=0.3,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.95, float(\"inf\")),\n sigmoid=\"linear\",\n margin=0.9,\n value_at_margin=0,\n )\n sit_reward = (0.5 * sitting + 0.5 * on_chair) * upright * sitting_posture\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n horizontal_velocity = self.robot.center_of_mass_velocity()[[0, 1]]\n dont_move = rewards.tolerance(horizontal_velocity, margin=2).mean()\n return small_control * sit_reward * dont_move, {\n \"small_control\": small_control,\n \"sit_reward\": sit_reward,\n \"dont_move\": dont_move,\n \"sitting\": sitting,\n \"upright\": upright,\n \"sitting_posture\": sitting_posture,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.basic_locomotion_envs": "_CRAWL_HEIGHT = 0.8\n_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.basic_locomotion_envs.Sit"+}diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/instruction.md b/tasks/humanoidbench-loco-sit-simple-i00-realtime/instruction.mdnew file mode 100644index 0000000000..fe57979e7d--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/instruction.md@@ -0,0 +1,119 @@+# sit_simple — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid (HumanoidBench / MuJoCo) **as if it were real hardware**: the+simulator runs in a service you cannot open, and you reach it only through a client. Like real hardware, it+does not wait for you: once an episode is running, the robot moves in real time while you think. **Task:** sit+the robot down on the chair and keep it seated, upright and still.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops+ below about half its standing height, not far below its height when seated), after 1000 control steps (20 s+ after its clock started), when your time is up, or on a simulator error. Nothing else ends it: a robot that+ has fallen without meeting one of these (lying on its side, sitting, face down) goes on until step 1000, so+ an episode that is still running does not mean the robot is still up. You have **50 resets**+ (`spec()["budget"]["reset"]`, which is authoritative: if it says 0, there is no reset and the one episode is+ the run). `r.reset()` puts the robot and the scene back on the same frozen starting state and starts a new+ episode, paused until your next `submit`, with its own 1000 control steps, either abandoning the running+ episode or after one has ended. Once an episode has ended, `submit` raises `EpisodeOver` (its message gives+ `resets_left`) until you reset. **Only the last episode is graded**: an episode you reset away is never+ graded, a success ends the run at once (no reset after it), and when your time is up the episode then+ running is the one graded. The wall clock is the run's and never restarts; `meta["episode"]` and+ `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not the chair's pose, not the reward: the service judges the episode and only tells+ you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a simulator error,+ else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's range, each+ joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the IMU's frame,+ each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**750**: a total of rewards, not a number of steps (an episode is at most 1000 steps). A product: the average+of two seat terms — how close the pelvis is to the height it has when seated, and whether the robot is over+the seat rather than beside it — multiplied by staying upright, a seated posture (the head's height above the+torso), not moving, and low actuator force. Every step spent not yet sitting forfeits the reward that step+could have paid, so both *how fast* and *how steadily* the robot sits matter. The service ends the episode the+moment the pass mark is reached (`reason = "success"`), and a fresh replay of the recorded episode, the servo+targets the robot actually executed at each control step, must reach it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 19 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (19,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 4.2 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 19 actuators are position servos, in this order: `left_hip_yaw`, `left_hip_roll`, `left_hip_pitch`,+`left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`, `right_hip_pitch`, `right_knee`, `right_ankle`,+`torso`, `left_shoulder_pitch`, `left_shoulder_roll`, `left_shoulder_yaw`, `left_elbow`,+`right_shoulder_pitch`, `right_shoulder_roll`, `right_shoulder_yaw`, `right_elbow`+(`spec()["action_types"]["joint_position"]["layout"]`; the proprioception measures the same joints in the same+order). A target of −1 or +1 commands one end of that servo's range,+`spec()["action_types"]["joint_position"]["range_rad"]` in radians, and the scale is linear, so 0 is+mid-range, not 0 rad. A constant action commands joint *targets*, not a pose: whether the robot stays there+depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/oracle_stream.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/oracle_stream.pynew file mode 100644index 0000000000..339ee2c597--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/oracle_stream.py@@ -0,0 +1,24 @@+"""Oracle (realtime mode): the reference actions (sit_simple_actions.npy, the privileged oracle's 999 rows of+normalised servo targets) streamed through the client into the running episode's plan, one row per control step+(dt = control_dt, hold): the first chunk starts the clock at step 0, the second is planned from the step where the+first ends, long before that step comes, so every row runs at its own control step. The service ends the episode the+moment the return reaches the pass mark."""+import os+import time++import numpy as np+from rcb_client import Robot++A = np.load(os.path.join(os.path.dirname(os.path.abspath(__file__)), "sit_simple_actions.npy"))+r = Robot()+dt = r.spec()["control_dt"]+rows = int(round(r.spec()["max_chunk_s"] / dt))+for i in range(0, len(A), rows):+ res = r.submit("joint_position", A[i:i + rows], dt=dt, interpolate="hold", at=i, cameras=[], note="oracle")+ print(f"rows {i}..{i + len(A[i:i + rows])} planned from step {res.meta['start_step']}, late {res.meta['late_steps']}",+ flush=True)+ if res.meta["late_steps"]:+ raise SystemExit("a chunk arrived late: the episode would not run the reference rows")+while not r.status()["done"]:+ time.sleep(0.5)+print(r.status())diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/sit_simple_actions.npy b/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/sit_simple_actions.npynew file mode 100644index 0000000000..44e316b31dBinary files /dev/null and b/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/sit_simple_actions.npy differdiff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/solve.sh b/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/solve.shnew file mode 100755index 0000000000..e2239bea1f--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/solution/solve.sh@@ -0,0 +1,8 @@+#!/bin/bash+# Oracle (realtime mode): the privileged oracle's recorded actions (solution/sit_simple_actions.npy) streamed+# through the client into the episode's plan, one row per control step. No privileged information at run time: only+# the recorded actions.+set -e+mkdir -p /app/output+cp /solution/oracle_stream.py /app/output/gen.py+python /solution/oracle_stream.pydiff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/task.toml b/tasks/humanoidbench-loco-sit-simple-i00-realtime/task.tomlnew file mode 100644index 0000000000..3e7cf5ee73--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/task.toml@@ -0,0 +1,67 @@+# Realtime-mode task of humanoidbench-loco-sit-simple-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-loco-sit-simple-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench sit_simple on the same frozen instance as humanoidbench-loco-sit-simple-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1", "locomotion", "realtime-mode", "trajectory", "sit_simple"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1"+family = "sit_simple"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 2.0+oracle = "full"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/grade.py b/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/test.sh b/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-loco-sit-simple-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-loco-walk-i00-realtime/README.md b/tasks/humanoidbench-loco-walk-i00-realtime/README.mdnew file mode 100644index 0000000000..85136cd4d9--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/README.md@@ -0,0 +1,63 @@+# humanoidbench-loco-walk-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-loco-walk-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1` |+| Family / instance | `walk` / seed 0 |+| Oracle | `none` — Claude Opus 5.5's privileged-mode trajectory (834.80 / 700), run `claude_code-claude_opus_5_5-medium` on the results site; today's privileged verifier scores it 1, and so does the standard service when it is sent through the client; published RL passes as well (SAC 854, FastTD3 928); in realtime, the same rows planned into the episode from step 0 (submit's `at`) run at the same control steps and are graded the same; human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-loco-walk-i00-privileged`: the same frozen instance and the same success rule as it and as+`humanoidbench-loco-walk-i00-standard`, but time does not stop. Once an episode's clock starts (its first+`submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks, so+the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see flat floor and the horizon (there is nothing else in the scene). `room_camera`+stands behind and above the start and looks down the course (`RCB_ROOM_CAMERA_POSE` in the compose file): the+robot stays in frame from x = 0 to 20 m.++## Difficulty+See `humanoidbench-loco-walk-i00-privileged`'s README; here the agent also has no state, no reward and no+simulator of its own, and 50 resets instead of unlimited ones, as in `humanoidbench-loco-walk-i00-standard`.+On top of that the robot does not wait: a model's turn takes seconds and the H1 falls within a few seconds of+robot time without active balance, so the agent cannot steer it turn by turn. It has to write a controller+that closes the loop at the control rate from proprioception, run it in the background, and improve it between+episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no base velocity, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 19 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-loco-walk-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-loco-walk-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-loco-walk-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..827101ffc6--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,38 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: walk+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Walk the robot forward and keep walking, without falling."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops near the ground), after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ RCB_ROOM_CAMERA_POSE: "4,0,0.11,10,0,-26" # room_camera behind and above the start, looking down+ # the course: the robot stays in frame from x=0 to 20 m+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..13f4ff8ec8Binary files /dev/null and b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..d05d3f6646--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Walk",+ "module": "humanoid_bench.envs.basic_locomotion_envs",+ "name": "get_reward",+ "source": "def get_reward(self):\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.9, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n if self._move_speed == 0:\n horizontal_velocity = self.robot.center_of_mass_velocity()[[0, 1]]\n dont_move = rewards.tolerance(horizontal_velocity, margin=2).mean()\n return small_control * stand_reward * dont_move, {\n \"small_control\": small_control,\n \"stand_reward\": stand_reward,\n \"dont_move\": dont_move,\n \"standing\": standing,\n \"upright\": upright,\n }\n else:\n com_velocity = self.robot.center_of_mass_velocity()[0]\n move = rewards.tolerance(\n com_velocity,\n bounds=(self._move_speed, float(\"inf\")),\n margin=self._move_speed,\n value_at_margin=0,\n sigmoid=\"linear\",\n )\n move = (5 * move + 1) / 6\n reward = small_control * stand_reward * move\n return reward, {\n \"stand_reward\": stand_reward,\n \"small_control\": small_control,\n \"move\": move,\n \"standing\": standing,\n \"upright\": upright,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.basic_locomotion_envs": "_CRAWL_HEIGHT = 0.8\n_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.basic_locomotion_envs.Walk"+}diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/instruction.md b/tasks/humanoidbench-loco-walk-i00-realtime/instruction.mdnew file mode 100644index 0000000000..bf8ec91bdd--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/instruction.md@@ -0,0 +1,118 @@+# walk — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid (HumanoidBench / MuJoCo) **as if it were real hardware**: the+simulator runs in a service you cannot open, and you reach it only through a client. Like real hardware, it+does not wait for you: once an episode is running, the robot moves in real time while you think. **Task:**+walk the robot forward and keep walking, without falling.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops near+ the ground), after 1000 control steps (20 s after its clock started), when your time is up, or on a+ simulator error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on its+ side, sitting, face down) goes on until step 1000, so an episode that is still running does not mean the+ robot is still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says+ 0, there is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the+ same frozen starting state and starts a new episode, paused until your next `submit`, with its own 1000+ control steps, either abandoning the running episode or after one has ended. Once an episode has ended,+ `submit` raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is+ graded**: an episode you reset away is never graded, a success ends the run at once (no reset after it), and+ when your time is up the episode then running is the one graded. The wall clock is the run's and never+ restarts; `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not how fast it is moving, not the reward: the service judges the episode and only+ tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a simulator+ error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's range,+ each joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the IMU's+ frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**700**: a total of rewards, not a number of steps (an episode is at most 1000 steps). Each step is judged on+three things at once — how fast the centre of mass is moving forward, how upright the robot is (head at+standing height, torso vertical), and how little actuator force it is using — and they multiply. Not being+upright is what makes a step worth nothing; heavy actuator force costs part of it. The forward-speed target is+on the order of 1 m/s. The service ends the episode the moment the pass mark is reached+(`reason = "success"`), and a fresh replay of the recorded episode, the servo targets the robot actually+executed at each control step, must reach it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 19 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (19,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 4.1 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 19 actuators are position servos, in this order: `left_hip_yaw`, `left_hip_roll`, `left_hip_pitch`,+`left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`, `right_hip_pitch`, `right_knee`, `right_ankle`,+`torso`, `left_shoulder_pitch`, `left_shoulder_roll`, `left_shoulder_yaw`, `left_elbow`,+`right_shoulder_pitch`, `right_shoulder_roll`, `right_shoulder_yaw`, `right_elbow`+(`spec()["action_types"]["joint_position"]["layout"]`; the proprioception measures the same joints in the same+order). A target of −1 or +1 commands one end of that servo's range,+`spec()["action_types"]["joint_position"]["range_rad"]` in radians, and the scale is linear, so 0 is+mid-range, not 0 rad. A constant action commands joint *targets*, not a pose: whether the robot stays there+depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/task.toml b/tasks/humanoidbench-loco-walk-i00-realtime/task.tomlnew file mode 100644index 0000000000..ff86cc8564--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-loco-walk-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-loco-walk-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench walk on the same frozen instance as humanoidbench-loco-walk-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1", "locomotion", "realtime-mode", "trajectory", "walk"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1"+family = "walk"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 4.0+oracle = "none"+oracle_evidence = "Claude Opus 5.5's privileged-mode trajectory (834.80 / 700), run `claude_code-claude_opus_5_5-medium` on the results site; today's privileged verifier scores it 1, and so does the standard service when it is sent through the client; published RL passes as well (SAC 854, FastTD3 928); in realtime, the same rows planned into the episode from step 0 (submit's `at`) run at the same control steps and are graded the same; human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-loco-walk-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-loco-walk-i00-realtime/tests/grade.py b/tasks/humanoidbench-loco-walk-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-loco-walk-i00-realtime/tests/test.sh b/tasks/humanoidbench-loco-walk-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-loco-walk-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/README.md b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/README.mdnew file mode 100644index 0000000000..a6e2264d70--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/README.md@@ -0,0 +1,63 @@+# humanoidbench-manip-bookshelf-simple-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-manip-bookshelf-simple-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1hand` |+| Family / instance | `bookshelf_simple` / seed 0 |+| Oracle | `none` — none known: no run has passed it, and published RL reaches 41% of the bar (paper 763, FastTD3 822, bar 2000); human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-manip-bookshelf-simple-i00-privileged`: the same frozen instance and the same success rule as+it and as `humanoidbench-manip-bookshelf-simple-i00-standard`, but time does not stop. Once an episode's clock+starts (its first `submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the+agent thinks, so the agent drives it with a controller program of its own that streams targets through the+client. HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for+rendering; `environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode,+records every control step it ran, replays the last episode in a fresh simulator and renders the replay video;+the verifier grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see the inside of one shelf. `room_camera` looks at the shelves from behind the robot's+right shoulder (`RCB_ROOM_CAMERA_POSE` in the compose file) and sees the bookshelf, the objects' places on it+and the robot.++## Difficulty+See `humanoidbench-manip-bookshelf-simple-i00-privileged`'s README; here the agent also has no state, no+reward and no simulator of its own, and 50 resets instead of unlimited ones, as in+`humanoidbench-manip-bookshelf-simple-i00-standard`. On top of that the robot does not wait: a model's turn+takes seconds and the H1 falls within a few seconds of robot time without active balance, so the agent cannot+steer it turn by turn. It has to write a controller that closes the loop at the control rate from+proprioception, run it in the background, and improve it between episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no object positions, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 61 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..03aa40ec2c--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,38 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: bookshelf_simple+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Put each of the five red-tinted objects (the other seven are grey) onto its place on the bookshelf, shown as a translucent copy of it in the same colour, one at a time, palest first and darkest last."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops too low), when the object being placed goes below about half a metre, once all five are placed, after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ RCB_ROOM_CAMERA_POSE: "0.85,-0.1,0.9,3.2,35,-20" # room_camera behind the robot's right shoulder: every+ # shelf, and the places the objects go, in view+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..ecfcfdbf4cBinary files /dev/null and b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..b7280883fc--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "BookshelfBase",+ "module": "humanoid_bench.envs.bookshelf",+ "name": "get_reward",+ "source": "def get_reward(self):\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.9, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n curr_reach_obj_pos = self._env.named.data.xpos[\n self.bookshelf_objects[self.task_index]\n ]\n curr_placement_goal = self.placement_goals[self.task_index]\n obj_goal_dist = np.linalg.norm(curr_reach_obj_pos - curr_placement_goal)\n\n reward_proximity = rewards.tolerance(\n obj_goal_dist, bounds=(0, 0.15), margin=1, sigmoid=\"linear\"\n )\n left_hand_distance = (\n self._env.named.data.site_xpos[\"left_hand\"] - curr_reach_obj_pos\n )\n righ_hand_distance = (\n self._env.named.data.site_xpos[\"right_hand\"] - curr_reach_obj_pos\n )\n reward_hand_proximity = np.exp(\n -min(\n [np.linalg.norm(left_hand_distance), np.linalg.norm(righ_hand_distance)]\n )\n )\n\n reward = (\n 0.2 * (stand_reward * small_control)\n + 0.4 * reward_proximity\n + 0.4 * reward_hand_proximity\n )\n if obj_goal_dist < 0.15:\n self.task_index += 1\n reward += 100 * self.task_index\n\n return reward, {\n \"stand_reward\": stand_reward,\n \"small_control\": small_control,\n \"reward_proximity\": reward_proximity,\n \"reward_hand_proximity\": reward_hand_proximity,\n \"obj_goal_dist\": obj_goal_dist,\n \"success_subtasks\": self.task_index,\n \"success\": self.task_index == 5,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.bookshelf": "_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.bookshelf.BookshelfSimple"+}diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/instruction.md b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/instruction.mdnew file mode 100644index 0000000000..341169e9e7--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/instruction.md@@ -0,0 +1,124 @@+# bookshelf_simple — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid with two Shadow dexterous hands (HumanoidBench / MuJoCo) **as if+it were real hardware**: the simulator runs in a service you cannot open, and you reach it only through a+client. Like real hardware, it does not wait for you: once an episode is running, the robot moves in real time+while you think. **Task:** put each of the five red-tinted objects (the other seven are grey) onto its place+on the bookshelf, shown as a translucent copy of it in the same colour, one at a time, palest first and+darkest last.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops too+ low), when the object being placed goes below about half a metre, once all five are placed, after 1000+ control steps (20 s after its clock started), when your time is up, or on a simulator error. Nothing else+ ends it: a robot that has fallen without meeting one of these (lying on its side, sitting, face down) goes+ on until step 1000, so an episode that is still running does not mean the robot is still up. You have **50+ resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says 0, there is no reset and the one+ episode is the run). `r.reset()` puts the robot and the scene back on the same frozen starting state and+ starts a new episode, paused until your next `submit`, with its own 1000 control steps, either abandoning+ the running episode or after one has ended. Once an episode has ended, `submit` raises `EpisodeOver` (its+ message gives `resets_left`) until you reset. **Only the last episode is graded**: an episode you reset away+ is never graded, a success ends the run at once (no reset after it), and when your time is up the episode+ then running is the one graded. The wall clock is the run's and never restarts; `meta["episode"]` and+ `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not where the objects or their places are, not the reward: the service judges the+ episode and only tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"`+ after a simulator error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each+ actuator's range, each joint's axis and position, each link's mass and centre of mass, the feet's collision+ boxes, the IMU's frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**2000**: a total of rewards, not a number of steps (an episode is at most 1000 steps). Only the object you+are currently on is scored: each step pays for standing steadily, for a hand being near that object and for+its progress towards its assigned slot. Placing it pays a one-off bonus that grows with how many you have+placed — together the bonuses are most of the bar — and moves the scoring on to the next one. So the return is+cut into five plateaus and stalling on the first caps the episode low. The service ends the episode the moment+the pass mark is reached (`reason = "success"`), and a fresh replay of the recorded episode, the servo targets+the robot actually executed at each control step, must reach it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 61 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (61,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 7.0 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 61 actuators are position servos. The first 21 are the body, in this order: `left_hip_yaw`,+`left_hip_roll`, `left_hip_pitch`, `left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`,+`right_hip_pitch`, `right_knee`, `right_ankle`, `torso`, `left_shoulder_pitch`, `left_shoulder_roll`,+`left_shoulder_yaw`, `left_elbow`, `left_wrist_yaw`, `right_shoulder_pitch`, `right_shoulder_roll`,+`right_shoulder_yaw`, `right_elbow`, `right_wrist_yaw`. The remaining 40 are the two Shadow hands, 20 each —+`lh_A_*` then `rh_A_*`; each hand's first 2 are its wrist (`*_A_WRJ2`, `*_A_WRJ1`) and the other 18 its+fingers, where each of the 4 `*J0` ones (`*_A_FFJ0`, `*_A_MFJ0`, `*_A_RFJ0`, `*_A_LFJ0`) drives a tendon that+moves two finger joints together (`spec()["action_types"]["joint_position"]["layout"]`). The proprioception+measures joints, not actuators: 69 of them, in `spec()["proprio"]["joints"]` order. A target of −1 or +1+commands one end of that servo's range, `spec()["action_types"]["joint_position"]["range_rad"]` in radians,+and the scale is linear, so 0 is mid-range, not 0 rad. A constant action commands joint *targets*, not a pose:+whether the robot stays there depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/task.toml b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/task.tomlnew file mode 100644index 0000000000..87a92484d7--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-manip-bookshelf-simple-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-manip-bookshelf-simple-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench bookshelf_simple on the same frozen instance as humanoidbench-manip-bookshelf-simple-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1hand", "manipulation", "realtime-mode", "trajectory", "bookshelf_simple"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1hand"+family = "bookshelf_simple"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 10.0+oracle = "none"+oracle_evidence = "none known: no run has passed it, and published RL reaches 41% of the bar (paper 763, FastTD3 822, bar 2000); human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/grade.py b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/test.sh b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-manip-bookshelf-simple-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-manip-cube-i00-realtime/README.md b/tasks/humanoidbench-manip-cube-i00-realtime/README.mdnew file mode 100644index 0000000000..04b5b87757--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/README.md@@ -0,0 +1,63 @@+# humanoidbench-manip-cube-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-manip-cube-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1hand` |+| Family / instance | `cube` / seed 0 |+| Oracle | `none` — none known: no run has passed it, and published RL reaches 60% of the bar (paper 135, FastTD3 221, bar 370); human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-manip-cube-i00-privileged`: the same frozen instance and the same success rule as it and as+`humanoidbench-manip-cube-i00-standard`, but time does not stop. Once an episode's clock starts (its first+`submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks, so+the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see the cube above one palm. `room_camera` looks at the hands close up+(`RCB_ROOM_CAMERA_POSE` in the compose file) and sees both hands with their cubes and, floating above them,+the goal orientation (upstream's `target_cube`); the legs are out of frame.++## Difficulty+See `humanoidbench-manip-cube-i00-privileged`'s README; here the agent also has no state, no reward and no+simulator of its own, and 50 resets instead of unlimited ones, as in `humanoidbench-manip-cube-i00-standard`.+On top of that the robot does not wait: a model's turn takes seconds and the H1 falls within a few seconds of+robot time without active balance, so the agent cannot steer it turn by turn. It has to write a controller+that closes the loop at the control rate from proprioception, run it in the background, and improve it between+episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no cube poses, no goal orientation, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 61 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-manip-cube-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-manip-cube-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-manip-cube-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..abc57b26dd--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,40 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: cube+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "A cube starts just above and in front of each palm. Rotate both cubes, without dropping them, to the goal orientation: that of the translucent cube with the same faces floating above and between the hands."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops too low), when either cube drops, after 500 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ RCB_ROOM_CAMERA_POSE: "0.47,0,1.25,0.9,-129,-25" # room_camera close on the hands: both cubes and the+ # floating goal orientation+ RCB_IMAGE_HW: "512" # every camera at 512×512, not upstream's 256: at 256 a+ # cube in room_camera is 15-35 px across+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..a9b62e5a30Binary files /dev/null and b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..f7e963f983--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Cube",+ "module": "humanoid_bench.envs.cube",+ "name": "get_reward",+ "source": "def get_reward(self):\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.9, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n horizontal_velocity = self.robot.center_of_mass_velocity()[[0, 1]]\n dont_move = rewards.tolerance(horizontal_velocity, margin=2).mean()\n\n left_cube_orientation = self._env.data.body(\"left_cube_to_rotate\").xquat\n right_cube_orientation = self._env.data.body(\"right_cube_to_rotate\").xquat\n target_cube_orientation = self._env.data.body(\"target_cube\").xquat\n\n left_orientation_alignment_reward = rewards.tolerance(\n np.linalg.norm(left_cube_orientation - target_cube_orientation), margin=0.3\n )\n right_orientation_alignment_reward = rewards.tolerance(\n np.linalg.norm(right_cube_orientation - target_cube_orientation), margin=0.3\n )\n orientation_alignment_reward = (\n left_orientation_alignment_reward + right_orientation_alignment_reward\n ) / 2\n\n left_hand_cube_distance = np.linalg.norm(\n self._env.named.data.site_xpos[\"left_hand\"]\n - self._env.named.data.xpos[\"left_cube_to_rotate\"]\n )\n right_hand_cube_distance = np.linalg.norm(\n self._env.named.data.site_xpos[\"right_hand\"]\n - self._env.named.data.xpos[\"right_cube_to_rotate\"]\n )\n left_hand_cube_proximity = rewards.tolerance(\n left_hand_cube_distance, bounds=(0, 0.1), margin=0.5\n )\n right_hand_cube_proximity = rewards.tolerance(\n right_hand_cube_distance, bounds=(0, 0.1), margin=0.5\n )\n\n cube_closeness_reward = (\n left_hand_cube_proximity + right_hand_cube_proximity\n ) / 2\n\n reward = (\n 0.2 * (small_control * stand_reward * dont_move)\n + 0.5 * orientation_alignment_reward\n + 0.3 * cube_closeness_reward\n )\n\n return reward, {\n \"small_control\": small_control,\n \"stand_reward\": stand_reward,\n \"dont_move\": dont_move,\n \"standing\": standing,\n \"upright\": upright,\n \"orientation_alignment_reward\": orientation_alignment_reward,\n \"cube_closeness_reward\": cube_closeness_reward,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.cube": "_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.cube.Cube"+}diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/instruction.md b/tasks/humanoidbench-manip-cube-i00-realtime/instruction.mdnew file mode 100644index 0000000000..bef6c4124c--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/instruction.md@@ -0,0 +1,125 @@+# cube — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid with two Shadow dexterous hands (HumanoidBench / MuJoCo) **as if+it were real hardware**: the simulator runs in a service you cannot open, and you reach it only through a+client. Like real hardware, it does not wait for you: once an episode is running, the robot moves in real time+while you think. **Task:** a cube starts just above and in front of each palm. Rotate both cubes, without+dropping them, to the goal orientation: that of the translucent cube with the same faces floating above and+between the hands.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops too+ low), when either cube drops, after 500 control steps (10 s after its clock started), when your time is up,+ or on a simulator error. Nothing else ends it: a robot that has fallen without meeting one of these (lying+ on its side, sitting, face down) goes on until step 500, so an episode that is still running does not mean+ the robot is still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it+ says 0, there is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on+ the same frozen starting state and starts a new episode, paused until your next `submit`, with its own 500+ control steps, either abandoning the running episode or after one has ended. Once an episode has ended,+ `submit` raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is+ graded**: an episode you reset away is never graded, a success ends the run at once (no reset after it), and+ when your time is up the episode then running is the one graded. The wall clock is the run's and never+ restarts; `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 512×512), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not the cubes' orientations, not the goal orientation, not the reward: the service+ judges the episode and only tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`,+ `"error"` after a simulator error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF+ would: each actuator's range, each joint's axis and position, each link's mass and centre of mass, the+ feet's collision boxes, the IMU's frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**370**: a total of rewards, not a number of steps (an episode is at most 500 steps). A weighted sum: the+largest term is how closely each cube's orientation matches the goal, averaged over the two hands; then how+close each hand is to its cube; then a small share for standing steadily without moving. The orientation term+is sharp — it falls away quickly as the orientation drifts, so approximate alignment earns much less than it+looks like it should — and it compares orientations as quaternions, sign included: of the two ways to turn a+cube onto the goal, only one scores, because the other ends at the same orientation with the opposite sign,+which counts as far off. The service ends the episode the moment the pass mark is reached+(`reason = "success"`), and a fresh replay of the recorded episode, the servo targets the robot actually+executed at each control step, must reach it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (500), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 61 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (61,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 6.8 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 61 actuators are position servos. The first 21 are the body, in this order: `left_hip_yaw`,+`left_hip_roll`, `left_hip_pitch`, `left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`,+`right_hip_pitch`, `right_knee`, `right_ankle`, `torso`, `left_shoulder_pitch`, `left_shoulder_roll`,+`left_shoulder_yaw`, `left_elbow`, `left_wrist_yaw`, `right_shoulder_pitch`, `right_shoulder_roll`,+`right_shoulder_yaw`, `right_elbow`, `right_wrist_yaw`. The remaining 40 are the two Shadow hands, 20 each —+`lh_A_*` then `rh_A_*`; each hand's first 2 are its wrist (`*_A_WRJ2`, `*_A_WRJ1`) and the other 18 its+fingers, where each of the 4 `*J0` ones (`*_A_FFJ0`, `*_A_MFJ0`, `*_A_RFJ0`, `*_A_LFJ0`) drives a tendon that+moves two finger joints together (`spec()["action_types"]["joint_position"]["layout"]`). The proprioception+measures joints, not actuators: 69 of them, in `spec()["proprio"]["joints"]` order. A target of −1 or +1+commands one end of that servo's range, `spec()["action_types"]["joint_position"]["range_rad"]` in radians,+and the scale is linear, so 0 is mid-range, not 0 rad. A constant action commands joint *targets*, not a pose:+whether the robot stays there depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/task.toml b/tasks/humanoidbench-manip-cube-i00-realtime/task.tomlnew file mode 100644index 0000000000..5b4a5c4308--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-manip-cube-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-manip-cube-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench cube on the same frozen instance as humanoidbench-manip-cube-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1hand", "manipulation", "realtime-mode", "trajectory", "cube"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1hand"+family = "cube"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 8.0+oracle = "none"+oracle_evidence = "none known: no run has passed it, and published RL reaches 60% of the bar (paper 135, FastTD3 221, bar 370); human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-manip-cube-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-manip-cube-i00-realtime/tests/grade.py b/tasks/humanoidbench-manip-cube-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-manip-cube-i00-realtime/tests/test.sh b/tasks/humanoidbench-manip-cube-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-manip-cube-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-manip-door-i00-realtime/README.md b/tasks/humanoidbench-manip-door-i00-realtime/README.mdnew file mode 100644index 0000000000..c03b55366d--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/README.md@@ -0,0 +1,63 @@+# humanoidbench-manip-door-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-manip-door-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1` |+| Family / instance | `door` / seed 0 |+| Oracle | `none` — Claude Opus 5.5's privileged-mode trajectory (676.02 / 600), run `claude_code-claude_opus_5_5-medium` on the results site; today's privileged verifier scores it 1, and so does the standard service when it is sent through the client; in realtime, the same rows planned into the episode from step 0 (submit's `at`) run at the same control steps and are graded the same; human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-manip-door-i00-privileged`: the same frozen instance and the same success rule as it and as+`humanoidbench-manip-door-i00-standard`, but time does not stop. Once an episode's clock starts (its first+`submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks, so+the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see the door right in front of the robot. `room_camera` looks at the doorway from the+side (`RCB_ROOM_CAMERA_POSE` in the compose file): the robot, the door and the floor beyond it stay in frame+from x = -0.5 to 4 m.++## Difficulty+See `humanoidbench-manip-door-i00-privileged`'s README; here the agent also has no state, no reward and no+simulator of its own, and 50 resets instead of unlimited ones, as in `humanoidbench-manip-door-i00-standard`.+On top of that the robot does not wait: a model's turn takes seconds and the H1 falls within a few seconds of+robot time without active balance, so the agent cannot steer it turn by turn. It has to write a controller+that closes the loop at the control rate from proprioception, run it in the background, and improve it between+episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no door or handle state, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 19 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-manip-door-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-manip-door-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-manip-door-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..180ef8bf61--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,39 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: door+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Walk to the door, press the handle, pull the door open towards the robot, and walk through it."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops too low), after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ RCB_ROOM_CAMERA_POSE: "1.8,0,0.9,7,105,-25" # room_camera beside the doorway: the robot, the door+ # and the floor beyond it stay in frame from x=-0.5 to 4+ # m+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..14dbf8d549Binary files /dev/null and b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..8cb49bfeef--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Door",+ "module": "humanoid_bench.envs.door",+ "name": "get_reward",+ "source": "def get_reward(self):\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.9, float(\"inf\")),\n sigmoid=\"linear\",\n margin=0.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n door_openness_reward = min(\n 1, (self._env.data.qpos[-2] / 1) * abs(self._env.data.qpos[-2] / 1)\n )\n door_hatch_openness_reward = rewards.tolerance(\n self._env.data.qpos[-1], bounds=(0.75, 2), margin=0.75, sigmoid=\"linear\"\n )\n\n left_hand_hatch_closeness = np.linalg.norm(\n self._env.data.body(\"door_hatch\").xpos\n - self._env.named.data.site_xpos[\"left_hand\"]\n )\n right_hand_hatch_closeness = np.linalg.norm(\n self._env.data.body(\"door_hatch\").xpos\n - self._env.named.data.site_xpos[\"right_hand\"]\n )\n hand_hatch_proximity_reward = rewards.tolerance(\n min(right_hand_hatch_closeness, left_hand_hatch_closeness),\n bounds=(0, 0.25),\n margin=1,\n sigmoid=\"linear\",\n )\n\n passage_reward = rewards.tolerance(\n self._env.named.data.site_xpos[\"imu\", \"x\"],\n bounds=(1.2, float(\"inf\")),\n margin=1,\n value_at_margin=0,\n sigmoid=\"linear\",\n )\n\n reward = (\n 0.1 * stand_reward * small_control\n + 0.45 * door_openness_reward\n + 0.05 * door_hatch_openness_reward\n + 0.05 * hand_hatch_proximity_reward\n + 0.35 * passage_reward\n )\n\n return reward, {\n \"stand_reward\": stand_reward,\n \"small_control\": small_control,\n \"door_openness_reward\": door_openness_reward,\n \"door_hatch_openness_reward\": door_hatch_openness_reward,\n \"hand_hatch_proximity_reward\": hand_hatch_proximity_reward,\n \"passage_reward\": passage_reward,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.door": "_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.door.Door"+}diff --git a/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-manip-door-i00-realtime/instruction.md b/tasks/humanoidbench-manip-door-i00-realtime/instruction.mdnew file mode 100644index 0000000000..008244e0da--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/instruction.md@@ -0,0 +1,117 @@+# door — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid (HumanoidBench / MuJoCo) **as if it were real hardware**: the+simulator runs in a service you cannot open, and you reach it only through a client. Like real hardware, it+does not wait for you: once an episode is running, the robot moves in real time while you think. **Task:**+walk to the door, press the handle, pull the door open towards the robot, and walk through it.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops too+ low), after 1000 control steps (20 s after its clock started), when your time is up, or on a simulator+ error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on its side,+ sitting, face down) goes on until step 1000, so an episode that is still running does not mean the robot is+ still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says 0, there+ is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the same+ frozen starting state and starts a new episode, paused until your next `submit`, with its own 1000 control+ steps, either abandoning the running episode or after one has ended. Once an episode has ended, `submit`+ raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is graded**:+ an episode you reset away is never graded, a success ends the run at once (no reset after it), and when your+ time is up the episode then running is the one graded. The wall clock is the run's and never restarts;+ `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not the door's or the handle's angle, not the reward: the service judges the episode+ and only tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a+ simulator error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's+ range, each joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the+ IMU's frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**600**: a total of rewards, not a number of steps (an episode is at most 1000 steps). This one is a weighted+sum rather than a product, so partial credit is real: getting the door open and getting the torso past the+doorway are the two large terms, with reaching for the handle, moving the handle, and posture as small ones.+The bar cannot be reached without opening the door. The service ends the episode the moment the pass mark is+reached (`reason = "success"`), and a fresh replay of the recorded episode, the servo targets the robot+actually executed at each control step, must reach it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 19 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (19,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 4.2 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 19 actuators are position servos, in this order: `left_hip_yaw`, `left_hip_roll`, `left_hip_pitch`,+`left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`, `right_hip_pitch`, `right_knee`, `right_ankle`,+`torso`, `left_shoulder_pitch`, `left_shoulder_roll`, `left_shoulder_yaw`, `left_elbow`,+`right_shoulder_pitch`, `right_shoulder_roll`, `right_shoulder_yaw`, `right_elbow`+(`spec()["action_types"]["joint_position"]["layout"]`; the proprioception measures the same joints in the same+order). A target of −1 or +1 commands one end of that servo's range,+`spec()["action_types"]["joint_position"]["range_rad"]` in radians, and the scale is linear, so 0 is+mid-range, not 0 rad. A constant action commands joint *targets*, not a pose: whether the robot stays there+depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-manip-door-i00-realtime/task.toml b/tasks/humanoidbench-manip-door-i00-realtime/task.tomlnew file mode 100644index 0000000000..1ecd9196be--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-manip-door-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-manip-door-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench door on the same frozen instance as humanoidbench-manip-door-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1", "manipulation", "realtime-mode", "trajectory", "door"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1"+family = "door"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 6.0+oracle = "none"+oracle_evidence = "Claude Opus 5.5's privileged-mode trajectory (676.02 / 600), run `claude_code-claude_opus_5_5-medium` on the results site; today's privileged verifier scores it 1, and so does the standard service when it is sent through the client; in realtime, the same rows planned into the episode from step 0 (submit's `at`) run at the same control steps and are graded the same; human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-manip-door-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-manip-door-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-manip-door-i00-realtime/tests/grade.py b/tasks/humanoidbench-manip-door-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-manip-door-i00-realtime/tests/test.sh b/tasks/humanoidbench-manip-door-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-manip-door-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/README.md b/tasks/humanoidbench-manip-powerlift-i00-realtime/README.mdnew file mode 100644index 0000000000..a059a966c0--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/README.md@@ -0,0 +1,62 @@+# humanoidbench-manip-powerlift-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-manip-powerlift-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1hand` |+| Family / instance | `powerlift` / seed 0 |+| Oracle | `none` — none known: no run has passed it, and published RL reaches 41% of the bar (paper 286, FastTD3 325, bar 800); human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-manip-powerlift-i00-privileged`: the same frozen instance and the same success rule as it and+as `humanoidbench-manip-powerlift-i00-standard`, but time does not stop. Once an episode's clock starts (its+first `submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks,+so the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see floor and horizon only; the dumbbell at the feet is out of their view.+`room_camera` (the recording camera frozen at its starting pose) sees the robot and the dumbbell.++## Difficulty+See `humanoidbench-manip-powerlift-i00-privileged`'s README; here the agent also has no state, no reward and+no simulator of its own, and 50 resets instead of unlimited ones, as in+`humanoidbench-manip-powerlift-i00-standard`. On top of that the robot does not wait: a model's turn takes+seconds and the H1 falls within a few seconds of robot time without active balance, so the agent cannot steer+it turn by turn. It has to write a controller that closes the loop at the control rate from proprioception,+run it in the background, and improve it between episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no dumbbell pose, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 61 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..1b53e88ca0--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,36 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: powerlift+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Lift the dumbbell from the floor to overhead — close to two metres up — and hold it there."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops near the ground), after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..dc24e5ad35Binary files /dev/null and b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..7994dbc77a--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Powerlift",+ "module": "humanoid_bench.envs.powerlift",+ "name": "get_reward",+ "source": "def get_reward(self):\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 3,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.8, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n dumbbell_height = self._env.named.data.xpos[\"dumbbell\", \"z\"]\n reward_dumbbell_lifted = rewards.tolerance(\n dumbbell_height, bounds=(1.9, 2.1), margin=2\n )\n\n reward = 0.2 * (small_control * stand_reward) + 0.8 * reward_dumbbell_lifted\n return reward, {\n \"stand_reward\": stand_reward,\n \"small_control\": small_control,\n \"reward_dumbbell_lifted\": reward_dumbbell_lifted,\n \"standing\": standing,\n \"upright\": upright,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.powerlift": "_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.powerlift.Powerlift"+}diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/instruction.md b/tasks/humanoidbench-manip-powerlift-i00-realtime/instruction.mdnew file mode 100644index 0000000000..390c7b8fc7--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/instruction.md@@ -0,0 +1,122 @@+# powerlift — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid with two Shadow dexterous hands (HumanoidBench / MuJoCo) **as if+it were real hardware**: the simulator runs in a service you cannot open, and you reach it only through a+client. Like real hardware, it does not wait for you: once an episode is running, the robot moves in real time+while you think. **Task:** lift the dumbbell from the floor to overhead — close to two metres up — and hold it+there.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops near+ the ground), after 1000 control steps (20 s after its clock started), when your time is up, or on a+ simulator error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on its+ side, sitting, face down) goes on until step 1000, so an episode that is still running does not mean the+ robot is still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says+ 0, there is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the+ same frozen starting state and starts a new episode, paused until your next `submit`, with its own 1000+ control steps, either abandoning the running episode or after one has ended. Once an episode has ended,+ `submit` raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is+ graded**: an episode you reset away is never graded, a success ends the run at once (no reset after it), and+ when your time is up the episode then running is the one graded. The wall clock is the run's and never+ restarts; `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not the dumbbell's pose, not the reward: the service judges the episode and only tells+ you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a simulator error,+ else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's range, each+ joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the IMU's frame,+ each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**800**: a total of rewards, not a number of steps (an episode is at most 1000 steps). A weighted sum of two+things: a small share for standing steadily, and the bulk for how high the dumbbell is, measured against a+target band around two metres. Be warned that the height term is broad. Clearing the bar means lifting it high+and keeping it there for most of the episode, not improving the height a little. The service ends the episode+the moment the pass mark is reached (`reason = "success"`), and a fresh replay of the recorded episode, the+servo targets the robot actually executed at each control step, must reach it as well and end in the same+state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 61 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (61,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 5.5 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 61 actuators are position servos. The first 21 are the body, in this order: `left_hip_yaw`,+`left_hip_roll`, `left_hip_pitch`, `left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`,+`right_hip_pitch`, `right_knee`, `right_ankle`, `torso`, `left_shoulder_pitch`, `left_shoulder_roll`,+`left_shoulder_yaw`, `left_elbow`, `left_wrist_yaw`, `right_shoulder_pitch`, `right_shoulder_roll`,+`right_shoulder_yaw`, `right_elbow`, `right_wrist_yaw`. The remaining 40 are the two Shadow hands, 20 each —+`lh_A_*` then `rh_A_*`; each hand's first 2 are its wrist (`*_A_WRJ2`, `*_A_WRJ1`) and the other 18 its+fingers, where each of the 4 `*J0` ones (`*_A_FFJ0`, `*_A_MFJ0`, `*_A_RFJ0`, `*_A_LFJ0`) drives a tendon that+moves two finger joints together (`spec()["action_types"]["joint_position"]["layout"]`). The proprioception+measures joints, not actuators: 69 of them, in `spec()["proprio"]["joints"]` order. A target of −1 or +1+commands one end of that servo's range, `spec()["action_types"]["joint_position"]["range_rad"]` in radians,+and the scale is linear, so 0 is mid-range, not 0 rad. A constant action commands joint *targets*, not a pose:+whether the robot stays there depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/task.toml b/tasks/humanoidbench-manip-powerlift-i00-realtime/task.tomlnew file mode 100644index 0000000000..034db0b0e2--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-manip-powerlift-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-manip-powerlift-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench powerlift on the same frozen instance as humanoidbench-manip-powerlift-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1hand", "manipulation", "realtime-mode", "trajectory", "powerlift"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1hand"+family = "powerlift"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 8.0+oracle = "none"+oracle_evidence = "none known: no run has passed it, and published RL reaches 41% of the bar (paper 286, FastTD3 325, bar 800); human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/grade.py b/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/test.sh b/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-manip-powerlift-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-manip-room-i00-realtime/README.md b/tasks/humanoidbench-manip-room-i00-realtime/README.mdnew file mode 100644index 0000000000..3877e6cb7f--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/README.md@@ -0,0 +1,62 @@+# humanoidbench-manip-room-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-manip-room-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1hand` |+| Family / instance | `room` / seed 0 |+| Oracle | `none` — none known: no run has passed it, and published RL reaches 44% of the bar (paper 128, FastTD3 177, bar 400); human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-manip-room-i00-privileged`: the same frozen instance and the same success rule as it and as+`humanoidbench-manip-room-i00-standard`, but time does not stop. Once an episode's clock starts (its first+`submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks, so+the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see a bookshelf and a table. `room_camera` looks down on the whole room+(`RCB_ROOM_CAMERA_POSE` in the compose file): all six objects and the robot are in frame.++## Difficulty+See `humanoidbench-manip-room-i00-privileged`'s README; here the agent also has no state, no reward and no+simulator of its own, and 50 resets instead of unlimited ones, as in `humanoidbench-manip-room-i00-standard`.+On top of that the robot does not wait: a model's turn takes seconds and the H1 falls within a few seconds of+robot time without active balance, so the agent cannot steer it turn by turn. It has to write a controller+that closes the loop at the control rate from proprioception, run it in the background, and improve it between+episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no object positions, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 61 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-manip-room-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-manip-room-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-manip-room-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..d36fe82325--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,38 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: room+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Six loose objects — a chair, a trophy, a pair of headphones, two packages and a snow globe — are scattered around the room (the table and the bookshelf are fixed). Tidy up: gather them together, so they end up close to one another rather than spread out."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops near the ground), after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ RCB_ROOM_CAMERA_POSE: "0.3,0,0,11,0,-70" # room_camera above the room: all six objects and the+ # robot are in frame+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..521346a0dcBinary files /dev/null and b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..3511408dcb--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Room",+ "module": "humanoid_bench.envs.room",+ "name": "get_reward",+ "source": "def get_reward(self):\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.9, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n room_object_positions = np.vstack(\n [\n self._env.named.data.xpos[obj_name]\n for obj_name in [\n \"chair\",\n \"trophy\",\n \"headphone\",\n \"package_a\",\n \"package_b\",\n \"snow_globe\",\n ]\n ]\n )\n room_object_entropies = np.array(\n [np.var(room_object_positions[:, col_id]) for col_id in range(2)]\n )\n\n room_object_organized = rewards.tolerance(\n np.max(room_object_entropies),\n margin=3,\n )\n\n reward = 0.2 * (small_control * stand_reward) + 0.8 * room_object_organized\n return reward, {\n \"stand_reward\": stand_reward,\n \"small_control\": small_control,\n \"standing\": standing,\n \"upright\": upright,\n \"room_object_organized\": room_object_organized,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.room": "_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.room.Room"+}diff --git a/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-manip-room-i00-realtime/instruction.md b/tasks/humanoidbench-manip-room-i00-realtime/instruction.mdnew file mode 100644index 0000000000..76b0fd901b--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/instruction.md@@ -0,0 +1,123 @@+# room — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid with two Shadow dexterous hands (HumanoidBench / MuJoCo) **as if+it were real hardware**: the simulator runs in a service you cannot open, and you reach it only through a+client. Like real hardware, it does not wait for you: once an episode is running, the robot moves in real time+while you think. **Task:** six loose objects — a chair, a trophy, a pair of headphones, two packages and a+snow globe — are scattered around the room (the table and the bookshelf are fixed). Tidy up: gather them+together, so they end up close to one another rather than spread out.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops near+ the ground), after 1000 control steps (20 s after its clock started), when your time is up, or on a+ simulator error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on its+ side, sitting, face down) goes on until step 1000, so an episode that is still running does not mean the+ robot is still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says+ 0, there is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the+ same frozen starting state and starts a new episode, paused until your next `submit`, with its own 1000+ control steps, either abandoning the running episode or after one has ended. Once an episode has ended,+ `submit` raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is+ graded**: an episode you reset away is never graded, a success ends the run at once (no reset after it), and+ when your time is up the episode then running is the one graded. The wall clock is the run's and never+ restarts; `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not where the objects are, not the reward: the service judges the episode and only+ tells you that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a simulator+ error, else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's range,+ each joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the IMU's+ frame, each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**400**: a total of rewards, not a number of steps (an episode is at most 1000 steps). Unusually, this task+gives you no target positions at all. The dominant term measures how *spread out* the six objects are — the+variance of their horizontal coordinates — and rewards making that small; a smaller term pays for standing+steadily. Any arrangement that brings the objects close together scores, wherever in the room you do it. The+service ends the episode the moment the pass mark is reached (`reason = "success"`), and a fresh replay of the+recorded episode, the servo targets the robot actually executed at each control step, must reach it as well+and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 61 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (61,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 4.5 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 61 actuators are position servos. The first 21 are the body, in this order: `left_hip_yaw`,+`left_hip_roll`, `left_hip_pitch`, `left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`,+`right_hip_pitch`, `right_knee`, `right_ankle`, `torso`, `left_shoulder_pitch`, `left_shoulder_roll`,+`left_shoulder_yaw`, `left_elbow`, `left_wrist_yaw`, `right_shoulder_pitch`, `right_shoulder_roll`,+`right_shoulder_yaw`, `right_elbow`, `right_wrist_yaw`. The remaining 40 are the two Shadow hands, 20 each —+`lh_A_*` then `rh_A_*`; each hand's first 2 are its wrist (`*_A_WRJ2`, `*_A_WRJ1`) and the other 18 its+fingers, where each of the 4 `*J0` ones (`*_A_FFJ0`, `*_A_MFJ0`, `*_A_RFJ0`, `*_A_LFJ0`) drives a tendon that+moves two finger joints together (`spec()["action_types"]["joint_position"]["layout"]`). The proprioception+measures joints, not actuators: 69 of them, in `spec()["proprio"]["joints"]` order. A target of −1 or +1+commands one end of that servo's range, `spec()["action_types"]["joint_position"]["range_rad"]` in radians,+and the scale is linear, so 0 is mid-range, not 0 rad. A constant action commands joint *targets*, not a pose:+whether the robot stays there depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-manip-room-i00-realtime/task.toml b/tasks/humanoidbench-manip-room-i00-realtime/task.tomlnew file mode 100644index 0000000000..ef82fb7a25--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-manip-room-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-manip-room-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench room on the same frozen instance as humanoidbench-manip-room-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1hand", "manipulation", "realtime-mode", "trajectory", "room"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1hand"+family = "room"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 10.0+oracle = "none"+oracle_evidence = "none known: no run has passed it, and published RL reaches 44% of the bar (paper 128, FastTD3 177, bar 400); human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-manip-room-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-manip-room-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-manip-room-i00-realtime/tests/grade.py b/tasks/humanoidbench-manip-room-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-manip-room-i00-realtime/tests/test.sh b/tasks/humanoidbench-manip-room-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-manip-room-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.logdiff --git a/tasks/humanoidbench-manip-window-i00-realtime/README.md b/tasks/humanoidbench-manip-window-i00-realtime/README.mdnew file mode 100644index 0000000000..bbce226dcc--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/README.md@@ -0,0 +1,62 @@+# humanoidbench-manip-window-i00-realtime++| | |+|---|---|+| Benchmark | HumanoidBench |+| Mode | realtime (`eai-standard/2.3`) |+| Twin | `humanoidbench-manip-window-i00-privileged` |+| Simulator / robot | `humanoidbench` / `unitree-h1hand` |+| Family / instance | `window` / seed 0 |+| Oracle | `none` — none known: no run has passed it, and published RL reaches 94% of the bar (paper 227, FastTD3 613, bar 650); human review in PR #83 |+| Agent time budget | 3600 s; tokens: protocol default (PROTOCOL §5) |+| Base image | `ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6` |++## Task+Realtime mode (robot as a service on a clock that does not stop, `eai-standard/2.3`, PROTOCOL §4b) of+`humanoidbench-manip-window-i00-privileged`: the same frozen instance and the same success rule as it and as+`humanoidbench-manip-window-i00-standard`, but time does not stop. Once an episode's clock starts (its first+`submit`), the robot runs at 50 Hz in real time and holds its last servo targets while the agent thinks, so+the agent drives it with a controller program of its own that streams targets through the client.+HumanoidBench runs in the `sim` sidecar (`environment/docker-compose.yaml`, GPU on the sidecar for rendering;+`environment/sim/rcb_realtime.py` runs the clock), which holds the reward, judges the episode, records every+control step it ran, replays the last episode in a fresh simulator and renders the replay video; the verifier+grades the sidecar's record in a container of its own (`tests/Dockerfile`, no network).++Note: the head cameras see the window. `room_camera` (the recording camera frozen at its starting pose) sees+the window, the wiping tool and the robot.++## Difficulty+See `humanoidbench-manip-window-i00-privileged`'s README; here the agent also has no state, no reward and no+simulator of its own, and 50 resets instead of unlimited ones, as in+`humanoidbench-manip-window-i00-standard`. On top of that the robot does not wait: a model's turn takes+seconds and the H1 falls within a few seconds of robot time without active balance, so the agent cannot steer+it turn by turn. It has to write a controller that closes the loop at the control rate from proprioception,+run it in the background, and improve it between episodes from what it observed.++## Reference solution+None. Requiring one would cap the benchmark at what we can already solve, which systematically removes the+interesting tasks. What replaces it (docs/PROTOCOL.md §2.7): the floor above (doing nothing cannot pass), the+evidence that the task has a solution (`oracle_evidence` in task.toml), and a human reviewer.++## Verification+`success` = live success (the episode's return reached the pass mark) ∧ replay success ∧ deterministic (the+fresh replay ends in the live run's state), computed in the sidecar for the last episode. The replay+re-executes the servo targets the simulator applied at each control step (`trajectory_live.f64`), not the ones+the agent submitted, so it equals the live run whatever the timing was; `ticks.f64` records each step's wall+time and the call that planned it, `calls.jsonl` and `submitted.f64` every call and every row as sent, and+`final.json` the clock's lag (`max_lag_s`, `stalls`) and the held and late steps. `tests/grade.py` reads+`/data/final.json` and copies the evidence to `/logs/verifier/` (`replay.mp4`). A simulator crash or error is+an infrastructure failure (`infra_error = 1`), not the agent's (PROTOCOL §4b). No oracle; human review in PR+#83. `nop` scores 0.++Generated by `scripts/humanoidbench/gen_tasks.py` from `subset.toml`, `templates/realtime/`,+`templates/standard/`, `shared/standard/` and `shared/realtime/`.++## Details+| | |+|---|---|+| mode / sensors | realtime: head-camera RGB + joint angles and velocities (HumanoidBench's own `image` and `proprio` sensors), a pelvis IMU and a camera fixed in the room; no root pose, no tool pose, no reward; 50 resets (a new episode from the frozen start each, paused until its first `submit`; only the last episode is graded) |+| time | the episode's clock starts at its first `submit` (or `get_obs(seconds > 0)`) and runs one control step every 0.02 s of wall time until the episode ends; `submit` writes a plan of future targets (from step `at`) and answers at once; the last target is held when the plan runs out |+| action space | HumanoidBench's own: 61 normalised position targets, 50 Hz, streamed one row at a time or in chunks of up to 10 s |+| knobs (compose env) | `RCB_SLOWDOWN` (1: real time, the official configuration; S: one control step every S x 0.02 s, results reported apart from S = 1), `RCB_RESET_BUDGET` (50: the realtime mode's default, PROTOCOL §4b), `RCB_OBS_PROPRIO` (1), `RCB_OBS_IMU` (1), `RCB_OBS_ROOM_CAMERA` (1), `RCB_OBS_DEPTH` (0): the defaults are this task's configuration; a run that changes one is reported separately, and `episode.jsonl` records the `/spec` it ran under |+| agent API | `environment/rcb_client.py` (the realtime client; its docstring is the reference); `instruction.md` lists the endpoints |diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/Dockerfile b/tasks/humanoidbench-manip-window-i00-realtime/environment/Dockerfilenew file mode 100644index 0000000000..053591514d--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/Dockerfile@@ -0,0 +1,8 @@+# Standard mode: the agent container has no simulator and no HumanoidBench code -- only Python and the client.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY client-requirements.txt /tmp/+RUN pip install --no-cache-dir -r /tmp/client-requirements.txt+COPY rcb_client.py rcb1.py /opt/tools/+ENV PYTHONPATH=/opt/tools+RUN mkdir -p /app/output+WORKDIR /appdiff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/client-requirements.txt b/tasks/humanoidbench-manip-window-i00-realtime/environment/client-requirements.txtnew file mode 100644index 0000000000..8aa73a968f--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/client-requirements.txt@@ -0,0 +1,11 @@+# Exact pins for what the standard agent container installs (and a verifier, if it installs anything): the closure of+# `pip install requests numpy pillow scipy`, the unpinned line of every standard task at 9eb18edf, resolved on+# 2026-10-01 in CLIENT_IMAGE (client.env: Python 3.12.14, pip 25.0.1). A change here is a shared-files PR.+certifi==2026.7.22+charset-normalizer==3.5.2+idna==3.20+numpy==2.5.3+pillow==12.3.0+requests==2.34.2+scipy==1.18.1+urllib3==2.8.0diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/docker-compose.yaml b/tasks/humanoidbench-manip-window-i00-realtime/environment/docker-compose.yamlnew file mode 100644index 0000000000..53ecca5f07--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/docker-compose.yaml@@ -0,0 +1,36 @@+# Realtime mode (PROTOCOL §4b): the simulator runs in the `sim` sidecar (the agent cannot open it), on its own clock once an+# episode starts (sim/rcb_realtime.py); the agent only reaches port 8000.+# No `expose`/`ports` on sim: under Harbor's egress control it shares main's network namespace (localhost:8000).+# GPU on sim: MuJoCo physics is CPU either way, but every image the agent asks for is rendered here, and EGL on the+# GPU takes ~0.5 ms a frame against ~130 ms for CPU osmesa. `graphics` is the capability that mounts the EGL ICD.+services:+ main:+ depends_on: [sim]+ sim:+ image: ghcr.io/mll-lab-nu/eai-humanoidbench:0.1.6+ volumes:+ - ./sim:/srv:ro+ command: python /srv/rcb_realtime.py+ restart: on-failure:6 # a crash resumes the same episode, paused, never a reset+ environment:+ RCB_TASK: window+ RCB_INSTANCE: /srv/instance.npz+ RCB_TASK_TEXT: "Grasp the wiping tool — it starts in the air just in front of the hands and falls within the first second unless it is held — and wipe the window with it, moving steadily up and down."+ RCB_EPISODE: "Ends when the summed reward reaches the pass mark (success), when the robot falls (its pelvis drops too low), when the tool drops, after 1000 control steps, when your time is up (timeout), or on a simulator error."+ RCB_WALL_CLOCK_S: "3600.0"+ # Knobs: ${NAME:-default} takes NAME from the shell that runs harbor. The defaults are the standard+ # configuration; results under other settings are reported separately.+ RCB_SLOWDOWN: "${RCB_SLOWDOWN:-1}" # 1: real time (official); S: one step every S x control_dt s, reported apart+ RCB_RESET_BUDGET: "${RCB_RESET_BUDGET:-50}" # realtime mode's default: 50 resets (PROTOCOL §4b); 0: one episode+ RCB_OBS_PROPRIO: "${RCB_OBS_PROPRIO:-1}" # 1: + joint angles/velocities; 0: images only+ RCB_OBS_IMU: "${RCB_OBS_IMU:-1}" # 1: + pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA: "${RCB_OBS_ROOM_CAMERA:-1}" # 1: + room_camera, fixed in the room+ RCB_OBS_DEPTH: "${RCB_OBS_DEPTH:-0}" # 1: + depth (m) from both head cameras, as from the H1's head RGB-D camera+ NVIDIA_DRIVER_CAPABILITIES: compute,graphics,utility+ deploy:+ resources:+ reservations:+ devices:+ - driver: nvidia+ count: 1+ capabilities: [gpu]diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/rcb1.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/rcb_client.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/rcb_client.pynew file mode 100644index 0000000000..8e057bb6e9--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/rcb_client.py@@ -0,0 +1,209 @@+"""eai-standard/2.3 client (realtime mode): your whole interface to the robot (installed at /opt/tools/rcb_client.py, on+PYTHONPATH).++ from rcb_client import Robot, EpisodeOver+ r = Robot() # finds the service (SIM_URL, http://sim:8000, http://localhost:8000), waits until it is ready++TIME DOES NOT STOP (spec()["timing"]). An episode starts paused; its clock starts at its first submit() or+get_obs(seconds > 0) (again after every reset()) and from then on runs one control step every spec()["control_dt"] x+spec()["timing"]["slowdown"] seconds of wall time, whether or not you call, until the episode ends. The service keeps a+plan, one servo-target row per coming control step: submit() writes into it and answers at once; when the plan runs out+the last target is held. Whatever you do in between -- think, print, run a model -- the robot keeps executing.++Calls (each is one HTTP request; every answer that carries data is an RCB1 frame, decoded into a Result):+ r.spec() GET /spec, fetched when connecting: the robot's facts. "action_types"+ ("joint_position": dim, layout (actuator order), lower/upper, range_rad,+ units, semantics), "control_dt" (0.02 s), "max_control_steps" (the+ episode), "timing" (realtime, slowdown, max_ahead_steps), "cameras" {name:+ rgb, depth, hw, fovy_deg, pose, mounted}, "proprio_layout" and "proprio"+ (joints, axes, positions), "imu", "links" (masses, centres of mass, the+ feet's boxes), "depth", "budget" {"reset", "wall_clock_s"}, "episode"+ r.status() GET /status: calls, rows, control_steps, done, reason, elapsed_s, running,+ planned_steps, lag_s (and episode, resets_left with a reset budget)+ r.get_obs(cameras=None, seconds=0.0, video_fps=10, include_depth=False, video="frames")+ POST /observe. seconds=0: the robot now, at once. seconds>0 (at most+ spec()["max_watch_s"]): waits while that much robot time runs (starting the+ clock if it is not running) and returns what the cameras saw; it never+ changes the plan+ r.submit("joint_position", actions, dt=None, interpolate="linear", at=None, cameras=None, video_fps=10,+ video="none", include_depth=False, note="")+ POST /submit: answers at once with the robot as it is now (the rows+ have not run yet). actions (T, dim): rows dt apart (a multiple of+ control_dt, the default; T x dt at most spec()["max_chunk_s"]); each step's+ target is the row (interpolate="hold") or a straight line from the previous+ row's ("linear", starting from the target in force just before `at`). They+ become the plan from control step `at` (an episode step number; None = the+ next step), replacing whatever was planned from `at` on and keeping what is+ planned before it; at most spec()["timing"]["max_ahead_steps"] ahead. Rows+ for steps already gone when the request arrives are dropped+ (meta["late_steps"]). The first submit of an episode starts its clock+ r.reset(cameras=None, include_depth=False)+ POST /reset, only when spec()["budget"]["reset"] > 0: abandons the+ running episode and starts a new one, paused, from the same starting state;+ only the last episode is graded, and a success ends the trial++cameras=None means every camera in spec()["cameras"]; cameras=[] means no images at all, only proprioception (the+fastest call: what a controller loop wants). video: "mp4" (one H.264 file per camera), "frames" (the images as arrays),+"both" or "none" (only the last frame); a submit returns no video (no time passes inside it). include_depth adds depth+for the cameras that have it.++Result (what get_obs / submit / reset return):+ res.meta dict: calls, rows, control_steps (steps run in this episode: the next one is this number),+ sim_time_s, time_s, time_left_s, proprio {"layout", "now"}, done, reason ("success" |+ "failed" | "timeout" | "error"; None while the episode runs), running, planned_steps,+ lag_s; per submit: start_step, late_steps, plan_end_step, clipped [{row, index, reason}]+ (values outside [-1, 1] are clipped, not refused); with a reset budget also episode,+ resets_left+ res.last[cam] uint8 H x W x 3: the last frame (unless cameras=[])+ res.frames[cam] uint8 N x H x W x 3 (get_obs, video="frames"/"both") res.video[cam] mp4 bytes+ res.depth[cam] float16 N x H x W, metres, 0 = nothing in range (include_depth)+ res.proprio float32 rows of spec()["proprio_layout"]: one at the call's start, one every 1/video_fps s+ of a watch, one at its end+ res.done the episode is over+ res.save(dir) meta.json, <cam>.mp4, last/<cam>.jpg, frames/<cam>/NNN.jpg, depth_<cam>.npy, proprio.npy++Errors: ValueError -- refused, nothing changed (400: wrong type or shape, NaN, a bad dt or at, too many frames, an+unknown camera; or a request over 64 MB, refused before it is sent); EpisodeOver -- the episode, or the trial, is over+(409); RuntimeError -- the service failed (5xx: see spec()["on_service_error"]) or the endpoint does not exist in this+run (404).+"""+from __future__ import annotations++import json+import os+import pathlib+import time++import numpy as np+import requests++import rcb1++PROTOCOLS = ("eai-standard/2.", "rcb-limited/2.") # any 2.x service, under either name (rcb-limited is the old one)+MAX_BODY = 64 * 1024 * 1024 # the protocol's request limit: a larger request is refused+++class EpisodeOver(RuntimeError):+ pass+++class Result:+ """A decoded RCB1 answer: meta, video, frames, last, depth, proprio; save(dir) writes them all out."""++ def __init__(self, meta: dict, parts: dict):+ self.meta, self.raw = meta, parts+ self.video, self.frames, self.last, self.depth, self.proprio = {}, {}, {}, {}, None+ for name, p in parts.items():+ kind, data = p["kind"], p["data"]+ if name.startswith("video/") and kind == "mp4":+ self.video[name[6:]] = data+ elif name.startswith("frames/"):+ self.frames[name[7:]] = data+ elif name.startswith("last/"):+ self.last[name[5:]] = data+ elif name.startswith("depth/"):+ self.depth[name[6:]] = data+ elif name == "proprio":+ self.proprio = data++ @property+ def done(self) -> bool:+ return bool(self.meta.get("done"))++ def save(self, out_dir: str) -> str:+ from PIL import Image+ out = pathlib.Path(out_dir)+ out.mkdir(parents=True, exist_ok=True)+ (out / "meta.json").write_text(json.dumps(self.meta, indent=1))+ for cam, b in self.video.items():+ (out / f"{cam}.mp4").write_bytes(b)+ for cam, img in self.last.items():+ (out / "last").mkdir(exist_ok=True)+ Image.fromarray(np.ascontiguousarray(img)).save(out / "last" / f"{cam}.jpg", quality=90)+ for cam, arr in self.frames.items():+ d = out / "frames" / cam+ d.mkdir(parents=True, exist_ok=True)+ for i, f in enumerate(arr):+ Image.fromarray(np.ascontiguousarray(f)).save(d / f"{i:03d}.jpg", quality=85)+ for cam, arr in self.depth.items():+ np.save(out / f"depth_{cam}.npy", arr)+ if self.proprio is not None:+ np.save(out / "proprio.npy", self.proprio)+ return str(out)+++class Robot:+ def __init__(self, base: str | None = None, timeout: float = 600.0):+ candidates = [c for c in (base or os.environ.get("SIM_URL"), "http://sim:8000", "http://localhost:8000") if c]+ t0, last = time.time(), None+ while time.time() - t0 < timeout:+ for b in candidates:+ try:+ r = requests.get(b + "/spec", timeout=(5, 60))+ if r.status_code == 503:+ last = "warming up"+ continue+ r.raise_for_status()+ self.base, self._spec = b, r.json()+ if not str(self._spec.get("protocol", "")).startswith(PROTOCOLS):+ raise RuntimeError(f"not an eai-standard/2.x service: {self._spec.get('protocol')}")+ return+ except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:+ last = e # not up yet (the scene takes about a minute), or busy+ time.sleep(2)+ raise RuntimeError(f"sim service unavailable: {last}")++ def spec(self) -> dict:+ return self._spec++ def status(self) -> dict:+ r = requests.get(self.base + "/status", timeout=(5, 60))+ r.raise_for_status()+ return r.json()++ def get_obs(self, cameras=None, seconds: float = 0.0, video_fps: int = 10, include_depth: bool = False,+ video: str = "frames") -> Result:+ meta = {"cameras": _names(cameras), "seconds": float(seconds), "video_fps": int(video_fps),+ "include_depth": bool(include_depth), "video": video}+ return self._post("/observe", meta, [], timeout=900)++ def submit(self, action_type: str, actions, dt: float | None = None, interpolate: str = "linear",+ at: int | None = None, cameras=None, video_fps: int = 10, video: str = "none",+ include_depth: bool = False, note: str = "") -> Result:+ a = np.asarray(actions, dtype=np.float64)+ if a.ndim == 1:+ a = a[None, :]+ if not np.all(np.isfinite(a)):+ raise ValueError("actions contain NaN or inf")+ meta = {"type": action_type, "dt": None if dt is None else float(dt), "interpolate": interpolate,+ "at": None if at is None else int(at), "cameras": _names(cameras), "video_fps": int(video_fps),+ "video": video,+ "include_depth": bool(include_depth), "note": note}+ return self._post("/submit", meta, [rcb1.array_part("actions", a)], timeout=1800)++ def reset(self, cameras=None, include_depth: bool = False) -> Result:+ return self._post("/reset", {"cameras": _names(cameras), "include_depth": bool(include_depth)}, [], timeout=900)++ def _post(self, path: str, meta: dict, parts: list, timeout: float) -> Result:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_BODY: # the service would close the connection on it unread+ raise ValueError(f"{path}: {len(body)} bytes, over the protocol's {MAX_BODY}: send fewer rows")+ r = requests.post(self.base + path, data=body, headers={"Content-Type": "application/x-rcb1"},+ timeout=(5, timeout))+ if r.headers.get("Content-Type", "").startswith("application/x-rcb1") and r.status_code == 200:+ return Result(*rcb1.decode(r.content))+ try:+ err = r.json()+ except ValueError:+ err = {"error": r.text[:300]}+ msg = f"{r.status_code} {path}: {err.get('error')}" + (f" ({err['detail']})" if err.get("detail") else "")+ if r.status_code == 409:+ raise EpisodeOver(msg + (f"; reason {err['reason']}" if err.get("reason") else "")+ + (f"; resets_left {err['resets_left']}" if "resets_left" in err else ""))+ if r.status_code in (400, 413):+ raise ValueError(msg)+ raise RuntimeError(msg)+++def _names(cameras) -> list[str] | None:+ return None if cameras is None else [cameras] if isinstance(cameras, str) else list(cameras)diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/instance.npz b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/instance.npznew file mode 100644index 0000000000..d24d7bfb26Binary files /dev/null and b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/instance.npz differdiff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb1.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb1.pynew file mode 100644index 0000000000..6938c57e38--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb1.py@@ -0,0 +1,83 @@+"""RCB1 — a tiny binary frame for one JSON header plus N raw blobs (videos, image arrays, depth, proprio).++ frame = b"RCB1" | header_len uint32 LE | header JSON (UTF-8) | blob_0 | blob_1 | ...+ header = {"v": 1, "meta": {...}, "parts": [{"name", "kind", "len", ...}, ...]} # blobs in the same order++kinds: "mp4" (fps, n), "jpeg", "array" (dtype, shape; C-order raw bytes), "json" (utf-8), "bytes".+Zero dependencies for bytes/json parts; numpy only when you ask for arrays. Used on both sides (server and client),+over HTTP bodies (Content-Type: application/x-rcb1) and equally usable over a raw socket. `python -m rcb1 unpack f dir`.+"""+from __future__ import annotations+import json, struct, sys, pathlib++MAGIC = b"RCB1"+++def encode(meta: dict, parts: list[tuple[str, str, bytes, dict]]) -> bytes:+ """parts: [(name, kind, blob, extra), ...]; extra carries dtype/shape/fps/n."""+ hdr_parts = []+ for name, kind, blob, extra in parts:+ d = {"name": name, "kind": kind, "len": len(blob)}+ d.update({k: v for k, v in (extra or {}).items() if v is not None})+ hdr_parts.append(d)+ header = json.dumps({"v": 1, "meta": meta, "parts": hdr_parts}, separators=(",", ":")).encode()+ return b"".join([MAGIC, struct.pack("<I", len(header)), header] + [blob for _, _, blob, _ in parts])+++def array_part(name: str, arr, extra: dict | None = None) -> tuple[str, str, bytes, dict]:+ import numpy as np+ a = np.ascontiguousarray(arr)+ return (name, "array", a.tobytes(), {"dtype": str(a.dtype), "shape": list(a.shape), **(extra or {})})+++def decode(buf: bytes, arrays: bool = True) -> tuple[dict, dict]:+ """-> (meta, parts) where parts[name] = {"kind", ..., "data"}; arrays become numpy arrays when arrays=True."""+ if buf[:4] != MAGIC:+ raise ValueError("not an RCB1 frame")+ n = struct.unpack("<I", buf[4:8])[0]+ header = json.loads(buf[8:8 + n].decode()); off = 8 + n+ out = {}+ for p in header["parts"]:+ blob = buf[off:off + p["len"]]; off += p["len"]+ d = dict(p)+ if p["kind"] == "array" and arrays:+ import numpy as np+ d["data"] = np.frombuffer(blob, dtype=np.dtype(p["dtype"])).reshape(p["shape"])+ elif p["kind"] == "json":+ d["data"] = json.loads(blob.decode())+ else:+ d["data"] = blob+ out[p["name"]] = d+ return header.get("meta", {}), out+++def unpack_to_dir(buf: bytes, out_dir: str) -> list[str]:+ """Write a frame's parts as files: mp4/jpeg/bytes verbatim, arrays as .npy, meta as meta.json."""+ import numpy as np+ out = pathlib.Path(out_dir); out.mkdir(parents=True, exist_ok=True)+ meta, parts = decode(buf, arrays=True); written = []+ (out / "meta.json").write_text(json.dumps(meta, indent=1)); written.append("meta.json")+ for name, p in parts.items():+ safe = name.replace("/", "__")+ if p["kind"] == "array":+ f = out / f"{safe}.npy"; np.save(f, p["data"])+ elif p["kind"] == "mp4":+ f = out / f"{safe}.mp4"; f.write_bytes(p["data"])+ elif p["kind"] == "jpeg":+ f = out / f"{safe}.jpg"; f.write_bytes(p["data"])+ elif p["kind"] == "json":+ f = out / f"{safe}.json"; f.write_text(json.dumps(p["data"], indent=1))+ else:+ f = out / f"{safe}.bin"; f.write_bytes(p["data"])+ written.append(f.name)+ return written+++if __name__ == "__main__":+ if len(sys.argv) == 4 and sys.argv[1] == "unpack":+ print("\n".join(unpack_to_dir(open(sys.argv[2], "rb").read(), sys.argv[3])))+ elif len(sys.argv) == 3 and sys.argv[1] == "info":+ meta, parts = decode(open(sys.argv[2], "rb").read(), arrays=False)+ print(json.dumps(meta, indent=1)); [print(n, {k: v for k, v in p.items() if k != "data"}) for n, p in parts.items()]+ else:+ print(__doc__)diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_hb.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_hb.pynew file mode 100644index 0000000000..0bd149981c--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_hb.py@@ -0,0 +1,573 @@+"""rcb_hb.py — task-local tooling for HumanoidBench: frozen instances, recording, deterministic replay.++Mirrors tasks/robosuite-cube-stack-i00-privileged/environment/rcb_traj.py so the agent's experience is the same+across simulators; the differences HumanoidBench forces are marked [HB].++Concepts+--------+instance the frozen scene, saved as .npz: the MuJoCo state (qpos, qvel, time, qacc_warmstart) plus+ the task state that lives outside it -- goals drawn at reset, subtask counters, and the+ model arrays a task writes its target into. Upstream draws the latter from the global+ np.random, which reset(seed=) does not reach, so make_instance() seeds both generators and+ the snapshot restores both; an instance is therefore reproducible from its seed and pins+ the scene for the agent and the verifier alike.+trajectory (T, nu) normalised joint-position targets in [-1, 1], one row per control step (50 Hz).+ Replayed open-loop from the same instance it reproduces bit-for-bit (CPU MuJoCo).+success [HB] episode_return >= task.success_bar -- a threshold on the accumulated dense reward,+ not a predicate on the final state. NOT computed here: the image ships no reward function+ (see below), so scoring happens in the verifier.++CLI+---+ python rcb_hb.py run-oracle --instance inst.npz --code gen.py --out traj.npz+ python rcb_hb.py replay --instance inst.npz --traj traj.npz [--repeat 2]+ python rcb_hb.py render --instance inst.npz --traj traj.npz --out replay.mp4++Python API+----------+ from rcb_hb import Session+ s = Session(instance="/app/instance.npz") # which task and env this is comes from the instance+ s.reset() # back to the frozen state (unlimited)+ obs = s.obs() # state vector (its layout: instruction.md)+ with s.recording() as rec: # [HB] context manager, not record_start/record_stop+ for _ in range(s.max_steps):+ obs, r, done = s.step(my_controller(obs))+ if done:+ break+ s.replay(rec.actions) # self-check: resets, replays open-loop, returns status()+ s.save_trajectory(rec.actions, "/app/output/trajectory.npz")++Scoring is not visible from here+--------------------------------+`s.scored` is False and `s.step()` returns `None` for the reward. HumanoidBench's reward functions are+dense and hand-tuned, so reading one turns a control problem into an algebra problem -- the formula is+therefore removed from the image and restored only inside the verifier, after the agent is finished.++What you have instead is the full physical state: `s.obs()` (its layout is in instruction.md: qpos and, for most+tasks, qvel, plus this task's goal where it has one), and the MuJoCo model is right there in `s.model`. Everything the reward could tell you+-- how fast the centre of mass is moving, how upright the torso is, how far the hand is from the target+-- is computable from that, and `instruction.md` says which of those things the task is judged on. Define+the measure you think the task calls for, and optimise against it.+"""+from __future__ import annotations++import argparse+import contextlib+import hashlib+import json+import os+import subprocess+import sys+from pathlib import Path+from typing import Any++import numpy as np+++# --- the part of a task's state that does NOT live in (qpos, qvel) -----------------------------------+# Upstream keeps some of it on the Task object and some in `model`: a goal drawn at reset, a destination+# or target orientation written into the model, a placement permutation, and counters that reset_model+# forgets to clear. None of it is reachable from a qpos/qvel snapshot, and the random draws come from the+# LEGACY GLOBAL np.random, which gym's reset(seed=) does not touch. Without freezing it, this session and+# the verifier would solve different problems, and a second rollout in one process would be scored by a+# different reward function than the first. Snapshotting vars(task) covers that with no special-casing;+# where the environment was already well behaved it is simply a no-op.+_TASK_STATE_SKIP = ("_env", "robot", "unwrapped") # back-references to the env, not state+++def _task_state(task) -> dict[str, Any]:+ """vars(task) minus the back-references and anything reward-shaped (a name containing "reward", the scrub's own+ rule): the instance ships into the agent's image, and push keeps its scoring weights in `reward_dict`, which the+ image strips and only the verifier puts back (rcb_rewards.py)."""+ return {k: v for k, v in vars(task).items() if k not in _TASK_STATE_SKIP and "reward" not in k.lower()}+++def _unscored_bookkeeping(task) -> None:+ """[HB] Bookkeeping that upstream does only inside get_reward(), which the image deletes, but that the agent+ can otherwise see: a `step_counter` that moves a target in the observation, and a checkpoint pointer and+ heading that `update_move_direction()` advances (recolouring the checkpoints). Neither is the formula, so+ where this process cannot score, step() does it here and the agent sees what the verifier's replay sees.+ The other progress get_reward() keeps (which object is current, what is done) is the bonus test itself and+ stays out; instruction.md says where it does not advance."""+ if hasattr(task, "update_move_direction"):+ task.update_move_direction()+ if hasattr(task, "step_counter"):+ task.step_counter += 1+++def _task_state_dumps(state: dict) -> str:+ """Serialise to JSON, never pickle: an instance file is data the verifier loads with+ allow_pickle=False, and it must stay that way -- a pickle in that position is code execution."""+ def enc(o):+ if isinstance(o, np.ndarray):+ return {"__ndarray__": o.tolist(), "dtype": str(o.dtype)}+ if isinstance(o, np.integer):+ return int(o)+ if isinstance(o, np.floating):+ return float(o)+ if isinstance(o, np.bool_):+ return bool(o)+ raise TypeError(f"task state field of type {type(o).__name__} is not JSON-serialisable: {o!r}")+ return json.dumps(state, default=enc)+++def _task_state_loads(blob: str) -> dict:+ def dec(d):+ if "__ndarray__" in d:+ return np.array(d["__ndarray__"], dtype=np.dtype(d["dtype"]))+ return d+ return json.loads(blob, object_hook=dec)+++def _resolve_gl_backend() -> str:+ """Pick MuJoCo's offscreen GL backend. An explicit MUJOCO_GL always wins.++ `egl` renders on the GPU (~0.4 ms/frame), `osmesa` on the CPU (~130 ms/frame); physics is CPU-only+ either way, so rendering is the only thing a GPU changes here.++ "Has a GPU" and "can render on it" are different questions: the container runtime mounts the device+ node and the driver's GL libraries under separate capabilities, and whether libglvnd can actually+ reach the vendor driver depends on the host's driver packaging. Guessing from file paths got this+ wrong once already, so just try it -- MuJoCo binds its backend at `import mujoco`, once per process,+ which is why the attempt has to happen in a throwaway subprocess. The device-node check keeps that+ cost at zero on CPU-only machines, and exporting the result means forked workers inherit it instead+ of each paying for their own probe.+ """+ if explicit := os.environ.get("MUJOCO_GL"):+ return explicit+ if not os.path.exists("/dev/nvidiactl"):+ return "osmesa"+ probe = "import os; os.environ['MUJOCO_GL'] = 'egl'; import mujoco; mujoco.GLContext(8, 8).free()"+ try:+ ok = subprocess.run([sys.executable, "-c", probe], capture_output=True, timeout=120).returncode == 0+ except (OSError, subprocess.SubprocessError):+ ok = False+ return "egl" if ok else "osmesa"+++# MuJoCo reads this once, at `import mujoco`, so import this module before anything imports mujoco.+GL_BACKEND = _resolve_gl_backend()+os.environ["MUJOCO_GL"] = GL_BACKEND+os.environ["PYOPENGL_PLATFORM"] = GL_BACKEND+++class _Recording:+ """The actions captured inside a `with session.recording()` block."""++ def __init__(self) -> None:+ self._rows: list[np.ndarray] = []++ def append(self, action: np.ndarray) -> None:+ self._rows.append(np.asarray(action, dtype=np.float64).copy())++ def clear(self) -> None:+ self._rows.clear()++ @property+ def actions(self) -> np.ndarray:+ """(T, nu), or an empty (0, 0) when the block stepped nothing -- np.stack would raise on an+ empty list, and a traceback here hides the thing the caller actually needs to be told."""+ if not self._rows:+ return np.zeros((0, 0), dtype=np.float64)+ return np.stack(self._rows)++ def __len__(self) -> int:+ return len(self._rows)+++class Session:+ """One HumanoidBench environment pinned to one frozen scene instance."""++ def __init__(self, task: str | None = None, instance: str | Path | None = None,+ enable_render: bool = False, env_id: str | None = None, image_hw: int | None = None):+ """Open the environment this instance belongs to.++ The instance file records which HumanoidBench task and env id it was drawn from, so normally+ neither has to be passed: `Session(instance="/app/instance.npz")` is enough. Passing `task` as+ well turns it into an assertion -- that is what the verifier does, so a trajectory produced for+ one task cannot be graded against another's scene.++ `env_id` is only for creating an instance from scratch, when there is no file to read it from.+ `image_hw` is the side of every rendered image in pixels (None: upstream's 256).+ """+ if instance is not None:+ inst = np.load(instance, allow_pickle=False)+ file_task, file_env = str(inst["task"]), str(inst["env_id"])+ if task is not None and task != file_task:+ raise ValueError(f"instance is for task {file_task!r}, session asked for {task!r}")+ if env_id is not None and env_id != file_env:+ raise ValueError(f"instance is for env {file_env!r}, session asked for {env_id!r}")+ task, env_id = file_task, file_env+ if task is None or env_id is None:+ raise ValueError("pass instance=..., or both task= and env_id= to build a fresh scene")+ self.task, self.env_id = task, env_id++ import gymnasium as gym+ import humanoid_bench # noqa: F401 (registers every robot x task env id)++ # [HB] HumanoidEnv defaults to render_mode="rgb_array" and Task.__init__ then builds an offscreen+ # viewer eagerly, so even a physics-only gym.make() would need a GL context. render_mode=None+ # skips the viewer entirely and is markedly faster.+ self.env = gym.make(self.env_id, render_mode="rgb_array" if enable_render else None,+ **({"width": image_hw, "height": image_hw} if image_hw else {}))+ self.enable_render = enable_render+ self.low = self.env.unwrapped+ self.model, self.data = self.low.model, self.low.data+ self.nu = int(self.model.nu)+ self.max_steps = int(self.env.spec.max_episode_steps)+ self.success_bar = float(self.low.task.success_bar)+ # [HB] Whether this process can score at all. The image deletes every Task subclass's get_reward+ # (images/humanoidbench/scrub_oracles.py), which leaves upstream's base returning (0, {}); the+ # verifier patches the real one back before opening its Session. Detecting it beats a flag file:+ # there is exactly one truth, and it is the object that would do the scoring.+ from humanoid_bench.tasks import Task as _Task+ self.scored = type(self.low.task).get_reward is not _Task.get_reward++ self.instance_state: np.ndarray | None = None+ self.instance_extra: dict[str, Any] | None = None # task state + the model arrays tasks write+ self._geom_rgba: np.ndarray | None = None # the model's colours at the frozen start+ self._recording: _Recording | None = None+ self._ret = 0.0 # accumulated reward since the last reset+ self._steps = 0+ self._last_info: dict[str, Any] = {}+ self._sim_steps_total = 0 # simulation budget, reported by status() (PROTOCOL.md §5)+ self._resets = 0++ self.env.reset(seed=0) # one reset so the gym wrappers start in a legal state+ if instance is not None:+ self.instance_state = np.array(inst["state"], dtype=np.float64)+ self.instance_extra = {"task_state": str(inst["task_state"]),+ "body_pos": np.array(inst["body_pos"], dtype=np.float64),+ "body_quat": np.array(inst["body_quat"], dtype=np.float64)}+ self._geom_rgba = self.model.geom_rgba.copy() # nothing has stepped yet: the scene as it starts+ self.reset()++ # ---- instances ---------------------------------------------------------+ def _snapshot(self) -> np.ndarray:+ d = self.data+ return np.concatenate([+ np.asarray(d.qpos, dtype=np.float64),+ np.asarray(d.qvel, dtype=np.float64),+ np.array([d.time], dtype=np.float64),+ np.asarray(d.qacc_warmstart, dtype=np.float64), # the field everyone forgets+ ])++ def _snapshot_extra(self) -> dict[str, Any]:+ """The other half of the frozen instance: everything outside (qpos, qvel). See _TASK_STATE_SKIP.++ `body_pos` / `body_quat` are here because two tasks write their goal into the MODEL rather than+ into data -- package puts the destination in body_pos[-1], cube the target orientation in+ body_quat[-1]. Copying the whole array is a few KB and keeps this free of per-task knowledge.+ """+ return {+ "task_state": _task_state_dumps(_task_state(self.low.task)),+ "body_pos": np.asarray(self.model.body_pos, dtype=np.float64).copy(),+ "body_quat": np.asarray(self.model.body_quat, dtype=np.float64).copy(),+ }++ def _restore_extra(self) -> None:+ """Must run between env.reset() and mj_forward: reset() redraws the goal and leaves accumulated+ counters alone, and body_pos feeds the kinematics that mj_forward is about to recompute."""+ if self._geom_rgba is not None: # maze recolours its checkpoints in the model as they are+ self.model.geom_rgba[:] = self._geom_rgba # passed, and upstream's reset leaves them as they were+ vars(self.low.task).update(_task_state_loads(self.instance_extra["task_state"]))+ self.model.body_pos[:] = self.instance_extra["body_pos"]+ self.model.body_quat[:] = self.instance_extra["body_quat"]++ def _restore(self, snap: np.ndarray) -> None:+ """Put the simulator back on the frozen state.++ A full env.reset() MUST come first. Writing qpos/qvel into a dirty `data` leaves hidden state+ behind -- data.ctrl still holds the last action, plus data.act, applied forces and the wrapper+ counters. mj_forward then folds that stale ctrl into the constraint solve, so the "restored"+ state silently differs from the snapshot and replays diverge within a step or two.+ rcb_traj.py does the same thing (_fresh_reset before _restore); it is not optional.+ """+ import mujoco+ nq, nv = self.model.nq, self.model.nv+ self.env.reset() # 1. clear ctrl / act / forces / counters+ self._restore_extra() # 1b. goals, counters, model-side targets+ self.data.qpos[:] = snap[:nq] # 2. overwrite the pose+ self.data.qvel[:] = snap[nq:nq + nv]+ self.data.time = float(snap[nq + nv])+ self.data.ctrl[:] = 0.0+ mujoco.mj_forward(self.model, self.data) # 3. recompute derived quantities+ self.data.qacc_warmstart[:] = snap[nq + nv + 1:] # 4. AFTER forward -- forward overwrites it+ self._restore_bookkeeping()++ def _restore_bookkeeping(self) -> None:+ """[HB] Much lighter than robosuite's: no controllers to re-anchor and no cached observables, and gymnasium's+ TimeLimit counter is zeroed by the env.reset() every restore starts with. Only this session's own counters."""+ self._ret, self._steps, self._last_info = 0.0, 0, {}++ def make_instance(self, seed: int | None = None) -> np.ndarray:+ """Draw a scene and freeze it. Reproducible: the same seed gives the same instance anywhere.++ Two RNGs have to be pinned, not one. `reset(seed=)` seeds gymnasium's per-instance generator,+ which is what perturbs every qpos by +-0.01. Task-level draws -- a goal position, a destination,+ a target orientation, a placement order -- come from the LEGACY GLOBAL np.random instead, which+ `reset(seed=)` does not touch; without seeding it+ here, make_instance() returns a different scene in every process and an instance file could only+ ever be copied, never regenerated. The global state is saved and restored so that seeding it is+ invisible to whatever else the caller is doing with numpy's random module.+ """+ rng_state = np.random.get_state()+ try:+ np.random.seed(0 if seed is None else seed)+ self.env.reset(seed=seed)+ finally:+ np.random.set_state(rng_state)+ self.instance_state = self._snapshot()+ self.instance_extra = self._snapshot_extra() # raises if a task keeps unserialisable state+ self._restore_bookkeeping()+ return self.instance_state++ def instance_sha(self) -> str:+ """Digest of the WHOLE frozen instance. Two instances can share a pose and differ only in the+ goal, so this has to cover the extra as well -- state_sha() hashes the live pose and is a+ different question (it is what the determinism check compares)."""+ h = hashlib.sha1(np.ascontiguousarray(self.instance_state).tobytes())+ h.update(self.instance_extra["task_state"].encode())+ for k in ("body_pos", "body_quat"):+ h.update(np.ascontiguousarray(self.instance_extra[k]).tobytes())+ return h.hexdigest()[:12]++ def save_instance(self, path: str | Path, seed: int | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("call make_instance() first")+ np.savez(path, state=self.instance_state, task=self.task, env_id=self.env_id,+ seed=-1 if seed is None else seed, sha=self.instance_sha(), **self.instance_extra)++ def state_sha(self) -> str:+ return hashlib.sha1(np.ascontiguousarray(self._snapshot()).tobytes()).hexdigest()[:12]++ def reset(self) -> np.ndarray:+ """Return to the frozen instance state. Unlimited.++ Inside a `with recording()` block this restarts the take instead of ending it: a reset means+ "this attempt again", so the actions recorded so far are dropped and recording continues.+ """+ if self.instance_state is None:+ raise RuntimeError("no instance: open the Session with instance=..., or call make_instance()")+ if self._recording is not None:+ self._recording.clear()+ self._resets += 1+ self._restore(self.instance_state)+ return self.obs()++ # ---- stepping ----------------------------------------------------------+ def obs(self) -> np.ndarray:+ return self.low.task.get_obs()++ def step(self, action) -> tuple[np.ndarray, float | None, bool]:+ """One control step at 50 Hz. `action` is (nu,) and is clipped to [-1, 1].++ Returns (obs, reward, done). **`reward` is None unless this process can score** -- see the module+ docstring; inside the agent's container it always is. `obs` and `done` are always real.+ """+ a = np.clip(np.asarray(action, dtype=np.float32).reshape(self.nu), -1.0, 1.0)+ if self._recording is not None:+ self._recording.append(a)+ obs, r, term, trunc, info = self.env.step(a)+ if self.scored:+ self._ret += float(r)+ else:+ _unscored_bookkeeping(self.low.task)+ self._steps += 1+ self._sim_steps_total += 1+ self._last_info = info+ return obs, (float(r) if self.scored else None), bool(term or trunc)++ @contextlib.contextmanager+ def _not_recording(self):+ """A self-check or a video is not part of the take: suspend recording for its duration."""+ rec, self._recording = self._recording, None+ try:+ yield+ finally:+ self._recording = rec++ @contextlib.contextmanager+ def recording(self):+ """Capture every action stepped inside the block:++ with s.recording() as rec:+ ...+ actions = rec.actions # (T, nu)++ Actions taken before an exception are kept, so a crashed rollout is still inspectable.+ """+ rec = _Recording()+ self._recording = rec+ try:+ yield rec+ finally:+ self._recording = None++ # ---- status / replay ---------------------------------------------------+ def status(self) -> dict[str, Any]:+ """What this process can honestly say about the episode just run.++ [HB] `scored` is False in the agent's container, and then the scoring fields are absent rather+ than zero -- a missing key is a question you cannot answer, a zero is a wrong answer. What stays+ is what is observable either way: how many steps ran, and the hash of the state they ended in+ (which is what makes an open-loop replay checkable without a reward).+ """+ out = {+ "scored": self.scored,+ "sim_steps": int(self._steps),+ "state_sha": self.state_sha(),+ "sim_steps_total": int(self._sim_steps_total),+ "resets": int(self._resets),+ }+ if not self.scored:+ return out+ return out | {+ "success": bool(self._ret >= self.success_bar),+ "episode_return": float(self._ret),+ "success_bar": self.success_bar,+ "final_reward": float(self._last_info.get("per_timestep_reward", 0.0)),+ }++ def replay(self, actions: np.ndarray) -> dict[str, Any]:+ """Reset, replay open-loop, return status(). Use this to self-check before submitting.++ Safe to call from inside a `with recording()` block: a self-check is not part of the take,+ so recording is suspended for the duration.+ """+ with self._not_recording():+ self.reset()+ for a in np.asarray(actions, dtype=np.float64):+ if self.step(a)[2]:+ break+ return self.status()++ def save_trajectory(self, actions: np.ndarray, path: str | Path, meta: dict | None = None) -> None:+ if self.instance_state is None:+ raise RuntimeError("no instance loaded")+ acts = np.asarray(actions, dtype=np.float64)+ if acts.ndim != 2 or acts.shape[1] != self.nu:+ raise ValueError(f"actions must be (T, {self.nu}), got {acts.shape}")+ if len(acts) > self.max_steps:+ raise ValueError(f"trajectory longer than the episode limit ({len(acts)} > {self.max_steps})")+ Path(path).parent.mkdir(parents=True, exist_ok=True)+ np.savez(+ path,+ actions=acts,+ task=self.task,+ env_id=self.env_id,+ instance_sha=self.instance_sha(),+ action_layout=f"{self.nu} normalised joint-position targets in [-1,1], one row per 50 Hz step",+ meta=json.dumps(meta or {}),+ )++ def render_video(self, actions: np.ndarray, path: str | Path, *, every: int = 5, fps: int = 20) -> dict[str, Any]:+ """Replay and write an MP4. Needs Session(..., enable_render=True). Like replay(), not part of a take.+ Rendering is for humans; the verdict always comes from replay()."""+ import imageio.v2 as imageio+ if not self.enable_render:+ raise RuntimeError("render_video needs Session(..., enable_render=True)")+ with self._not_recording():+ self.reset()+ frames = [self.env.render()]+ for i, a in enumerate(np.asarray(actions, dtype=np.float64)):+ done = self.step(a)[2]+ if (i + 1) % every == 0:+ frames.append(self.env.render())+ if done:+ break+ frames.append(self.env.render())+ path = Path(path)+ path.parent.mkdir(parents=True, exist_ok=True)+ with imageio.get_writer(str(path), fps=fps, format="FFMPEG", codec="libx264", macro_block_size=1) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ imageio.imwrite(str(path.with_suffix(".last.png")), np.ascontiguousarray(frames[-1]))+ return {**self.status(), "n_frames": len(frames), "video": str(path), "gl_backend": GL_BACKEND}++ def exec_code(self, code: str) -> dict[str, Any]:+ """Run a script with this session (as `s` and `session`) and numpy in scope."""+ g: dict[str, Any] = {"__name__": "__main__", "np": np, "s": self, "session": self}+ try:+ exec(code, g, g)+ return {"ok": True}+ except BaseException as exc: # noqa: BLE001+ return {"ok": False, "error": repr(exc)}++ def close(self) -> None:+ with contextlib.suppress(Exception):+ self.env.close()+++# --------------------------------------------------------------------------- CLI+def _load_traj(path: str) -> np.ndarray:+ return np.load(path, allow_pickle=False)["actions"]+++def _record(s: Session, code_path: str) -> tuple[np.ndarray | None, dict]:+ with s.recording() as rec:+ r = s.exec_code(Path(code_path).read_text())+ return (rec.actions if r["ok"] else None), r+++def cmd_run_oracle(a) -> int:+ s = Session(a.task, instance=a.instance) # task is optional; when given it asserts+ acts, r = _record(s, a.code)+ if acts is None:+ print(json.dumps({"error": r["error"]}), file=sys.stderr)+ return 1+ s.save_trajectory(acts, a.out, meta={"source": Path(a.code).name})+ # status() drops the scoring fields where there is no reward function, so this prints fewer keys+ # inside the task image than it does verifier-side. The trajectory is written either way.+ print(json.dumps({"out": a.out, "n_actions": len(acts), **s.replay(acts)}))+ return 0+++def cmd_replay(a) -> int:+ """Replay a trajectory `--repeat` times and report what this process can see.++ Determinism is the part that is always available, and it is the part worth checking before+ submitting: identical state_sha across repeats means the grader's replay will land where yours did.+ The scoring fields appear only when this process can score (see Session.status).+ """+ s = Session(a.task, instance=a.instance)+ outs = [s.replay(_load_traj(a.traj)) for _ in range(a.repeat)]+ out = {"deterministic": len({o["state_sha"] for o in outs}) == 1, **outs[0]}+ if s.scored:+ out |= {"all_success": all(o["success"] for o in outs),+ "returns": [o["episode_return"] for o in outs]}+ print(json.dumps(out))+ return 0+++def cmd_render(a) -> int:+ s = Session(a.task, instance=a.instance, enable_render=True)+ print(json.dumps(s.render_video(_load_traj(a.traj), a.out, every=a.every, fps=a.fps)))+ return 0+++def main() -> int:+ p = argparse.ArgumentParser(prog="rcb_hb.py")+ sub = p.add_subparsers(dest="cmd", required=True)++ # --task is optional wherever an --instance is given: the instance names its own task, and passing+ # it anyway turns it into an assertion (which is what the verifier wants).+ q = sub.add_parser("run-oracle"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--code", required=True)+ q.add_argument("--out", required=True); q.set_defaults(fn=cmd_run_oracle)++ q = sub.add_parser("replay"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--repeat", type=int, default=2); q.set_defaults(fn=cmd_replay)++ q = sub.add_parser("render"); q.add_argument("--task")+ q.add_argument("--instance", required=True); q.add_argument("--traj", required=True)+ q.add_argument("--out", required=True); q.add_argument("--every", type=int, default=5)+ q.add_argument("--fps", type=int, default=20); q.set_defaults(fn=cmd_render)++ a = p.parse_args()+ return a.fn(a)+++if __name__ == "__main__":+ sys.exit(main())diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_realtime.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_realtime.pynew file mode 100644index 0000000000..410284bf27--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_realtime.py@@ -0,0 +1,479 @@+"""eai-standard/2.3, the realtime mode's protocol: the sim service of HumanoidBench's `-realtime` tasks (docs/PROTOCOL.md+§4b, docs/LIMITED_MODE_PROTOCOL.md "Realtime mode"; docs/benchmarks/humanoidbench.md). Entry point of the sidecar:+`python /srv/rcb_realtime.py` (the adapter is server.py's, unchanged). 2.3 is 2.2 plus the realtime capability, every 2.2+addition optional: this service is rcb_service's 2.1 service with the capability, and reports eai-standard/2.3.++It extends rcb_service.Service: the same endpoints, RCB1 frames, budgets (reset, wall clock), evidence, crash recovery+and finalize. Only the time model differs. In standard mode time moves only inside calls; here the episode has a clock:++- An episode starts paused, at the frozen start with the servos holding their starting targets. Its clock starts at its+ first POST /submit or POST /observe with seconds > 0 (again after every reset, and after a crash recovery). From then+ on one control step runs every control_dt x slowdown seconds of wall time, whether or not the agent sends anything,+ until the episode ends; the clock stops there.+- The service keeps a plan: one target row per coming control step. /submit expands its chunk (rows dt apart, `linear`+ or `hold`, exactly as 2.1 does) into control steps and writes it into the plan from control step `at` (default: the+ next one), replacing whatever was planned from there on, and answers at once. A chunk that arrives after its `at`+ loses the rows whose steps are gone (`late_steps`). When the plan runs out the last target is held (zero-order+ hold): a slow agent's robot keeps its last servo targets.+- /observe with seconds = 0 answers at once; with seconds > 0 it watches the running episode that long (robot time)+ and returns what the cameras saw. It never changes the plan.+- `cameras = []` asks for no images at all (in 2.1 an empty list means every camera): a controller's proprio-only call.++Grading is unchanged: every executed control step's sim row is journaled (trajectory_live.f64) and the graded episode's+rows are replayed in a fresh simulator at the end. What is graded is the rows the simulator applied, not what was+submitted, so the replay equals the live run however the timing went. Evidence added (never visible to the agent):+ticks.f64 (per journal row: wall time, and the call whose chunk gave the row, -1 for a held one), calls.jsonl (one line+per call; no calls/NNNNNN/ directories: a controller calls 50 times a second) and submitted.f64 (every row as sent:+call id, then the row). final.json adds realtime, slowdown, max_lag_s, stalls, held_steps and late_steps.++Threads: the simulator and its GL context belong to the main thread, which runs the clock and serves every request in+between control steps; the HTTP threads only read, queue and encode. A control step that is due runs before any waiting+request. A service that falls behind (a long render) runs the missed steps back to back, skipping none; more than+MAX_LAG_S behind, it drops the debt instead (a stall, counted) rather than fast-forward the robot.++A crash resumes the same episode from its last checkpoint (one per second of robot time while the clock runs), paused.+A simulator crash is an infrastructure failure, never the agent's (PROTOCOL §4b, owner 2026-10-08). No call spans a+crash here (submit answers at once), so a crash is never put down to the agent's call: past RCB_MAX_RESUMES restarts the+trial ends `sim_crashed` (infra_error), a failed resume `resume_failed` (infra_error), and a simulator error that ends+the graded episode (reason "error") sets infra_error in final.json too.+"""+from __future__ import annotations++import collections+import json+import os+import queue+import sys+import threading+import time+import traceback+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer+from urllib.parse import urlparse++import numpy as np++import rcb_service+from rcb_service import MAX_BODY, Recorder, Refused, Service, bad_request++PROTOCOL = "eai-standard/2.3" # the realtime mode's protocol (PROTOCOL §4b); rcb_service.py itself serves 2.1+MAX_LAG_S = 0.5 # behind the clock by more than this: drop the debt (a stall) instead of catching up+CHECKPOINT_S = 1.0 # robot time between checkpoints while the clock runs (the most a crash loses)+++class _Job:+ """One HTTP request, handed from its HTTP thread to the simulator's thread and back."""++ def __init__(self, method: str, path: str, body: bytes, content_length: int | None = None):+ self.method, self.path, self.body, self.content_length = method, path, body, content_length+ self.done = threading.Event()+ self.reply: tuple = () # ("raw", code, ctype, body) | ("rcb1", meta, parts) | ("rec", meta, Recorder)++ def finish(self, *reply) -> None:+ self.reply = reply+ self.done.set()+++class _Watch:+ """A pending /observe with seconds > 0: completes once its control steps have run (or the episode ended)."""++ def __init__(self, job: _Job, rec: Recorder, start: int, steps: int, cameras: list[str], obs_id: int):+ self.job, self.rec, self.cameras, self.obs_id = job, rec, cameras, obs_id+ self.start, self.end = start, start + steps+++class RealtimeService(Service):+ """rcb_service.Service with the realtime clock (module docstring)."""++ def __init__(self, sim, *, slowdown: float | None = None, **kw):+ self.slowdown = float(os.environ.get("RCB_SLOWDOWN") or 1.0) if slowdown is None else float(slowdown)+ if not self.slowdown > 0:+ raise ValueError("RCB_SLOWDOWN must be > 0")+ self.wall_dt = sim.control_dt * self.slowdown+ self.max_ahead = int(round(float(kw.get("max_chunk_s", 10.0)) / sim.control_dt)) # how far ahead `at` may be+ self.plan: collections.deque = collections.deque() # (type, row, call id) for each coming control step+ self.running = False+ self.anchor_t = self.anchor_n = 0 # the clock: control step anchor_n was due at anchor_t+ self.watches: list[_Watch] = []+ self.q: queue.Queue = queue.Queue()+ self.max_lag = self.lag = 0.0+ self.stalls = self.held_steps = self.late_steps = 0+ self._ticks: list[tuple[float, float]] = [] # (wall time, call id) per journal row not yet on disk+ super().__init__(sim, **kw)+ self.ticks_path = os.path.join(self.data, "ticks.f64")+ self.ckpt_every = max(1, int(round(CHECKPOINT_S / sim.control_dt)))++ # ------------------------------------------------------------------ spec, status, evidence+ def _spec(self) -> dict:+ spec = super()._spec()+ spec["protocol"] = PROTOCOL+ cdt = self.sim.control_dt+ spec["timing"] = {+ "realtime": True, "slowdown": self.slowdown, "control_dt": cdt,+ "wall_s_per_control_step": cdt * self.slowdown,+ "clock": "an episode starts paused; its clock starts at its first submit or observe with seconds > 0 (also "+ "after a reset) and then runs one control step every wall_s_per_control_step seconds, whatever "+ "you do, until the episode ends",+ "submit": "answers at once: the chunk (rows dt apart, linear or hold) becomes the plan from control step "+ "`at` (default: the next one), replacing what was planned from there; rows for steps already "+ "gone are dropped (meta.late_steps); after the plan's last row that target is held",+ "observe": "seconds = 0: now, at once; seconds > 0: watches the running episode that long and returns what "+ "the cameras saw; never changes the plan",+ "max_ahead_steps": self.max_ahead,+ "no_images": "cameras = [] returns no images, only proprioception (the fastest call)"}+ return spec++ def _realtime_counters(self) -> dict:+ return {"running": self.running, "planned_steps": len(self.plan), "lag_s": round(self.lag, 4)}++ def status(self) -> dict:+ return {**super().status(), **self._realtime_counters()}++ def meta(self) -> dict:+ return {**super().meta(), **self._realtime_counters()}++ def _timing_record(self) -> dict:+ return {"protocol": PROTOCOL, "realtime": 1, "slowdown": self.slowdown, "max_lag_s": round(self.max_lag, 4),+ "stalls": self.stalls, "held_steps": self.held_steps, "late_steps": self.late_steps}++ def _write_final(self) -> None:+ self.verdict.update(self._timing_record()) # final.json merges the verdict; none of these keys is a verdict's+ if self.reason == "error": # a simulator error ended the graded episode: infrastructure (§4b)+ self.verdict["infra_error"] = 1+ else: # (an episode that erred and was reset away is not graded)+ self.verdict.pop("infra_error", None)+ super()._write_final()++ def _checkpoint(self, ev: str, **kw) -> None:+ if self._ticks: # the tick log first: a crash in between leaves it too long+ with open(self.ticks_path, "ab") as f:+ f.write(np.asarray(self._ticks, dtype=np.float64).tobytes())+ self._ticks = []+ super()._checkpoint(ev, **kw)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ keep = ("type", "n_rows", "dt", "interpolate", "at", "start_step", "late_steps", "planned_steps", "seconds",+ "control_steps", "episode", "done", "reason", "n_clipped", "lag_s", "note")+ line = {"call": self.calls, "ts": time.time(), **{k: meta[k] for k in keep if k in meta}}+ with open(os.path.join(self.data, "calls.jsonl"), "a") as f:+ f.write(json.dumps(line) + "\n")+ if actions is not None:+ rows = np.concatenate([np.full((len(actions), 1), float(self.calls)), actions], axis=1)+ with open(os.path.join(self.data, "submitted.f64"), "ab") as f:+ f.write(np.asarray(rows, dtype=np.float64).tobytes())++ # ------------------------------------------------------------------ start, clock+ def start(self) -> None:+ super().start()+ rows = np.fromfile(self.ticks_path, dtype=np.float64) if os.path.exists(self.ticks_path) else np.zeros(0)+ n = self.journal_rows+ if rows.size > 2 * n: # the steps a crash lost from the journal go from the tick log too+ rows[: 2 * n].tofile(self.ticks_path)+ elif rows.size < 2 * n: # (an older tick log, short of the journal: pad it as unknown)+ np.concatenate([rows, np.full(2 * n - rows.size, np.nan)]).tofile(self.ticks_path)+ self.running = False # a resumed episode waits, paused, for the agent's next call++ def _start_clock(self) -> None:+ if not self.running and not self.done and not self.stopping:+ self.running, self.anchor_t, self.anchor_n = True, time.monotonic(), len(self.episode)+ self.log("clock_start", control_steps=len(self.episode), episode=self.resets + 1)++ def _stop_clock(self, why: str) -> None:+ if self.running:+ self.running = False+ self.log("clock_stop", why=why, control_steps=len(self.episode), max_lag_s=round(self.max_lag, 4))+ self.plan.clear()++ def _end(self, reason: str) -> None:+ super()._end(reason)+ self._stop_clock(reason) # pending watches are completed by whoever ended it, after this++ def due_in(self, now: float) -> float | None:+ """Seconds until the next control step is due (<= 0: due now); None while the clock is stopped."""+ if not self.running or self.done or self.stopping:+ return None+ return self.anchor_t + (len(self.episode) - self.anchor_n) * self.wall_dt - now++ def tick(self) -> None:+ """One control step of the clock: the plan's row for it, or the held target."""+ now = time.monotonic()+ lag = -(self.due_in(now) or 0.0)+ if lag > MAX_LAG_S: # far behind: re-anchor rather than rush the robot through the debt+ self.stalls += 1+ self.log("stall", behind_s=round(lag, 3), control_steps=len(self.episode))+ self.anchor_t, self.anchor_n, lag = now, len(self.episode), 0.0+ self.lag, self.max_lag = max(lag, 0.0), max(self.max_lag, lag)+ if self.plan:+ typ, row, src = self.plan.popleft()+ else:+ (typ, row), src = self.sim.hold(), -1+ self.held_steps += 1+ before = self.journal_rows+ self._ticks.append((time.time(), float(src)))+ try:+ self._step(typ, row)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ if self.journal_rows == before:+ self._ticks.pop()+ self.log("error", path="clock", error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ return+ for w in list(self.watches):+ w.rec.step()+ if self.done or len(self.episode) >= w.end:+ self._complete_watch(w)+ if not self.done and len(self.episode) % self.ckpt_every == 0:+ self._checkpoint("tick")++ # ------------------------------------------------------------------ requests (simulator thread)+ def dispatch(self, job: _Job) -> None:+ """Serve one queued request; a watch finishes later, from tick()."""+ try:+ self.first_request()+ path = urlparse(job.path).path+ if job.method == "GET" and path in ("/spec", "/status"):+ return job.finish("raw", *self._reply_json(200, self.spec if path == "/spec" else self.status()))+ routes = {"/submit": self._rt_submit, "/observe": self._rt_observe}+ if self.reset_budget:+ routes["/reset"] = self._rt_reset+ if job.method != "POST" or path not in routes:+ return job.finish("raw", *self._reply_json(404, {"error": "no such endpoint"}))+ if (len(job.body) if job.content_length is None else job.content_length) > MAX_BODY:+ return job.finish("raw", *self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"}))+ try:+ meta, parts = self._decode(job.body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return job.finish("raw", *self._reply_json(400, {"error": f"malformed body: {exc}"[:300]}))+ try:+ routes[path](meta, parts, job)+ except Refused as exc: # 400 (bad_request), 409: nothing changed+ job.finish("raw", *self._reply_json(exc.code, exc.body))+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ self._release_watches()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]}))+ except Exception as exc: # noqa: BLE001 -- never leave an HTTP thread waiting forever+ traceback.print_exc()+ if not job.done.is_set():+ job.finish("raw", *self._reply_json(500, {"error": "service error", "detail": repr(exc)[:200]}))++ def _view_rt(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ """2.1's _view, plus cameras = [] for no images."""+ none = isinstance(meta.get("cameras"), list) and not meta["cameras"]+ cams, fps, video, depth = self._view(meta, default_video)+ return ([], fps, "none", False) if none else (cams, fps, video, depth)++ def _planned(self, step: int, nxt: int, typ: str) -> np.ndarray:+ """The target in force at control step `step` (>= nxt - 1) as things stand: the plan's row for it, else the+ plan's last row (held), else -- nothing planned, or a step already run -- the current command."""+ i = step - nxt+ if i < 0 or not self.plan:+ return np.asarray(self.sim.command(typ), dtype=np.float64)+ return np.asarray(self.plan[min(i, len(self.plan) - 1)][1], dtype=np.float64)++ def _rt_submit(self, meta: dict, parts: dict, job: _Job) -> None:+ if self.done or self.stopping:+ raise self._conflict()+ nxt = len(self.episode)+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"),+ dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: "+ f"more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ at = meta.get("at")+ if at is not None:+ if isinstance(at, bool) or not isinstance(at, (int, float)) or at != int(at) or at < 0:+ raise ValueError("at must be a control step of this episode (an integer >= 0), "+ "or null for the next one")+ at = int(at)+ if at - nxt > self.max_ahead:+ raise ValueError(f"at = {at} is more than max_ahead_steps = {self.max_ahead} control steps ahead "+ f"of the next one ({nxt})")+ at = nxt if at is None else at+ cams, fps, video, depth = self._view_rt(meta, "none")+ rows, clipped = self.sim.clip(typ, A)+ prev = self._planned(at - 1, nxt, typ) # linear starts from the target in force just before `at`+ steps = []+ for row in rows:+ for j in range(1, k + 1):+ steps.append(row if interp == "hold" else prev + (row - prev) * (j / k))+ prev = row+ late = max(0, nxt - at)+ keep = at - nxt if at > nxt else 0+ self.calls += 1+ self.rows += len(A)+ head = list(self.plan)[:keep]+ if len(head) < keep: # a gap before `at`: the target in force then is held through it+ fill = self._planned(at - 1, nxt, typ)+ head += [(typ, fill, -1)] * (keep - len(head))+ self.plan = collections.deque(head + [(typ, s, self.calls) for s in steps[late:]])+ self.late_steps += min(late, len(steps))+ self._start_clock()+ rec = Recorder(self.sim, cams, fps, depth, "none")+ rec.start()+ rec.finish()+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "n_rows": int(len(A)), "dt": dt,+ "interpolate": interp, "at": at, "start_step": max(at, nxt), "late_steps": late,+ "plan_end_step": nxt + len(self.plan), "clipped": clipped[:200], "n_clipped": len(clipped), "ok": True}+ self._save_call({**out, "note": str(meta.get("note") or "")[:200]}, A)+ job.finish("rec", out, rec)++ def _rt_observe(self, meta: dict, parts: dict, job: _Job) -> None:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view_rt(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ self.calls += 1+ rec.start()+ if not n:+ rec.finish()+ out = {**self.meta(), "obs_id": self.calls, "seconds": 0.0, "control_steps_watch": 0, "ok": True}+ self._save_call(out, None)+ return job.finish("rec", out, rec)+ self._start_clock()+ self.watches.append(_Watch(job, rec, len(self.episode), n, cams, self.calls))++ def _complete_watch(self, w: _Watch) -> None:+ self.watches.remove(w)+ w.rec.finish()+ ran = max(0, min(len(self.episode), w.end) - w.start)+ out = {**self.meta(), "obs_id": w.obs_id, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._save_call(out, None)+ w.job.finish("rec", out, w.rec)++ def _release_watches(self) -> None:+ for w in list(self.watches):+ self._complete_watch(w)++ def _rt_reset(self, meta: dict, parts: dict, job: _Job) -> None:+ self._release_watches()+ out, prts = self._reset(meta, parts) # 2.1's reset: checks, abandons the episode, a new one (paused)+ self._stop_clock("reset")+ job.finish("rcb1", {**out, **self._realtime_counters()}, prts)++ # ------------------------------------------------------------------ the loop (simulator thread) and HTTP+ def poll(self) -> None:+ """After SIGUSR1: end the episode, answer the pending watches, finalize (Service.poll, plus the watches)."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._release_watches()+ self._finalize()++ def loop(self, until: threading.Event | None = None) -> None:+ """The simulator thread: control steps when due, requests in between, the wrap-up after SIGUSR1."""+ while until is None or not until.is_set():+ self.poll()+ wait = self.due_in(time.monotonic())+ if wait is not None and wait <= 0:+ self.tick()+ continue+ try:+ job = self.q.get(timeout=0.25 if wait is None else min(wait, 0.25))+ except queue.Empty:+ continue+ self.dispatch(job)++ def submit_job(self, job: _Job) -> tuple[int, str, bytes]:+ """From an HTTP thread: queue the request, wait for the simulator thread, encode the answer here."""+ self.q.put(job)+ job.done.wait()+ kind, *rest = job.reply+ if kind == "raw":+ return tuple(rest)+ meta, payload = rest # a Recorder's parts are encoded here, off the simulator's thread+ return self._reply_rcb1(meta, payload.parts() if kind == "rec" else payload)++ def http_server(self, address: tuple[str, int]) -> ThreadingHTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.submit_job(_Job("GET", self.path, b"")))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.submit_job(_Job("POST", self.path, b"", content_length=n)))+ self._send(*svc.submit_job(_Job("POST", self.path, self.rfile.read(n))))++ class Server(ThreadingHTTPServer):+ daemon_threads = True++ return Server(address, Handler)++ def serve(self, port: int = 8000) -> None:+ import signal+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ threading.Thread(target=srv.serve_forever, name="http", daemon=True).start()+ print(f"{rcb_service.PROTOCOL} sim service (realtime, slowdown {self.slowdown:g}) ready on :{port}", flush=True)+ self.loop()+++def main(build, port: int = 8000) -> None:+ """rcb_service.main -- the start count, back-off, startup watchdog, resume -- with this service in place of+ Service (RCB_SLOWDOWN: 1 = real time), and eai-standard/2.3 in everything it logs and writes."""+ rcb_service.Service = RealtimeService+ rcb_service.PROTOCOL = PROTOCOL+ rcb_service.main(build, port)+++if __name__ == "__main__":+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+ import server # noqa: E402 -- the HumanoidBench adapter; picks the GL backend and restores the reward on import++ main(server.build)diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_rewards.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_rewards.pynew file mode 100644index 0000000000..7b3cfa3d28--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_rewards.py@@ -0,0 +1,103 @@+"""Put this task's scoring formula back, where the scoring happens: the privileged verifier (tests/) and the+standard-mode sim sidecar (environment/sim/).++This file and the rewards.json beside it are the only copies of the formula a task carries, and the agent can+read neither place: the privileged verifier runs in its own container once the agent's is gone, and the agent+cannot open the sidecar. The image itself has the formula deleted; see images/humanoidbench/scrub_oracles.py.++Patching it back, rather than keeping a private reimplementation, is what keeps our scores comparable with+the paper and with FastTD3: the verifier runs upstream's exact expression, byte for byte as it was lifted+by scripts/humanoidbench/extract_rewards.py.++Call restore() BEFORE opening a Session -- Session decides whether it can score by looking at the class.+"""+from __future__ import annotations++import importlib+import json+from pathlib import Path+++def _reinstate(cls, attr: str, values: dict) -> None:+ """Merge scoring weights back into an attribute the constructor builds.++ `Push.__init__` sets `self.reward_dict = {...}`; the image keeps only the keys that non-scoring code+ reads (`terminate`, which `get_terminated` uses) and the weights live here. They are per-instance, so+ they go back through the constructor -- restore() runs before any Session exists, by contract. (A frozen+ instance never carries them: rcb_hb leaves reward-shaped attributes out of its snapshot.)+ """+ original = cls.__init__++ def __init__(self, *args, **kwargs):+ original(self, *args, **kwargs)+ if hasattr(self, attr): # push returns early when env is None, before assigning+ getattr(self, attr).update(values)++ cls.__init__ = __init__+++def restore(spec: dict | str | Path = None) -> list[str]:+ """Put back everything the image had removed for this task. Returns what was restored.++ `spec` is one task's entry -- a path to the tests/rewards.json beside this file (the default), or the+ entry itself, which is how gen_tasks.py feeds it one family out of the full table. Three kinds:++ module_globals constants the scrub found to be write-only once the formula was gone, e.g.+ `_STAND_HEIGHT = 1.65`. They go back FIRST, because the methods close over them.+ methods the reward functions themselves, and the helpers the scrub deleted or cut down+ with them (truck's `upon_table`, maze's `update_move_direction`) as upstream wrote them.+ params scoring weights the constructor assigns (push only) -- see _reinstate.++ Everything is compiled against the *defining module's own namespace*, so it closes over exactly what+ it closed over upstream: numpy, dm_control's `rewards`, and those constants. That is why this cannot+ be a plain import -- the function has to end up with the original __globals__.+ """+ if not isinstance(spec, dict):+ spec = json.loads(Path(spec or Path(__file__).with_name("rewards.json")).read_text())+ done = []+ for name, source in spec.get("module_globals", {}).items():+ module = importlib.import_module(name)+ exec(compile(source, f"<rewards:{name} constants>", "exec"), vars(module))+ done.append(f"{name} constants")+ for m in spec["methods"]:+ module = importlib.import_module(m["module"])+ namespace = vars(module)+ exec(compile(m["source"], f"<rewards:{m['cls']}.{m['name']}>", "exec"), namespace)+ setattr(getattr(module, m["cls"]), m["name"], namespace[m["name"]])+ done.append(f"{m['module']}.{m['cls']}.{m['name']}")+ for name, entries in spec.get("params", {}).items():+ module = importlib.import_module(name)+ for p in entries:+ _reinstate(getattr(module, p["cls"]), p["attr"], p["values"])+ done.append(f"{name}.{p['cls']}.{p['attr']}")+ if not done:+ raise RuntimeError("restored nothing -- the verifier cannot score")+ return done+++# How far a multi-stage task got: upstream's own bookkeeping, which the scoring code advances (in the agent's container,+# rcb_hb's _unscored_bookkeeping advances maze_stage; the rest stays frozen there). Read after a replay, for the+# diagnostics; it never enters the verdict.+PROGRESS = ("current_subtask", "task_index", "maze_stage", "stage", "tasks_to_complete", "packages_on_table")+++def progress(task) -> dict:+ """{attribute: value} for the PROGRESS attributes `task` (a Session's `low.task`) has: a number or a name as+ it is, a list as its length (packages on the table), a dict as its sorted keys (kitchen's chores left)."""+ out = {}+ for k in PROGRESS:+ v = getattr(task, k, None)+ if isinstance(v, dict):+ out[k] = sorted(map(str, v))+ elif isinstance(v, (list, tuple)):+ out[k] = len(v)+ elif isinstance(v, str):+ out[k] = v+ elif v is not None:+ out[k] = int(v)+ return out+++if __name__ == "__main__":+ for name in restore():+ print(f"restored {name}")diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_service.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_service.pynew file mode 100644index 0000000000..14c53a477b--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rcb_service.py@@ -0,0 +1,759 @@+"""eai-standard/2.1 sim service: the simulator-agnostic half of a standard-mode sidecar (docs/PROTOCOL.md §4,+docs/LIMITED_MODE_PROTOCOL.md).++The robot as a service: the agent reaches a simulator it cannot open over HTTP. GET /spec, GET /status, POST /observe+and POST /submit; POST /reset exists only when the run grants resets (budget.reset > 0); everything else is 404.+Responses that carry data are RCB1 frames (rcb1.py), never base64. Everything simulator-specific comes in through an+adapter (the `Sim` contract below), so this file imports no simulator: a sidecar is its benchmark's adapter plus this.++One thread does all of it -- the HTTP requests, the SIGUSR1 wrap-up, the finalize -- because a simulator's GL context+belongs to the thread that made it (MuJoCo renders black frames from any other).++The episode starts when the service is ready. It ends on success, by the task's own rule ("failed": the robot fell,+the episode's control steps ran out), on a simulator error ("error"), or at SIGUSR1 ("timeout": the collect hook, once+the agent's container is gone). With a reset budget, POST /reset abandons the running episode and starts another from+the frozen instance; only the last episode is graded, and a success ends the trial.++A request is checked in full before anything runs: whatever is wrong with it is a 400 and nothing ran. Once it runs,+any failure is the simulator's: a 500, and the episode ends ("error").++Evidence under DATA, never visible to the agent:+ episode.jsonl one line per event; every state the service may resume from is a checkpoint (it carries+ state_sha and journal_rows)+ trajectory_live.f64 the journal: every executed control step in the simulator's own action space, float64+ calls/NNNNNN/ each call's response meta and the actions as sent+ startups.jsonl one line per process start+ final.json kept current (written atomically); "finalized": true once the verdict is in+ trajectory.npz, replay.mp4, replay.last.png the graded episode and its fresh replay++Crash recovery: compose restarts a crashed service (restart: on-failure). It replays the journal up to the last+checkpoint, checks the state hash, and continues the SAME episode -- never a reset. A call in flight is lost as a+whole (its caller got an error, not a result, so nothing was learned). More than RCB_MAX_RESUMES restarts since the+episode began (every process start counts, also one that died building the scene or replaying the journal) end the+trial: sim_crashed_on_call if every crash happened while serving a call (the agent's failure), else sim_crashed+(infrastructure, infra_error = 1). A verdict already on disk is never overwritten; a crash while rendering the video+loses only the video.+"""+from __future__ import annotations++import contextlib+import errno+import faulthandler+import json+import os+import signal+import tempfile+import time+import traceback+from http.server import BaseHTTPRequestHandler, HTTPServer+from typing import Callable+from urllib.parse import urlparse++import numpy as np++import rcb1++PROTOCOL = "eai-standard/2.1"+MAX_BODY = 64 * 1024 * 1024 # request bodies above this are refused (413)+MAX_RESPONSE = 256 * 1024 * 1024 # responses above this drop frames/ and depth/ (meta.degraded says so)+MAX_VIDEO_FPS = 30+MAX_FRAMES_PER_CALL = 3000 # images in one response (cameras x frames, depth counted again): bounds memory+VIDEOS = ("mp4", "frames", "both", "none")+ON_SERVICE_ERROR = ("if a call fails with a connection error or a 5xx, the service may have crashed and resumed the "+ "same episode (never a reset): the state is the one after the last call that completed on the "+ "service, which you may not have received; check GET /status before continuing")+++class Sim:+ """The adapter contract, duck-typed (this class only documents it). One object = one frozen scene.++ Rows come in three forms: what the agent sends (an action of one of spec()["action_types"]), the same after+ clip(), and a "sim row" (to_sim()), the action in the simulator's own space, which step() executes and the+ journal and the graded trajectory record (raw_dim floats)."""+ control_dt: float # seconds per control step; a chunk's dt is a whole multiple of it+ max_control_steps: int # the task's episode length: the episode ends there+ cameras: tuple[str, ...] # every camera the agent may ask for+ depth_cameras: tuple[str, ...] # the ones that also give depth+ proprio_layout: list[str] # the proprio row's columns+ raw_dim: int # a sim row's width++ def spec(self) -> dict: ... # simulator, robot, task_text, episode, action_types, cameras, robot facts+ def interpolable(self, type: str) -> bool: ... # may a row of this type span several control steps?+ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]: ... # ({cam: uint8 HxWx3}, {cam: float32 HxW m})+ def proprio(self) -> np.ndarray: ... # float32, one value per proprio_layout column+ def clip(self, type: str, rows: np.ndarray) -> tuple[np.ndarray, list]: ... # into the limits; [{row, index, reason}]+ def to_sim(self, type: str, row: np.ndarray) -> np.ndarray: ... # an agent row -> a sim row+ def command(self, type: str) -> np.ndarray: ... # the last row executed, exactly as clip() returned it: a chunk+ # interpolates from it, so a chunk equals its rows sent one by one+ def hold(self) -> tuple[str, np.ndarray]: ... # the row that holds the last command (/observe with seconds > 0)+ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]: ... # one control step -> (done, success, info)+ def reset(self) -> None: ... # back to the frozen instance+ def state_sha(self) -> str: ...+ def verify(self, rows: np.ndarray) -> dict: ... # replay in a fresh simulator -> {"success", "state_sha"}+ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None: ...+ def render_video(self, rows: np.ndarray, path: str) -> None: ... # path and path.last.png+++class GiveUp(Exception):+ """The service will not run this trial's episode again; final.json says why."""+++class Refused(Exception):+ """A request the service turns down with `code` and the JSON `body`; nothing was executed."""++ def __init__(self, code: int, body: dict):+ super().__init__(body.get("error"))+ self.code, self.body = code, body+++def bad_request():+ """Checking a request, before anything runs: whatever goes wrong here is the request's fault (400)."""+ try:+ yield+ except Refused:+ raise+ except Exception as exc: # noqa: BLE001 -- a wrong type, value or shape of any field+ raise Refused(400, {"error": str(exc)[:300] or type(exc).__name__}) from None+++def count_start(data: str) -> int:+ """Record one process start in startups.jsonl; returns how many there have been."""+ path = os.path.join(data, "startups.jsonl")+ with open(path, "a") as f:+ f.write(json.dumps({"ts": time.time(), "pid": os.getpid()}) + "\n")+ with open(path) as f:+ return sum(1 for _ in f)+++def write_json_atomic(path: str, obj: dict) -> None:+ """Write then rename: the collect hook polling the file never reads half of it."""+ fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), suffix=".tmp")+ with os.fdopen(fd, "w") as f:+ json.dump(obj, f)+ os.replace(tmp, path)+++def encode_mp4(frames: np.ndarray, fps: float) -> bytes:+ import imageio.v2 as imageio+ fd, path = tempfile.mkstemp(suffix=".mp4")+ os.close(fd)+ try:+ with imageio.get_writer(path, format="FFMPEG", mode="I", fps=fps, codec="libx264", macro_block_size=1,+ output_params=["-movflags", "+faststart", "-pix_fmt", "yuv420p"]) as w:+ for f in frames:+ w.append_data(np.ascontiguousarray(f))+ with open(path, "rb") as f:+ return f.read()+ finally:+ os.unlink(path)+++class Recorder:+ """What one call returns: a proprio row at its start, every `every` control steps (the requested video_fps, rounded to+ whole control steps) and at its end; with them the cameras' frames when a video or the frames are asked for, else+ only the last frame (rendering is most of a call's cost)."""++ def __init__(self, sim, cameras: list[str], fps: int, depth: bool, video: str):+ self.sim, self.cams, self.depth, self.video = sim, cameras, depth, video+ self.every = max(1, round(1.0 / (sim.control_dt * fps)))+ self.fps = 1.0 / (self.every * sim.control_dt)+ self.images = video != "none"+ self.frames = {c: [] for c in cameras}+ self.dep = {c: [] for c in cameras}+ self.prop: list[np.ndarray] = []+ self.k, self.grabbed_at = 0, None++ def images_per_call(self, steps: int) -> int:+ per_frame = len(self.cams) + (len(set(self.cams) & set(self.sim.depth_cameras)) if self.depth else 0)+ return (steps // self.every + 2 if self.images else 1) * per_frame++ def start(self) -> None:+ self._grab(self.images)++ def step(self) -> None:+ self.k += 1+ if self.k % self.every == 0:+ self._grab(self.images)++ def finish(self) -> None:+ if self.grabbed_at != self.k:+ self._grab(self.images)+ if not self.images:+ self._grab(True, proprio=False) # the one frame a call without video returns: the last++ def _grab(self, images: bool, proprio: bool = True) -> None:+ if images:+ rgb, dep = self.sim.images(self.cams, self.depth)+ for c in self.cams:+ self.frames[c].append(rgb[c])+ if c in dep:+ self.dep[c].append(dep[c])+ if proprio:+ self.prop.append(np.asarray(self.sim.proprio(), np.float32))+ self.grabbed_at = self.k++ def parts(self) -> list:+ out = []+ for c in self.cams:+ fr = np.stack(self.frames[c])+ if self.video in ("mp4", "both"):+ out.append((f"video/{c}", "mp4", encode_mp4(fr, self.fps), {"fps": round(self.fps, 3), "n": int(len(fr))}))+ if self.video in ("frames", "both"):+ out.append(rcb1.array_part(f"frames/{c}", fr))+ out.append(rcb1.array_part(f"last/{c}", fr[-1]))+ if self.dep[c]:+ out.append(rcb1.array_part(f"depth/{c}", np.stack(self.dep[c]).astype(np.float16)))+ out.append(rcb1.array_part("proprio", np.stack(self.prop)))+ return out+++class Service:+ """Episode bookkeeping, evidence and the HTTP endpoints around one `Sim`."""++ def __init__(self, sim, *, data: str = "/data", reset_budget: int = 0, wall_clock_s: float = 3600.0,+ max_chunk_s: float = 10.0, max_watch_s: float = 30.0, max_frames_per_call: int = MAX_FRAMES_PER_CALL,+ max_resumes: int = 2):+ self.sim, self.data = sim, data+ self.reset_budget, self.wall_clock_s = int(reset_budget), float(wall_clock_s)+ self.max_chunk_s, self.max_watch_s = float(max_chunk_s), float(max_watch_s)+ self.max_frames, self.max_resumes = int(max_frames_per_call), int(max_resumes)+ os.makedirs(os.path.join(data, "calls"), exist_ok=True)+ self.journal_path = os.path.join(data, "trajectory_live.f64")+ self.spec = self._spec()+ self.types = {k: v for k, v in self.spec["action_types"].items() if isinstance(v, dict)}+ self.calls = self.rows = self.resets = 0+ self.journal_rows = 0 # control steps executed over the whole trial (the journal's length)+ self.ep_start = 0 # where the current episode begins in the journal+ self.episode: list[np.ndarray] = [] # the current episode's sim rows: what finalize grades+ self.done, self.reason, self.success = False, None, False+ self.stopping = False # SIGUSR1 seen: the agent is gone+ self.finalized, self.verdict = False, {}+ self.t0 = self.t_end = None # the clock starts at the agent's first request+ self.starts = 1+ self._unwritten: list[np.ndarray] = [] # sim rows executed since the last checkpoint++ # ------------------------------------------------------------------ spec and status+ def _spec(self) -> dict:+ own = self.sim.spec()+ core = {"protocol": PROTOCOL, "control_dt": self.sim.control_dt, "max_control_steps": self.sim.max_control_steps,+ "max_chunk_s": self.max_chunk_s, "max_watch_s": self.max_watch_s, "max_video_fps": MAX_VIDEO_FPS,+ "max_frames_per_call": self.max_frames, "proprio_layout": list(self.sim.proprio_layout),+ "budget": {"reset": self.reset_budget, "wall_clock_s": self.wall_clock_s},+ "clock": "time_s and elapsed_s count from your first request to the service; time_left_s is "+ "budget.wall_clock_s minus time_s",+ "on_service_error": ON_SERVICE_ERROR}+ if clash := sorted(set(core) & set(own)):+ raise ValueError(f"the adapter's spec() must not set {clash}")+ cams = {c: {"rgb": True, "depth": c in self.sim.depth_cameras, **own["cameras"].get(c, {})}+ for c in self.sim.cameras}+ spec = {**core, **own, "cameras": cams}+ if self.reset_budget:+ spec["reset"] = ("POST /reset abandons the running episode and starts a new one from the same starting "+ "state, budget.reset times in all; only the last episode is graded, and a success ends "+ "the trial")+ return spec++ def clock(self) -> float:+ return 0.0 if self.t0 is None else time.time() - self.t0++ def _counters(self) -> dict:+ out = {"calls": self.calls, "rows": self.rows, "control_steps": len(self.episode), "done": self.done,+ "reason": self.reason}+ if self.reset_budget:+ out.update(episode=self.resets + 1, control_steps_total=self.journal_rows,+ resets_left=0 if self.success or self.stopping else self.reset_budget - self.resets)+ return out++ def status(self) -> dict:+ return {**self._counters(), "elapsed_s": round(self.clock(), 1)}++ def meta(self) -> dict:+ t = self.clock()+ return {**self._counters(), "sim_time_s": round(len(self.episode) * self.sim.control_dt, 4),+ "time_s": round(t, 2), "time_left_s": max(0.0, round(self.wall_clock_s - t, 1)),+ "proprio": {"layout": list(self.sim.proprio_layout),+ "now": np.asarray(self.sim.proprio(), np.float32).tolist()}}++ # ------------------------------------------------------------------ evidence+ def log(self, ev: str, **kw) -> None:+ with open(os.path.join(self.data, "episode.jsonl"), "a") as f:+ f.write(json.dumps({"ts": time.time(), "ev": ev, **kw}) + "\n")++ def _events(self) -> list[dict]:+ try:+ with open(os.path.join(self.data, "episode.jsonl")) as f:+ return [json.loads(line) for line in f if line.strip()]+ except OSError:+ return []++ def _checkpoint(self, ev: str, **kw) -> None:+ """Log a state this service may resume from, after appending everything executed since the last one to the+ journal. Rows of a call that never reaches its checkpoint are lost with it, as the call is."""+ if self._unwritten:+ with open(self.journal_path, "ab") as f:+ f.write(np.asarray(self._unwritten, dtype=np.float64).tobytes())+ self._unwritten = []+ self.log(ev, **kw, calls=self.calls, rows=self.rows, resets=self.resets, ep_start=self.ep_start,+ journal_rows=self.journal_rows, done=self.done, reason=self.reason, success=self.success,+ state_sha=self.sim.state_sha())+ self._write_final()++ def _write_final(self) -> None:+ end = self.t_end or time.time()+ d = {"protocol": PROTOCOL, "success": False, "live_success": self.success, "done": self.done,+ "reason": self.reason, "calls": self.calls, "rows": self.rows, "control_steps": len(self.episode),+ "control_steps_total": self.journal_rows, "episodes": self.resets + 1, "resets": self.resets,+ "reset_budget": self.reset_budget, "elapsed_s": round(end - self.t0, 1) if self.t0 else 0.0,+ "sim_startups": self.starts, "finalized": self.finalized, **self.verdict}+ if "deterministic" in self.verdict:+ d["success"] = bool(self.success and self.verdict["replay_success"] and self.verdict["deterministic"])+ write_json_atomic(os.path.join(self.data, "final.json"), d)++ def _save_call(self, meta: dict, actions: np.ndarray | None) -> None:+ d = os.path.join(self.data, "calls", f"{self.calls:06d}")+ os.makedirs(d, exist_ok=True)+ with open(os.path.join(d, "meta.json"), "w") as f:+ json.dump(meta, f)+ if actions is not None:+ np.save(os.path.join(d, "actions.npy"), actions)++ # ------------------------------------------------------------------ start, resume, give up+ def start(self) -> None:+ """A fresh episode, or -- after a crash -- the same one again. Raises GiveUp when it must not run again."""+ try:+ with open(os.path.join(self.data, "startups.jsonl")) as f:+ self.starts = max(1, sum(1 for _ in f))+ except OSError:+ self.starts = 1+ ev = self._events()+ if not any(e["ev"] == "start" for e in ev):+ open(self.journal_path, "wb").close()+ self._checkpoint("start", protocol=PROTOCOL, spec=self.spec, startup=self.starts)+ else:+ self._resume(ev)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))++ def _resume(self, ev: list[dict]) -> None:+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if fin.get("finalized") or "deterministic" in fin:+ # Graded already. A verdict that is not finalized: the process died rendering the video, all it lost.+ if not fin.get("finalized"):+ write_json_atomic(path, {**fin, "finalized": True, "video": False})+ self._give_up("restarted after finalize", infra=False)+ restarts = self.starts - int(next(e for e in ev if e["ev"] == "start")["startup"])+ last = max(i for i, e in enumerate(ev) if e["ev"] in ("start", "resuming"))+ in_call = False # did the process that died do so while serving a call?+ for e in ev[last:]:+ in_call = {"call_begin": True, "call_end": False}.get(e["ev"], in_call)+ in_calls = sum(bool(e["crashed_in_call"]) for e in ev if e["ev"] == "resuming") + int(in_call)+ if restarts > self.max_resumes:+ agent = in_calls == restarts # every crash happened inside a call: the agent's commands did it+ self._give_up("sim_crashed_on_call" if agent else "sim_crashed", infra=not agent)+ self.log("resuming", crashed_in_call=in_call, restart=restarts)+ cp = [e for e in ev if "state_sha" in e and "journal_rows" in e][-1]+ n, dim = int(cp["journal_rows"]), int(self.sim.raw_dim)+ rows = np.fromfile(self.journal_path, dtype=np.float64) if os.path.exists(self.journal_path) else np.zeros(0)+ if rows.size < n * dim:+ self._give_up("resume_failed: journal shorter than the log", infra=True)+ rows = rows[: n * dim].reshape(n, dim)+ for row in rows[cp["ep_start"]:]:+ self.sim.step(row)+ if self.sim.state_sha() != cp["state_sha"]:+ self._give_up("resume_failed: replayed state differs from the logged one", infra=True)+ rows.tofile(self.journal_path) # drop the steps of the call that was in flight+ self.calls, self.rows, self.resets = int(cp["calls"]), int(cp["rows"]), int(cp["resets"])+ self.ep_start, self.journal_rows = int(cp["ep_start"]), n+ self.episode = [r.copy() for r in rows[self.ep_start:]]+ self.done, self.reason, self.success = bool(cp["done"]), cp["reason"], bool(cp["success"])+ self.t0 = next((e["t0"] for e in ev if e["ev"] == "first_request"), None)+ if self.done:+ self.t_end = next((e["ts"] for e in reversed(ev) if e["ev"] == "end"), cp["ts"])+ self._checkpoint("resumed", downtime_s=round(time.time() - float(ev[-1]["ts"]), 1))++ def _give_up(self, reason: str, infra: bool) -> None:+ """End the trial without running its episode again: mark it failed (a finalized final.json is kept as it is),+ leave the PID file for the collect hook, and stop."""+ self.log("restart_refused", reason=reason)+ path = os.path.join(self.data, "final.json")+ try:+ with open(path) as f:+ fin = json.load(f)+ except (OSError, ValueError):+ fin = {}+ if not fin.get("finalized"):+ fin.update({"protocol": PROTOCOL, "success": False, "done": True, "reason": reason, "finalized": True,+ **({"infra_error": 1} if infra else {})})+ write_json_atomic(path, fin)+ with open(os.path.join(self.data, "server.pid"), "w") as f:+ f.write(str(os.getpid()))+ raise GiveUp(reason)++ # ------------------------------------------------------------------ requests+ def first_request(self) -> None:+ if self.t0 is None:+ self.t0 = time.time()+ self.log("first_request", t0=self.t0)++ def handle(self, method: str, path: str, body: bytes = b"", content_length: int | None = None) -> tuple[int, str, bytes]:+ """One request -> (status, content type, body): the whole HTTP surface, without the HTTP."""+ self.first_request()+ path = urlparse(path).path+ if method == "GET" and path in ("/spec", "/status"):+ return self._reply_json(200, self.spec if path == "/spec" else self.status())+ routes = {"/submit": self._submit, "/observe": self._observe}+ if self.reset_budget:+ routes["/reset"] = self._reset+ if method != "POST" or path not in routes:+ return self._reply_json(404, {"error": "no such endpoint"})+ if (len(body) if content_length is None else content_length) > MAX_BODY:+ return self._reply_json(413, {"error": f"request body over {MAX_BODY} bytes"})+ try:+ meta, parts = self._decode(body)+ except Exception as exc: # noqa: BLE001 -- any malformed body is the caller's error+ return self._reply_json(400, {"error": f"malformed body: {exc}"[:300]})+ self.log("call_begin", path=path, call=self.calls + 1)+ try:+ out, blobs = routes[path](meta, parts)+ except Refused as exc: # 400 (bad_request), 409: nothing ran+ return self._reply_json(exc.code, exc.body)+ except Exception as exc: # noqa: BLE001 -- it ran and failed: the episode ends, as on hardware+ traceback.print_exc()+ self.log("error", path=path, error=repr(exc)[:500])+ self._end("error")+ return self._reply_json(500, {"error": "simulator error", "detail": repr(exc)[:200]})+ finally:+ self.log("call_end", path=path)+ return self._reply_rcb1(out, blobs)++ @staticmethod+ def _decode(body: bytes) -> tuple[dict, dict]:+ if body[:4] == rcb1.MAGIC:+ meta, parts = rcb1.decode(body)+ else:+ meta, parts = json.loads(body or b"{}"), {}+ if not isinstance(meta, dict):+ raise ValueError("the body must be a JSON object or an RCB1 frame")+ return meta, parts++ @staticmethod+ def _reply_json(code: int, obj: dict) -> tuple[int, str, bytes]:+ return code, "application/json", json.dumps(obj).encode()++ @staticmethod+ def _reply_rcb1(meta: dict, parts: list) -> tuple[int, str, bytes]:+ body = rcb1.encode(meta, parts)+ if len(body) > MAX_RESPONSE:+ parts = [p for p in parts if not p[0].startswith(("frames/", "depth/"))]+ meta = {**meta, "degraded": f"response over {MAX_RESPONSE} bytes: frames/ and depth/ dropped"}+ body = rcb1.encode(meta, parts)+ return 200, "application/x-rcb1", body++ def _view(self, meta: dict, default_video: str) -> tuple[list[str], int, str, bool]:+ cams = meta.get("cameras") or list(self.sim.cameras)+ if isinstance(cams, str):+ cams = [c for c in cams.split(",") if c]+ if not isinstance(cams, list) or (unknown := sorted(set(map(str, cams)) - set(self.sim.cameras))):+ raise ValueError(f"unknown camera(s) {unknown if isinstance(cams, list) else cams}; "+ f"the cameras are {list(self.sim.cameras)}")+ try:+ fps = int(meta.get("video_fps", 10))+ except (TypeError, ValueError, OverflowError):+ raise ValueError("video_fps must be an integer") from None+ if not 1 <= fps <= MAX_VIDEO_FPS:+ raise ValueError(f"video_fps must be in [1, {MAX_VIDEO_FPS}]")+ video = meta.get("video", default_video)+ if video not in VIDEOS:+ raise ValueError(f"video must be one of {list(VIDEOS)}")+ depth = bool(meta.get("include_depth", False))+ if depth and not set(cams) & set(self.sim.depth_cameras):+ raise ValueError(f"include_depth: none of {cams} has depth; spec()['cameras'][name]['depth'] says which do")+ return cams, fps, video, depth++ def _frame_budget(self, rec: Recorder, steps: int) -> None:+ n = rec.images_per_call(steps)+ if n > self.max_frames:+ raise ValueError(f"this call would return {n} images (cameras x frames, depth counted again); the limit is "+ f"{self.max_frames}: ask for fewer cameras, a lower video_fps or a shorter chunk")++ def _conflict(self) -> Refused:+ if self.stopping:+ return Refused(409, {"error": "trial finished: your time is up", "reason": self.reason})+ body = {"error": "episode finished", "reason": self.reason}+ if self.reset_budget:+ body["resets_left"] = 0 if self.success else self.reset_budget - self.resets+ return Refused(409, body)++ def _step(self, typ: str, row: np.ndarray) -> dict:+ """One control step: execute, journal, and end the episode if it is over."""+ sim_row = np.asarray(self.sim.to_sim(typ, row), dtype=np.float64)+ done, success, info = self.sim.step(sim_row)+ self._unwritten.append(sim_row)+ self.journal_rows += 1+ self.episode.append(sim_row)+ if success:+ self._end("success")+ elif done or len(self.episode) >= self.sim.max_control_steps:+ self._end("failed")+ return info++ def _end(self, reason: str) -> None:+ if self.done:+ return+ self.done, self.reason, self.success = True, reason, reason == "success"+ self.t_end = time.time()+ try:+ self._checkpoint("end")+ except Exception as exc: # noqa: BLE001 -- a broken simulator may not even hash: the end is still logged+ self.log("end", reason=reason, checkpoint_error=repr(exc)[:300])+ self._write_final()++ def _submit(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.done or self.stopping:+ raise self._conflict()+ with bad_request():+ typ = meta.get("type")+ if not isinstance(typ, str) or typ not in self.types:+ raise ValueError(f"type must be one of {sorted(self.types)}")+ try:+ A = np.asarray(parts["actions"]["data"] if "actions" in parts else meta.get("actions"), dtype=np.float64)+ except (TypeError, ValueError, KeyError):+ raise ValueError("send the actions as an RCB1 array part `actions` or a JSON list of rows") from None+ dim = int(self.types[typ]["dim"])+ if A.ndim != 2 or A.shape[0] < 1 or A.shape[1] != dim:+ raise ValueError(f"{typ} actions must be (T, {dim}) with T >= 1, got {list(A.shape)}")+ if not np.all(np.isfinite(A)):+ raise ValueError("actions contain NaN or inf")+ cdt = self.sim.control_dt+ try:+ dt = cdt if meta.get("dt") is None else float(meta["dt"])+ except (TypeError, ValueError):+ raise ValueError("dt must be a number of seconds") from None+ k = round(dt / cdt) if np.isfinite(dt / cdt) else 0+ if k < 1 or abs(k * cdt - dt) > 1e-9:+ raise ValueError(f"dt must be a positive whole multiple of control_dt = {cdt} s (never rounded)")+ if k > 1 and not self.sim.interpolable(typ):+ raise ValueError(f"{typ}: one row is one control step, so dt must be control_dt = {cdt} s")+ if len(A) * k * cdt > self.max_chunk_s + 1e-9:+ raise ValueError(f"the chunk covers {len(A) * k * cdt:g} s: more than max_chunk_s = {self.max_chunk_s:g}")+ interp = meta.get("interpolate", "linear")+ if interp not in ("linear", "hold"):+ raise ValueError("interpolate must be linear or hold")+ cams, fps, video, depth = self._view(meta, "mp4")+ rec = Recorder(self.sim, cams, fps, depth, video)+ self._frame_budget(rec, len(A) * k)+ rows, clipped = self.sim.clip(typ, A)+ before, info, executed, stop = len(self.episode), {}, 0, False+ prev = np.asarray(self.sim.command(typ), dtype=np.float64)+ rec.start()+ for i, row in enumerate(rows):+ for j in range(1, k + 1):+ info = self._step(typ, row if interp == "hold" else prev + (row - prev) * (j / k))+ rec.step()+ if self.done or self.stopping or info.get("blocked"):+ stop = True+ break+ executed, prev = i + 1, row+ if stop:+ break+ rec.finish()+ self.calls += 1+ self.rows += executed+ out = {**self.meta(), "chunk_id": self.calls, "type": typ, "executed_rows": executed,+ "control_steps_chunk": len(self.episode) - before, "clipped": clipped[:200], "n_clipped": len(clipped),+ **info, "ok": True}+ self._checkpoint("submit", type=typ, n_rows=len(A), dt=dt, interpolate=interp, cameras=cams,+ note=str(meta.get("note") or "")[:500], executed_rows=executed, n_clipped=len(clipped), **info)+ self._save_call(out, A)+ return out, rec.parts()++ def _observe(self, meta: dict, parts: dict) -> tuple[dict, list]:+ with bad_request():+ try:+ seconds = float(meta.get("seconds") or 0.0)+ except (TypeError, ValueError):+ raise ValueError("seconds must be a number") from None+ if not 0.0 <= seconds <= self.max_watch_s:+ raise ValueError(f"seconds must be in [0, {self.max_watch_s:g}]")+ cams, fps, video, depth = self._view(meta, "frames")+ n = int(round(seconds / self.sim.control_dt)) if not (self.done or self.stopping) else 0+ rec = Recorder(self.sim, cams, fps, depth, video if n or video != "mp4" else "frames") # no time: no video+ self._frame_budget(rec, n)+ before = len(self.episode)+ rec.start()+ if n:+ typ, row = self.sim.hold()+ for _ in range(n):+ self._step(typ, row)+ rec.step()+ if self.done or self.stopping:+ break+ rec.finish()+ self.calls += 1+ ran = len(self.episode) - before+ out = {**self.meta(), "obs_id": self.calls, "seconds": round(ran * self.sim.control_dt, 4),+ "control_steps_watch": ran, "ok": True}+ self._checkpoint("observe", seconds=out["seconds"], cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ def _reset(self, meta: dict, parts: dict) -> tuple[dict, list]:+ if self.success or self.stopping:+ raise Refused(409, {"error": "trial finished: " + ("an episode succeeded" if self.success else "your time is up"),+ "reason": self.reason})+ if self.resets >= self.reset_budget:+ raise Refused(409, {"error": "reset budget exhausted", "budget": self.reset_budget})+ with bad_request():+ cams, fps, video, depth = self._view(meta, "frames")+ if not self.done:+ self._end("reset") # the running episode is abandoned+ self.sim.reset()+ self.resets += 1+ self.ep_start, self.episode = self.journal_rows, []+ self.done, self.reason, self.success, self.t_end = False, None, False, None+ rec = Recorder(self.sim, cams, fps, depth, "frames" if video == "mp4" else video) # no time: no video+ rec.start()+ rec.finish()+ self.calls += 1+ out = {**self.meta(), "ok": True}+ self._checkpoint("reset", cameras=cams)+ self._save_call(out, None)+ return out, rec.parts()++ # ------------------------------------------------------------------ the end of the trial+ def on_sigusr1(self, *_) -> None:+ """The agent is gone (the [[verifier.collect]] hook). Only a flag: the handler may fire in the middle of a+ request; a chunk stops at its next control step and poll() wraps up between requests."""+ self.stopping = True++ def poll(self) -> None:+ """Between requests (the HTTP loop calls it every 0.5 s): after SIGUSR1, end the episode and finalize."""+ if self.stopping and not self.finalized:+ if not self.done:+ self._end("timeout")+ self._finalize()++ def _finalize(self) -> None:+ """The graded episode (every control step since the last reset) as a trajectory, its replay in a fresh+ simulator (success, and bit-exact agreement with the live run), and a video for humans."""+ try:+ rows = np.asarray(self.episode, dtype=np.float64).reshape(-1, self.sim.raw_dim)+ live_sha = self.sim.state_sha()+ self.sim.save_trajectory(rows, os.path.join(self.data, "trajectory.npz"),+ {"source": "standard-mode episode", "protocol": PROTOCOL, "reason": self.reason,+ "episode": self.resets + 1})+ rep = self.sim.verify(rows)+ self.verdict = {"n_actions": int(len(rows)), "replay_success": bool(rep["success"]),+ "deterministic": rep["state_sha"] == live_sha, "state_sha": live_sha, "video": False}+ self.log("replay", **self.verdict)+ self._write_final() # the verdict is on disk before any video is rendered+ try:+ self.sim.render_video(rows, os.path.join(self.data, "replay.mp4"))+ self.verdict["video"] = True+ except Exception: # noqa: BLE001 -- the video is for humans; it never decides the verdict+ traceback.print_exc()+ except Exception as exc: # noqa: BLE001+ traceback.print_exc()+ self.log("finalize_error", error=repr(exc)[:500])+ self.verdict = {"finalize_error": repr(exc)[:300]}+ self.finalized = True+ self._write_final()++ # ------------------------------------------------------------------ HTTP+ def http_server(self, address: tuple[str, int]) -> HTTPServer:+ svc = self++ class Handler(BaseHTTPRequestHandler):+ timeout = 120 # a client that stalls mid-request is dropped: one thread serves everyone++ def log_message(self, *a):+ pass++ def _send(self, code: int, ctype: str, body: bytes) -> None:+ self.send_response(code)+ self.send_header("Content-Type", ctype)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def do_GET(self):+ self._send(*svc.handle("GET", self.path))++ def do_POST(self):+ try:+ n = int(self.headers.get("Content-Length") or 0)+ except ValueError:+ return self._send(*svc._reply_json(400, {"error": "bad Content-Length"}))+ if n > MAX_BODY: # refused unread: drop the connection with it+ self.close_connection = True+ return self._send(*svc.handle("POST", self.path, b"", content_length=n))+ self._send(*svc.handle("POST", self.path, self.rfile.read(n)))++ class Server(HTTPServer):+ def service_actions(self):+ svc.poll()++ return Server(address, Handler)++ def bind(self, port: int) -> HTTPServer:+ """The HTTP server on `port`. Something else may hold the port -- in the network namespace it shares with the+ agent's container, an agent's process can take it while this service (re)starts -- so wait, not die (a death+ here would count as a crash); the trial can still end meanwhile (SIGUSR1)."""+ busy = False+ while True:+ try:+ return self.http_server(("0.0.0.0", port))+ except OSError as exc:+ if exc.errno != errno.EADDRINUSE:+ raise+ if not busy:+ busy = True+ self.log("port_busy", port=port)+ self.poll()+ time.sleep(1.0)++ def serve(self, port: int = 8000) -> None:+ signal.signal(signal.SIGUSR1, self.on_sigusr1)+ srv = self.bind(port)+ print(f"{PROTOCOL} sim service ready on :{port}", flush=True)+ srv.serve_forever()+++def main(build: Callable[[], Sim], port: int = 8000) -> None:+ """A sidecar's life: count the start, back off after a failed one, build the scene under a startup watchdog, resume+ or start the episode, serve. Settings: RCB_DATA (/data), RCB_RESET_BUDGET (0), RCB_WALL_CLOCK_S (3600),+ RCB_STARTUP_TIMEOUT_S (300), RCB_MAX_RESUMES (2)."""+ env = os.environ.get+ data = env("RCB_DATA", "/data")+ os.makedirs(data, exist_ok=True)+ starts = count_start(data)+ # Compose restarts a failed start at once; a scene that failed to build (e.g. the GPU full of other trials) would+ # meet the same conditions every time, so wait before rebuilding -- only before the episode exists.+ if starts > 1 and not os.path.exists(os.path.join(data, "episode.jsonl")):+ time.sleep(min(120, 20 * (starts - 1)))+ pending: list[int] = []+ signal.signal(signal.SIGUSR1, lambda *_: pending.append(1)) # its default action would kill the process+ # A scene build that hangs (no crash, so compose never restarts it) must end the process. Neither a Python thread+ # (a native hang may hold the GIL) nor SIGALRM's default action (PID 1 ignores it) can; faulthandler's C watchdog+ # dumps every thread's traceback and exits. Before the scene exists no episode exists: the retry is no reset.+ faulthandler.dump_traceback_later(float(env("RCB_STARTUP_TIMEOUT_S", "300")), exit=True)+ sim = build()+ faulthandler.cancel_dump_traceback_later() # a resume replay may take minutes+ svc = Service(sim, data=data, reset_budget=int(env("RCB_RESET_BUDGET") or 0),+ wall_clock_s=float(env("RCB_WALL_CLOCK_S") or 3600), max_resumes=int(env("RCB_MAX_RESUMES") or 2))+ try:+ svc.start()+ except GiveUp as exc:+ print(f"restart refused: {exc}", flush=True)+ signal.signal(signal.SIGUSR1, lambda *_: None)+ while True:+ time.sleep(3600)+ if pending:+ svc.on_sigusr1()+ svc.serve(port)diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rewards.json b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rewards.jsonnew file mode 100644index 0000000000..d1f89db1cc--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/rewards.json@@ -0,0 +1,14 @@+{+ "methods": [+ {+ "cls": "Window",+ "module": "humanoid_bench.envs.window",+ "name": "get_reward",+ "source": "def get_reward(self):\n self.window_pane_id = self._env.named.data.geom_xpos.axes.row.names.index(\n \"window_pane_collision\"\n )\n\n self.window_wipe_id = self._env.named.data.geom_xpos.axes.row.names.index(\n \"window_wipe_collision\"\n )\n\n standing = rewards.tolerance(\n self.robot.head_height(),\n bounds=(_STAND_HEIGHT, float(\"inf\")),\n margin=_STAND_HEIGHT / 4,\n )\n upright = rewards.tolerance(\n self.robot.torso_upright(),\n bounds=(0.9, float(\"inf\")),\n sigmoid=\"linear\",\n margin=1.9,\n value_at_margin=0,\n )\n stand_reward = standing * upright\n small_control = rewards.tolerance(\n self.robot.actuator_forces(),\n margin=10,\n value_at_margin=0,\n sigmoid=\"quadratic\",\n ).mean()\n small_control = (4 + small_control) / 5\n\n window_contact_reward = np.min(\n [\n rewards.tolerance(\n self._env.named.data.site_xpos[site_name, \"x\"],\n bounds=(0.92, 0.92),\n margin=0.4,\n sigmoid=\"linear\",\n )\n for site_name in [\n \"wipe_contact_site_a\",\n \"wipe_contact_site_b\",\n \"wipe_contact_site_c\",\n \"wipe_contact_site_d\",\n \"wipe_contact_site_e\",\n ]\n ]\n )\n window_contact_filter = 0\n for pair in self._env.data.contact.geom:\n if (\n self.window_pane_id in pair and self.window_wipe_id in pair\n ): # if has hand\n window_contact_filter = 1\n break\n\n left_hand_tool_distance = np.linalg.norm(\n self._env.named.data.site_xpos[\"left_hand\"]\n - self._env.named.data.xpos[\"window_wiping_tool\"]\n )\n right_hand_tool_distance = np.linalg.norm(\n self._env.named.data.site_xpos[\"right_hand\"]\n - self._env.named.data.xpos[\"window_wiping_tool\"]\n )\n hand_tool_proximity_reward = min(\n [\n rewards.tolerance(left_hand_tool_distance, bounds=(0, 0.2), margin=0.5),\n rewards.tolerance(\n right_hand_tool_distance, bounds=(0, 0.2), margin=0.5\n ),\n ]\n )\n\n moving_wipe_reward = rewards.tolerance(\n abs(self._env.named.data.sensordata[\"window_wiping_tool_subtreelinvel\"][2]),\n bounds=(0.5, 0.5),\n margin=0.5,\n )\n\n head_window_distance_reward = rewards.tolerance(\n np.linalg.norm(self._env.named.data.site_xpos[\"head\"] - self.head_pos0),\n bounds=(0.4, 0.4),\n margin=0.1,\n )\n\n manipulation_reward = (\n 0.2 * (stand_reward * small_control * head_window_distance_reward)\n + 0.4 * moving_wipe_reward\n + 0.4 * hand_tool_proximity_reward\n )\n window_contact_total_reward = window_contact_filter * window_contact_reward\n reward = 0.5 * manipulation_reward + 0.5 * window_contact_total_reward\n\n return reward, {\n \"stand_reward\": stand_reward,\n \"small_control\": small_control,\n \"moving_wipe_reward\": moving_wipe_reward,\n \"hand_tool_proximity_reward\": hand_tool_proximity_reward,\n \"window_contact_reward\": window_contact_reward,\n \"window_contact_filter\": window_contact_filter,\n \"window_contact_total_reward\": window_contact_total_reward,\n }"+ }+ ],+ "module_globals": {+ "humanoid_bench.envs.window": "_STAND_HEIGHT = 1.65"+ },+ "task_class": "humanoid_bench.envs.window.Window"+}diff --git a/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/server.py b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/server.pynew file mode 100644index 0000000000..f0d02d2798--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/environment/sim/server.py@@ -0,0 +1,290 @@+"""eai-standard/2.1 sim service for HumanoidBench -- the sidecar's entry point (`python /srv/server.py`).++The simulator-specific half only: `HumanoidBench` implements rcb_service.Sim for one frozen instance. The protocol,+the episode, crash recovery and the finalize live in rcb_service.py, which imports no simulator.+Settings come from environment/docker-compose.yaml:+ RCB_TASK, RCB_INSTANCE which HumanoidBench task, which frozen scene (the privileged twin's instance)+ RCB_TASK_TEXT one sentence for /spec+ RCB_EPISODE what ends an episode, for /spec (the instruction's words)+ RCB_OBS_PROPRIO 1 (standard): + joint angles and velocities; 0: images only+ RCB_OBS_IMU 1 (standard): + a pelvis IMU (angular velocity, gravity direction)+ RCB_OBS_ROOM_CAMERA 1 (standard): + `room_camera`, a camera fixed in the room+ RCB_OBS_DEPTH 0 (standard); 1: + depth from both head cameras, as from an RGB-D camera+ RCB_ROOM_CAMERA_POSE where room_camera stands, "lookat_x,lookat_y,lookat_z,distance,azimuth,elevation" (a MuJoCo+ free camera, metres and degrees); empty: the recording camera frozen at t=0+ RCB_IMAGE_HW the side of every camera's image in pixels; empty: upstream's 256+ RCB_RESET_BUDGET, RCB_WALL_CLOCK_S, RCB_DATA the service's own (rcb_service.main)+"""+from __future__ import annotations++import itertools+import math+import os+import sys++import numpy as np++sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))+# rcb_hb first: it picks MuJoCo's GL backend, which `import mujoco` binds once per process. Imported after it, the+# depth renderer's context ended up inside the viewer's and head-camera images came back as another camera's.+from rcb_hb import GL_BACKEND, Session # noqa: E402+import mujoco # noqa: E402+from rcb_rewards import restore # noqa: E402++restore() # the scoring formula lives in this sidecar only, and must be back before a Session is opened+from rcb_service import main # noqa: E402+from humanoid_bench.tasks import Task # noqa: E402++# HumanoidBench's own non-privileged sensors (humanoid_bench/wrappers.py, ObservationWrapper): `image` renders+# these two head cameras; `proprio` is the robot's joint angles and velocities, without the root pose.+CAMERAS = ("left_eye_camera", "right_eye_camera")+# The head cameras' near plane. MuJoCo's default is 1% of the scene's extent (5.5 cm in door), and the crouch agents+# brace against the door puts an eye 4 cm from it: the door was clipped away and the eye saw the floor behind it.+HEAD_NEAR_M = 0.01+DEPTH_MAX_M = 10.0 # how far the depth reaches (a RealSense D435i, the H1's head RGB-D camera, reads to about 10 m)+IMU = ("imu_gyro_x", "imu_gyro_y", "imu_gyro_z", "imu_gravity_x", "imu_gravity_y", "imu_gravity_z")+++class HumanoidBench:+ """One frozen HumanoidBench instance behind the rcb_service.Sim contract. The agent never gets the root pose,+ object or goal state, or the reward -- the reward is live here only so the service can judge."""++ def __init__(self, task: str | None, instance: str, *, proprio: bool = True, imu: bool = False,+ room_camera: bool = False, room_pose: tuple | None = None, depth: bool = False, task_text: str = "",+ episode: str = "", image_hw: int | None = None):+ self.task, self.instance = task, instance+ self.s = Session(task, instance=instance, enable_render=True, image_hw=image_hw)+ if not self.s.scored:+ raise RuntimeError("the reward was not restored: this service could not judge success")+ self.task_text, self.episode = task_text, episode+ self.robot, m = self.s.low.robot, self.s.model+ assert m.jnt_type[0] == mujoco.mjtJoint.mjJNT_FREE and m.body(int(m.jnt_bodyid[0])).name == "pelvis"+ self.joints = [m.joint(j).name for j in range(m.njnt) if 7 <= m.jnt_qposadr[j] < self.robot.dof]+ self.nu = self.raw_dim = int(self.s.nu)+ self.control_dt, self.max_control_steps = float(self.s.low.dt), int(self.s.max_steps)+ self.lo, self.hi = (np.asarray(v, dtype=np.float64) for v in m.actuator_ctrlrange[:self.nu].T)+ self.proprio_on, self.imu = proprio, imu+ self.proprio_layout = ([f"{j}_pos" for j in self.joints] + [f"{j}_vel" for j in self.joints] if proprio else []) \+ + (list(IMU) if imu else [])+ self.depth_cameras = CAMERAS if depth else ()+ self._depth_renderer = None+ # reach and push override upstream's Task.render() to draw their goal as a marker; every image here goes+ # through the same viewer, so running that hook puts the goal in the head and room cameras too.+ self._marks = type(self.s.low.task).render is not Task.render+ self.cameras = CAMERAS + (("room_camera",) if room_camera else ())+ self._room = (room_pose or self._frozen_camera(self.s.low.camera_name or "cam_default")) if room_camera else None+ self._axes, self._anchors, self._links, self._camera_facts = self._hardware()+ self.image_hw = list(self._render(CAMERAS[0]).shape[:2]) # also warms up the GL context+ self.cmd = self._command_now()++ def _command_now(self) -> np.ndarray:+ """The servo targets the actuators hold now, in the action's normalised units."""+ return 2.0 * (np.asarray(self.s.data.ctrl[:self.nu], dtype=np.float64) - self.lo) / (self.hi - self.lo) - 1.0++ def _frozen_camera(self, name: str) -> tuple:+ """`room_camera`: the task's recording camera (which follows the robot) frozen where it is at the+ instance's initial state -- a tripod in the room. Kept as free-camera parameters for the shared+ offscreen viewer; pixel-identical to the recording camera at t=0 (checked on sit_simple)."""+ m = self.s.model+ d = mujoco.MjData(m); d.qpos[:], d.qvel[:] = self.s.data.qpos, self.s.data.qvel+ mujoco.mj_forward(m, d) # a scratch copy: the live sim is untouched+ c, dist = m.camera(name).id, 3.0 # the free camera orbits a point 3 m ahead+ fwd = -d.cam_xmat[c].reshape(3, 3)[:, 2]+ return d.cam_xpos[c] + dist * fwd, dist, math.degrees(math.atan2(fwd[1], fwd[0])), math.degrees(math.asin(fwd[2]))++ def _hardware(self) -> tuple[list, list, dict, dict]:+ """What a real robot's documentation (its URDF) gives, read off the model: each joint's axis and a point on+ it, each link's parent, mass and centre of mass, each foot's collision shape as a box, and each head+ camera's pose, all in the pelvis frame with every joint at 0; and where room_camera stands in the robot's+ starting frame (origin on the floor under the pelvis at t = 0, x the way the robot faces then, z up). The+ robot is the pelvis's subtree: the scene's objects are subtrees of their own, so none of them is here."""+ m, r3 = self.s.model, lambda v: [round(float(x), 3) for x in v]+ d = mujoco.MjData(m)+ d.qpos[:] = m.qpos0+ d.qpos[7:self.robot.dof] = 0 # a scratch copy: the live sim is untouched+ mujoco.mj_forward(m, d)+ pelvis = m.body("pelvis").id+ R, p = d.xmat[pelvis].reshape(3, 3), d.xpos[pelvis]+ axes = [r3(R.T @ d.xaxis[m.joint(n).id]) for n in self.joints]+ anchors = [r3(R.T @ (d.xanchor[m.joint(n).id] - p)) for n in self.joints]+ robot = [b for b in range(m.nbody) if m.body_rootid[b] == pelvis]++ def box(b): # the link's colliding geoms' bounding boxes, corner by corner, in the pelvis frame+ pts = [d.geom_xpos[g] + d.geom_xmat[g].reshape(3, 3) @ (m.geom_aabb[g, :3] + m.geom_aabb[g, 3:] * np.array(c))+ for g in range(m.ngeom) if m.geom_bodyid[g] == b and (m.geom_contype[g] or m.geom_conaffinity[g])+ for c in itertools.product((-1, 1), repeat=3)]+ q = np.array([R.T @ (x - p) for x in pts])+ return [r3(q.min(0)), r3(q.max(0))]+ links = {"frame": "pelvis (x forward, y left, z up), every joint at 0", "names": [m.body(b).name for b in robot],+ "parent": [None if b == pelvis else m.body(int(m.body_parentid[b])).name for b in robot],+ "mass_kg": [round(float(m.body_mass[b]), 3) for b in robot], # a massless link (the bare H1's hands):+ "com_m": [r3(R.T @ ((d.xipos[b] if m.body_mass[b] > 0 else d.xpos[b]) - p)) for b in robot], # its origin+ "feet": {m.body(b).name: box(b) for b in robot if "ankle" in m.body(b).name},+ "feet_units": "each foot's collision shape as a box: [min xyz, max xyz], m"}+ cams = {}+ for c in CAMERAS:+ i, C = m.camera(c).id, d.cam_xmat[m.camera(c).id].reshape(3, 3) # a camera looks down its -z+ cams[c] = {"mounted": "head", "fovy_deg": round(float(m.cam_fovy[i]), 1),+ "pose": {"frame": "pelvis (x forward, y left, z up), every joint at 0",+ "position_m": r3(R.T @ (d.cam_xpos[i] - p)), "forward": r3(R.T @ -C[:, 2]),+ "up": r3(R.T @ C[:, 1]), "right": r3(R.T @ C[:, 0])}} # right: the image's +x+ if self._room is not None:+ lookat, dist, az, el = self._room+ a, e = math.radians(az), math.radians(el) # MuJoCo's free camera, as the viewer places it+ fwd = np.array([math.cos(e) * math.cos(a), math.cos(e) * math.sin(a), math.sin(e)])+ up = np.array([-math.sin(e) * math.cos(a), -math.sin(e) * math.sin(a), math.cos(e)])+ x0, R0 = self.s.data.xpos[pelvis], self.s.data.xmat[pelvis].reshape(3, 3)+ yaw = math.atan2(R0[1, 0], R0[0, 0])+ Y = np.array([[math.cos(yaw), -math.sin(yaw), 0], [math.sin(yaw), math.cos(yaw), 0], [0, 0, 1]])+ cams["room_camera"] = {"mounted": "room (fixed)", "fovy_deg": round(float(m.vis.global_.fovy), 1), "pose": {+ "frame": "the robot's start: origin on the floor under the pelvis at t = 0, x the way it faces, y left, z up",+ "position_m": r3(Y.T @ (np.asarray(lookat) - dist * fwd - [x0[0], x0[1], 0])),+ "forward": r3(Y.T @ fwd), "up": r3(Y.T @ up), "right": r3(Y.T @ np.cross(fwd, up))}}+ return axes, anchors, links, cams++ def _render(self, camera: str) -> np.ndarray:+ r = self.s.low.mujoco_renderer+ if self._marks:+ if r.viewer is None or not r.viewer._markers:+ self.s.low.task.render() # adds the marker, which the viewer keeps+ for mk in r.viewer._markers: # and, like that hook on every call, puts it on the+ if mk["objid"] == 789: # goal: a reset gives task.goal a new array (push's+ mk["pos"] = self.s.low.task.goal # reset_model first rewrites the old one in place)+ if camera != "room_camera":+ vis, znear = self.s.model.vis.map, self.s.model.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / self.s.model.stat.extent) # the room camera keeps the default+ try:+ return r.render("rgb_array", camera_name=camera)+ finally:+ vis.znear = znear+ cam = r.viewer.cam # the viewer exists: __init__ rendered once+ cam.lookat[:], cam.distance, cam.azimuth, cam.elevation = self._room+ return r.render("rgb_array", camera_id=-1) # -1 = the free camera just set++ def _depth(self, camera: str) -> np.ndarray:+ """What an RGB-D camera in a head camera's place reads, noise-free: metres along its forward axis (planar+ depth), pixel for pixel with its image; 0 where nothing is within DEPTH_MAX_M. MuJoCo's own renderer:+ the viewer's gives no metric depth, and its markers (reach's and push's goal) are pictures, not things."""+ m = self.s.model+ if self._depth_renderer is None:+ self._depth_renderer = mujoco.Renderer(m, *self.image_hw)+ self._depth_renderer.enable_depth_rendering()+ vis, znear = m.vis.map, m.vis.map.znear+ vis.znear = min(znear, HEAD_NEAR_M / m.stat.extent) # as the head cameras' images+ try:+ self._depth_renderer.update_scene(self.s.data, camera=camera)+ d = self._depth_renderer.render()+ finally:+ vis.znear = znear+ self.s.low.mujoco_renderer.viewer.make_context_current() # the viewer's GL context again, for its images+ return np.where(d < DEPTH_MAX_M, d, 0.0).astype(np.float32)++ def _imu(self) -> list[float]:+ """What an IMU on the pelvis reads, noise-free: the angular velocity in the pelvis frame (the root free+ joint's qvel[3:6] already is that), and the gravity direction in that frame (tilt, not heading)."""+ rot = np.zeros(9); mujoco.mju_quat2Mat(rot, self.s.data.qpos[3:7])+ return [*self.s.data.qvel[3:6], *(rot.reshape(3, 3).T @ [0.0, 0.0, -1.0])]++ # ------------------------------------------------------------------ the rcb_service.Sim contract+ def spec(self) -> dict:+ m = self.s.model+ tendons = any(m.actuator_trntype[i] == mujoco.mjtTrn.mjTRN_TENDON for i in range(self.nu))+ return {+ "simulator": "humanoidbench", "robot": self.s.env_id.split("-")[0], "task_text": self.task_text,+ "episode": self.episode,+ "action_types": {"joint_position": {+ "dim": self.nu, "layout": [m.actuator(i).name for i in range(self.nu)],+ "lower": [-1.0] * self.nu, "upper": [1.0] * self.nu,+ "range_rad": [[round(float(lo), 4), round(float(hi), 4)] for lo, hi in zip(self.lo, self.hi)],+ "units": "HumanoidBench's own action: position-servo targets, -1 and +1 commanding the two ends of each "+ "actuator's range (range_rad), linearly, so 0 is mid-range, not 0 rad"+ + ("; a tendon actuator's range is the summed angle of the two joints it moves" if tendons else ""),+ "dtype": "float32: the simulator applies each target as a 32-bit float",+ "semantics": "rows dt apart (a multiple of control_dt); each control step's targets are the row (hold) or "+ "a straight line from the previous row's (linear); values outside [-1, 1] are clipped and "+ "reported"}},+ "cameras": {c: {**self._camera_facts[c], "hw": self.image_hw} for c in self.cameras},+ "proprio": {"joints": self.joints, "units": "*_pos rad, *_vel rad/s", "axis": self._axes,+ "axis_frame": "the joint's rotation axis in the pelvis frame, every joint at 0; a positive "+ "angle turns the joint's child link about it by the right-hand rule: about an axis "+ "near +y (left), a link hanging below the joint swings backward",+ "position_m": self._anchors, "position_frame": "a point on that axis (the joint's centre), m, same frame"}+ if self.proprio_on else None,+ "imu": {"mounted": "pelvis", "frame": "pelvis: x forward, y left, z up",+ "gyro": "imu_gyro_*: angular velocity, rad/s, pelvis frame, right-hand rule: y > 0 while pitching "+ "forward (nose down), x > 0 while rolling to the right, z > 0 while turning left",+ "gravity": "imu_gravity_*: unit gravity direction, pelvis frame: (0, 0, -1) when upright; it tips "+ "toward the way the pelvis leans: x > 0 leaning forward, y > 0 leaning left"} if self.imu else None,+ "links": self._links,+ "depth": {"cameras": list(CAMERAS), "units": "metres along the camera's forward axis (planar depth, as an "+ "RGB-D camera reports it), pixel for pixel with that camera's image; noise-free",+ "range_m": [HEAD_NEAR_M, DEPTH_MAX_M], "nothing_in_range": 0.0} if self.depth_cameras else None,+ "gl_backend": GL_BACKEND,+ }++ def interpolable(self, typ: str) -> bool:+ return True++ def images(self, cameras: list[str], depth: bool) -> tuple[dict, dict]:+ rgb = {c: self._render(c) for c in cameras}+ dep = {c: self._depth(c) for c in cameras if depth and c in self.depth_cameras}+ return rgb, dep++ def proprio(self) -> np.ndarray:+ own = [*self.robot.joint_angles(), *self.robot.joint_velocities()] if self.proprio_on else []+ return np.array(own + (self._imu() if self.imu else []), dtype=np.float32)++ def clip(self, typ: str, rows: np.ndarray) -> tuple[np.ndarray, list]:+ out = np.clip(rows, -1.0, 1.0)+ report = [{"row": int(i), "index": int(j), "reason": "outside [-1, 1]"} for i, j in np.argwhere(out != rows)]+ # At the precision the simulator applies (to_sim), which is also what command() gives back: a chunk then+ # interpolates from exactly the targets the same rows sent one by one would start from.+ return out.astype(np.float32).astype(np.float64), report++ def to_sim(self, typ: str, row: np.ndarray) -> np.ndarray:+ return np.asarray(row, dtype=np.float32).astype(np.float64) # what Session.step applies++ def command(self, typ: str) -> np.ndarray:+ return self.cmd++ def hold(self) -> tuple[str, np.ndarray]:+ return "joint_position", self.cmd++ def step(self, row: np.ndarray) -> tuple[bool, bool, dict]:+ done = self.s.step(row)[2]+ self.cmd = np.asarray(row, dtype=np.float64)+ return done, bool(self.s.status()["success"]), {}++ def reset(self) -> None:+ self.s.reset()+ self.cmd = self._command_now()++ def state_sha(self) -> str:+ return self.s.state_sha()++ def verify(self, rows: np.ndarray) -> dict:+ """The recorded episode from the frozen scene, in a fresh HumanoidBench environment (physics only)."""+ st = Session(self.task, instance=self.instance).replay(rows)+ return {"success": st["success"], "state_sha": st["state_sha"]}++ def save_trajectory(self, rows: np.ndarray, path: str, meta: dict) -> None:+ self.s.save_trajectory(rows, path, meta)++ def render_video(self, rows: np.ndarray, path: str) -> None:+ self.s.render_video(rows, path, every=5, fps=20)+++def build() -> HumanoidBench:+ env = os.environ.get+ pose = [float(v) for v in env("RCB_ROOM_CAMERA_POSE", "").split(",") if v.strip()]+ if pose and len(pose) != 6:+ raise SystemExit("RCB_ROOM_CAMERA_POSE needs 6 numbers: lookat_x,lookat_y,lookat_z,distance,azimuth,elevation")+ return HumanoidBench(env("RCB_TASK"), env("RCB_INSTANCE", "/srv/instance.npz"),+ proprio=env("RCB_OBS_PROPRIO", "1") == "1", imu=env("RCB_OBS_IMU", "1") == "1",+ room_camera=env("RCB_OBS_ROOM_CAMERA", "1") == "1", depth=env("RCB_OBS_DEPTH", "0") == "1",+ room_pose=(np.array(pose[:3]), *pose[3:]) if pose else None, task_text=env("RCB_TASK_TEXT", ""),+ episode=env("RCB_EPISODE", ""), image_hw=int(env("RCB_IMAGE_HW") or 0) or None)+++if __name__ == "__main__":+ main(build)diff --git a/tasks/humanoidbench-manip-window-i00-realtime/instruction.md b/tasks/humanoidbench-manip-window-i00-realtime/instruction.mdnew file mode 100644index 0000000000..ab9a1495e0--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/instruction.md@@ -0,0 +1,122 @@+# window — instance 00 — REALTIME MODE (robot as a service; time does not stop)++You control a simulated Unitree H1 humanoid with two Shadow dexterous hands (HumanoidBench / MuJoCo) **as if+it were real hardware**: the simulator runs in a service you cannot open, and you reach it only through a+client. Like real hardware, it does not wait for you: once an episode is running, the robot moves in real time+while you think. **Task:** grasp the wiping tool — it starts in the air just in front of the hands and falls+within the first second unless it is held — and wipe the window with it, moving steadily up and down.++## Rules (real-robot rules)+- **Time does not stop.** An episode starts paused, on the frozen starting state, its servos holding their+ starting targets. Its clock starts at your first `submit` (or `get_obs(seconds=s)`) and from then on the+ robot runs at 50 Hz in real time, one control step every 0.02 s of wall-clock time, whether or not you send+ anything, until the episode ends. Between your commands it executes what you planned, then holds the last+ servo targets you sent; nothing pauses for you, not your thinking, not your tool calls.+- **You are far too slow to steer it turn by turn.** One turn of yours (a model response, a tool call) takes+ seconds, and the robot falls within a few seconds of robot time without active balance. Drive it with a+ program: a controller of your own (Python in your container, with numpy and the client) that runs a loop at+ the control rate, reads the proprioception and sends targets, started in the background+ (`nohup python /app/output/gen.py > /app/output/run.log 2>&1 &`) so that your session is free while it runs.+ Use your turns to start episodes, read what happened, and improve the program.+- **Episodes and resets.** An episode ends when the task succeeds, when the robot falls (its pelvis drops too+ low), when the tool drops, after 1000 control steps (20 s after its clock started), when your time is up, or+ on a simulator error. Nothing else ends it: a robot that has fallen without meeting one of these (lying on+ its side, sitting, face down) goes on until step 1000, so an episode that is still running does not mean the+ robot is still up. You have **50 resets** (`spec()["budget"]["reset"]`, which is authoritative: if it says+ 0, there is no reset and the one episode is the run). `r.reset()` puts the robot and the scene back on the+ same frozen starting state and starts a new episode, paused until your next `submit`, with its own 1000+ control steps, either abandoning the running episode or after one has ended. Once an episode has ended,+ `submit` raises `EpisodeOver` (its message gives `resets_left`) until you reset. **Only the last episode is+ graded**: an episode you reset away is never graded, a success ends the run at once (no reset after it), and+ when your time is up the episode then running is the one graded. The wall clock is the run's and never+ restarts; `meta["episode"]` and `meta["resets_left"]` count the episodes.+- **No privileged state, no score.** You see what the robot's two head cameras see (RGB, 256×256), its own+ joint angles and joint velocities, a pelvis IMU and `room_camera`, a camera fixed in the room. Not where the+ robot is in the room, not the tool's pose, not the reward: the service judges the episode and only tells you+ that it ended and why (`meta["done"]`, `meta["reason"]`: `"success"`, `"error"` after a simulator error,+ else `"failed"`). `spec()` gives the robot's own facts, as its URDF would: each actuator's range, each+ joint's axis and position, each link's mass and centre of mass, the feet's collision boxes, the IMU's frame,+ each camera's field of view and pose.+- There is nothing to hand in: the service records every control step it ran, replays the last episode in a+ fresh simulator and grades the replay. Keep the controller that drives the robot at `/app/output/gen.py` for+ the record: save it early and keep it current.+- Wall-clock budget: 1 h. It ends without warning, and ending your session earlier ends the run the same way+ (and stops your background programs), so do not stop early: while the episode runs, or a reset is left, the+ run can still pass.++## Time and streaming+- `submit` answers at once, with the robot as it is now: the rows you sent have not run yet. They become the+ plan from control step `at` (an episode step number; by default the next step), replacing whatever was+ planned from `at` on and keeping what is planned before it. When the plan runs out, the last target is held.+ `res.meta["control_steps"]` is the number of steps run so far in this episode (so the next step has that+ number), `res.meta["proprio"]["now"]` the robot's proprioception now, `res.meta["plan_end_step"]` where the+ plan now ends.+- Closed loop: send one row per control step, `r.submit("joint_position", a[None], cameras=[])`, and compute+ the next one from the proprioception it returns. `cameras=[]` skips the images (the fastest call); sending+ faster than 50 Hz only replaces the same step's target again.+- Open loop: send up to 10 s of rows at once (a chunk, `dt` a multiple of 0.02) and the next chunk before the+ plan runs out, with `at` = the step where it should take over; `at` may be up to+ `spec()["timing"]["max_ahead_steps"]` steps ahead.+- Latency: a reading is a few milliseconds old when it reaches you, and a row takes effect at the next control+ step at the earliest. A row whose step has already run when the request arrives is dropped+ (`res.meta["late_steps"]` counts them); the rest of its chunk applies from the next step.+- `get_obs()` reads the robot now; `get_obs(seconds=s)` (at most 30) waits while s seconds of robot time run+ and returns what the cameras saw meanwhile (the robot keeps executing its plan). `spec()["timing"]` states+ all this; `r.status()` shows `running` and `lag_s`.++## Scoring+HumanoidBench scores the *whole episode*, not a final state: it passes iff the summed per-step reward reaches+**650**: a total of rewards, not a number of steps (an episode is at most 1000 steps). A large share of each+step is paid only while the tool is in contact with the glass — while it is not, that share is zero, so the+bar cannot be reached without contact. The rest pays for moving the tool vertically at a target speed on the+order of half a metre per second, for keeping both hands near the tool, and for standing with the head about+40 cm from where it started. The service ends the episode the moment the pass mark is reached+(`reason = "success"`), and a fresh replay of the recorded episode, the servo targets the robot actually+executed at each control step, must reach it as well and end in the same state.++## Client (`/opt/tools/rcb_client.py`, protocol `eai-standard/2.3`, realtime mode)+Your whole interface to the robot. Its docstring is the full reference+(`python -c "import rcb_client; help(rcb_client)"`); the calls and the endpoints they use:++| call | endpoint | what it does |+|---|---|---|+| `r = Robot()` | | connects (`http://sim:8000`, then `http://localhost:8000`) and waits until the scene is ready |+| `r.spec()` | `GET /spec` | the robot's facts: the actuators' order and ranges, `control_dt` (0.02 s), `max_control_steps` (1000), `timing`, the cameras, `proprio_layout`, `budget` |+| `r.status()` | `GET /status` | `calls`, `rows`, `control_steps`, `done`, `reason`, `elapsed_s`, `running`, `planned_steps`, `lag_s`, and `episode`, `resets_left`, `control_steps_total` (all episodes) |+| `r.submit("joint_position", A, dt=0.02, interpolate="linear", at=None, cameras=None)` | `POST /submit` | plans `A`, rows of 61 targets `dt` seconds apart (a chunk covers at most 10 s), from step `at`; answers at once; the first one starts the episode's clock |+| `r.get_obs(cameras=None, seconds=0, include_depth=False, video="frames")` | `POST /observe` | the robot now, or what the cameras saw over the next `seconds` |+| `r.reset(cameras=None, include_depth=False)` | `POST /reset` | a new episode from the frozen starting state, paused (at most 50 in all); returns the start's frames |++The answers are binary (RCB1) and come back decoded: `res.meta` (the counters, `done`, `reason`, `proprio["now"]`,+`clipped`), `res.last[camera]` (the last frame), `res.frames` or `res.video` (what the cameras saw during a watch),+`res.proprio` (proprioception rows). Values outside [-1, 1] are clipped and reported in `meta["clipped"]`, never+refused; a malformed request raises `ValueError` and changes nothing.+```python+from rcb_client import Robot+r = Robot()+a = ... # (61,) servo targets in [-1, 1]+while True: # the first submit starts the clock+ res = r.submit("joint_position", a[None], cameras=[])+ if res.done:+ break+ a = ... # from res.meta["proprio"]["now"]+```+Measured cost here in standard mode: about 4.9 ms per one-row call asking for one head camera, physics+included; a call with `cameras=[]` skips the rendering. A control step is 20 ms.++Each proprioception row (`meta["proprio"]["now"]`, `res.proprio`) has, in `spec()["proprio_layout"]` order,+the joints' angles (`*_pos`, rad), their velocities (`*_vel`, rad/s), then the IMU (`imu_gyro_*`,+`imu_gravity_*`).++The 61 actuators are position servos. The first 21 are the body, in this order: `left_hip_yaw`,+`left_hip_roll`, `left_hip_pitch`, `left_knee`, `left_ankle`, `right_hip_yaw`, `right_hip_roll`,+`right_hip_pitch`, `right_knee`, `right_ankle`, `torso`, `left_shoulder_pitch`, `left_shoulder_roll`,+`left_shoulder_yaw`, `left_elbow`, `left_wrist_yaw`, `right_shoulder_pitch`, `right_shoulder_roll`,+`right_shoulder_yaw`, `right_elbow`, `right_wrist_yaw`. The remaining 40 are the two Shadow hands, 20 each —+`lh_A_*` then `rh_A_*`; each hand's first 2 are its wrist (`*_A_WRJ2`, `*_A_WRJ1`) and the other 18 its+fingers, where each of the 4 `*J0` ones (`*_A_FFJ0`, `*_A_MFJ0`, `*_A_RFJ0`, `*_A_LFJ0`) drives a tendon that+moves two finger joints together (`spec()["action_types"]["joint_position"]["layout"]`). The proprioception+measures joints, not actuators: 69 of them, in `spec()["proprio"]["joints"]` order. A target of −1 or +1+commands one end of that servo's range, `spec()["action_types"]["joint_position"]["range_rad"]` in radians,+and the scale is linear, so 0 is mid-range, not 0 rad. A constant action commands joint *targets*, not a pose:+whether the robot stays there depends on whether that pose is stable under gravity and contact.diff --git a/tasks/humanoidbench-manip-window-i00-realtime/task.toml b/tasks/humanoidbench-manip-window-i00-realtime/task.tomlnew file mode 100644index 0000000000..dddfb59684--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/task.toml@@ -0,0 +1,68 @@+# Realtime-mode task of humanoidbench-manip-window-i00-privileged (docs/PROTOCOL.md §4b). Generated from scripts/humanoidbench/templates/realtime/task.toml -- edit+# subset.toml or the template, not this.+schema_version = "1.3"+artifacts = [+ { source = "/data/final.json", service = "sim" },+ { source = "/data/episode.jsonl", service = "sim" },+ { source = "/data/trajectory.npz", service = "sim" },+ { source = "/data/trajectory_live.f64", service = "sim" },+ { source = "/data/replay.mp4", service = "sim" },+ { source = "/data/replay.last.png", service = "sim" },+ { source = "/data/ticks.f64", service = "sim" },+ { source = "/data/calls.jsonl", service = "sim" },+ { source = "/data/submitted.f64", service = "sim" },+ { source = "/data/startups.jsonl", service = "sim" },+ "/app/output/",+]++[task]+name = "robot-coding-bench/humanoidbench-manip-window-i00-realtime"+description = "REALTIME MODE (robot as a service on a clock that does not stop, eai-standard/2.3): HumanoidBench window on the same frozen instance as humanoidbench-manip-window-i00-privileged. Time does not stop: once an episode starts, the robot runs at 50 Hz in real time and holds its last targets while the agent thinks, so the agent streams joint-position targets from a controller program of its own through the client (/observe, /submit with a plan from step `at`, /reset; head-camera RGB, joint proprioception, a pelvis IMU, a fixed room camera). Up to 50 resets, no privileged state, no reward; the sim service records every executed control step, replays the last episode in a fresh simulator and grades it."+authors = [{ name = "pingyue" }]+keywords = ["robotics", "humanoidbench", "mujoco", "h1hand", "manipulation", "realtime-mode", "trajectory", "window"]++[metadata]+author_name = "pingyue"+task_type = "trajectory"+simulator = "humanoidbench"+robot = "unitree-h1hand"+family = "window"+mode = "realtime"+protocol = "eai-standard/2.3"+instance_seed = 0+expert_time_estimate_hours = 8.0+oracle = "none"+oracle_evidence = "none known: no run has passed it, and published RL reaches 94% of the bar (paper 227, FastTD3 613, bar 650); human review in PR #83"++[verifier]+timeout_sec = 900.0+environment_mode = "separate"+network_mode = "no-network"++# The agent's time is up: stop whatever it left running in its container. Harbor stops that container only after+# collecting from it (~12 s), and a script still running there would keep driving the robot past the deadline.+[[verifier.collect]]+service = "main"+command = "kill -9 -1 2>/dev/null; exit 0"+timeout_sec = 30++# Then the episode ends: SIGUSR1 to the service (re-sent every 30 s, in case it was restarting), which grades it.+[[verifier.collect]]+service = "sim"+command = "for i in $(seq 1 90); do [ -f /data/server.pid ] && break; sleep 2; done; for i in $(seq 1 400); do grep -q '\"finalized\": true' /data/final.json 2>/dev/null && break; [ $((i % 15)) -eq 1 ] && kill -USR1 $(cat /data/server.pid) 2>/dev/null; sleep 2; done; cat /data/final.json"+timeout_sec = 900++[agent]+timeout_sec = 3600.0+# The agent container holds no simulator and no reward, but upstream is a public commit: like every HumanoidBench+# task, the agent reaches the model APIs and nothing else (scripts/set_agent_egress.py HOSTS).+network_mode = "allowlist"+allowed_hosts = ["chatgpt.com", "*.chatgpt.com", "auth.openai.com", "api.openai.com", "*.openai.com", "api.anthropic.com", "*.anthropic.com", "openrouter.ai", "*.openai.azure.com", "*.cognitiveservices.azure.com"]++[environment]+build_timeout_sec = 1800.0+cpus = 4+memory_mb = 16384++[verifier.environment]+build_timeout_sec = 600.0diff --git a/tasks/humanoidbench-manip-window-i00-realtime/tests/Dockerfile b/tasks/humanoidbench-manip-window-i00-realtime/tests/Dockerfilenew file mode 100644index 0000000000..0ecd542d19--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/tests/Dockerfile@@ -0,0 +1,4 @@+# Separate verifier sandbox: grades the sim service's own artifacts; needs nothing but Python.+FROM docker.io/library/python:3.12.14-slim@sha256:f77ac9e44ae96ef2c90b8053ea08c31f8be030f824196b0ae4db6d462c84e51f+COPY test.sh grade.py /tests/+RUN chmod +x /tests/test.shdiff --git a/tasks/humanoidbench-manip-window-i00-realtime/tests/grade.py b/tasks/humanoidbench-manip-window-i00-realtime/tests/grade.pynew file mode 100644index 0000000000..d3d7f6bf9e--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/tests/grade.py@@ -0,0 +1,63 @@+"""Standard-mode verifier (eai-standard/2.1): grades what the sim service -- which the agent could not touch -- wrote.++/data/final.json holds the live run's outcome and the verdict of a fresh replay of the graded episode:+success = live_success AND replay_success AND deterministic (the replay ends in the live run's final state).+Writes /logs/verifier/reward.txt (1/0) and a numeric-only reward.json, and copies the evidence (replay video, last+frame, trajectory, event log) next to them for humans; the evidence never decides the verdict.+infra_error = 1: no verdict on the agent (the service crashed past its restarts, never finished, or failed to finalize).+"""+import json+import os+import shutil+import traceback++DATA = os.environ.get("RCB_DATA", "/data")+LOG = os.environ.get("RCB_VERIFIER_LOG_DIR", "/logs/verifier")+os.makedirs(LOG, exist_ok=True)+++def write(ok: bool, out: dict) -> None:+ with open(os.path.join(LOG, "reward.txt"), "w") as f:+ f.write("1\n" if ok else "0\n")+ with open(os.path.join(LOG, "reward.json"), "w") as f:+ json.dump(out, f)+ print(json.dumps(out))+++def main() -> None:+ path = os.path.join(DATA, "final.json")+ if not os.path.exists(path):+ write(False, {"success": 0, "missing_log": 1, "infra_error": 1, "grader_error": 0})+ return+ try:+ with open(path) as f:+ d = json.load(f)+ live, replay, det = (bool(d.get(k)) for k in ("live_success", "replay_success", "deterministic"))+ failed_finalize = bool(d.get("finalize_error"))+ unfinished = not d.get("finalized")+ infra = bool(d.get("infra_error")) or failed_finalize or unfinished+ ok = live and replay and det and not infra+ out = {"success": int(ok), "live_success": int(live), "replay_success": int(replay), "deterministic": int(det),+ "finalized": int(not unfinished), "missing_log": 0, "infra_error": int(infra),+ "grader_error": int(failed_finalize),+ "sim_crashed_on_call": int(d.get("reason") == "sim_crashed_on_call"),+ "n_actions": int(d.get("n_actions") or 0), "sim_steps": int(d.get("control_steps_total") or 0),+ "calls": int(d.get("calls") or 0), "rows": int(d.get("rows") or 0), "resets": int(d.get("resets") or 0),+ "reset_budget": int(d.get("reset_budget") or 0),+ "sim_startups": int(d.get("sim_startups") or 0), "elapsed_s": float(d.get("elapsed_s") or 0.0),+ "video_rendered": int(bool(d.get("video")))}+ except Exception: # noqa: BLE001 -- an unreadable final.json is the grader's failure, not the agent's+ traceback.print_exc()+ write(False, {"success": 0, "missing_log": 0, "infra_error": 1, "grader_error": 1})+ return+ for name in ("replay.mp4", "replay.last.png", "trajectory.npz", "episode.jsonl"):+ try:+ if os.path.exists(os.path.join(DATA, name)):+ shutil.copy(os.path.join(DATA, name), os.path.join(LOG, name))+ except OSError:+ traceback.print_exc()+ write(ok, out)+++if __name__ == "__main__":+ main()diff --git a/tasks/humanoidbench-manip-window-i00-realtime/tests/test.sh b/tasks/humanoidbench-manip-window-i00-realtime/tests/test.shnew file mode 100755index 0000000000..75622ada21--- /dev/null+++ b/tasks/humanoidbench-manip-window-i00-realtime/tests/test.sh@@ -0,0 +1,5 @@+#!/bin/bash+# Verifier (separate sandbox): the sim sidecar's artifacts were uploaded to /data by Harbor.+mkdir -p /logs/verifier+rm -rf /logs/verifier/*+python /tests/grade.py 2> /logs/verifier/grade.log