0101. RESTORE op and tombstones: undo of deleting another user’s nodes
Status: Accepted (2026-11-27, coordinator-approved). Point 3, DELETE’s inverse: superseded by 0107 (2026-12-01): RESTORE blocks per parent in sibling order, not one per root. Owner: WP-A. Changes shared/src/ops.ts, codec.ts, doc-api.ts, messages.ts: DOC_RULES_VERSION 2, PROTOCOL_VERSION 2.
Context
Undo of a DELETE must re-create the nodes with their original ids, so collaborators, comments and references keep the node’s identity (ADR 0008, Figma behaviour). The stateless anti-spoof rule clientOf(id) === client (ADR 0019) rejects that whenever another client created the node: nobody could undo deleting someone else’s shapes. The rule exists so that a client cannot pre-create ids another client will allocate later.
Decision
- New op kind
RESTORE = 5, payload = the CREATE_BULK node block. It is not a flag on CREATE or CREATE_BULK:
- the gateway keeps its cheap stateless ownership check for fresh creates;
- the intent is visible in
BatchScan.opKind, journals and metrics;
- there is still a single block format.
- Rules. RESTORE stateless = every CREATE_BULK block check except ownership. Structural, per node in order:
- NODE_EXISTS;
- FOREIGN_ID unless
clientOf(id) === client or the id is a restorable tombstone;
- PARENT_MISSING / BAD_PARENT for the block parent (first node);
- duplicates (NODE_EXISTS), then the node limit;
- all-or-nothing.
- Inverses. DELETE’s inverse is one RESTORE per deleted root (original ids, keys, props), in reverse delete order. RESTORE’s inverse is a DELETE of its roots.
- Sequenced apply skips id ownership and tombstones (FOREIGN_ID). The server already decided. This also lets the sync client revert pending structural ops by applying their inverses as
sequenced.
- Tombstones.
- Every node removed by an accepted DELETE gets a tombstone (id, page seq before the batch). Any create or restore of the id clears it.
- Restorable = tombstoned and
seq - deleteSeq <= TOMBSTONE_RETAIN_SEQS (1,000,000 batches). At most MAX_TOMBSTONES_PER_PAGE (1,000,000) are kept, youngest first (packages/doc rules.ts).
- Not canonical state. Tombstones are not in the canonical snapshot. Replicas never consult them, because sequenced batches skip the check, so snapshot byte-identity across replicas is unaffected. They decide only:
- the server’s authoritative verdict (StructIndex);
- the client’s optimistic prediction. A wrong prediction (the tombstone was GC’d server-side) is an extra server rejection, which sync rolls back.
- Storage and GC.
- StructIndex: a deleted root is a flagged slot (dead slots double as tombstones), with its delete seq in a small slot map. At each delta merge, dead slots fold into a sorted list of 12 B entries (id f64 + delete seq u32, wrap-safe). Expired ones are dropped there.
- Structure section CFST format 2 carries that list (id f64 + age u32), so tombstones survive compaction and index reloads.
- DocCore keeps an id → seq map. The compactor carries tombstones forward with
loadTombstones(doc, previousStructure); encodeStructure() applies retention.
Alternatives
- Relax ownership to “structural only” for CREATE_BULK. Spoofed creates would then reach the room owner, and the intent would be invisible. Rejected.
- Tombstones in the canonical snapshot with seq-based expiry. A replica’s pending local delete is stamped with a different seq than the server assigns, which breaks byte-identity. Rejected.
- Expiry by a sequenced PURGE op. Deterministic, but it needs a new server-issued op and schema state. Not needed once tombstones are non-canonical.
- Undo with fresh ids. Loses node identity for references and comments. Rejected.
Consequences
- Memory: a tombstone costs at most a live node’s 14 B in the index, then 12 B after a merge, with a 12 MB cap per page. In DocCore it costs ~24 B (hash map entry), only for deleted nodes.
- Restoring a foreign node more than 1,000,000 batches after its delete (or past the cap) fails with FOREIGN_ID; own nodes are always restorable.
- Tested in
packages/doc/test/restore.test.ts:
- restoring another user’s node (also after an index reload and after a merge);
- restoring a never-deleted foreign id (rejected) and a never-deleted own id (accepted);
- restoring after GC (rejected);
- a concurrent edit to the deleted subtree;
- restore ordering (a child before its parent is rejected).
- Covered by the 100K-batch rule-equivalence test, with 649 accepted foreign restores.