Motion SDK

Patches

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

Agents build scenes by streaming RFC 6902 JSON Patches. A patch is one operation, an array of operations, or the same as JSON text. Paths are RFC 6901 JSON Pointers into the scene document. Each patch applies atomically.

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

Operations

OpFieldsDescription
addpath, valueAdd an object member or insert into an array. /- appends. An existing member is replaced.
removepathRemove a member or array item.
replacepath, valueReplace a value that exists.
movefrom, pathMove a value, for example to rename an id.
copyfrom, pathCopy a value.
testpath, valueFail 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.

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\":...}"
    }
  ]
}
FieldDescription
severityerror blocks rendering. warning doesn't.
codeA stable code. See Error Codes.
pathA JSON Pointer to the bad value. "" is the root.
messageWhat's wrong, for people and models.
suggested_fixWhat 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.

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.

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
MethodDescription
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 for the agent loop.