Skip to content

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.

13.0 KB
Raw Blame
"""Fresh frozen-instance replay of the remote battery swap; trajectory metadata cannot change the scene."""

from __future__ import annotations

import argparse
import hashlib
import json
from pathlib import Path
import zipfile

import numpy as np

from runtime import Session
from scene import hands

TASK = "remote_battery_swap"
# Marvin layout: 14 arm joint targets (left J1-J7, right J1-J7), then the left and the right hand fingers.
WIDTHS = {"sharpa": 58, "wuji": 54}  # Marvin arm rig: 14 arm joints + 2 x 22 (Sharpa) or 2 x 20 (Wuji) fingers
MAX_ROWS = 6000
VIDEO_MAX_FRAMES = 300   # the replay video's frame budget: longer episodes are filmed at a larger stride
VIDEO_LABEL = {"speed": 1}   # the overlay's playback speed, set by replay() from the filmed stride
MAX_BYTES = 64_000_000  # the archive file and its expanded members (tests/grade.py: the same 64 MB)


def load_trajectory(path, allow_empty=False):
    """Validate an agent or service archive and return ``(actions, hand, actuator_names)``.

    ``actions`` is the only numeric input. ``hand`` (default ``sharpa``) selects the frozen scene to rebuild;
    the width must equal that hand's action width exactly. Optional ``task`` / ``actuator_names`` must match.
    Every other member (claimed success, states, configs) is ignored.
    """
    path = Path(path)
    if path.stat().st_size > MAX_BYTES:
        raise ValueError("Trajectory archive exceeds 64 MB")
    with zipfile.ZipFile(path) as archive:
        entries = archive.infolist()
        if len(entries) > 32 or sum(item.file_size for item in entries) > MAX_BYTES:
            raise ValueError("Expanded archive exceeds 64 MB")
        members = [item for item in entries if item.filename == "actions.npy"]
        if len(members) != 1:
            raise ValueError("Expected exactly one actions.npy member")
        with archive.open(members[0]) as stream:
            version = np.lib.format.read_magic(stream)
            if version == (1, 0):
                shape, _fortran, dtype = np.lib.format.read_array_header_1_0(stream, max_header_size=10000)
            elif version == (2, 0):
                shape, _fortran, dtype = np.lib.format.read_array_header_2_0(stream, max_header_size=10000)
            else:
                raise ValueError("Unsupported NumPy array format")
            minimum = 0 if allow_empty else 1
            if (
                len(shape) != 2
                or shape[1] not in WIDTHS.values()
                or not minimum <= shape[0] <= MAX_ROWS
                or dtype.kind not in "fiu"
            ):
                raise ValueError("Invalid action array header")
            if stream.tell() + shape[0] * shape[1] * dtype.itemsize != members[0].file_size:
                raise ValueError("Action array header differs from its payload")
    actions, hand, names = hands.read_trajectory(
        path, widths=WIDTHS, max_rows=MAX_ROWS, task=TASK, allow_empty=allow_empty, max_bytes=MAX_BYTES
    )
    if (len(actions) == 0 and not allow_empty) or len(actions) > MAX_ROWS or not np.isfinite(actions).all():
        raise ValueError(f"Expected 1 to {MAX_ROWS} finite action rows")
    return np.asarray(actions, dtype=np.float64), hand, names


def load_actions(path, allow_empty=False):
    """Validated actions only (any supported hand); see ``load_trajectory``."""
    return load_trajectory(path, allow_empty)[0]


def overlay(frame, features, phase, hand="Sharpa Wave"):
    from PIL import Image, ImageDraw, ImageFont

    canvas = Image.fromarray(frame)
    draw = ImageDraw.Draw(canvas)
    scale = frame.shape[1] / 1280
    try:
        font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", round(19 * scale))
    except OSError:
        font = ImageFont.load_default(size=round(19 * scale))
    label = "PASS" if features["success"] else "INCOMPLETE"
    if features["failure"]:
        label = "FAIL: " + features["failure"]
    draw.rectangle((0, 0, frame.shape[1], round(96 * scale)), fill=(23, 35, 29))
    phase = str(phase).replace("\n", " ").replace("\r", " ")[:64]
    draw.text(
        (round(18 * scale), round(10 * scale)),
        hand + " | remote battery swap | " + label + " | " + phase,
        font=font,
        fill="white",
    )
    draw.text(
        (round(18 * scale), round(39 * scale)),
        f"Stages {features.get('stage', 0)}/4 (door open, old AA in tray, new AA seated + to gold, door shut) | "
        f"door {1000 * features.get('cover_slide_m', 0):.0f} mm | released hold {features.get('hold_steps', 0)}/50",
        font=font,
        fill=(244, 214, 157),
    )
    draw.text(
        (round(18 * scale), round(67 * scale)),
        f"Native actuator replay, {VIDEO_LABEL['speed']:g}x | "
        "passive sliding door (friction detent), free AA cells, remote and tray",
        font=font,
        fill=(185, 205, 185),
    )
    return np.asarray(canvas)


class BadTrajectory(ValueError):
    """The submitted archive is unusable (reward 0, bad_trajectory = 1). Only this is reported as the submitter's
    fault; a scene-build or simulator error propagates (non-zero exit = infrastructure error)."""


def replay(path, video=None, camera="overview", fps=10, trusted_service=False, expected_hand=None, phases_path=None):
    try:
        actions, hand, names = load_trajectory(path, allow_empty=trusted_service)
    except Exception as error:  # noqa: BLE001 - any unreadable archive is the submitter's, never an infra error
        raise BadTrajectory(f"Invalid trajectory archive: {type(error).__name__}: {error}") from error
    if expected_hand is not None and hands.canonical(expected_hand) != hand:
        raise BadTrajectory(f"Trajectory hand {hand!r} differs from the recorded episode hand {expected_hand!r}")
    session = Session(TASK, hand=hand)  # not caught: a build error is an infrastructure error
    lo, hi = session.model.actuator_ctrlrange.T
    if names is not None and names != session.actuator_names:
        session.close()
        raise BadTrajectory("Trajectory actuator_names differ from the " + hand + " actuator layout")
    if len(actions) and (np.any(actions < lo - 1e-6) or np.any(actions > hi + 1e-6)):
        session.close()
        raise BadTrajectory("Actuator targets exceed native joint bounds")
    hand_label = session.hand_info["model"]
    phases = np.full(len(actions), "actuator replay")
    if video:
        try:
            with np.load(phases_path or path, allow_pickle=False) as archive:
                if (
                    "phases" in archive
                    and archive["phases"].shape == (len(actions),)
                    and archive["phases"].dtype.kind in "US"
                ):
                    phases = archive["phases"].copy()
        except Exception:  # noqa: BLE001 - phase labels only caption the video: an unreadable
            pass  # member keeps the default labels, never costs the video (review 0146)
    writer = None
    if video:
        import imageio.v2 as imageio

        video = Path(video)
        video.parent.mkdir(parents=True, exist_ok=True)
        # These are drawing settings only. Every original physics step is replayed.
        session.model.vis.quality.shadowsize = 1024
        session.model.vis.quality.offsamples = 1
        model = session.model
        for gid in range(model.ngeom):
            # Hide the Wuji vendor's translucent collision meshes (group 2); its visual meshes stay. Colors never
            # enter the physics.
            if (
                session.owner.get(int(model.geom_bodyid[gid])) == "hand"
                and (model.geom(gid).name or "").startswith("r_wuji_")
                and "_collision" in model.geom(gid).name
            ):
                model.geom_rgba[gid, 3] = 0.0
        writer = imageio.get_writer(video, fps=fps, codec="libx264", quality=8, macro_block_size=16)
    stride = round(1 / (session.control_dt * fps))
    if video:
        # At most VIDEO_MAX_FRAMES filmed steps, so the longest archive the row limit allows is filmed
        # within the grader's time. Frames only read the scene: physics, verdict and state hash are unchanged.
        stride = max(stride, -(-len(actions) // VIDEO_MAX_FRAMES))
        VIDEO_LABEL["speed"] = round(stride * session.control_dt * fps, 3)
    timeline = []
    try:
        if writer:
            writer.append_data(
                overlay(
                    session.render_frame(camera, width=960, height=544, shadows=False),
                    session.features(),
                    "initial state",
                    hand_label,
                )
            )
        for i, (action, phase) in enumerate(zip(actions, phases)):
            f = session.step(action)
            if writer and (i + 1) % stride == 0:
                frame = session.render_frame(camera, width=960, height=544, shadows=False)
                writer.append_data(overlay(frame, f, str(phase), hand_label))
            if writer and i % 500 == 0:
                print(f"Recorded native action {i}/{len(actions)}", flush=True)
            if i % 50 == 0 or i == len(actions) - 1 or session.done:
                timeline.append({"action": i, "phase": str(phase), **f})
            if session.done:
                # The episode ends at the first success or the first broken invariant (fail fast);
                # later rows are not executed.
                if writer and (i + 1) % stride:
                    frame = session.render_frame(camera, width=960, height=544, shadows=False)
                    writer.append_data(overlay(frame, f, str(phase), hand_label))
                break
        result = {
            "success": session.success,
            "bad_trajectory": 0,
            "reason": "success" if session.success else (session.failure or ""),
            "hand": session.hand,
            "action_dim": session.action_dim,
            "failure": session.failure,
            "failure_detail": session.failure_detail,
            "n_actions": len(actions),
            "rows_submitted": len(actions),
            "steps": session.steps,
            "initial_sha": session.initial_sha,
            "stage": session.features()["stage"],
            "state_sha": session.state_sha(),
            "verdict": session.features(),
            "timeline": timeline,
            "trajectory_sha256": hashlib.sha256(Path(path).read_bytes()).hexdigest(),
            "scene_sha256": hashlib.sha256(session.xml.encode()).hexdigest(),
            "recording": {
                "fps": fps,
                "playback_speed": round(stride * session.control_dt * fps, 3),
                "width": 960,
                "height": 544,
                "shadows": False,
                "shadowsize": 1024,
                "offsamples": 1,
            }
            if video
            else None,
        }
        if writer:
            from PIL import Image

            final = overlay(
                session.render_frame(camera, width=960, height=544, shadows=False),
                session.features(),
                "final released state" if session.success else "final state",
                hand_label,
            )
            for _ in range(fps * 2):
                writer.append_data(final)
            Image.fromarray(final).save(video.with_suffix(".png"))
    finally:
        if writer:
            writer.close()
        session.close()
    if video:
        result["video_sha256"] = hashlib.sha256(video.read_bytes()).hexdigest()
        result.update(video=True, video_stride=stride)
        video.with_suffix(".json").write_text(json.dumps(result, indent=2) + "\n")
    return result


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--traj", required=True)
    parser.add_argument("--task", default=TASK, choices=[TASK])
    parser.add_argument("--trusted-service", action="store_true")
    parser.add_argument("--out")
    parser.add_argument("--video")
    parser.add_argument("--camera", default="overview")
    parser.add_argument("--fps", type=int, choices=[5, 10, 25, 50])
    parser.add_argument("--hand", help="hand recorded by the trusted service; must equal the archive's hand")
    parser.add_argument("--phases", help="optional .npz with a 'phases' string array (video captions only)")
    args = parser.parse_args()
    try:
        # Software rendering in the bounded service collector uses fewer samples.
        # Both defaults execute every original control; the video is 1x up to VIDEO_MAX_FRAMES filmed steps.
        fps = args.fps if args.fps is not None else (5 if args.trusted_service else 10)
        result = replay(args.traj, args.video, args.camera, fps, args.trusted_service, args.hand, args.phases)
    except BadTrajectory as error:  # archive-read / trajectory validation only (L6)
        result = {
            "success": False,
            "bad_trajectory": 1,
            "reason": "bad_trajectory",
            "error": str(error)[:500],
            "hand": args.hand,
            "n_actions": 0,
            "steps": 0,
            "stage": 0,
        }
    if args.out:
        Path(args.out).write_text(json.dumps(result, indent=2) + "\n")
    print(json.dumps({k: v for k, v in result.items() if k != "timeline"}, indent=2))