Skip to content

The agent that grows with you

fix(desktop): hand a create-window follow-up to the new chat

unsupportedpastels8c33b4f281e0
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
index 5923adb34b9..3c4f46e6b2b 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts
@@ -34,6 +34,7 @@ import {
} from '@/store/composer-draft-retry'
import { isBrowsingHistory } from '@/store/composer-input-history'
import { $composerPopout } from '@/store/composer-popout'
+import { migrateQueuedPrompts } from '@/store/composer-queue'
import { clearDraftSuggestions, sampleComposerDraft } from '@/store/composer-suggestions'
import {
@@ -581,7 +582,14 @@ export function useComposerDraft({
// of vanishing. Keyed on the scope alone: the runtime id can land a resume
// later than the route flips the scope, so it is not a usable signal here.
if ((isFreshDraftScope(draftScopeRef.current) || !draftScopeRef.current) && activeQueueSessionKey) {
- adoptNewSessionDraft(activeQueueSessionKey)
+ const freshKey = draftScopeRef.current || freshDraftScope()
+
+ // A follow-up queued while session.create was in flight sits under the
+ // fresh key; it belongs to this chat too. The queue re-key never moves
+ // fresh → stored (#139190), so only this announced handoff does.
+ if (adoptNewSessionDraft(activeQueueSessionKey)) {
+ migrateQueuedPrompts(freshKey, activeQueueSessionKey)
+ }
} else if (!activeQueueSessionKey) {
// The reverse handoff: a session the user was typing into turned out
// to be gone and the window dropped to a fresh draft (#111868). The
diff --git a/apps/desktop/src/app/chat/composer/new-chat-first-send.test.tsx b/apps/desktop/src/app/chat/composer/new-chat-first-send.test.tsx
index e002194a0d9..a9f1b81a257 100644
--- a/apps/desktop/src/app/chat/composer/new-chat-first-send.test.tsx
+++ b/apps/desktop/src/app/chat/composer/new-chat-first-send.test.tsx
@@ -142,3 +142,75 @@ it('a new chat first send keeps its creation-time draft, queues on the new chat
expect(getQueuedPrompts('session-a').map(entry => entry.id)).toEqual(['for-a'])
expect(getQueuedPrompts(freshDraftKey)).toEqual([])
})
+
+it('a follow-up queued while session.create is in flight moves to the new chat and sends there', async () => {
+ setSessionsLoading(false)
+ const freshDraftKey = rotateFreshDraftKey()
+ const onSubmit = vi.fn<ChatBarProps['onSubmit']>(async () => true)
+ const latest: { current: Composer | null } = { current: null }
+
+ $sessionStates.set({ 'session-a': { storedSessionId: 'session-a' } as ClientSessionState })
+ enqueueQueuedPrompt('session-a', { attachments: [], id: 'for-a', text: 'for A' })
+
+ const view = render(
+ <Harness busy freshDraftKey={freshDraftKey} latest={latest} onSubmit={onSubmit} queueSessionKey={null} sessionId="session-a" />
+ )
+
+ const show = (scope: Scope) =>
+ act(() => view.rerender(<Harness freshDraftKey={freshDraftKey} latest={latest} onSubmit={onSubmit} {...scope} />))
+
+ expect(latest.current?.scope).toBe(freshDraftKey)
+
+ // session.create is in flight: the follow-up can only land under the chat's fresh draft key.
+ act(() => {
+ latest.current!.draftRef.current = 'follow-up'
+ })
+ await act(async () => {
+ await latest.current!.queueCurrentDraft()
+ })
+ expect(getQueuedPrompts(freshDraftKey).map(entry => entry.text)).toEqual(['follow-up'])
+
+ // Creation assigns the stored id: announce, bind the runtime, route onto it.
+ announceNewSessionDraftKey('stored-new')
+ $sessionStates.set({
+ 'session-a': { storedSessionId: 'session-a' } as ClientSessionState,
+ 'rt-new': { storedSessionId: 'stored-new' } as ClientSessionState
+ })
+ show({ busy: true, queueSessionKey: 'stored-new', sessionId: 'rt-new' })
+
+ expect(latest.current?.scope).toBe('stored-new')
+ expect(getQueuedPrompts('stored-new').map(entry => entry.text)).toEqual(['follow-up'])
+ expect(getQueuedPrompts(freshDraftKey)).toEqual([])
+
+ // The first turn settles: the follow-up drains into the new chat; A is untouched.
+ show({ busy: false, queueSessionKey: 'stored-new', sessionId: 'rt-new' })
+ await vi.waitFor(() => expect(onSubmit).toHaveBeenCalledTimes(1))
+ expect(onSubmit.mock.calls[0]?.[0]).toBe('follow-up')
+ expect(onSubmit.mock.calls[0]?.[1]).toMatchObject({ sessionId: 'rt-new', storedSessionId: 'stored-new' })
+ expect(getQueuedPrompts('session-a').map(entry => entry.id)).toEqual(['for-a'])
+ expect(getQueuedPrompts(freshDraftKey)).toEqual([])
+})
+
+it('a session already claimed elsewhere never hands its queue to the new chat on creation', () => {
+ const freshDraftKey = rotateFreshDraftKey()
+ const latest: { current: Composer | null } = { current: null }
+ const onSubmit = vi.fn<ChatBarProps['onSubmit']>(async () => true)
+
+ $sessionStates.set({ 'session-a': { storedSessionId: 'session-a' } as ClientSessionState })
+ enqueueQueuedPrompt('session-a', { attachments: [], id: 'for-a', text: 'for A' })
+
+ const view = render(
+ <Harness busy freshDraftKey={freshDraftKey} latest={latest} onSubmit={onSubmit} queueSessionKey="session-a" sessionId="session-a" />
+ )
+
+ // A stale announcement naming another chat while A is routed: A's queue stays put.
+ announceNewSessionDraftKey('stored-new')
+ act(() =>
+ view.rerender(
+ <Harness busy freshDraftKey={freshDraftKey} latest={latest} onSubmit={onSubmit} queueSessionKey="stored-new" sessionId="rt-new" />
+ )
+ )
+
+ expect(getQueuedPrompts('session-a').map(entry => entry.id)).toEqual(['for-a'])
+ expect(getQueuedPrompts('stored-new')).toEqual([])
+})
diff --git a/apps/desktop/src/store/composer.ts b/apps/desktop/src/store/composer.ts
index b652d904cda..2c4c553b281 100644
--- a/apps/desktop/src/store/composer.ts
+++ b/apps/desktop/src/store/composer.ts
@@ -575,12 +575,20 @@ export function announceNewSessionDraftKey(toKey: string | null | undefined): vo
announcedNewSessionDraftKey = toKey?.trim() || null
}
-/** Consume the announcement; move the `__new__` draft when it names `toKey`. */
+/** Consume the announcement; move the `__new__` draft when it names `toKey`.
+ * True when the announcement named `toKey` (the caller hands the rest of the
+ * pre-session chat over), even if there was no draft text to move. */
export function adoptNewSessionDraft(toKey: string | null | undefined): boolean {
const announced = announcedNewSessionDraftKey
announcedNewSessionDraftKey = null
- return !!announced && announced === toKey?.trim() && migrateSessionDraft(null, toKey)
+ if (!announced || announced !== toKey?.trim()) {
+ return false
+ }
+
+ migrateSessionDraft(null, toKey)
+
+ return true
}
/**