Skip to content

The agent that grows with you

fix(desktop): send the first prompt of a fresh-draft chat

thefiredev-clouda6803ce0ebc1
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.restore-scoping.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.restore-scoping.test.tsx
index 307fab3b66f..e1b461139e5 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.restore-scoping.test.tsx
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.restore-scoping.test.tsx
@@ -12,6 +12,7 @@ import {
announceNewSessionDraftKey,
clearSessionDraft,
mainComposerScope,
+ rotateFreshDraftKey,
stashSessionDraft,
takeSessionDraft
} from '@/store/composer'
@@ -499,3 +500,38 @@ it('does not treat arbitrary navigation during a real create as assignment of th
act(() => navigate(null))
expect(editorText()).toBe('unsent first question')
})
+
+it('sends the first prompt of a chat keyed by its per-lifecycle fresh-draft scope', async () => {
+ // The composer keys an unsaved chat by `__new__:<uuid>` (composer/index.tsx
+ // activeQueueSessionKey), not null. That scope must still read as
+ // "pre-session" so the created session is not taken for composer drift.
+ const freshKey = rotateFreshDraftKey()
+ stashSessionDraft(freshKey, 'hello from a fresh chat', [])
+ $newChatRoute.set({ connectionId: 'connection-fresh-key', profile: 'default' })
+ vi.mocked(requestGatewayForAgent).mockImplementation(async (_connection, _profile, method) => {
+ if (method === 'session.create') {
+ return { session_id: 'rt-B', stored_session_id: 'stored-B', info: {} }
+ }
+
+ if (method === 'prompt.submit') {
+ return { status: 'streaming' }
+ }
+
+ throw new Error(`unexpected ${method}`)
+ })
+
+ seed({ pendingScope: freshKey }, null)
+ expect(editorText()).toBe('hello from a fresh chat')
+ act(() => handles.submit.submitDraft())
+ await waitFor(() => expect(requestGatewayForAgent).toHaveBeenCalledTimes(2))
+ expect(
+ vi.mocked(requestGatewayForAgent).mock.calls.map(([, , method, params]) => ({
+ method,
+ text: (params as { text?: string } | undefined)?.text
+ }))
+ ).toEqual([
+ { method: 'session.create', text: undefined },
+ { method: 'prompt.submit', text: 'hello from a fresh chat' }
+ ])
+ expect(route).toBe('stored-B')
+})
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
index ef414a14ef5..2304fac0f8a 100644
--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-submit.ts
@@ -5,7 +5,7 @@ import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
import { isSideTaskSlashCommand } from '@/lib/desktop-slash-commands'
import { triggerHaptic } from '@/lib/haptics'
import { hasClarifyRequest, skipClarifyRequest } from '@/store/clarify'
-import { clearSessionDraft, type ComposerAttachment } from '@/store/composer'
+import { clearSessionDraft, type ComposerAttachment, isFreshDraftScope } from '@/store/composer'
import { resetBrowseState } from '@/store/composer-input-history'
import { enqueueQueuedPrompt, type QueuedPromptEntry } from '@/store/composer-queue'
import { hasConnectionRequest, skipConnectionRequest } from '@/store/connection-request'
@@ -91,7 +91,11 @@ export function useComposerSubmit({
// only while the same session still owns the visible composer; a late reject
// must not publish an old session's text into the newly focused one.
const dispatchSubmit = (text: string, attachments?: ComposerAttachment[], displayKind?: 'hidden') => {
- const submittedScope = draftScopeRef.current
+ // A fresh chat's composer is keyed by its per-lifecycle fresh-draft key
+ // (`__new__:<uuid>`), but the submit contract spells "no session yet" as
+ // null: the create handoff below and the composer drift prong both key off
+ // it, and draftKey(null) resolves to that same fresh bucket.
+ const submittedScope = isFreshDraftScope(draftScopeRef.current) ? null : draftScopeRef.current
let restoreScope = submittedScope
const submittedAttachments = attachments ?? []
@@ -109,7 +113,7 @@ export function useComposerSubmit({
const restore = () => {
stashAt(restoreScope, text, submittedAttachments)
- if (draftScopeRef.current === restoreScope) {
+ if ((isFreshDraftScope(draftScopeRef.current) ? null : draftScopeRef.current) === restoreScope) {
loadIntoComposer(text, submittedAttachments)
}
}