From 35f57d05195097d820f47c5f2e8c113ad054ee82 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Feb 2026 02:17:14 +0000 Subject: [PATCH] Fix sync effects firing on mount and thread upsert wiping turn diff summaries - Add prevSyncedThreadIdRef to skip sync effects on mount and thread switch, preventing redundant server writes and race conditions - Fix turnDiffSummaries in APPLY_STATE_EVENT to check .length before using payload value, since server always sends empty array --- apps/web/src/components/ChatView.tsx | 10 ++++++++++ apps/web/src/store.ts | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index bdd2c87dac..361ac9267e 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -764,8 +764,11 @@ export default function ChatView({ threadId }: ChatViewProps) { const hasReachedTerminalLimit = (activeThread?.terminalIds.length ?? 0) >= MAX_THREAD_TERMINAL_COUNT; + const prevSyncedThreadIdRef = useRef(undefined); + useEffect(() => { if (!api || !activeThread) return; + if (prevSyncedThreadIdRef.current !== activeThread.id) return; void api.threads .updateTitle({ threadId: activeThread.id, @@ -776,6 +779,7 @@ export default function ChatView({ threadId }: ChatViewProps) { useEffect(() => { if (!api || !activeThread) return; + if (prevSyncedThreadIdRef.current !== activeThread.id) return; void api.threads .updateModel({ threadId: activeThread.id, @@ -786,6 +790,7 @@ export default function ChatView({ threadId }: ChatViewProps) { useEffect(() => { if (!api || !activeThread) return; + if (prevSyncedThreadIdRef.current !== activeThread.id) return; void api.threads .updateBranch({ threadId: activeThread.id, @@ -797,6 +802,7 @@ export default function ChatView({ threadId }: ChatViewProps) { useEffect(() => { if (!api || !activeThread) return; + if (prevSyncedThreadIdRef.current !== activeThread.id) return; void api.threads .updateTerminalState({ threadId: activeThread.id, @@ -819,6 +825,10 @@ export default function ChatView({ threadId }: ChatViewProps) { api, ]); + useEffect(() => { + prevSyncedThreadIdRef.current = activeThread?.id; + }, [activeThread?.id]); + const revokePreviewUrls = useCallback((images: Array<{ previewUrl?: string }>) => { for (const image of images) { if (!image.previewUrl) continue; diff --git a/apps/web/src/store.ts b/apps/web/src/store.ts index 87d09d5bbb..d51820784f 100644 --- a/apps/web/src/store.ts +++ b/apps/web/src/store.ts @@ -649,9 +649,9 @@ export function reducer(state: AppState, action: Action): AppState { ...(threadPayload as unknown as Omit), messages: existingStateMessages, turnDiffSummaries: - (threadPayload?.turnDiffSummaries as Thread["turnDiffSummaries"] | undefined) ?? - existing?.turnDiffSummaries ?? - [], + (threadPayload?.turnDiffSummaries as Thread["turnDiffSummaries"] | undefined)?.length + ? (threadPayload!.turnDiffSummaries as Thread["turnDiffSummaries"]) + : (existing?.turnDiffSummaries ?? []), }; const nextThread = hydrateThreadFromBootstrap(bootstrapThread, existing); const nextThreads = existing