HARD GATE #3 surfaced 2 real bugs in engine-driven recovery flow that
synthetic OnSessionClose injection didn't expose. Both fixed here.
Bug #1 — catch-up exhaustion missed escalation when Decision was
re-classified mid-flight:
Symptom: stray auto-probe arriving between catch-up retries
re-classified Recovery.Decision (R<S → Rebuild). Exhaustion-branch
checked stale Decision and fell through to terminal-degraded
instead of round-47 escalation.
Fix: key on st.Session.Kind == SessionCatchUp (truth = what
session actually failed), not on mutable Recovery.Decision.
Bug #2 — WALRecycled set Decision=Rebuild but never emitted
StartRebuild; subsequent probe could downgrade:
Symptom: WALRecycled escalation only emitted PublishDegraded; no
rebuild command. A subsequent probe with R>=S could re-classify
back to CatchUp before any rebuild fired.
Fix: new RecoveryTruth.RebuildPinned sticky bool. Set by both
WALRecycled escalation AND catch-up exhaustion. decide() honors
it as override — forces Decision=Rebuild + emits StartRebuild
regardless of R/S/H. Cleared by successful rebuild
SessionCompleted (or identity reset).
WALRecycled also now emits StartRebuild immediately (in addition
to PublishDegraded for diagnostic surface).
Bug #3 (architect-required rule) — already correct:
Rebuild's MaxRetries=0 routes the first rebuild failure to the
existing terminal-degraded branch. No code change needed.
New invariants pinned:
INV-REPL-REBUILD-DECISION-STICKY — once escalated to rebuild
(WALRecycled or exhaustion), probe re-classification cannot
downgrade to CatchUp before rebuild completes.
INV-REPL-REBUILD-EMITTED-ON-WAL-RECYCLED — WALRecycled escalation
emits StartRebuild immediately, not just PublishDegraded.
Full L2 tests (HARD GATE #3) — all PASS through real
engine→adapter→executor wiring (NOT synthetic injection):
- TestT4d4_FullL2_CatchupRetryExhausted_EscalatesToRebuild
- TestT4d4_FullL2_WALRecycled_EscalatesImmediate
- TestT4d4_FullL2_LastSentMonotonic_AcrossRetries (QA #8 full form)
Files:
- core/engine/state.go — RebuildPinned field with invariant doc
- core/engine/apply.go — sticky-rebuild gate in decide();
WALRecycled emits StartRebuild + pins; exhaustion uses
Session.Kind + pins; rebuild SessionCompleted clears pin
- core/engine/recovery_test.go — fixtures set Session.Kind:
SessionCatchUp (was relying on old Decision-based check)
- core/replication/component/t4d4_full_l2_matrix_test.go — NEW,
3 HARD GATE #3 tests via engine-driven flow with substrate
fault injection wraps
Verified: full V3 suite green; m01 -race ×20 stress on the 3 HARD
GATE tests, no flakes.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds an explicit FenceAtEpoch command + FenceCompleted/FenceFailed
event pair on the caught-up recovery branch. A replica with R >= H
under a newer identity epoch is no longer declared healthy until the
replica's lineage gate has been bumped to match Identity.Epoch via a
FenceCompleted — closing the window where stale old-epoch traffic
could still land on the replica while the projection already reads
healthy.
Contract surface:
* Caught-up path emits ONLY FenceAtEpoch — no catch-up / no rebuild.
Fence is a command+event pair, not a session lifecycle.
* PublishHealthy held until FenceCompleted advances FencedEpoch.
Publication and DeriveProjection both gate on this so the
operator-visible mode reflects the ack-gated contract, not
just the command stream.
* FenceFailed is fail-closed: FencedEpoch is NOT advanced; retry
is probe-driven (next successful probe re-runs decide() and
re-emits FenceAtEpoch).
* Session completion (catch-up / rebuild) implicitly fences,
because mutating traffic at the current epoch already bumped
the replica's gate.
* Stale FenceCompleted (epoch < Identity.Epoch, or same epoch
with older endpointVersion) is rejected without mutating state.
* FencedEpoch is engine-internal; NOT exposed in ReplicaProjection.
Adapter-side fence dispatch:
* fenceInFlight keyed by fence LINEAGE
(replicaID, epoch, endpointVersion), NOT by replicaID alone.
This prevents a newer-epoch fence from being starved by an
older in-flight fence — both occupy distinct slots, the old
one is dropped by the engine as stale when its callback lands,
and the newer one drives publication to healthy without
waiting for an unrelated later probe.
Proof tests:
* engine: TestFence_CaughtUpPath_UsesNoDataRecovery
* engine: TestFence_HealthyOnlyAfterFenceComplete
* engine: TestFence_FailureLeavesFencedEpochUnchanged
* engine: TestFence_StaleOldEpochRejectedAfterFenceOnCaughtUp
* adapter: TestFullRoute_CaughtUp_NotHealthyUntilFenceComplete
* adapter: TestFence_NewerEpochNotStarvedByOlderInFlightFence
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>