Skip to content

The agent that grows with you

fix(desktop): a queued send's attachments stage on, recover and submit to ITS session, not the chat on screen

John Paul Solivaaf9d98673f3c
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.tsx
index 1a6271e5623..ef6a97dc6c3 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
@@ -2217,6 +2217,76 @@ describe('usePromptActions submit / queue drain semantics', () => {
).toBe(true)
})
+ it('a fromQueue drain with an image recovers and stages against ITS session, not the chat on screen', async () => {
+ // #46194, the attachments edition: the text drain was bound to its origin
+ // session (#74581), but the attachment sync still took its recovery id and
+ // transport rule from the SELECTED chat. A queued image send to B, drained
+ // after the user moved to A with B's cached runtime dead (sleep/wake), then
+ // resumed A, staged the image on A and submitted B's text into A — and
+ // rebound stored B to A's runtime for every later send.
+ const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
+ const sessionId = String(params?.session_id ?? '')
+
+ if (method === 'session.resume') {
+ return { session_id: sessionId === 'stored-session-b' ? 'rt-session-b-live' : 'rt-session-a-live' } as never
+ }
+
+ if (method === 'image.attach') {
+ if (sessionId === 'rt-session-b-dead') {
+ throw new Error('4007 session not found')
+ }
+
+ return { attached: true, path: '/tmp/shot.png' } as never
+ }
+
+ return {} as never
+ })
+
+ const activeSessionIdRef: MutableRefObject<string | null> = { current: 'rt-session-a' }
+
+ const runtimeIdByStoredSessionIdRef: MutableRefObject<Map<string, string>> = {
+ current: new Map([
+ ['stored-session-a', 'rt-session-a'],
+ ['stored-session-b', 'rt-session-b-dead']
+ ])
+ }
+
+ let handle: HarnessHandle | null = null
+ render(
+ <Harness
+ activeSessionId="rt-session-a"
+ activeSessionIdRef={activeSessionIdRef}
+ getRuntimeIdForStoredSession={storedId => runtimeIdByStoredSessionIdRef.current.get(storedId) ?? null}
+ onReady={h => (handle = h)}
+ refreshSessions={async () => undefined}
+ requestGateway={requestGateway}
+ runtimeIdByStoredSessionIdRef={runtimeIdByStoredSessionIdRef}
+ storedSessionId="stored-session-a"
+ />
+ )
+
+ const accepted = await handle!.submitText('queued for B with a screenshot', {
+ attachments: [{ id: 'att-1', kind: 'image', label: 'shot.png', path: '/tmp/shot.png' } as never],
+ fromQueue: true,
+ sessionId: 'rt-session-b-dead',
+ storedSessionId: 'stored-session-b'
+ })
+
+ expect(accepted).toBe(true)
+ const calls = requestGateway.mock.calls.map(([method, params]) => [method, (params as { session_id?: string })?.session_id])
+
+ // The recovery resumes the queued send's OWN session…
+ expect(calls).toContainEqual(['session.resume', 'stored-session-b'])
+ expect(calls).not.toContainEqual(['session.resume', 'stored-session-a'])
+ // …stages the image there, and submits there.
+ expect(calls).toContainEqual(['image.attach', 'rt-session-b-live'])
+ expect(calls).toContainEqual(['prompt.submit', 'rt-session-b-live'])
+ expect(calls.some(([, sessionId]) => sessionId === 'rt-session-a' || sessionId === 'rt-session-a-live')).toBe(false)
+ // A background drain never steals the foreground, and B's binding now names B's live runtime.
+ expect(activeSessionIdRef.current).toBe('rt-session-a')
+ expect(runtimeIdByStoredSessionIdRef.current.get('stored-session-b')).toBe('rt-session-b-live')
+ })
+
it('a fromQueue drain rebinds to the centrally recorded runtime when its explicit id is stale', async () => {
// Same window, but B's runtime binding is already known centrally — the
// drain should adopt the authoritative binding directly (no resume
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
index 5a61075310d..570693f0b93 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.ts
@@ -339,18 +339,32 @@ export function usePromptActions({
async (
sessionId: string,
attachments: ComposerAttachment[],
- options: { updateComposerAttachments?: boolean } = {}
+ options: { storedSessionId?: null | string; updateComposerAttachments?: boolean } = {}
): Promise<{ attachments: ComposerAttachment[]; sessionId: string }> => {
const updateComposerAttachments = options.updateComposerAttachments ?? true
- const storedSessionId = selectedStoredSessionIdRef.current
+ // The submit's own target, never the chat on screen: a queued send drains
+ // after the user has moved on, so a stale runtime here must recover the
+ // session the text belongs to — not stage the files on, and then submit
+ // into, whichever chat is selected now (#46194, the attachments edition).
+ const storedSessionId = options.storedSessionId ?? selectedStoredSessionIdRef.current
+ const targetIsForeground = storedSessionId === selectedStoredSessionIdRef.current
const remote = isSessionRemote(storedSessionId ?? sessionId)
let liveSessionId = sessionId
const synced: ComposerAttachment[] = []
const onSessionRecovered = (recoveredId: string) => {
liveSessionId = recoveredId
- activeSessionIdRef.current = recoveredId
- setActiveSessionId(recoveredId)
+
+ if (storedSessionId) {
+ runtimeIdByStoredSessionIdRef.current.set(storedSessionId, recoveredId)
+ }
+
+ // Only a foreground send may retarget the foreground: a background
+ // drain that recovers its own session must not steal the view.
+ if (targetIsForeground) {
+ activeSessionIdRef.current = recoveredId
+ setActiveSessionId(recoveredId)
+ }
}
for (const original of attachments) {
@@ -417,7 +431,7 @@ export function usePromptActions({
return { attachments: synced, sessionId: liveSessionId }
},
- [activeSessionIdRef, requestGateway, selectedStoredSessionIdRef]
+ [activeSessionIdRef, requestGateway, runtimeIdByStoredSessionIdRef, selectedStoredSessionIdRef]
)
// Stage a freshly dropped file as soon as it lands (when a session already
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts
index 89fb5173f89..17eb6006fed 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
@@ -70,7 +70,7 @@ interface SubmitPromptDeps {
syncAttachmentsForSubmit: (
sessionId: string,
attachments: ComposerAttachment[],
- options?: { updateComposerAttachments?: boolean }
+ options?: { storedSessionId?: null | string; updateComposerAttachments?: boolean }
) => Promise<{ attachments: ComposerAttachment[]; sessionId: string }>
updateSessionState: (
sessionId: string,
@@ -731,6 +731,7 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {
// plain text survived sleep/wake but images reported "session not
// found". The attach path recovers and reports the live id back here.
const attachResult = await syncAttachmentsForSubmit(sessionId, attachments, {
+ storedSessionId: targetStoredSessionId,
updateComposerAttachments: usingComposerAttachments
})