0410. Sync settlement: rebase on every removed op, retire older batches, stale dedupe verdicts
Status: Accepted (2026-11-28, DC2). Refines 0401; relies on the ordering in 0202.
Context
On HEAD (wave 2), the convergence sim had extra server rejections on 10 of 1,000 seeds and one real divergence (seed 181: a replica ended with a stale fill). The 0401 follow-up called the residue “rare undo/restore interleavings”. There were three causes:
- Reverts captured over values that later stopped being canonical. A plain SET has no revert, but a later pending DELETE’s RESTORE block copies the node with the SET’s value. Settling the SET without a rebase (it was rejected, or already contained in the snapshot a reload rebuilt from) left that stale value in the block. The next rebase restored it.
- Older batches re-applied after their ACK was lost. A reload re-applies every in-flight batch on the fresh snapshot, including ones the server had already sequenced inside it. Later ops were then predicted over a doubly applied state.
- Dedupe ACKs of all-rejected batches carry a verdict from an earlier server state. No broadcast places such a batch, so the client compared the verdict with its current state.
Decision
- Settling removes ops only through a full rebase. When any op the replica shows is rejected, or turns out to be already in the base snapshot (
seq <= baseSeq), the session rebases. Every revert is then recaptured from canonical values. If an out-of-order ACK leaves an accepted key overridden while pending reverts exist, the page reloads.
- Retire older batches. A client’s batches reach the server in clientSeq order, and the server never processes a clientSeq below the highest it processed (0202). So when batch
j settles (ACK or own broadcast in a tail), every older batch still in flight was processed before it and lost its ACK with a connection. Anything it accepted is already canonical, because it would otherwise have come earlier in the tail. Such batches are dropped (with a rebase), and their commits resolve with the rejections that were predicted. This also ends hangs when the dedupe cache no longer holds an ACK.
- Stale verdicts are counted apart. Each pending op records every prediction made since its batch was first sent. A rejection on a possible dedupe ACK is a stale verdict (
stats.staleVerdicts) rather than an extra rejection when:
- the batch was sent before the current subscription, and
- the client predicted that verdict at some point in the window, or a reload skipped part of the window (no states were observed there).
It is rolled back like any rejection, and the final state equals the no-disconnect case.
- Bulk ops (ADR 0103), within the same model:
- pending TRANSFORM / SET_GEOMETRY ops apply alone with their exact SET_GEOMETRY revert;
- a remote batch with a bulk op, or a remote geometry SET on a node one of ours lists, triggers a rebase;
- a SET’s shadow under a pending bulk op comes from that op’s revert;
- an ACK settles each key by replaying the batch’s accepted writes in op order (absolute SETs and creates, relative TRANSFORM, absolute SET_GEOMETRY);
- direct value fixes skip nodes that a later pending bulk op moves.
- Busy / rate_limited for
j resends only j after retryAfterMs; j+1.. follow j’s ACK. Resending the whole tail multiplied server work under overload (R1’s load test).
Alternatives
- Server-side eager dedupe ACKs at subscribe: a protocol change, and verdicts would still predate the tail.
- Re-process an all-rejected batch on dedupe: breaks per-client op order when later batches were accepted.
- Hide in-flight batches during a reload until their ACK: flickers the other way, and needs the same retirement rule anyway.
Consequences
- Proof: the convergence sim over seeds 1-1000, with random TRANSFORM / SET_GEOMETRY in the op mix. All 1,000 replicas are byte-identical to the journal replay, with 0 extra rejections and 0 mismatches. 20 stale verdicts occurred in 9 seeds, and 373 older batches were retired.
- The extra rebases happen only on rejections, reload settlements and retirements: rare, and O(pending ops).
- Tests:
packages/sync-client/test/sim.test.ts (LINORA_SYNC_SIM_SEEDS for sweeps) and test/wave3.test.ts.