Metadata-Version: 2.5
Name: rfabric-control-wire
Version: 0.1.1
Summary: Wire format primitives for the rFabric realtime control bridge (UDS/CBOR Frame envelope + payload constants)
Project-URL: Homepage, https://rfabric.io
Project-URL: Repository, https://github.com/rfabric/rfabric
Author-email: rFabric <engineering@rfabric.io>
License: Apache-2.0
Keywords: cbor,rfabric,robotics,teleoperation,uds
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: cbor2<6.0.0,>=5.6.0
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Description-Content-Type: text/markdown

# rfabric-control-wire

The control wire of the rFabric realtime control bridge: one CBOR frame format,
version 2, carried on the room's data tracks (operator, inference server,
robot) and on the Unix socket between `rfabricd` and a follower.

This repository holds every codec but the operator app's:

| Path | What |
| --- | --- |
| `src/rfabric_control_wire/` | Python package `rfabric-control-wire` (followers: `rfabric-yam-bridge`, `lerobot-teleoperator-rfabricd`) |
| `rust/` | Rust crate `rfabric-control-wire` (lib `rfabric_control_wire`; `rfabricd`, the `inference.serve` room client) |
| `vectors/` | Shared cases every codec decodes and re-encodes byte for byte, including the operator app's `controlWire.ts` |

## Frame

A CBOR map with exactly these keys, encoded in this order:

| Key | Type | Meaning |
| --- | --- | --- |
| `v` | uint | Control-wire version, `2` (`VERSION`). A frame with another or no `v` is dropped (`VersionError` / `Error::Version`). |
| `kind` | text | `control`, `state`, `ack`, `mode` |
| `seq` | uint | Increases by one per frame per (participant, track); receivers drop a `seq` not above the last accepted one. |
| `tsNs` | uint | Sender wall clock, ns since the Unix epoch; latency reporting only. |
| `payload` | map | `payload.kind` discriminates. |

On the Unix socket each frame is `[u32 LE length][CBOR frame]`, at most 64 KiB
(`MAX_FRAME_BYTES`). A frame on a lossy room track encodes to at most 1200
bytes (`MAX_LOSSY_FRAME_BYTES`).

Encoding is deterministic: definite-length maps and arrays, map keys in the
order of the contract, integers in the shortest form, floats as float32 when
the value round-trips exactly through float32 and float64 otherwise (NaN as
the float64 `0x7ff8000000000000`), byte strings as major type 2.

## Payloads

| Frame kind | `payload.kind` | Python | Rust |
| --- | --- | --- | --- |
| `control` | `joint_targets`, `joint_deltas`, `joint_velocity`, `end_effector_velocity`, `stop`, `home` (operator sources), `heartbeat` (rfabricd → follower), `action_chunk` (inference source) | `parse_control_payload` | `ControlPayload` |
| `state` | `snapshot` | `parse_state_payload` | `SnapshotPayload` |
| `ack` | `ping`, `pong`, `stop` (a follower's stop ack) | `parse_ack_payload` | `AckPayload` |
| `mode` | `capabilities` (robot, follower and inference forms), `takeover`, `release` | `parse_mode_payload` | `ModePayload` |

Every typed payload encodes back with `to_payload()` (Python) or `to_value()`
(Rust). A payload that fails to parse raises `PayloadError` /
`Error::Payload`; a follower answers it by holding position.

- `snapshot`: `values` and `applied` are float32 little-endian bytes in
  `Embodiment.state` and `Embodiment.actions` order. On the socket the follower
  sends `kind`, `embodiment`, `values`; rfabricd stamps `tick`, `applied`,
  `selected` and, when it changed, `received`: per control source its newest
  admitted chunk as `[rosterIndex, seq, observationTick, receivedTick,
  receivedUs]` (`ReceivedChunk`), `receivedUs` the robot's wall clock in
  microseconds at admission, the clock of the video frames' `user_timestamp`.
- `action_chunk`: `rows` is row-major little-endian, `i16` or `f32`; a row
  concatenates the source's action groups. `pack_rows` / `unpack_rows` pack
  them; `quantize_i16` / `dequantize_i16` map one element over its `low..high`
  bounds.
- `embodiment` (uint32) is `embodiment_fingerprint(state, actions)`: FNV-1a 32
  over `s:<name>:<width>;...|a:<name>:<width>;...`.
- Follower `capabilities`: `positionHold`, `home` (each position group's home
  row) and `velocityScale` (each group that takes `joint_velocity`: per
  element, the physical speed in the group's units per second of normalised
  velocity 1). `joint_velocity` values are normalised to `[-1, 1]`; rfabricd
  bounds them in physical units through `velocityScale` and sends them
  normalised again, so the follower's gain drives at most the group's
  `maxRate`. A group without `velocityScale` takes no `joint_velocity` in a
  session with motion limits.

## Follower rules

`conformance.MANDATORY_SAFETY_RULES`: rfabricd sends one command per action
group per control tick; a heartbeat at least every 100 ms; the follower holds
position within 200 ms of a stall and on a malformed payload, and declares that
it holds position in `capabilities.positionHold`.

## Vectors

`vectors/frames.json` (`{name, hex, frame}`; bytes in `frame` as
`{"$bytes": "<hex>"}`; a case with `"reject": "version"` must be dropped),
`vectors/fingerprints.json` and `vectors/quantization.json` are written by
`uv run python scripts/generate_vectors.py` and checked in. Regenerate only
after a contract change; every codec's vector test must then pass unchanged.
`vectors/motion-limit-ramp.json` is written by rfabricd's control arbiter test.

## Gates

```sh
uv run pytest
uv run ruff check
uv run mypy src
cd rust
cargo fmt --check
cargo clippy --all-targets --all-features -- -D warnings
cargo test --all-features
```

The Rust crate's async framing (`read_frame_async`, `write_frame_async`) is
behind the `tokio` feature.
