From 059b5cc0f549d2ca02466cd6220ae6ccbfe4c27c Mon Sep 17 00:00:00 2001 From: engineer Date: Tue, 2 Jun 2026 00:41:42 -0700 Subject: [PATCH] fix(sessions): don't flash error / refresh wrong session on send failure sendMessage captured currentSession at call time; if the user switched sessions while a prompt was in flight and it failed, the catch handler surfaced the error on and refetched the NOW-current session. Clear the sending flag for the session we actually sent to, but only set error / refreshMessages when it's still on screen. Found via runtime bug audit. Co-Authored-By: Claude Opus 4.8 --- src/stores/sessions.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/stores/sessions.ts b/src/stores/sessions.ts index dc32557..feedd9c 100644 --- a/src/stores/sessions.ts +++ b/src/stores/sessions.ts @@ -306,13 +306,25 @@ export const useSessions = create((set, get) => ({ // Fire and forget - SSE events will update messages/parts/status in real-time client.session.prompt(session.id, { parts: promptParts, model, agent }).catch((err) => { console.error("Failed to send message:", err) - set((state) => ({ error: String(err), sending: { ...state.sending, [session.id]: false } })) - get().refreshMessages() + // The user may have switched sessions while this send was in flight. Clear + // the sending flag for the session we actually sent to (keyed by id, safe), + // but only surface the error / refresh messages if it's still on screen — + // otherwise we'd flash an error on, and refetch, the wrong session. + const stillCurrent = get().currentSession?.id === session.id + set((state) => ({ + ...(stillCurrent ? { error: String(err) } : {}), + sending: { ...state.sending, [session.id]: false }, + })) + if (stillCurrent) get().refreshMessages() }) } catch (err) { console.error("[sendMessage] error:", err) - set((state) => ({ error: String(err), sending: { ...state.sending, [session.id]: false } })) - get().refreshMessages() + const stillCurrent = get().currentSession?.id === session.id + set((state) => ({ + ...(stillCurrent ? { error: String(err) } : {}), + sending: { ...state.sending, [session.id]: false }, + })) + if (stillCurrent) get().refreshMessages() } },