From 4a6bed802bed6d9f4c4f1d8d3af615f57f1f63e0 Mon Sep 17 00:00:00 2001 From: chanx <1243304602@qq.com> Date: Wed, 12 Aug 2026 14:57:20 +0800 Subject: [PATCH] fix: prevent stale conversation messages during session switch (#18140) --- .../chat/chat-box/single-chat-box.tsx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/web/src/pages/next-chats/chat/chat-box/single-chat-box.tsx b/web/src/pages/next-chats/chat/chat-box/single-chat-box.tsx index 9fa115ad9e..68f1cc2c2d 100644 --- a/web/src/pages/next-chats/chat/chat-box/single-chat-box.tsx +++ b/web/src/pages/next-chats/chat/chat-box/single-chat-box.tsx @@ -63,6 +63,16 @@ export function SingleChatBox({ // in-progress answer. if (activeStreamsRef.current.has(conversationId)) return; + // Skip when the conversation prop is stale — its id doesn't match the + // URL's current conversationId. This happens during a switch (e.g. + // clicking "+" to create a new session): child effects fire before the + // parent's clear/load effect, so for one render the prop still holds the + // previous conversation's messages. Applying them here would leak the old + // conversation's content into the newly switched (or new) conversation. + // The cache + prologue logic in useSelectNextMessages handles restoring + // or seeding messages for the new conversationId. + if (conversation?.id && conversation.id !== conversationId) return; + const messages = conversation?.messages; if (Array.isArray(messages)) { setDerivedMessages((prevMessages) => { @@ -83,7 +93,13 @@ export function SingleChatBox({ })); }); } - }, [conversation?.messages, conversationId, setDerivedMessages, activeStreamsRef]); + }, [ + conversation?.messages, + conversation?.id, + conversationId, + setDerivedMessages, + activeStreamsRef, + ]); useEffect(() => { // Clear the message list after deleting the conversation.