The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep the first send when only the active runtime is retargeted
Faer1x2f1d3088506a
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsxindex 2239894785b..65b8325c701 100644--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx@@ -4686,6 +4686,54 @@ describe('usePromptActions submit session-context isolation (#54527)', () => {})})+ it('still submits the first message when a background event retargets only the active runtime during create', async () => {+ // A live session's stream retargets activeSessionIdRef while session.create+ // is in flight (#47709). Route and selection still name the chat create+ // just minted. Treating that ref mismatch as a user switch drops the+ // bubble and never calls prompt.submit, so the new tab stays empty.+ const calls: { method: string; params?: Record<string, unknown> }[] = []+ const selectedStoredSessionIdRef: MutableRefObject<string | null> = { current: null }+ const activeSessionIdRef: MutableRefObject<string | null> = { current: null }+ let routeToken = '/'++ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {+ calls.push({ method, params })++ return {} as never+ })++ const createBackendSessionForSend = vi.fn(async () => {+ activeSessionIdRef.current = 'rt-new-chat'+ selectedStoredSessionIdRef.current = 'stored-new-chat'+ routeToken = '/stored-new-chat'+ activeSessionIdRef.current = 'rt-other-streaming'++ return 'rt-new-chat'+ })++ let handle: HarnessHandle | null = null+ render(+ <Harness+ activeSessionId={null}+ activeSessionIdRef={activeSessionIdRef}+ createBackendSessionForSend={createBackendSessionForSend}+ getRouteToken={() => routeToken}+ getRuntimeIdForStoredSession={storedId => (storedId === 'stored-new-chat' ? 'rt-new-chat' : null)}+ onReady={h => (handle = h)}+ refreshSessions={async () => undefined}+ requestGateway={requestGateway}+ selectedStoredSessionIdRef={selectedStoredSessionIdRef}+ storedSessionId={null}+ />+ )+ await waitFor(() => expect(handle).not.toBeNull())++ expect(await handle!.submitText('hi')).toBe(true)+ expect(calls.find(c => c.method === 'prompt.submit')?.params).toMatchObject({+ session_id: 'rt-new-chat'+ })+ })+it('aborts when the user switches sessions during the tail of a successful create', async () => {// createBackendSessionForSend awaits once more (armed-YOLO apply) AFTER// committing the refs and returning a real id, so a switch in that windowdiff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.tsindex e440ee40667..8b30e3bc931 100644--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts@@ -44,7 +44,7 @@ import {resolveActiveTranscriptSession} from '../../../contrib/hooks/use-background-sync'import type { ClientSessionState } from '../../../types'-import { sessionContextDrift } from '../session-context-drift'+import { routeTargetFromToken, sessionContextDrift } from '../session-context-drift'import type { CreateBackendSessionForSend } from '../use-session-actions/create-overrides'import { resolveSessionProfile } from '../use-session-actions/utils'@@ -768,16 +768,28 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {return false}- // A successful create re-homes selection + route to the chat it just- // minted, so the pre-create baseline can't tell our own re-home from- // a user switch (judging it drift aborted EVERY first send of a new- // chat: no prompt.submit, no DB row, a stranded route that 404s- // "Session not found"). The drift signal for this window is the- // active ref instead: every switch path re-nulls or retargets it- // synchronously, so it only still equals the id create returned when- // nobody re-homed since.+ // A successful create re-homes selection and route onto the chat it+ // just minted. A background stream can still retarget the active+ // runtime ref during that window (#47709). That ref mismatch is not+ // a user switch when the route, selection, and stored→runtime map+ // still name this create. A real switch moves route and selection+ // onto a different chat, and that path still aborts.if (activeSessionIdRef.current !== sessionId) {- return abortForSessionSwitch(sessionId)+ // A background stream retargets only the active runtime (#47709).+ // Route and selection still name the chat create just minted, and+ // that stored id still maps to this runtime. That is not a user+ // switch. A real switch moves the route and selection onto a chat+ // whose runtime is not the id create returned.+ const selection = selectedStoredSessionIdRef.current+ const mapped = selection ? getRuntimeIdForStoredSession(selection) : null+ const routeTarget = routeTargetFromToken(getRouteToken())+ const routeAgrees = routeTarget === null || routeTarget === '__new__' || routeTarget === selection++ if (mapped === sessionId && routeAgrees) {+ activeSessionIdRef.current = sessionId+ } else {+ return abortForSessionSwitch(sessionId)+ }}// Re-pin the baseline to the created chat for the rest of the