# Patches

The JSON Patch operations a scene accepts, how they apply, and the report you get back.

Agents build scenes by streaming [RFC 6902](https://www.rfc-editor.org/rfc/rfc6902) JSON Patches. A patch is one operation, an array of operations, or the same as JSON text. Paths are [RFC 6901](https://www.rfc-editor.org/rfc/rfc6901) JSON Pointers into the scene document. Each patch applies atomically.

```json title="patch.json"
[
  { "op": "add", "path": "/nodes/orb", "value": { "mesh": { "geometry": { "sphere": {} } } } },
  { "op": "replace", "path": "/nodes/orb/transform/position", "value": [0, 1, 0] }
]
```

## Operations

| Op | Fields | Description |
| --- | --- | --- |
| `add` | `path`, `value` | Add an object member or insert into an array. `/-` appends. An existing member is replaced. |
| `remove` | `path` | Remove a member or array item. |
| `replace` | `path`, `value` | Replace a value that exists. |
| `move` | `from`, `path` | Move a value, for example to rename an id. |
| `copy` | `from`, `path` | Copy a value. |
| `test` | `path`, `value` | Fail the patch unless the value matches. Numbers compare numerically. |

Unknown fields on an op are rejected. Ids with `/` or `~` are escaped as `~1` and `~0`.

## How A Patch Applies

1. The ops apply in order to a copy of the document. If one fails, the patch is rejected with `patch_failed`.
2. The result is validated. In streaming mode, references to ids that aren't written yet are warnings.
3. If the result has errors, the patch is rejected and the scene is unchanged. The report explains why, with a fix for each issue.
4. Otherwise it's committed. The revision goes up by one and the document is re-serialized with defaults filled in.

Because defaults are filled in, you can `replace` `/nodes/orb/transform/position` right after adding an empty `orb`. The `transform` already exists.

## The Report

Every rejection, and every committed patch with warnings, comes with a report. It matches `schema/report.schema.json`.

```json title="report.json"
{
  "issues": [
    {
      "severity": "error",
      "code": "patch_failed",
      "path": "/nodes/ghost/transform",
      "message": "op 0: `/nodes/ghost` does not exist",
      "suggested_fix": "add it first with {\"op\":\"add\",\"path\":\"/nodes/ghost\",\"value\":...}"
    }
  ]
}
```

| Field | Description |
| --- | --- |
| `severity` | `error` blocks rendering. `warning` doesn't. |
| `code` | A stable code. See [Error Codes](/docs/reference/errors). |
| `path` | A JSON Pointer to the bad value. `""` is the root. |
| `message` | What's wrong, for people and models. |
| `suggested_fix` | What to do. Often a ready-made patch. |

## In The Browser

`player.patch()` queues a patch for the next frame. A rejected patch calls `onError` and bumps `stats.rejected`. See [Player](/docs/reference/player).

```ts title="stream.ts"
for await (const line of agentLines) player.patch(line)
```

## In Rust

`SceneStream` in `motion-sdk-spec` holds the document while an agent streams.

```rust title="stream.rs"
use motion_sdk_spec::SceneStream;

let mut stream = SceneStream::new();
match stream.apply_str(r#"{"op":"add","path":"/nodes/orb","value":{}}"#) {
    Ok(applied) => println!("revision {}", applied.revision),
    Err(report) => println!("{report}"),
}
let report = stream.finalize(); // final-mode check when the agent is done
```

| Method | Description |
| --- | --- |
| `SceneStream::new()` | Start from an empty scene with every top-level map present. |
| `SceneStream::from_value(&doc)` | Start from an existing document. |
| `.with_mode(Mode::Final)` | Validate commits in final mode. |
| `.apply(&ops)`, `.apply_str(json)` | Apply a patch atomically. Returns `Applied { revision, changes, report }` or the rejecting `Report`. |
| `.finalize()` | Validate the current scene in final mode. |
| `.scene()`, `.document()`, `.revision()` | The typed scene, the JSON document and the commit count. |

See [Streaming](/docs/concepts/streaming) for the agent loop.
