Skip to content

The agent that grows with you

fix(desktop): re-scope the composer queue when a runtime is claimed

unsupportedpastels19496bb66827
diff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsx
index e822b162cb0..4fa9c95c9a4 100644
--- a/apps/desktop/src/app/chat/composer/index.tsx
+++ b/apps/desktop/src/app/chat/composer/index.tsx
@@ -90,7 +90,7 @@ import { shouldConvertPasteToAttachment } from './large-paste'
import { ActionBadges } from './micro-actions'
import { chipTypedPathOnSpace, pathifyRefs } from './path-refs'
import { PreparedImageRecovery } from './prepared-image-recovery'
-import { composerQueueScopeKey, renderComposerQueueSlot } from './queue-slot'
+import { renderComposerQueueSlot, useComposerQueueScopeKey } from './queue-slot'
import { RestoredDraftNotice } from './restored-draft-notice'
import {
beginComposerComposition,
@@ -179,7 +179,7 @@ export function ChatBar({
// prompt owns its own dismissal (Skip, Reject, dialog close).
const awaitingInput = useStore(scope.$awaitingInput)
const blockingPrompt = useStore(useMemo(() => sessionBlockingPrompt(sessionId ?? null), [sessionId]))
- const activeQueueSessionKey = composerQueueScopeKey(queueSessionKey, sessionId, freshDraftKey)
+ const activeQueueSessionKey = useComposerQueueScopeKey(queueSessionKey, sessionId, freshDraftKey)
const { collapsed: statusDrawerCollapsed, toggle: toggleStatusDrawer } = useStatusDrawer(activeQueueSessionKey)
const statusDrawerId = useId()
const codingDrawerId = useId()
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 a9f1b81a257..b51f8023051 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
@@ -18,7 +18,7 @@ import { $sessionStates } from '@/store/session-states'
import type { QueueEditState } from './composer-utils'
import { useComposerDraft } from './hooks/use-composer-draft'
import { useComposerQueue } from './hooks/use-composer-queue'
-import { composerQueueScopeKey } from './queue-slot'
+import { useComposerQueueScopeKey } from './queue-slot'
import type { ChatBarProps } from './types'
vi.mock('@assistant-ui/react', () => ({
@@ -51,7 +51,7 @@ function Harness({ freshDraftKey, latest, onSubmit, ...scope }: Scope & {
latest: { current: Composer | null }
onSubmit: ChatBarProps['onSubmit']
}) {
- const activeQueueSessionKey = composerQueueScopeKey(scope.queueSessionKey, scope.sessionId, freshDraftKey)
+ const activeQueueSessionKey = useComposerQueueScopeKey(scope.queueSessionKey, scope.sessionId, freshDraftKey)
const queueEditRef = useRef<QueueEditState | null>(null)
const attachments = useStore(mainComposerScope.$attachments)
@@ -191,6 +191,23 @@ it('a follow-up queued while session.create is in flight moves to the new chat a
expect(getQueuedPrompts(freshDraftKey)).toEqual([])
})
+it('claiming the runtime re-scopes the composer without a sessionId or route change', () => {
+ const freshDraftKey = rotateFreshDraftKey()
+ const latest: { current: Composer | null } = { current: null }
+ const onSubmit = vi.fn<ChatBarProps['onSubmit']>(async () => true)
+
+ // session.create published the runtime before the mirror: nothing claims it yet.
+ render(
+ <Harness busy freshDraftKey={freshDraftKey} latest={latest} onSubmit={onSubmit} queueSessionKey={null} sessionId="rt-new" />
+ )
+ expect(latest.current?.scope).toBe('rt-new')
+
+ // The mirror lands; props and route stay put.
+ act(() => $sessionStates.set({ 'rt-new': { storedSessionId: 'stored-new' } as ClientSessionState }))
+
+ expect(latest.current?.scope).toBe(freshDraftKey)
+})
+
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 }
diff --git a/apps/desktop/src/app/chat/composer/queue-slot.tsx b/apps/desktop/src/app/chat/composer/queue-slot.tsx
index 0d143a3b750..3c4c90b8446 100644
--- a/apps/desktop/src/app/chat/composer/queue-slot.tsx
+++ b/apps/desktop/src/app/chat/composer/queue-slot.tsx
@@ -2,9 +2,11 @@ import type { ReactNode } from 'react'
import type { HermesGateway } from '@/hermes'
import type { Translations } from '@/i18n'
+import { useStoresSelector } from '@/lib/use-session-slice'
import { type QueuedPromptEntry, removeQueuedPrompt, unparkQueuedPrompts } from '@/store/composer-queue'
import { notifyError } from '@/store/notifications'
-import { storedSessionIdForRuntimeId } from '@/store/session-states'
+import { $activeSessionId, $selectedStoredSessionId } from '@/store/session'
+import { $sessionStates, $sessionTiles, storedSessionIdForRuntimeId } from '@/store/session-states'
import type { QueueEditState } from './composer-utils'
import { cancelQueuedPrompt, discardLostPrompt } from './discard-lost-prompt'
@@ -32,7 +34,7 @@ export interface ComposerQueueSlotInputs {
* claims it: a claimed runtime there is the chat just left, still active for a
* frame after the switch, and its queue is not this chat's (#139190).
*/
-export function composerQueueScopeKey(
+function composerQueueScopeKey(
queueSessionKey: null | string | undefined,
sessionId: null | string | undefined,
freshDraftKey: null | string | undefined
@@ -48,6 +50,19 @@ export function composerQueueScopeKey(
return freshDraftKey || null
}
+/** `composerQueueScopeKey` subscribed to every store the runtime-claim lookup
+ * reads: session.create publishes the runtime before its mirror, so a claim
+ * can land while `sessionId` and the route stay put. */
+export function useComposerQueueScopeKey(
+ queueSessionKey: null | string | undefined,
+ sessionId: null | string | undefined,
+ freshDraftKey: null | string | undefined
+): null | string {
+ return useStoresSelector([$sessionTiles, $sessionStates, $activeSessionId, $selectedStoredSessionId], () =>
+ composerQueueScopeKey(queueSessionKey, sessionId, freshDraftKey)
+ )
+}
+
/**
* The queue node handed to the status stack: a QueuePanel wired to the
* composer's queue engine, or null when there is nothing queued (the stack