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, ]);