fix: prevent config overwrite, session history race, and streaming message loss (#663)

Co-authored-by: zuolingxuan <zuolingxuan@bytedance.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lingxuan Zuo
2026-03-25 21:11:20 +08:00
committed by GitHub
Unverified
parent 8c3a6a5f7a
commit 83858fdf73
7 changed files with 215 additions and 5 deletions

View File

@@ -1141,6 +1141,10 @@ export const useChatStore = create<ChatState>((set, get) => ({
switchSession: (key: string) => {
if (key === get().currentSessionKey) return;
// Stop any background polling for the old session before switching.
// This prevents the poll timer from firing after the switch and loading
// the wrong session's history into the new session's view.
clearHistoryPoll();
set((s) => buildSessionSwitchPatch(s, key));
get().loadHistory();
},
@@ -1303,6 +1307,11 @@ export const useChatStore = create<ChatState>((set, get) => ({
const loadPromise = (async () => {
const applyLoadedMessages = (rawMessages: RawMessage[], thinkingLevel: string | null) => {
// Guard: if the user switched sessions while this async load was in
// flight, discard the result to prevent overwriting the new session's
// messages with stale data from the old session.
if (get().currentSessionKey !== currentSessionKey) return;
// Before filtering: attach images/files from tool_result messages to the next assistant message
const messagesWithToolImages = enrichWithToolResultFiles(rawMessages);
const filteredMessages = messagesWithToolImages.filter((msg) => !isToolResultRole(msg.role));