> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rua.hud.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Concepts

> The pieces of a rua robot rollout: the sim process and its bridge, the endpoint, the contract, slots and tokens, the rua/0 wire, the control interfaces, and telemetry.

A rua rollout has two sides. The **environment side** runs the simulator, serves the robot to the agent as
tools, and grades the episode. The **agent side** is a language model that calls those tools. HUD's rollout
engine sits above both: it starts the environment, runs a task template, hands the agent a `Run`, and records
the reward.

```mermaid theme={"dark"}
flowchart LR
    subgraph ENV["<b>HUD environment process</b>"]
        direction TB
        T["<b>task template</b><br/>sim.reset / sim.result"]
        E["<b>RobotEndpoint</b>"]
        C["<b>DirectControl / CodeControl</b><br/>checked moves"]
        T --> E
        E --- C
    end

    subgraph SIM["<b>sim process</b>"]
        direction TB
        B["<b>RobotBridge</b><br/>slots + barrier"]
        G["<b>your simulator</b>"]
        B --> G
    end

    subgraph AG["<b>agent</b>"]
        A["<b>LLM</b><br/>tool calls or code"]
    end

    E -->|"control channel<br/>(JSON-RPC)"| B
    C <-->|"robot wire<br/>(rua/0 WebSocket)"| B
    A <-->|"mcp"| C

    classDef node fill:#f4f0e8,stroke:#1a1511,stroke-width:1px,color:#1a1511;
    classDef sim fill:#f7e3ee,stroke:#1a1511,stroke-width:1px,color:#1a1511;
    class T,E,C,A node;
    class B,G sim;
    style ENV fill:transparent,stroke:#a89a8c,stroke-width:1px;
    style SIM fill:transparent,stroke:#a89a8c,stroke-width:1px;
    style AG fill:transparent,stroke:#a89a8c,stroke-width:1px;
```

## The sim process and its bridge

A simulator usually wants the main thread, a GPU, and minutes to boot. rua keeps it out of the HUD
environment's process: the sim runs in a **sim process** of its own, wrapped in a **bridge**.

`RobotBridge` is the base class. A subclass implements three hooks, all synchronous:

| Hook | Does |
| - | - |
| `reset(**task_args) -> str` | Reset the sim for a new episode and return the task prompt. |
| `step(action)` | Advance the sim by one tick with a batched action `[N, A]`. |
| `get_observation()` | Return `(data, terminated)`: arrays with a leading `[N]` dim, and `[N]` booleans. |

The bridge serves two things: the **robot wire**, a WebSocket the controls connect to, and the **control channel**,
a JSON-RPC link the environment uses to reset and grade episodes. When it serves (`bridge.serve()`), the
calling thread becomes the sim thread and runs every hook, so a simulator that must own the main thread works.

`GymBridge` is a ready-made bridge for any gym-style env, and `attach_gym` is the one-line way to use it. See
[Serving a gym env](https://docs.rua.hud.ai/guides/serving-a-gym-env.md) and [Writing a bridge](https://docs.rua.hud.ai/guides/writing-a-bridge.md).

## The endpoint

`RobotEndpoint` is the environment's handle on a sim process. It either **spawns** the sim program and owns
it, or attaches to a **remote** one something else runs. Its calls map one-to-one onto the control channel:

| Call | Returns |
| - | - |
| `await sim.capabilities()` | The `robot` capability (and any others the bridge serves) to publish on the env. |
| `await sim.reset(**task_args)` | `{"prompt": ..., "token": ...}` for a newly claimed episode. |
| `await sim.result(token=...)` | The episode's score dict, which also frees its slot. |

`attach_gym` builds the endpoint and wires it into the environment's `initialize`, `shutdown` and
`task_teardown` hooks for you.

## The contract

The **contract** is the env's self-description, published inside the `robot` capability. Both sides read it:
the controls learn the robot's spaces from it, and telemetry labels plots with it.

```json theme={"dark"}
{
  "control_rate": 10,
  "features": {
    "observation/image": {"role": "observation", "type": "rgb"},
    "observation/state": {"role": "observation", "names": ["x", "y", "grip"]},
    "action": {"role": "action", "names": ["x", "y", "grip"]}
  }
}
```

Each feature has a `role`: `"observation"` or `"action"`. Observation features whose `type` is an image type
(`rgb`, `bgr`, `gray`, `depth`) are cameras; the rest are state. There is one action feature. Every field rua
reads is listed in the [Contract reference](https://docs.rua.hud.ai/reference/contract.md).

## Slots and tokens

A bridge serves `num_envs` **slots**, one per parallel env in the simulator (1 by default). Each episode
claims one:

* `sim.reset(...)` claims a slot and returns a **token** naming it. When every slot is free, the claim also
  resets the whole simulator; later claims join that batch, and must pass the same task args.
* A client sends the token as the first frame on the wire, which binds its connection to the slot.
* `sim.result(token=...)` grades that slot and frees it. A freed slot is reused only once every slot is free
  again, so a batch always runs whole episodes together.

A single-env bridge doesn't need the token: with exactly one claimed slot, a tokenless claim binds to it. Direct
control and code control connect without one, so they drive single-env sims. A vectorized bridge needs the
token, which a template can pass to a client that opens the wire itself through the task's bindings:

```python theme={"dark"}
yield {"prompt": ep["prompt"], "bindings": {"robot": {"token": ep["token"]}}}
```

The simulator steps in **lockstep**. Each tick, the bridge waits until every live slot has sent an action, then
calls `step` once with all of them stacked. Slots that are free, finished or disconnected get a hold action
(zeros by default), so the batch keeps its shape. A slot silent for longer than `step_timeout` (30 s) is
dropped from the barrier so its peers can keep going.

`max_ticks`, passed to `sim.reset(max_ticks=...)`, caps an episode's ticks. The bridge enforces it and the sim
never sees it.

## The robot wire

The robot capability speaks `rua/0`: msgpack frames over a WebSocket, with numpy arrays encoded as tagged
maps. The **env is the server**, and the controls dial in. The full frame sequence is in the
[Capabilities reference](https://docs.rua.hud.ai/reference/capabilities.md#the-wire).

Importing `rua` registers `RobotClient` as HUD's client for `rua/0`, so `run.client.open("robot")` returns one.
Direct control and code control hold the wire themselves, so the agent never opens it.

## Direct control

`DirectControl` serves the robot as one MCP `move` tool, so an LLM can drive it with keyframed moves. It reads the sim over the wire, checks each move, plays it tick by tick and records it. See [Direct Control](https://docs.rua.hud.ai/interfaces/direct-control.md).

## Code control

`CodeControl` serves the robot to an LLM that writes Python. A kernel lives for the episode, and the code moves the robot with `robot.move`, which takes the same keyframes and runs the same checks as `move`. See [Code Control](https://docs.rua.hud.ai/interfaces/code-as-policy.md).

## Telemetry

Every run records onto its HUD trace as **robot steps**, tagged with the schema `hud.robot.step.v1`:

* an **observation** step per tick, with labelled numeric state;
* an **inference** step per request, with the rows it played and how it ended;
* **video segments**, one H.264 stream per camera, in CMAF fragments.

`DirectControl` and `CodeControl` record automatically. Reporting needs `HUD_API_KEY`; without it, recording is
a no-op. See [Recording traces](https://docs.rua.hud.ai/guides/recording-traces.md).

## The eval interface

Separate from all of the above, `rua` re-exports a minimal, synchronous eval interface for robot tasks:
a `Robot` with `tools`, `reset` and `step`, an `Agent` with `act`, and `run` to play one episode. It does not
use HUD, the wire or the contract. See [The eval interface](https://docs.rua.hud.ai/guides/eval-interface.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.