From bd934136c4ba118a742b5996dcdee148d2878bd9 Mon Sep 17 00:00:00 2001 From: olafura Date: Mon, 22 Jun 2026 21:14:07 +0200 Subject: [PATCH] fix: invalidate lazy-loaded older history when the live window reshapes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address review (PR #3510, Cursor Bugbot): - Reconnect / checkpoint revert can re-snapshot or filter the live activity window, but the prepended `olderActivities` weren't invalidated — leaving gaps or showing reverted history. Reset the lazy-load state when the live window's oldest activity id changes (it's stable while activities only append, so this doesn't fire during a normal turn), in addition to on thread switch. Web + mobile. - Web only deduped a new older page against already-loaded older pages, not the live window (mobile already did both). Dedup against both so a boundary overlap can't produce duplicate ids / React keys. Not changed — the "unsequenced cursor hides sequenced history" finding is a false positive: NULL-sequence (legacy) rows always sort oldest in the window/cursor ordering, so the oldest-loaded row is only unsequenced once every sequenced row is already loaded; the unsequenced cursor can never strand sequenced rows. Co-Authored-By: Claude Opus 4.8 --- .../src/state/use-thread-composer-state.ts | 11 ++++++++--- apps/web/src/components/ChatView.tsx | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/apps/mobile/src/state/use-thread-composer-state.ts b/apps/mobile/src/state/use-thread-composer-state.ts index 1639b777dc..4e77fc668a 100644 --- a/apps/mobile/src/state/use-thread-composer-state.ts +++ b/apps/mobile/src/state/use-thread-composer-state.ts @@ -113,14 +113,19 @@ export function useThreadComposerState() { : null; const activityRequestKeyRef = useRef(activityRequestKey); activityRequestKeyRef.current = activityRequestKey; + + const liveActivities = selectedThreadDetail?.activities ?? EMPTY_ACTIVITIES; + // The live window's oldest activity is stable while new activities only + // append; it changes when the window is re-snapshotted (reconnect) or rows are + // removed (checkpoint revert), either of which can make prepended older pages + // stale or gappy — so reset the lazy-load state when it (or the thread) changes. + const liveOldestActivityId = liveActivities[0]?.id ?? null; useEffect(() => { setOlderActivities([]); setOlderLoaded(false); setOlderHasMore(false); setLoadingOlderActivities(false); - }, [activityRequestKey]); - - const liveActivities = selectedThreadDetail?.activities ?? EMPTY_ACTIVITIES; + }, [activityRequestKey, liveOldestActivityId]); const mergedActivities = useMemo( () => olderActivities.length > 0 ? [...olderActivities, ...liveActivities] : liveActivities, diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index d73960f903..6eaa684e32 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -1881,14 +1881,19 @@ function ChatViewContent(props: ChatViewProps) { : null; const activeThreadActivityRequestKeyRef = useRef(activeThreadActivityRequestKey); activeThreadActivityRequestKeyRef.current = activeThreadActivityRequestKey; + const liveThreadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES; + // The live window's oldest activity is stable while new activities only + // append; it changes when the window is re-snapshotted (reconnect) or rows are + // removed (checkpoint revert), either of which can make prepended older pages + // stale or gappy — so reset the lazy-load state when it (or the thread) changes. + const liveOldestActivityId = liveThreadActivities[0]?.id ?? null; useEffect(() => { setOlderActivities([]); setOlderLoaded(false); setOlderHasMore(false); setLoadingOlderActivities(false); - }, [activeThreadActivityRequestKey]); + }, [activeThreadActivityRequestKey, liveOldestActivityId]); - const liveThreadActivities = activeThread?.activities ?? EMPTY_ACTIVITIES; const threadActivities = useMemo( () => olderActivities.length > 0 @@ -1937,7 +1942,13 @@ function ChatViewContent(props: ChatViewProps) { } const page = result.value; setOlderActivities((prev) => { + // Dedup against both already-loaded older pages and the live window so + // an overlap at the window boundary can't leave duplicate ids (which + // would break timeline keys and work-log derivation). const seen = new Set(prev.map((activity) => activity.id)); + for (const activity of liveThreadActivities) { + seen.add(activity.id); + } const fresh = page.activities.filter((activity) => !seen.has(activity.id)); return [...fresh, ...prev]; }); @@ -1957,6 +1968,7 @@ function ChatViewContent(props: ChatViewProps) { activeThreadActivityRequestKey, hasMoreOlderActivities, threadActivities, + liveThreadActivities, loadThreadActivities, ]);