# Streaming Patches

Send a scene as RFC 6902 patches. Every accepted patch leaves a scene that renders.

Agents don't have to write a whole scene and then show it. They can send it as [RFC 6902](https://www.rfc-editor.org/rfc/rfc6902) JSON Patch operations, one small step at a time, and viewers watch it build while the agent is still writing.

```json
{"op": "add", "path": "/nodes/knot", "value": {"mesh": {"geometry": {"torus_knot": {}}}}}
{"op": "add", "path": "/materials/glow", "value": {"preset": "neon"}}
{"op": "add", "path": "/nodes/knot/mesh/material", "value": "glow"}
```

## Valid at Every Step

Each patch is applied to a copy of the scene and validated in streaming mode before it's committed:

- If the result is renderable, the patch is committed and the next frame shows it.
- If it would break the scene, like a wrong type or an unknown field, it's rejected with an issue and a fix. The last good scene stays on screen.
- References to things that don't exist yet, like a material written later, are warnings while streaming and errors in [final mode](/docs/concepts/validation#modes).

## In Rust

```rust
use motion_sdk_spec::{Dirty, SceneStream};

let mut stream = SceneStream::new();

// Uses a material that doesn't exist yet: accepted, with a warning.
let step = stream
    .apply_str(r#"{"op":"add","path":"/nodes/knot","value":{"mesh":{"geometry":{"torus_knot":{}},"material":"glow"}}}"#)
    .unwrap();
assert_eq!(step.report.warnings().count(), 1);

stream
    .apply_str(r#"{"op":"add","path":"/materials/glow","value":{"preset":"neon"}}"#)
    .unwrap();

// The change set tells the renderer what to update: here, one transform.
let step = stream
    .apply_str(r#"{"op":"replace","path":"/nodes/knot/transform/position","value":[0,1,0]}"#)
    .unwrap();
assert_eq!(step.changes.dirty, Dirty::TRANSFORM);

// Done: strict validation passes.
assert!(stream.finalize().is_clean());
```

## In the Browser

```ts
player.patch({ op: "replace", path: "/nodes/orb/transform/position", value: [0, 1, 0] })
player.patch([op1, op2, op3]) // a batch
player.patch(ndjson)          // or NDJSON text, one op per line
```

Patches are batched and handed to the engine once per animation frame. Rejected patches call `onError` with the engine's explanation.

## Change Sets

Every committed patch reports a `ChangeSet`: which parts of the scene are dirty, like a transform, a material or the structure. The renderer updates only those. A transform patch writes one instance slot; it doesn't rebuild buffers.

## Patching Tips

- Add id-keyed entries one at a time: `/nodes/<id>`, `/materials/<id>`.
- Add nodes before the materials they use if that's the order you think in. It's only a warning until the end.
- Escape `/` and `~` in ids as `~1` and `~0`, as RFC 6901 says.
- For per-frame motion from a game or physics engine, use [runtime overrides](/docs/reference/player#runtime-overrides), not patches.
