0008. Undo/redo: per-client stacks of inverse ops
Status: Accepted (2026-11-27)
Context
src/history.ts stores closures that rewrite primitive buffers. In multiplayer, undo must revert only my own changes and must not clobber others’ later edits to other properties.
Decision
- Every local commit applies ops with
inverse: true; the doc returns inverse ops computed from the state just before (create <-> delete with full props and subtree, set <-> previous values).
- The session keeps undo/redo stacks of these inverse batches (limit 200). Undo = commit the inverse as a new transaction (it gets a fresh seq) and push its inverse onto redo.
- Gestures (draw, color drag, pencil) collapse into one entry: the inverse is captured at the first touch of each (node, prop).
- If someone else deleted a node my undo targets, those ops are rejected and skipped; the rest applies (Figma behaviour). Undo restores a property value even if someone changed it since (LWW, documented).
- Remote ops never enter my stacks. The MCP peer has its own stacks (
undo tool).
Alternatives
- Global undo: undoes other people’s work.
- Selective undo with transformation: complex, little user value.
Consequences
Undo needs no snapshots of primitive buffers; memory is proportional to what was edited. A bulk generate of 5M nodes stores its inverse as one sorted id list.