> ## 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.

# Capabilities

> Reference for rua.capabilities and the wire: robot_capability, RobotClient, the rua/0 frame sequence, the msgpack codec, and the sim's JSON-RPC control channel.

`rua.capabilities` holds the `rua/0` wire both sides share: the capability factory, the client, and the codec.
Direct control and code control are the clients of a running env, so an agent does not dial it.

```python theme={"dark"}
from rua.capabilities import PROTOCOL, RobotClient, packb, robot_capability, unpackb
```

## `robot_capability`

```python theme={"dark"}
robot_capability(*, name: str = "robot", url: str, contract: dict[str, Any]) -> hud.capabilities.Capability
```

Builds the manifest entry for a robot wire: protocol `rua/0`, the URL normalized with default scheme `ws`
and default port `9091`, and `params={"contract": contract}`. Bridges call it from `capabilities()`.

## `RobotClient`

```python theme={"dark"}
class RobotClient(hud.capabilities.CapabilityClient):
    protocol = "rua/0"
```

A live `rua/0` connection. Importing `rua` registers it with HUD, so `await run.client.open("robot")`
returns one. To dial directly:

```python theme={"dark"}
client = await RobotClient.connect(capability, token=token)
obs = await client.get_observation()
await client.send_action(action)
await client.close()
```

| Member | Description |
| - | - |
| `await RobotClient.connect(cap, *, token=None)` | Classmethod. Dial `cap.url` (30 s open timeout), read the metadata frame, and send the claim when the server requires one or `token` is given. Raises `RuntimeError` on an error frame or a rejected claim, and after 30 s without a first observation. |
| `contract` | Property: a copy of the capability's contract. |
| `await get_observation() -> dict` | The latest observation (see below). Raises `RuntimeError` on an error frame, and on every read after the connection ends. |
| `await send_action(action)` | Send one action as float32 under `"actions"`. |
| `await close()` | Stop receiving and close the socket. |

`get_observation` returns:

```python theme={"dark"}
{
    "data": {"observation/image": ndarray, "observation/state": ndarray},  # one env's row
    "terminated": bool,
    "reward": float,  # only when the sim sends one
    "meta": ...,      # only when the sim sends one
}
```

The client keeps only the newest unread observation; an older one is dropped when a newer one arrives.

## The wire

The robot wire (protocol `rua/0`, the constant `PROTOCOL`) is a WebSocket carrying msgpack frames, with numpy
arrays encoded by the [codec](#codec) below (`packb` / `unpackb`). The env is the server. A string (text) frame in either direction is
an error message, usually a traceback.

| # | Direction | Frame |
| - | - | - |
| 1 | server → client | Metadata: `{**bridge.metadata, "claim_required": True}`. |
| 2 | client → server | Claim: `{"claim": <token or None>}`, within `step_timeout` (30 s). |
| 3 | server → client | The first observation. |
| 4 | client → server | An action: `{"actions": float32 array}`. |
| 5 | server → client | The next observation, after the batch steps. Steps 4 and 5 repeat. |

An observation frame is the slot's row of every data array, plus `"terminated": bool`, plus `"reward"` when the
sim put one in its data. A frame with `terminated: true` is the slot's last; the bridge stops waiting for its
actions. Rejections (an unknown token, a slot that already has a connection, a malformed claim) arrive as a
string frame, then the socket closes.

`RobotClient` skips the claim when the server doesn't send `claim_required` and no token is given.

## The control channel

Each bridge also serves a TCP control channel speaking JSON-RPC 2.0, framed with HUD's control-channel framing
(`hud.environment.send_frame` / `read_frame`). `RobotEndpoint` is its client; requests are serialized, one in
flight at a time.

| Method | Params | Result |
| - | - | - |
| `capabilities` | `{"name": str}` | `{"capabilities": [manifest, ...]}` |
| `url` | `{}` | `{"url": "ws://host:port"}` |
| `contract` | `{}` | `{"contract": {...}}`, derived first if the bridge can |
| `reset` | task args, optionally `max_ticks` | `{"prompt": str, "token": str}` |
| `result` | `{"token": str \| None}` | The slot's grade; with `max_ticks`, plus `max_ticks` and, when the cap ended it, `"ended_by": "max_ticks"` |

Errors use code `-32001` when every slot is claimed (`SlotsFull`, which the endpoint retries) and `-32000`
otherwise.

Tokens look like `slot-<index>-<8 hex>`. A `reset` while every slot is free resets the sim for the whole batch;
later claims must pass identical task args, or get `ValueError`. A freed slot is reclaimable only once every slot
is free again.

## Codec

`packb(frame) -> bytes` and `unpackb(data)` are msgpack with numpy support, so nested arrays and numpy scalars
travel without manual wrapping. Both ends of the wire use them.

| Value | On the wire |
| - | - |
| `numpy.ndarray` | A map with `__ndarray__: true`, `data` (the raw bytes), `dtype` (for example `"<f4"`) and `shape`. |
| numpy scalar | A map with `__npgeneric__: true`, `data` (the Python value) and `dtype`. It comes back as the numpy scalar. |

Keys of those maps are byte strings. Structured, object and complex dtypes raise `ValueError` on `packb`. Frames
packed by one version of `rua/0` unpack under any other.


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