Skip to content

The agent that grows with you

fix(desktop): keep the background drain off fresh draft queue keys

unsupportedpastels625699a7de97
diff --git a/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsx b/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsx
index 7759e7e88d3..26de1e6d69b 100644
--- a/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsx
+++ b/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsx
@@ -3,12 +3,14 @@ import type { MutableRefObject } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createClientSessionState } from '@/lib/chat-runtime'
+import { rotateFreshDraftKey } from '@/store/composer'
import {
$parkedQueueSessions,
$queuedPromptsBySession,
enqueueQueuedPrompt,
getQueuedPrompts,
MAX_AUTO_DRAIN_ATTEMPTS,
+ migrateQueuedPrompts,
parkQueuedPrompts,
resetFrozenQueuedTransportsForTests
} from '@/store/composer-queue'
@@ -528,4 +530,68 @@ describe('useBackgroundQueueDrain', () => {
expect(getQueuedPrompts('stored-session-a').map(e => e.id)).toEqual(['queued-restored'])
expect($notifications.get()).toHaveLength(0)
})
+
+ it('leaves a follow-up queued under a fresh draft key for the creation handoff', async () => {
+ vi.useFakeTimers()
+
+ // A follow-up queued while session.create is in flight sits under the
+ // fresh draft key. No session answers to it yet, so a rejecting drain
+ // used to exhaust the budget and drop it as a gone session.
+ setSessions([])
+ const fresh = rotateFreshDraftKey()
+ const runtimeMap = { current: new Map<string, string>() }
+ const submitText = vi.fn(async () => false)
+
+ enqueueQueuedPrompt(fresh, { text: 'follow-up during pending create', attachments: [] })
+
+ render(<Harness runtimeMap={runtimeMap} selectedStoredSessionId={null} submitText={submitText} />)
+
+ for (let attempt = 0; attempt < MAX_AUTO_DRAIN_ATTEMPTS + 2; attempt++) {
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(750)
+ })
+ }
+
+ expect(submitText).not.toHaveBeenCalled()
+ expect(getQueuedPrompts(fresh)).toHaveLength(1)
+ expect($notifications.get()).toHaveLength(0)
+ })
+
+ it('sends a create-window follow-up under the stored id once the creation handoff moves it', async () => {
+ vi.useFakeTimers()
+
+ setSessions([])
+ const fresh = rotateFreshDraftKey()
+ const runtimeMap = { current: new Map([['stored-new-chat', 'rt-new-chat']]) }
+ const submitText = vi.fn(async () => true)
+
+ const entry = enqueueQueuedPrompt(fresh, { text: 'follow-up during pending create', attachments: [] })!
+
+ render(<Harness runtimeMap={runtimeMap} selectedStoredSessionId={null} submitText={submitText} />)
+
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(750 * 2)
+ })
+
+ expect(submitText).not.toHaveBeenCalled()
+
+ // The creation announcement's handoff (use-composer-draft) re-keys the
+ // fresh bucket onto the stored id; the drain then handles it normally.
+ await act(async () => {
+ setSessions([lineageSession({ id: 'stored-new-chat' })])
+ migrateQueuedPrompts(fresh, 'stored-new-chat')
+ await vi.advanceTimersByTimeAsync(0)
+ })
+
+ expect(submitText).toHaveBeenCalledTimes(1)
+ expect(submitText).toHaveBeenCalledWith('follow-up during pending create', {
+ submission_id: entry.id,
+ attachments: [],
+ fromQueue: true,
+ sessionId: 'rt-new-chat',
+ storedSessionId: 'stored-new-chat'
+ })
+ expect(getQueuedPrompts(fresh)).toHaveLength(0)
+ expect(getQueuedPrompts('stored-new-chat')).toHaveLength(0)
+ })
})
diff --git a/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts b/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts
index 223e73299f6..c5b0e14e707 100644
--- a/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts
+++ b/apps/desktop/src/app/session/hooks/use-background-queue-drain.ts
@@ -2,6 +2,7 @@ import { useStore } from '@nanostores/react'
import { type MutableRefObject, useCallback, useEffect, useRef, useState } from 'react'
import { useI18n } from '@/i18n'
+import { isFreshDraftScope } from '@/store/composer'
import { resetBrowseState } from '@/store/composer-input-history'
import {
$parkedQueueSessions,
@@ -238,6 +239,13 @@ export function useBackgroundQueueDrain({
const working = [...workingSessionIds]
for (const [sessionKey, entries] of Object.entries(queuedPromptsBySession)) {
+ // A fresh draft key has no session yet: its follow-ups wait for the
+ // creation handoff to re-key them onto the stored id. Draining here
+ // would exhaust the budget and drop them as a gone session.
+ if (isFreshDraftScope(sessionKey)) {
+ continue
+ }
+
const isSelected =
Boolean(selectedStoredSessionId) && idsShareLineage(sessionKey, selectedStoredSessionId!, sessions)