fix(localdb): snapshot a wiped peer whose deltas were already acked and pruned

A converged pair prunes its oplog on ack, so "no oplog rows at all" is the
steady state of a healthy pair — not an anomaly. ComputeSnapshotRequiredAsync
measured the peer's gap against the oldest surviving oplog row and treated an
empty oplog as "no gap possible", which made that steady state the one state
from which a wiped peer could never be healed: it reconnected with an empty
database and a zero watermark, was told it needed no snapshot, and stayed
empty indefinitely.

With an empty oplog the oldest seq we could still stream is one past
last_acked_seq — ack-pruning is the only thing that empties the oplog without
also flagging needs_snapshot, and it deletes exactly seq <= last_acked_seq.
Measuring the gap against that heals the wiped peer and leaves every other
case identical: a converged peer's watermark equals last_acked_seq (no
snapshot), and a pair that has never written anything has both at zero.

Found by the OtOpcUa LocalDb Phase 1 live gate (check 4), which recorded it as
a documented limitation. Covered by a test that reproduces the live-gate
scenario through the real session stack: RED before this fix (the wiped side
never converges, 15 s timeout), green after. 148/148 pass.

Version 0.1.2.

Claude-Session: https://claude.ai/code/session_01GASWkNEi68FSCtvr6rLoEW
This commit is contained in:
Joseph Doherty
2026-07-21 01:17:32 -04:00
parent c42348eff5
commit cad3bcb6bf
4 changed files with 66 additions and 6 deletions
@@ -418,9 +418,20 @@ internal sealed class SyncSession
var minRows = await _db.QueryAsync(
"SELECT COALESCE(MIN(seq), 0) FROM __localdb_oplog", static r => r.GetInt64(0), null, ct);
var minSeq = minRows[0];
// minSeq 0 => oplog empty. A gap exists when the peer's applied watermark falls below the
// oldest seq we can still stream (pruning discarded everything at or below its horizon).
return minSeq > 0 && peerHandshake.LastAppliedRemoteSeq < minSeq - 1;
// The oldest seq we could still stream as a delta. With rows in the oplog that is simply the
// oldest one; with an EMPTY oplog it is the next seq we will ever write — one past
// last_acked_seq, because ack-pruning is the only thing that empties the oplog without
// flagging needs_snapshot, and it deletes exactly seq <= last_acked_seq.
//
// Treating an empty oplog as "no gap possible" is what let a wiped peer starve: a converged
// pair prunes to empty on ack, so the healthy node held no delta at all, and a peer that
// came back with a zero watermark and an empty database was told it needed nothing. The
// steady state of a healthy pair was precisely the state that could not heal one.
var streamableFrom = minSeq > 0 ? minSeq : peerState.LastAckedSeq + 1;
// A gap exists when the peer's applied watermark falls below what we can still stream.
return peerHandshake.LastAppliedRemoteSeq < streamableFrom - 1;
}
private static async Task<SyncMessage> ReceiveOneAsync(SyncDuplex duplex, CancellationToken ct)