Skip to content

Add specification for message patches - #584

Open
ttypic wants to merge 3 commits into
mainfrom
message-patch
Open

ttypic wants to merge 3 commits into
mainfrom
message-patch

Conversation

@ttypic

@ttypic ttypic commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds a MESSAGE_PATCH message action, which changes individual values inside an existing message's JSON data and individual fields of its extras. Until now a message could only be replaced wholesale (MESSAGE_UPDATE), or have a string or binary appended to the whole of its data (MESSAGE_APPEND).

Example

A publisher streams an AI response into one message, then marks it complete:

MESSAGE_CREATE  serial: s1  data: {"content":[{"type":"text","text":"Hel"}]}
                            extras.headers: {"status":"streaming"}

channel.patchMessage(Message(serial: "s1", patches: {
  data: [
    { op: "append", path: "/content/0/text", data: "lo" },
    { op: "set",    path: "/content/-",      data: { "type": "tool" } }
  ],
  extras: { headers: [{ op: "set", key: "status", value: "complete" }] }
}))

Subscribers receive a MESSAGE_PATCH for s1 that carries just these operations. They apply it to the message they already have:

patchedMessage = Message.applyPatch(message, patch)
// action: MESSAGE_UPDATE, version: <the patch's version>
// data:   {"content":[{"type":"text","text":"Hello"},{"type":"tool"}]}
// extras.headers: {"status":"complete"}

What changed

  • Publishing (RSL15, RTL32): patchMessage() is added next to updateMessage, deleteMessage and appendMessage. It sends the operations in the new Message.patches field (TM2v).

  • Types (MPA, MPD, MPH, MPU, MPP): MessagePatches holds a list of data operations (set / append / remove, addressed by an RFC 6901 JSON Pointer), a list of header operations, and userClaim / privileged operations, which only Ably sends.

  • Encoding of operation values (MPD3, MPD4): this works differently from a message's data:

    • On send, every JSON value, including numbers, booleans and null, is sent as JSON text with encoding: json. Operation values are never encrypted.
    • On receive, the json transform is not reversed, so the value stays as JSON text. applyPatch can then insert it verbatim, and the user can parse it into the type of the value it replaces.
  • Message.applyPatch (TM9): a static function that returns a new Message and does not change its input. It handles two forms of data:

    • decoded JSON (objects and arrays);
    • JSON text held as a string or binary. Here the SDK edits the text exactly as Ably does (TM9f5), so a stored copy stays identical. Whitespace, member order and number text are preserved.

    If any operation can't be applied, the whole patch fails with 40000/400.

  • Protocol: protocol.md documents the patches field, which Ably delivers from protocol version 7. The spec version goes to 6.5.0 and the protocol version to 7.

  • Docs and tests: docstrings are in api-docstrings.md. UTS test specs cover patchMessage over REST and realtime, the patch types and their encoding, and applyPatch.

@ttypic ttypic changed the title message-patch Add specification for message patches Nov 24, 2026
@ttypic
ttypic marked this pull request as ready for review November 25, 2026 12:56
@ttypic
ttypic requested review from VeskeR and lmars November 25, 2026 12:56
Add a `MESSAGE_PATCH` message action, through which a message can
change individual values inside an existing message's JSON `data`
and individual fields of its `extras`, rather than replacing them
wholesale as `MESSAGE_UPDATE` does.

- `patchMessage()` is added to RSL15 and RTL32 alongside update,
  delete and append, sending the operations in the new TM2v
  `patches` field.
- New MessagePatches types (MPA, MPD, MPH, MPU, MPP) describe the
  data, header, userClaim and privileged operations. MPD3/MPD4 define
  how operation data is encoded on send and left as JSON text on
  receive.
- `Message.applyPatch` (TM9) lets users apply a received patch to
  their copy of the message. When the copy holds JSON as text, it
  edits the text the way Ably does, so the copy stays byte-for-byte
  identical to the stored message.
- The protocol doc describes the `patches` field, which is delivered
  from protocol version 7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ttypic and others added 2 commits November 27, 2026 13:54
Cover `patchMessage()` over REST and realtime, the MessagePatches
types and their encoding, and `Message.applyPatch`, and record them
in the completion status matrix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
staging/pull/584 — 6d600a3c Deployed Nov 27, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant