Skip to content

The agent that grows with you

fix(desktop): publish the recovered binding before the retried attach, not after it returns

John Paul Soliva8cc4ff5d665d
diff --git a/apps/desktop/src/app/contrib/session-rpc-dispatcher.test.ts b/apps/desktop/src/app/contrib/session-rpc-dispatcher.test.ts
index 7544aeeb115..baf69023e3b 100644
--- a/apps/desktop/src/app/contrib/session-rpc-dispatcher.test.ts
+++ b/apps/desktop/src/app/contrib/session-rpc-dispatcher.test.ts
@@ -149,6 +149,40 @@ describe('createSessionRpcDispatcher: exact owner rungs', () => {
expect(probe.resolveSessionOwner).not.toHaveBeenCalled()
})
+ it('routes a freshly recovered runtime id to its session\'s owner only once the binding is published', async () => {
+ // A session-scoped retry after withSessionNotFoundResume: the new runtime id is
+ // known to nobody until the recovering caller publishes stored → runtime. Before
+ // that, the dispatcher cannot translate it and fails closed; after, it reaches the
+ // stored session's owner. Callers therefore publish in `onRecovered`, which runs
+ // before the retried call — not after the retry has already returned.
+ setSessions([makeSessionInfo({ connection_id: 'local', id: 'stored-omar', profile: 'omar' })])
+ const runtimeIdByStoredSessionIdRef = { current: new Map([['stored-omar', 'rt-omar-dead']]) }
+ const ambientRequest = vi.fn(async () => ({ ambient: true }))
+
+ const request = createSessionRpcDispatcher({
+ ambientRequest: ambientRequest as never,
+ runtimeIdByStoredSessionIdRef,
+ selectedStoredSessionIdRef: { current: null },
+ sessionStateByRuntimeIdRef: { current: new Map() }
+ })
+
+ await expect(request('image.attach', { path: '/tmp/shot.png', session_id: 'rt-omar-live' })).rejects.toSatisfy(
+ isSessionOwnerResolutionError
+ )
+ expect(gatewayMocks.requestGatewayForAgent).not.toHaveBeenCalled()
+
+ runtimeIdByStoredSessionIdRef.current.set('stored-omar', 'rt-omar-live')
+
+ await expect(request('image.attach', { path: '/tmp/shot.png', session_id: 'rt-omar-live' })).resolves.toEqual({
+ routed: true
+ })
+ expect(gatewayMocks.requestGatewayForAgent).toHaveBeenCalledWith('local', 'omar', 'image.attach', {
+ path: '/tmp/shot.png',
+ session_id: 'rt-omar-live'
+ })
+ expect(ambientRequest).not.toHaveBeenCalled()
+ })
+
it('prefers the exact hint over an untagged row profile, and the probe result over nothing', async () => {
setSessions([makeSessionInfo({ id: 'stored-omar', profile: 'default' })])
setSessionOwnerHint('stored-omar', { connectionId: 'local', profile: 'omar' })
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 ef6a97dc6c3..00ca71b44c6 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
@@ -2224,6 +2224,22 @@ describe('usePromptActions submit / queue drain semantics', () => {
// 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 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']
+ ])
+ }
+
+ const updates: { sessionId: string; storedSessionId: null | string | undefined }[] = []
+ // What the production dispatcher can see at the moment the retried attach is
+ // issued: it translates the runtime id back to the stored session (and its
+ // owner) through exactly these bindings, so they must be published BEFORE
+ // the retry, not after it returns.
+ let bindingAtRetry: { central: boolean; map: string | undefined } | null = null
+
const requestGateway = vi.fn(async (method: string, params?: Record<string, unknown>) => {
const sessionId = String(params?.session_id ?? '')
@@ -2236,21 +2252,19 @@ describe('usePromptActions submit / queue drain semantics', () => {
throw new Error('4007 session not found')
}
+ if (sessionId === 'rt-session-b-live') {
+ bindingAtRetry = {
+ central: updates.some(u => u.sessionId === 'rt-session-b-live' && u.storedSessionId === 'stored-session-b'),
+ map: runtimeIdByStoredSessionIdRef.current.get('stored-session-b')
+ }
+ }
+
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
@@ -2258,6 +2272,7 @@ describe('usePromptActions submit / queue drain semantics', () => {
activeSessionIdRef={activeSessionIdRef}
getRuntimeIdForStoredSession={storedId => runtimeIdByStoredSessionIdRef.current.get(storedId) ?? null}
onReady={h => (handle = h)}
+ onUpdateState={(sessionId, storedSessionId) => updates.push({ sessionId, storedSessionId })}
refreshSessions={async () => undefined}
requestGateway={requestGateway}
runtimeIdByStoredSessionIdRef={runtimeIdByStoredSessionIdRef}
@@ -2285,6 +2300,8 @@ describe('usePromptActions submit / queue drain semantics', () => {
// 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')
+ // …and it did so BEFORE the retried attach went out, in both places the dispatcher reads.
+ expect(bindingAtRetry).toEqual({ central: true, map: 'rt-session-b-live' })
})
it('a fromQueue drain rebinds to the centrally recorded runtime when its explicit id is stale', async () => {
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 570693f0b93..131a94ab6f6 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
@@ -129,12 +129,14 @@ export async function uploadComposerAttachment(
sessionId: string
/** Durable id used to re-register after sleep/wake or a backend restart. */
storedSessionId?: null | string
+ /** Runs BEFORE the retried attach: publish the recovered stored→runtime binding here. */
+ onRecovered?: (sessionId: string) => void
/** Called when the attach recovered onto a fresh live id. */
onSessionRecovered?: (sessionId: string) => void
terminalBackend?: string
}
): Promise<ComposerAttachment> {
- const { backendCwd, remote, requestGateway, storedSessionId, onSessionRecovered, terminalBackend } = opts
+ const { backendCwd, remote, requestGateway, storedSessionId, onRecovered, onSessionRecovered, terminalBackend } = opts
const path = attachment.path ?? ''
const label = attachment.label || pathLabel(path)
const uploadBytes = remote || attachmentPathNeedsUpload(path, backendCwd, terminalBackend)
@@ -217,7 +219,11 @@ export async function uploadComposerAttachment(
opts.sessionId,
storedSessionId,
stageForSession,
- { requestGateway }
+ // onRecovered fires before the retried attach: the window dispatcher routes a
+ // session-scoped RPC by translating its runtime id back to the stored session
+ // and that session's owner, so the recovered binding must be published first
+ // or an off-screen remote / multi-profile retry is rejected as ownerless.
+ { requestGateway, onRecovered }
)
if (usedSessionId !== opts.sessionId) {
@@ -352,11 +358,12 @@ export function usePromptActions({
let liveSessionId = sessionId
const synced: ComposerAttachment[] = []
- const onSessionRecovered = (recoveredId: string) => {
- liveSessionId = recoveredId
-
+ // Published BEFORE the retried attach (see uploadComposerAttachment): the
+ // stored→runtime binding is what routes the retry to the session's owner.
+ const onRecovered = (recoveredId: string) => {
if (storedSessionId) {
runtimeIdByStoredSessionIdRef.current.set(storedSessionId, recoveredId)
+ updateSessionState(recoveredId, state => state, storedSessionId)
}
// Only a foreground send may retarget the foreground: a background
@@ -367,6 +374,10 @@ export function usePromptActions({
}
}
+ const onSessionRecovered = (recoveredId: string) => {
+ liveSessionId = recoveredId
+ }
+
for (const original of attachments) {
let attachment = original
@@ -400,6 +411,7 @@ export function usePromptActions({
requestGateway,
sessionId: liveSessionId,
storedSessionId,
+ onRecovered,
onSessionRecovered,
terminalBackend: $terminalBackend.get()
})
@@ -431,7 +443,7 @@ export function usePromptActions({
return { attachments: synced, sessionId: liveSessionId }
},
- [activeSessionIdRef, requestGateway, runtimeIdByStoredSessionIdRef, selectedStoredSessionIdRef]
+ [activeSessionIdRef, requestGateway, runtimeIdByStoredSessionIdRef, selectedStoredSessionIdRef, updateSessionState]
)
// Stage a freshly dropped file as soon as it lands (when a session already