From 51aaa5d57927f6fb16d5ed8ab2c9413f90ca1ac0 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 1 Apr 2026 21:18:51 +0000 Subject: [PATCH] Fix premature sequence advancement and missing archived-threads invalidation on snapshot recovery - Add filterNewEvents() to recovery coordinator to filter events without advancing latestSequence - Defer markEventBatchApplied() in applyEventBatch() until after the snapshot-recovery check passes, preventing latestSequence from advancing when events are not actually applied - Invalidate archived-threads query in runSnapshotRecovery() so the ArchivedThreadsPanel reflects snapshot state immediately --- apps/web/src/orchestrationRecovery.ts | 8 ++++++-- apps/web/src/routes/__root.tsx | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/web/src/orchestrationRecovery.ts b/apps/web/src/orchestrationRecovery.ts index ee81d5d539..c5f974565f 100644 --- a/apps/web/src/orchestrationRecovery.ts +++ b/apps/web/src/orchestrationRecovery.ts @@ -62,10 +62,14 @@ export function createOrchestrationRecoveryCoordinator() { return "apply"; }, - markEventBatchApplied(events: ReadonlyArray): ReadonlyArray { - const nextEvents = events + filterNewEvents(events: ReadonlyArray): ReadonlyArray { + return events .filter((event) => event.sequence > state.latestSequence) .toSorted((left, right) => left.sequence - right.sequence); + }, + + markEventBatchApplied(events: ReadonlyArray): ReadonlyArray { + const nextEvents = this.filterNewEvents(events); if (nextEvents.length === 0) { return []; } diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index 88afe75883..9bdeccc106 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -313,7 +313,7 @@ function EventRouter() { const applyEventBatch = ( events: ReadonlyArray, ): "applied" | "snapshot-recovery-needed" => { - const nextEvents = recovery.markEventBatchApplied(events); + const nextEvents = recovery.filterNewEvents(events); if (nextEvents.length === 0) { return "applied"; } @@ -327,6 +327,8 @@ function EventRouter() { return "snapshot-recovery-needed"; } + recovery.markEventBatchApplied(nextEvents); + const batchEffects = deriveOrchestrationBatchEffects(nextEvents); const needsProjectUiSync = nextEvents.some( (event) => @@ -417,6 +419,9 @@ function EventRouter() { if (!disposed) { syncServerReadModel(snapshot); reconcileSnapshotDerivedState(); + void queryClient.invalidateQueries({ + queryKey: orchestrationQueryKeys.archivedThreads(), + }); if (recovery.completeSnapshotRecovery(snapshot.snapshotSequence)) { void recoverFromSequenceGap(); }