0602. MCP edits without a replica: top-key cache, value-checked undo, live change log
Status: Accepted (2026-11-27). Refines 0012 (no document replica) and 0008 for the MCP peer (WP-F, wave 2).
Context
The adapter holds no DocCore, but it must still:
- place new nodes on top of siblings it has not read;
- undo its own edits without clobbering what humans did since;
- tell the agent what others changed.
Reading the whole sibling list before every create, or the whole page before every undo, would cost a doc-worker round trip and (on the 662K page, 8,000 root children) hundreds of KB per call.
Decision
- Top-key cache. Each parent the agent writes into gets an entry holding an upper bound of its children’s keys.
- The bound comes from one
getNodes(parent, childDepth 1, [key]), read with minSeq = the newest seq the agent has seen. The agent’s own creates and every live broadcast (CREATE, CREATE_BULK/RESTORE roots, SET PARENT_INDEX) are folded in.
- The entry is created before the read, so broadcasts that arrive during the read are folded too.
- Max is commutative and idempotent, and deletes never invalidate an upper bound.
- It is cleared on
snapshot_stale and capped at 4,096 parents.
- Inserting above a specific sibling (
afterId), arranging and duplicating read the sibling list.
- Ids and blocks. Ids come from the realtime connection’s clientId space. Trees become
createBulk blocks (@linorahq/doc/inverse).
- Text. Agent text is measured with
@linorahq/doc/text, and w/h go in the same op (ADR 0014).
- Undo steps. Undo is per page, for the agent’s own steps only, up to 200.
- Structural steps (create, duplicate, delete) store ops: DELETE of the new roots, and RESTORE of blocks read before the delete (up to 50,000 nodes per call, otherwise the delete is refused with a hint to split it).
- Property steps store
{id, before, after} in JSON-op form. Undo re-reads the current values and writes before only where the value still equals after; redo is the mirror image. Comparisons are f32-tolerant, opacity at 1/255, and parent+key as a pair. Skipped values are reported.
- This is stricter than 0008’s “ops on nodes others deleted are dropped”: a human’s later edit to the same property also survives.
- Others’ changes.
get_changes answers from a log of live broadcasts. Broadcasts never include the author’s own batches, so the log is exactly “others”.
- It first asks the doc worker for the head (
changes?limit=1) and waits until the room has reached it.
- Created-then-deleted ids are reported as deleted.
- The log is capped at 200K ids. Older ranges fall back to the journal route, which mixes authors.
- Presence. Before an edit, the cursor goes to the center of the touched page bounds and the selection to the touched ids (tool
agent). The connection’s 20 Hz throttle coalesces it.
- Geometry. Page-space operations (translate, moveTo, scale about an anchor, reparent keeping position) read each parent’s ancestor chain, one
getNodes per tree level, and use the doc’s matrix convention.
- Scaling a group scales its content; frames resize their own box (Figma behaviour).
Alternatives
- A replica per agent session: fast local reads, but tens of MB per big page per session and seconds to open (rejected in 0012).
- Inverse-op undo without the value check: simpler, but it overwrites human edits made after the agent’s.
changes route only: mixes authors (authors is per range, not per id), so the agent cannot tell its own edits from others’.
- A server-side “append on top” key (no read at all): needs a protocol change. The cache gets the same effect.
Consequences
- A create costs one realtime round trip once the parent’s key is cached: about 10 ms for 1,000 nodes and 35 ms for 10,000 (median, ACK included).
- Property undo costs one extra
getNodes.
- Trees over about 3 MiB of ops are sent in several ordered batches, so they are not atomic.
- The adapter keeps O(parents touched + recent change ids) memory per session, never O(page).