Linora

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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).
  6. 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.
  7. 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

Consequences