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
"""eai-standard/2.2 sim service for RoboDojo's simulation benchmark (RoboDojo's own evaluator on its Isaac Lab 2.3 fork /
Isaac Sim 5.1; two ARX X5 arms with parallel grippers; see docs/protocol/standard_mode.md). Owns the scene. HTTP surface
(rcb_limited): GET /spec, GET /status, POST /observe, POST /submit, POST /kinematics (RCB1 bodies, RCB1 responses: videos /
frames / depth / proprio as raw blobs, never base64). Logs every call to /data/episode.jsonl and keeps /data/final.json
current. Nothing about objects ever leaves this process.
Crash recovery (rcb_limited.Episode, docs/protocol/standard_mode.md §13, resume class `exact`, or `approx` for the tasks graded
by verdict): every executed action is
journaled (the 14-D row handed to Session.step), every completed call is checkpointed with the scene's state hash. After a
crash compose restarts the container and this service, a fresh process, rebuilds the frozen layout, replays the journal up
to the last checkpoint (fresh-process replays are bit-identical), checks the state hash and continues the SAME episode.
RoboDojo specifics:
- the joint action is RoboDojo's own 14-D one (6 joint targets + the normalised gripper command per arm), at the 25 Hz
action rate; RoboDojo's controller moves each arm to the row's targets over the action's 10 physics steps;
- `ee_pose` rows hold both arms' link6 targets (16 values with the gripper commands): each row is solved by the arms'
kinematic model (the same as /kinematics) and executed as a joint move at most 0.25 m/s, then checked;
- three cameras (cam_head: static; cam_left_wrist / cam_right_wrist: on each arm's link6), rendered when a frame is
recorded (rendering never steps physics), z-depth from the same renders;
- the episode ends as RoboDojo's does: at the first action where the task's check passes, or when a check fails for good;
or when the time is up. RoboDojo's per-task step limit (a policy's episode length in its evaluation; no check reads it)
is not applied: no step caps (PROTOCOL D1), only a runaway guard. A task whose service sets RCB_AGENT_END and
RCB_STEP_LIMIT (every RoboDojo standard task: PROTOCOL §3 "Agent-ended episodes", rcb_agent_end.py) ends it the
agent's way instead: after the task's step_lim actions, or when a check fails for good, or
at the agent's POST /finish or the end of its session; a success never ends it and is never shown, and the verdict is
RoboDojo's: whether the reward reached 1 in the episode;
- the episode is graded as it ran (no grading replay): when it ends the service writes
/data/trajectory.npz (the deliverable) + /data/episode.json (the live result: RoboDojo's check after every action; the
end-state hash), the collect hook's SIGUSR1 makes it exit, and environment/sim/finalize.sh writes final.json from it;
- the start state is checked before the agent is served: the rebuilt layout's state hash against the frozen instance's
(instance.json `state_sha`); a mismatch ends the trial as an infrastructure failure (`instance_mismatch`, a give-up:
nothing is served). A task graded by verdict has resets that are not bit-identical: its hash is recorded, not compared
(the layout itself comes from instance.json);
- the video is kept live (live_video.py) from renders the service makes anyway: RoboDojo's observation renders every
camera on demand (cached per action), so a frame of cam_head is kept only at an action whose observation a call already
rendered (the agent's frames at its video_fps, 5 per simulated second by default; the call's proprioception at its
end), at most one per 5 actions; no extra render is made for the video during the episode (one, of the end state, at
its end). The collect hook encodes live.mp4 after the verdict."""
from __future__ import annotations
import json, math, os, sys, threading, time, traceback
from typing import Callable
import numpy as np
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import rcb_limited as L # noqa: E402
import rcb_agent_end as AE # noqa: E402
import live_video # noqa: E402
PROTOCOL = L.PROTOCOL
DATA = os.environ.get("RCB_DATA", "/data"); os.makedirs(DATA, exist_ok=True); os.makedirs(os.path.join(DATA, "calls"), exist_ok=True)
TASK = os.environ.get("RCB_TASK", "stack_bowls"); INSTANCE = os.environ.get("RCB_INSTANCE", os.path.join(os.path.dirname(__file__), "instance.json"))
OBS_CALIBRATION = os.environ.get("RCB_OBS_CALIBRATION", "1") == "1"
CTRL_HZ = 25; SUBSTEPS = 10; ACTION_DIM = 14
MAX_CHUNK_S = float(os.environ.get("RCB_MAX_CHUNK_S", "10")); MAX_WATCH_S = float(os.environ.get("RCB_MAX_WATCH_S", "30"))
MAX_FRAMES = L.MAX_FRAMES_PER_CALL # images one call may capture (cameras x frames, per-frame depth counted again; 3000):
# every one is held in memory until the response is sent (L.check_frames; /spec max_frames_per_call)
LOCK = threading.Lock(); PID_FILE = os.path.join(DATA, "server.pid")
E = {"ready": False, "step": 0, "calls": 0, "done": False, "reason": None, "success": False, "t0": time.time(), "error": None}
# crash recovery replays the journal in a fresh process: bit-identical where the task's physics is (resume class exact);
# a task graded by verdict (instance.json "determinism": "verdict", e.g. GPU particle liquids) resumes `approx`
VERDICT = json.load(open(INSTANCE)).get("determinism") == "verdict"
RESUME = "approx" if VERDICT else "exact"
EP = L.Episode(DATA, dim=ACTION_DIM, resume=RESUME, persist=AE.persist())
EP.startup() # start count, back-off after failed starts, startup watchdog (RCB_STARTUP_TIMEOUT_S) until scene_built()
log = EP.log
def ctrl_steps() -> int:
return int(S.n_steps) # = EP.rows: every step Session.step executes is journaled (a resume replays them)
def write_final() -> None:
if L.final_verdict(DATA): # finalize.sh's verdict, or a give-up after crashes: never overwritten
return
n = int(E.get("episode_control_steps") or ctrl_steps())
L._write_json(os.path.join(DATA, "final.json"),
{"success": bool(E["success"]), "steps": E["step"], "calls": E["calls"], "control_steps": n, "physics_steps": n * SUBSTEPS,
"elapsed_s": round((E.get("t_end") or time.time()) - E["t0"], 1), "reason": E["reason"], "done": E["done"], "task": TASK,
"protocol": PROTOCOL, "finalized": False, "n_actions": E.get("n_actions"), "live_sha": E.get("live_sha"), **EP.final_fields(),
**AE.final_fields(E, n), **step_fields()})
def step_fields() -> dict:
"""final.json / episode.json: where the step limit comes from (nothing without one; AE.final_fields has the numbers)."""
return {"step_limit_source": "RoboDojo step_lim"} if AE.STEP_LIMIT is not None else {}
def dump() -> None:
"""The episode is over: save the recorded trajectory and the live result for the collect hook (lock held)."""
if E.get("dumped"): return
E["dumped"] = True
try:
acts = S.record_stop(); E["live_sha"] = S.state_sha(); E["n_actions"] = int(len(acts))
S.save_trajectory(acts, os.path.join(DATA, "trajectory.npz"), meta={"source": "standard-mode episode (eai-standard/2.2)", "steps": E["step"],
"reason": E["reason"], "resumes": EP.resumes})
except Exception as exc: # noqa
traceback.print_exc(); log({"ev": "dump_error", "error": repr(exc)})
n = int(E.get("episode_control_steps") or 0)
L._write_json(os.path.join(DATA, "episode.json"),
{"success": bool(E["success"]), "steps": E["step"], "calls": E["calls"], "control_steps": n, "physics_steps": n * SUBSTEPS,
"elapsed_s": round((E.get("t_end") or time.time()) - E["t0"], 1), "reason": E["reason"], "task": TASK, "protocol": PROTOCOL,
"n_actions": E.get("n_actions"), "live_sha": E.get("live_sha"), "determinism_verdict_mode": VERDICT, **INSTANCE_CHECK,
**EP.final_fields(), **AE.final_fields(E, n), **step_fields()})
write_final()
VIDEO.last(ctrl_steps(), frame=lambda: S.render_frame("cam_head")) # the end state, rendered once (after the verdict)
def finish(reason: str) -> None:
if E["done"]: return
E["done"] = True; E["reason"] = reason; E["t_end"] = time.time(); E["episode_control_steps"] = ctrl_steps()
if AE.ENABLED: # agent-ended episode: RoboDojo's reward is a latch: its verdict at an end-state end, a failed check
# and a simulator error alike (an error is judged as in a task without the feature: the check still runs)
try: E["success"] = reason in AE.END_STATE_REASONS + ("failed_check", "error") and S.latched()
except Exception: E["success"] = False
elif not E["success"]:
try: E["success"] = check_success()
except Exception: pass
log({"ev": "end", "reason": reason, "step": E["step"], "control_steps": ctrl_steps(), "elapsed_s": round(E["t_end"] - E["t0"], 1), "success": bool(E["success"])})
dump()
# ---------------------------------------------------------------- scene
import rcb_robodojo as M # noqa: E402
S = M.Session(TASK, instance=INSTANCE, strict=False, depth=True) # the start-state check is below (instance_mismatch)
# The frozen initial state, checked before anything is served: the rebuilt layout's state hash against instance.json's
# `state_sha` (what strict mode refused by raising, which crashed the service until the collect hook gave up). A task
# graded by verdict resets to a state that is not bit-identical by design (instance.json says why): recorded only.
INSTANCE_CHECK = {"instance_sha": S.instance_sha, "instance_expected_sha": (S.inst or {}).get("state_sha"),
"instance_exact": bool(S.instance_exact), "instance_checked": not VERDICT}
if not VERDICT and not S.instance_exact:
L._write_json(os.path.join(DATA, "final.json"), {"task": TASK, "protocol": PROTOCOL, "instance_mismatch": True, **INSTANCE_CHECK})
EP.give_up(f"instance_mismatch: the rebuilt layout's state {S.instance_sha} is not the frozen instance's "
f"{INSTANCE_CHECK['instance_expected_sha']}", infra=True) # never returns
def live_frame():
"""cam_head of the current state if a call already rendered its observation (Session.observe caches it per action),
else None: the video never makes the service render during the episode."""
if getattr(S, "_obs_key", None) != (S._resets, S.n_steps):
return None
return S.render_frame("cam_head")
VIDEO = live_video.LiveVideo(DATA, ctrl_hz=CTRL_HZ, frame=live_frame, log=EP.log)
ARMS = M.ARMS
JOINTS = [f"joint{i}" for i in range(1, 7)]
LAYOUT = [f"{a}_{j}" for a in ARMS for j in JOINTS + ["gripper"]]
CAMS = list(M.CAMERAS)
MOUNT = {"cam_head": "static, above and behind the arm bases, looking forward and down at the table",
"cam_left_wrist": "on the left arm's wrist (link6)", "cam_right_wrist": "on the right arm's wrist (link6)"}
def images() -> dict:
return {c: S.render_frame(c) for c in CAMS}
FIRST = images()
RSPEC = S.robot_spec()
def joint_limits() -> dict:
out = {}
for a in ARMS:
for j in JOINTS:
out[f"{a}_{j}"] = RSPEC["arms"][a]["joints"][f"{a}_{j}"]
out[f"{a}_gripper"] = [0.0, 1.0]
return out
LIMITS = joint_limits(); LIM_ARR = np.array([LIMITS[n] for n in LAYOUT], dtype=np.float64) # (14, 2)
CAM0 = S.camera_info(CAMS)
JPOS = [f"{a}_{j}" for a in ARMS for j in JOINTS + ["gripper_joint"]]
PROPRIO_LAYOUT = (JPOS + [f"{a}_tcp_{k}" for a in ARMS for k in ("x", "y", "z", "qw", "qx", "qy", "qz")] + [f"{a}_gripper_cmd" for a in ARMS]
+ [f"{j}_vel" for j in JPOS] + [f"{a}_gripper_opening" for a in ARMS] + [f"{a}_{j}_effort" for a in ARMS for j in JOINTS])
LINKS = [f"{a}_link{i}" for a in ARMS for i in range(1, 7)] + [f"{a}_camera" for a in ARMS]
KIN = {"endpoint": "/kinematics", "joint_names": [f"{a}_{j}" for a in ARMS for j in JOINTS], "frame": "the world frame of spec.world",
"quaternion_order": "wxyz", "links": LINKS, "tcp_frames": ["left_link6", "right_link6"], "ee_frames": ["left_link6", "right_link6"],
"ik_targets": ["left_link6", "right_link6"],
"requests": {"fk": {"op": "fk", "q": "12 joint angles: left joint1..6, right joint1..6"},
"ik": {"op": "ik", "targets": {"left_link6": "[x y z qw qx qy qz]", "right_link6": "(either or both)"},
"seed": "12 joint angles (optional; default: the current targets)"}},
"model": "numpy models of both arms from RoboDojo's ARX X5 URDF at their base poses (no physics, no simulated time)"}
STEP_GUARD = int(S.step_lim)
# Agent-ended episodes (PROTOCOL §3; RCB_AGENT_END, RCB_STEP_LIMIT in the task's sim service): the episode's step limit is
# the task's own RoboDojo step_lim (Session: env.step_lim; RCB_STEP_LIMIT must equal it), one step = one 25 Hz action (one
# row handed to Session.step, 10 physics steps), as RoboDojo's evaluator counts them; the end there is reason "step_limit",
# graded as it stands. The spec states the budget; every meta / status gives the steps left.
STEP_LIMIT = AE.STEP_LIMIT is not None
SPEC = {
"protocol": PROTOCOL, "protocol_compat": list(L.COMPAT), "simulator": "robodojo (RoboDojo's evaluator on Isaac Lab / Isaac Sim 5.1)",
"robot": "arx-x5-bimanual", "task_text": S.inst.get("goal_text") or S.instruction(), "control_hz": CTRL_HZ,
"physics_substeps": SUBSTEPS, "max_chunk_s": MAX_CHUNK_S, "max_watch_s": MAX_WATCH_S, "max_frames_per_call": MAX_FRAMES,
"cameras": {c: {"rgb": True, "depth": True, "mounted": MOUNT[c], "resolution": [int(FIRST[c].shape[1]), int(FIRST[c].shape[0])],
"calibration_in_obs": OBS_CALIBRATION, "intrinsic_cv": CAM0[c]["intrinsic_cv"], "mount": CAM0[c]["mount"]} for c in CAMS},
"default_cameras": list(CAMS),
"action_types": {
"joint_position": {"dim": ACTION_DIM, "layout": LAYOUT,
"units": "rad for <arm>_joint1..6; <arm>_gripper: RoboDojo's normalised gripper command, 1 = fully open .. 0 = closed",
"limits": LIMITS,
"semantics": "RoboDojo's own action: absolute joint targets, one row per 25 Hz action, RoboDojo's controller moving each "
"arm to the targets over the action's 10 physics steps; rows `dt` apart (a multiple of 1/25 s, at most "
"0.5 s; anything else is rejected, never rounded), hold or linear interpolation between rows; "
"out-of-limit values are clipped and reported"},
"ee_pose": {"dim": 16, "layout": [f"{a}_{k}" for a in ARMS for k in ("x", "y", "z", "qw", "qx", "qy", "qz", "gripper")],
"frame": "the world frame of spec.world", "quaternion": "wxyz, unit norm",
"semantics": "per row, both arms' link6 targets are solved by the arms' kinematic model (the same as /kinematics, "
"seeded with the current targets) and executed as a joint move at most 0.25 m/s; `dt` is ignored; an arm "
"whose solution misses its target by more than 1 cm, or ends more than 1 cm off, is reported in plan[]; "
"the grippers take the row's commands (0..1) at the row's first action",
"note": "the pose is the arm's link6 frame, the end-effector frame RoboDojo reports; same frame as the proprio tcp columns"},
"not_yet": ["joint_delta", "joint_velocity", "ee_delta", "joint_torque"]},
"proprio_layout": PROPRIO_LAYOUT,
"proprio_groups": {
"joint_pos": [0, 14, "measured joint positions: left joint1..6 (rad), left gripper joint (m), right joint1..6, right gripper joint"],
"tcp_pose": [14, 14, "measured link6 poses: left then right, each x y z (m) + quaternion wxyz, world frame"],
"gripper_cmd": [28, 2, "the gripper commands in force (0..1): left, right"],
"joint_vel": [30, 14, "measured joint velocities, the joints of joint_pos (rad/s, m/s for the gripper joints)"],
"gripper_opening": [44, 2, "the measured gripper joint mapped to 0..1 (1 = fully open, 0 = closed on nothing); a gripper closed on "
"something stops above 0"],
"joint_effort": [46, 12, "applied torques of left joint1..6 then right joint1..6 (N m): they grow when an arm pushes on something"]},
"static": {},
"world": {"frame": "RoboDojo's scene frame: x to the right as seen from behind the arms, y forward from the arm bases towards the "
"far side of the table, z up; the arm bases and every link are in it (spec.robot_spec)",
"up": "+z", "units": {"length": "m", "angle": "rad", "time": "s"}, "quaternion_order": "wxyz",
"camera_axes": "intrinsic_cv / extrinsic_cv: OpenCV camera axes (x right, y down, z forward); cam2world_gl: OpenGL axes (x right, y up, z back)"},
"budget": {"reset": 0, "steps": STEP_GUARD if STEP_LIMIT else None, "wall_clock_s": float(os.environ.get("RCB_WALL_CLOCK_S", "3600"))},
# PROTOCOL §4.5: no step budget, but a technical cap is stated here. The tooling's runaway guard ends the episode after
# STEP_GUARD actions (reason "step_guard"): 10 min of simulated time.
"max_control_steps": STEP_GUARD,
"episode": (f"one episode, no reset, with a step limit of {STEP_GUARD:,} steps (RoboDojo's own limit for this task; one step = one "
f"25 Hz action, 1/25 s of simulated time; meta control_steps / steps_left): it ends at the limit (reason \"step_limit\", "
f"graded as it stands), when a check fails for good, when your time is up, on a simulator error, or at POST /finish"
if STEP_LIMIT else
f"one episode, no reset: it ends at the first action where RoboDojo's check for the task passes (checked after every "
f"action), when a check fails for good, when your time is up, on a simulator error, or after {STEP_GUARD:,} actions "
f"({STEP_GUARD / CTRL_HZ / 60:.0f} min of simulated time: a technical cap, reason \"step_guard\", spec.max_control_steps; "
f"graded as it stands)"),
"robot_spec": RSPEC,
"kinematics": KIN,
"depth": {"modes": list(L.DEPTH_MODES), "default": "none", "kind": "z-depth", "unit": "m", "invalid": 0, "max_m": L.DEPTH_MAX_M,
"frames_encoding": "uint16 mm, zlib", "noise": L.DEPTH_NOISE},
}
L.check_spec(SPEC) # a spec that misses a 2.2 field or names a forbidden one never starts the episode
EP.scene_built() # the watchdog stands down: a long resume replay below is not a hung build
def check_success() -> bool:
return S.success()
def proprio_row() -> np.ndarray:
P = S.proprio()
return np.array(P["joint_pos"] + P["tcp_pose"] + P["gripper_cmd"] + P["joint_vel"] + P["gripper_opening"] + P["joint_effort"], dtype=np.float32)
def grab(cams, want_depth: bool):
return {c: S.render_frame(c) for c in cams}, ({c: S.render_depth(c) for c in cams} if want_depth else {})
def extrinsics(cams) -> dict:
return {c: np.asarray(ci["extrinsic_cv"], dtype=np.float64) for c, ci in S.camera_info(cams).items()}
def video_fps(meta: dict) -> int:
"""The request's frame rate (default 5): outside [1, 30] it is a 400 (2.1's rule), never a failed encode that would end
the episode."""
fps = meta.get("video_fps")
if fps is None:
return 5
if isinstance(fps, bool) or not isinstance(fps, (int, float)) or not (1 <= fps <= 30):
raise ValueError("video_fps must be in [1, 30]")
return int(fps)
class Rec(L.Recorder):
def _skip(self) -> None:
"""A frame past the budget (a long ee_pose move) is still rendered (the observation, cached for the state) when
the live video, which records only the states a call rendered, wants one there; it is not kept in the response."""
try:
if VIDEO.wants(ctrl_steps()):
S.observe()
except Exception: # noqa: BLE001 -- the video loses this frame; the call goes on
pass
def recorder(cams, fps: int, meta: dict) -> L.Recorder:
return Rec(cams, max(1, int(round(CTRL_HZ / max(1, fps)))), grab, proprio_row, extrinsics, ctrl_steps,
depth=L.depth_mode(meta), clock_hz=CTRL_HZ, max_images=MAX_FRAMES)
def meta_common(cams) -> dict:
n = ctrl_steps()
cal = {c: {k: ci[k] for k in ("intrinsic_cv", "extrinsic_cv", "cam2world_gl", "image_size")} for c, ci in S.camera_info(cams).items()}
return {"step": E["step"], "calls": E["calls"], "control_steps": n, "physics_steps": n * SUBSTEPS, "sim_time_s": round(n / CTRL_HZ, 3),
"time_s": round(L.agent_time(E), 2), "time_left_s": max(0.0, round(SPEC["budget"]["wall_clock_s"] - L.agent_time(E), 1)),
"proprio": {"layout": SPEC["proprio_layout"], "now": proprio_row().tolist()}, "done": E["done"], "reason": E["reason"],
**AE.steps_left(n),
**({"cameras": cal} if OBS_CALIBRATION else {})}
def save_call_evidence(idx: int, meta: dict, rec: L.Recorder) -> None:
d = os.path.join(DATA, "calls", f"{idx:06d}"); os.makedirs(d, exist_ok=True)
L._write_json(os.path.join(d, "meta.json"), L._jsonable(meta))
try:
from PIL import Image
for c in rec.cams:
fr = [f for f in rec.frames[c] if f is not None]
if fr: Image.fromarray(np.ascontiguousarray(fr[-1])).save(os.path.join(d, f"last_{c}.jpg"), quality=85)
except Exception: # noqa
pass
# ---------------------------------------------------------------- executing chunks
def clip_row(row: np.ndarray, clipped: list, ri: int) -> np.ndarray:
r = row.copy()
for j, name in enumerate(LAYOUT):
lo, hi = LIM_ARR[j]
if r[j] < lo or r[j] > hi:
clipped.append({"row": ri, "index": int(j), "reason": f"{name} limit [{lo:.4f},{hi:.4f}]"}); r[j] = float(np.clip(r[j], lo, hi))
return r
def end_reason() -> str:
return S.end_reason() or "step_guard" # success | failed_check | step_guard
def ctrl_step(a: np.ndarray, rec: L.Recorder) -> bool:
"""One recorded action, journaled for crash recovery. True when the call must stop here: the episode is over (success,
a failed check, the runaway guard), or the service stops the call (max_call_wall_s, the client hung up)."""
a = np.asarray(a, dtype=np.float32) # the exact row Session.step executes (and records)
over = S.step(a); stop = EP.journal(a)
rec.tick() # past the call's budget (a long ee_pose move) it films no more; the last frame is taken
VIDEO.offer(ctrl_steps()) # a frame only if the call rendered this action (past the budget too: Rec._skip)
if AE.ENABLED: # the reward only records the state's success; a failed check ends it, the step limit is the cap
AE.hidden_success(E, check_success(), log, ctrl_steps())
if over: finish("step_limit" if end_reason() == "step_guard" else end_reason())
return E["done"] or stop
if over: finish(end_reason())
return E["done"] or stop
def run_joint_position(A: np.ndarray, dt: float, interp: str, rec: L.Recorder, clipped: list) -> None:
steps_per_row = int(round(dt * CTRL_HZ))
q_prev = np.asarray(S.hold_action(), dtype=np.float64)
for ri, row in enumerate(A):
E["step"] += 1
target = clip_row(row.astype(np.float64), clipped, ri)
for k in range(1, steps_per_row + 1):
a = target.copy() if interp == "hold" else q_prev + (target - q_prev) * (k / steps_per_row)
if ctrl_step(a, rec): return
q_prev = target
def run_ee_pose(A: np.ndarray, rec: L.Recorder, plan: list, clipped: list) -> None:
speed, tol = 0.25, 0.01
for ri, row in enumerate(A):
E["step"] += 1
cur = np.asarray(S.hold_action(), dtype=np.float64)
targets, grips = {}, {}
for i, a in enumerate(ARMS):
r = np.asarray(row[8 * i:8 * i + 8], dtype=np.float64)
g = float(r[7])
if not (0.0 <= g <= 1.0):
clipped.append({"row": ri, "index": 8 * i + 7, "reason": f"{a}_gripper limit [0, 1]"}); g = float(np.clip(g, 0.0, 1.0))
targets[f"{a}_link6"] = list(r[0:3]) + list(r[3:7] / max(1e-9, float(np.linalg.norm(r[3:7]))))
grips[a] = g
q12 = np.concatenate([cur[M.ARM_SLICE[a]] for a in ARMS])
sol = S.ik(targets, seed=q12)
goal = cur.copy()
st, err = {}, {}
for k, a in enumerate(ARMS):
e = sol["error"][a]
goal[M.ARM_SLICE[a]] = sol["q"][6 * k:6 * k + 6]
goal[M.GRIP_INDEX[a]] = grips[a]
st[a] = "Success" if e["pos_m"] < tol and e["rot_rad"] < 0.05 else f"no solution within 1 cm (model error {e['pos_m']:.3f} m)"
p_now = S.fk(q12); p_goal = S.fk(goal[np.r_[0:6, 7:13]])
dist = max(float(np.linalg.norm(np.asarray(p_goal[f"{a}_link6"][:3]) - np.asarray(p_now[f"{a}_link6"][:3]))) for a in ARMS)
n = max(1, int(math.ceil(dist / (speed / CTRL_HZ))))
stopped = False
for k in range(1, n + 1):
act = cur + (goal - cur) * (k / n)
act[M.GRIP_INDEX["left"]], act[M.GRIP_INDEX["right"]] = grips["left"], grips["right"]
act = clip_row(act, clipped, ri)
if ctrl_step(act, rec):
stopped = True; break
tcp = np.asarray(S.proprio()["tcp_pose"])
for k, a in enumerate(ARMS):
err[a] = round(float(np.linalg.norm(tcp[7 * k:7 * k + 3] - np.asarray(targets[f"{a}_link6"][:3]))), 4)
if st[a] == "Success" and err[a] > tol:
st[a] = f"reached {err[a]:.3f} m from the target"
if stopped:
st[a] = "episode over" if E["done"] else f"stopped ({EP.stop_reason})"
plan.append({"row": ri, **{f"{a}_arm": st[a] for a in ARMS}, **{f"{a}_ee_error_m": err[a] for a in ARMS}})
if E["done"] or EP.stopping: return
# ---------------------------------------------------------------- request fields
# PROTOCOL §4.6: a malformed request is a 400 and executes nothing. Every field is read and checked by the functions below
# BEFORE the first action; a field that is absent or JSON null takes its default, a value of the wrong type or out of range
# is a ValueError (the layer's 400). as_400() turns anything else a bad value can raise while it is being read (TypeError,
# OverflowError, AttributeError, ...) into that 400 too: only an error while the simulator is stepping is a simulator error
# (500, the episode ends).
def number(meta: dict, key: str, default: float) -> float:
v = meta.get(key)
if v is None:
return float(default)
if isinstance(v, bool) or not isinstance(v, (int, float)) or not math.isfinite(v):
raise ValueError(f"{key} must be a finite number")
return float(v)
def choice(meta: dict, key: str, default: str, allowed: tuple) -> str:
v = meta.get(key)
if v is None:
return default
if not isinstance(v, str) or v not in allowed:
raise ValueError(f"{key} must be {'|'.join(allowed)}")
return v
def cameras(meta: dict) -> list:
v = meta.get("cameras")
if v is None:
return list(CAMS)
if isinstance(v, str):
v = [v]
if not isinstance(v, (list, tuple)) or not all(isinstance(c, str) for c in v):
raise ValueError(f"cameras must be a list of camera names ({', '.join(CAMS)})")
return list(dict.fromkeys(c for c in v if c in CAMS)) or list(CAMS) # each camera once, in the order asked for
def note_text(meta: dict) -> str:
v = meta.get("note")
if v is None:
return ""
if not isinstance(v, str):
raise ValueError("note must be a string")
return v[:500]
def as_400(parse: Callable, *args):
try:
return parse(*args)
except ValueError:
raise
except Exception as exc: # noqa: BLE001 -- nothing has been executed yet: a bad request, not a simulator error
raise ValueError(f"malformed request: {exc!r}"[:300]) from None
EE_POS_MAX_M = 3.0 # an ee_pose target further than this from the world origin on any axis is a 400 (each arm reaches < 1 m)
def check_ee_rows(A: np.ndarray) -> None:
"""ee_pose rows that cannot be meant (PROTOCOL §4.6: a 400, nothing executed): a position outside the +-EE_POS_MAX_M
box around the world origin (a unit slip), or a quaternion of zero length."""
for ri, row in enumerate(A):
for i, a in enumerate(ARMS):
r = row[8 * i:8 * i + 8]
if float(np.max(np.abs(r[0:3]))) > EE_POS_MAX_M:
raise ValueError(f"ee_pose row {ri}: the {a} position {[round(float(v), 4) for v in r[0:3]]} is outside the "
f"+-{EE_POS_MAX_M} m box around the world origin (metres, world frame)")
if float(np.linalg.norm(r[3:7])) < 1e-6:
raise ValueError(f"ee_pose row {ri}: the {a} quaternion (wxyz) has zero length")
def check_frames(cams, fps: int, depth: str, control_steps: int) -> None:
"""A call whose length is known before it runs (a joint_position chunk, an /observe) is refused when it would capture
more than MAX_FRAMES images (the layer's check: a 400, nothing executed): the Recorder films every `every`-th action
plus the first and the last frame, whatever `video` asks to be returned."""
every = max(1, int(round(CTRL_HZ / max(1, fps))))
L.check_frames(len(cams), L.frames_per_call(control_steps, every), depth in ("frames", "legacy"), MAX_FRAMES)
def parse_submit(meta: dict, A: np.ndarray) -> tuple:
"""Every field of a /submit, checked; nothing is executed here."""
typ = meta.get("type")
if not isinstance(typ, str) or typ not in ("joint_position", "ee_pose"):
raise ValueError(f"type must be one of joint_position, ee_pose (not yet: {SPEC['action_types']['not_yet']})")
dt = number(meta, "dt", 1.0 / CTRL_HZ); interp = choice(meta, "interpolate", "linear", ("hold", "linear"))
cams = cameras(meta); fps = video_fps(meta); video = choice(meta, "video", "mp4", ("mp4", "frames", "both", "none"))
note = note_text(meta); depth = L.depth_mode(meta)
dim = SPEC["action_types"][typ]["dim"]
if A.ndim != 2 or A.shape[1] != dim: raise ValueError(f"{typ} actions must be (T, {dim}), got {list(A.shape)}")
if not np.all(np.isfinite(A)): raise ValueError("actions contain NaN/inf")
if not (1.0 / CTRL_HZ - 1e-9 <= dt <= 0.5): raise ValueError(f"dt must be in [{1.0 / CTRL_HZ:.4f}, 0.5] s")
if abs(dt * CTRL_HZ - round(dt * CTRL_HZ)) > 1e-6:
raise ValueError(f"dt must be a multiple of the 1/{CTRL_HZ} s control period (k/{CTRL_HZ} s, k = 1..{int(0.5 * CTRL_HZ)}); got {dt}")
if typ == "joint_position" and A.shape[0] * dt > MAX_CHUNK_S + 1e-9: raise ValueError(f"chunk longer than max_chunk_s={MAX_CHUNK_S}: {A.shape[0]} rows x {dt} s")
if typ == "ee_pose" and A.shape[0] > 50: raise ValueError("ee_pose: at most 50 rows per submit")
if typ == "ee_pose": check_ee_rows(A)
# joint_position: the chunk's length is known here. ee_pose: a row lasts as long as its move (known after its IK), so
# the recorder stops filming once the call has captured MAX_FRAMES images and the rows go on (meta "frames_skipped")
if typ == "joint_position": check_frames(cams, fps, depth, A.shape[0] * int(round(dt * CTRL_HZ)))
return typ, dt, interp, cams, fps, video, note
def do_submit(meta: dict, A: np.ndarray) -> tuple[dict, list]:
typ, dt, interp, cams, fps, video, note = as_400(parse_submit, meta, A) # a 400 before anything moves
rec = recorder(cams, fps, meta); clipped: list = []; plan: list = []
E["calls"] += 1; step0 = E["step"]
before = ctrl_steps(); rec.tick(force=True)
if typ == "joint_position": run_joint_position(A, dt, interp, rec, clipped)
else: run_ee_pose(A, rec, plan, clipped)
rec.tick(force=True, last=True); VIDEO.offer(ctrl_steps())
succ = bool(E.get("now_success")) if AE.ENABLED else (E["success"] or check_success()) # agent-ended: never latched
if not AE.ENABLED: E["success"] = succ
out = {**meta_common(cams), "chunk_id": E["calls"], "type": typ, "executed_rows": E["step"] - step0, "control_steps_chunk": ctrl_steps() - before,
"physics_steps_chunk": (ctrl_steps() - before) * SUBSTEPS, "clipped": clipped[:200], "n_clipped": len(clipped), "plan": plan, "rejected": None, "ok": True,
**({"frames_skipped": rec.skipped} if rec.skipped else {})}
log({"ev": "submit", "call": E["calls"], "type": typ, "rows": E["step"] - step0, "dt": dt, "interpolate": interp, "cameras": cams, "note": note,
"depth": rec.mode, "control_steps": ctrl_steps() - before, "n_clipped": len(clipped), "plan": plan[:50], "success": succ,
**({"stopped": EP.stop_reason} if EP.stopping else {})})
save_call_evidence(E["calls"], {k: v for k, v in out.items() if k != "cameras"}, rec)
if succ and not AE.ENABLED: finish("success")
out["done"] = E["done"]; out["reason"] = E["reason"]
return out, rec.parts(video, fps)
def parse_observe(meta: dict) -> tuple:
"""Every field of an /observe, checked; nothing is executed here."""
cams = cameras(meta); seconds = number(meta, "seconds", 0.0); fps = video_fps(meta)
video = choice(meta, "video", "frames", ("mp4", "frames", "both", "none")); depth = L.depth_mode(meta)
if seconds < 0 or seconds > MAX_WATCH_S: raise ValueError(f"seconds must be in [0, {MAX_WATCH_S}]")
check_frames(cams, fps, depth, int(round(seconds * CTRL_HZ)))
return cams, seconds, fps, video
def do_observe(meta: dict) -> tuple[dict, list]:
cams, seconds, fps, video = as_400(parse_observe, meta) # a 400 before anything moves
rec = recorder(cams, fps, meta); before = ctrl_steps()
if seconds > 0 and not E["done"]:
rec.tick(force=True)
hold = np.asarray(S.hold_action(), dtype=np.float64)
for _ in range(int(round(seconds * CTRL_HZ))):
if ctrl_step(hold, rec): break
rec.tick(force=True, last=True); VIDEO.offer(ctrl_steps())
if not AE.ENABLED: # agent-ended: ctrl_step recorded the state's success, nothing ends here
succ = E["success"] or check_success(); E["success"] = succ
if succ: finish("success")
else:
rec.tick(force=True, last=True) # the current frames: no physics
E["calls"] += 1
out = {**meta_common(cams), "obs_id": E["calls"], "seconds": seconds, "control_steps_watch": ctrl_steps() - before,
"physics_steps_watch": (ctrl_steps() - before) * SUBSTEPS, "ok": True}
log({"ev": "observe", "call": E["calls"], "seconds": seconds, "cameras": cams, "depth": rec.mode, "control_steps": ctrl_steps() - before,
**({"stopped": EP.stop_reason} if EP.stopping else {})})
if seconds > 0: save_call_evidence(E["calls"], {k: v for k, v in out.items() if k != "cameras"}, rec)
return out, rec.parts(video if seconds > 0 else ("frames" if video == "mp4" else video), fps)
def do_kinematics(req: dict) -> dict:
"""Stateless FK / IK on the arms' numpy models (never the simulator)."""
return S.kinematics(req)
def status() -> dict:
return {"step": E["step"], "calls": E["calls"], "control_steps": ctrl_steps(), "done": E["done"], "reason": E["reason"],
"elapsed_s": round(time.time() - E["t0"], 1),
**AE.steps_left(ctrl_steps())}
def end_from_signal() -> None:
"""The collect hook's SIGUSR1 (rcb_limited.Episode runs this in a thread): end the episode, dump it, exit 0."""
with LOCK:
E["exiting"] = True
if not E["done"]: finish("timeout")
else: dump()
log({"ev": "exit", "reason": "collect hook: episode dumped (graded by the live episode), releasing the simulator"})
if not os.path.exists(PID_FILE): # a SIGUSR1 that came in during a resume: the hook still needs to see this process end
open(PID_FILE, "w").write(str(os.getpid()))
sys.stdout.flush(); sys.stderr.flush()
os._exit(0) # frees the GPU and the memory: finalize.sh writes final.json once this process is gone
def replay_row(row) -> None:
"""One journaled row during a resume (no rendering asked for). A row the scene can no longer take (the episode ended
earlier than logged) is skipped: the state hash check then refuses the resume (class exact)."""
if not S.done:
S.step(np.asarray(row, dtype=np.float32))
S.record_start() # the resume replay is recorded too: trajectory.npz is the whole episode
VIDEO.offer(0) # the start state (rendered for /spec's images; a restarted service has it on disk already)
resumed = EP.begin(state=E, step=replay_row, state_sha=S.state_sha, proprio=proprio_row, spec=SPEC, on_end=end_from_signal,
start_event={"task": TASK, "layout": S.inst.get("layout_file"), "cameras": CAMS, "kinematics_check": S.kinematics_check()})
if resumed:
VIDEO.truncate(ctrl_steps()) # frames of the call lost in the crash show rolled-back states
E["ready"] = True; write_final()
open(PID_FILE, "w").write(str(os.getpid()))
print(f"{PROTOCOL} sim ready (startup {EP.n_startups}, resumes {EP.resumes})", flush=True)
AE.serve(AE.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=log, episode=EP, state_sha=S.state_sha, proprio=proprio_row,
**({"finish": lambda: finish("finished")} if AE.ENABLED else {})))