0401. Sync client: full rebase of pending ops, per-key shadows with seq, tail settlement
Status: Accepted (2026-11-27). Refines the client side of 0019; server behaviour in ADR 0202.
Context
ADR 0019 sketched the client as “canonical + pending”: shadows for property writes, and a revert/re-apply of pending structural ops only. Building it against the convergence simulation (real ScanBatch + StructIndex, disconnects, reloads, undo) showed four gaps in that sketch:
- Re-applying only structural ops clobbers later property writes and loses a SET that rides with a PARENT_INDEX.
- A pending DELETE’s inverse is a copy of the subtree taken when it was applied. Remote edits under that subtree, made directly or under nodes our own pending undo restored, are missing from the copy, so reverting it rebuilds a stale canonical state.
- A prediction of rejection can become wrong later (a remote RESTORE brings the target back), so drop-on-rebase is not final.
- After a reconnect, the server replays our own committed batches in the tail as BROADCAST, and dedupe ACKs carry the original, older seq.
Decision
- Full rebase: on a remote batch that changes structure, or while one of our DELETEs is predicted to pass, or that creates nodes while a pending op is predicted to fail, revert all pending ops (structural reverts as
sequenced, property keys via shadows), apply the remote batch, and re-apply all pending ops with local validation. Predictions are recomputed every time and become final only at the ACK.
- Per-key settlement by seq: a pending key stores the canonical value under it and the seq that produced it. ACKs settle by seq, so out-of-order dedupe ACKs are correct. Creating ops count as writes of every prop of their nodes. Remote writes to a key with a live pending write update only the shadow.
- Tail settlement: own BROADCASTs in a catch-up tail are matched to unacked batches by op bytes and settled in tail order; the rest are resent after
synced.
- Reload re-applies pending ops on a fresh snapshot and swaps
session.doc. Re-applied RESTOREs are validated structurally only, because snapshots carry no tombstones.
- Mismatch handling: extra server rejections roll back; server-accepted ops that were predicted to fail reload the page.
Alternatives
- Structural-only rebase (0019 sketch): cheaper, but fails cases 1–3 above in the simulation.
- Rebase on every remote batch: simplest, but O(pending) per incoming batch even for property edits at 60 Hz.
- Resync from snapshot on any doubt: correct but too slow and heavy for 682K-node pages.
Consequences
Remote structural batches and in-flight deletes cost O(pending ops) (normally a frame’s worth). The convergence sim (20K ops, 5 clients) is byte-identical on every seed swept so far. A small residue of extra rejections in rare undo/restore interleavings is rolled back correctly and tracked as a follow-up.