Skip to content

The agent that grows with you

fix: keep canonical RPC refusals out of Desktop resume recovery

Teknium9ba2582bbae1
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 cee4203f447..dc06b6c769e 100644
--- a/apps/desktop/src/app/contrib/session-rpc-dispatcher.test.ts
+++ b/apps/desktop/src/app/contrib/session-rpc-dispatcher.test.ts
@@ -209,6 +209,26 @@ describe('createSessionRpcDispatcher: exact owner rungs', () => {
})
describe('createSessionRpcDispatcher: stale runtime recovery', () => {
+ it('surfaces canonical refusals without requesting another resume of the healthy selected session', async () => {
+ setSessions([makeSessionInfo({ connection_id: 'local', id: 'stored-omar', profile: 'omar' })])
+ const { request } = dispatcher(undefined, 'stored-omar')
+
+ for (const reason of ['invalid_params', 'permission_denied', 'stale_generation', 'profile_mismatch']) {
+ const error = Object.assign(new Error(reason), { code: 4001, data: { reason } })
+ gatewayMocks.requestGatewayForAgent.mockRejectedValueOnce(error)
+ await expect(request('config.get', { session_id: 'rt-omar' })).rejects.toBe(error)
+ expect(sessionMocks.requestSessionResume).not.toHaveBeenCalled()
+ }
+
+ const missing = Object.assign(new Error('not_found'), { code: 4001, data: { reason: 'not_found' } })
+ gatewayMocks.requestGatewayForAgent.mockRejectedValueOnce(missing)
+ await expect(request('process.list', { session_id: 'rt-omar' })).rejects.toBe(missing)
+ expect(sessionMocks.requestSessionResume).toHaveBeenCalledExactlyOnceWith('stored-omar', {
+ connectionId: 'local',
+ profile: 'omar'
+ })
+ })
+
it('requests a durable rebind for the visible session after a structured 4001', async () => {
setSessions([makeSessionInfo({ connection_id: 'local', id: 'stored-omar', profile: 'omar' })])
gatewayMocks.requestGatewayForAgent.mockRejectedValueOnce(
diff --git a/apps/desktop/src/store/runtime-gone.test.ts b/apps/desktop/src/store/runtime-gone.test.ts
index 7c0198825ba..16fda9e366b 100644
--- a/apps/desktop/src/store/runtime-gone.test.ts
+++ b/apps/desktop/src/store/runtime-gone.test.ts
@@ -140,6 +140,27 @@ describe('markRuntimeGone', () => {
})
describe('refreshBackgroundProcesses recovery', () => {
+ it('does not unbind a live session for canonical non-missing failures sharing code 4001', async () => {
+ $sessionTiles.set([tile(STORED, RUNTIME)])
+ $sessionStates.set({ [RUNTIME]: cachedState(STORED) })
+ $activeSessionId.set(RUNTIME)
+ const request = vi.fn()
+ $gateway.set({ request } as never)
+
+ for (const reason of ['invalid_params', 'permission_denied', 'stale_generation', 'profile_mismatch']) {
+ request.mockRejectedValueOnce(new JsonRpcGatewayError(reason, { code: 4001, data: { reason } }))
+ await refreshBackgroundProcesses(RUNTIME)
+ expect($sessionTiles.get()[0]?.runtimeId).toBe(RUNTIME)
+ expect($sessionResumeRequest.get()).toBeNull()
+ expect(isSessionGone(RUNTIME)).toBe(false)
+ }
+
+ request.mockRejectedValueOnce(new JsonRpcGatewayError('not_found', { code: 4001, data: { reason: 'not_found' } }))
+ await refreshBackgroundProcesses(RUNTIME)
+ expect($sessionTiles.get()[0]?.runtimeId).toBeUndefined()
+ expect($sessionResumeRequest.get()?.sessionId).toBe(STORED)
+ })
+
it('carries the gateway 4001 verdict to the view instead of only going quiet', async () => {
$sessionTiles.set([tile(STORED, RUNTIME)])
$sessionStates.set({ [RUNTIME]: cachedState(STORED) })
diff --git a/apps/desktop/src/store/session-gone-latch.ts b/apps/desktop/src/store/session-gone-latch.ts
index 5e22ebf5502..2ecf909e340 100644
--- a/apps/desktop/src/store/session-gone-latch.ts
+++ b/apps/desktop/src/store/session-gone-latch.ts
@@ -26,11 +26,20 @@ export const healsByStoredId = new Map<string, number>()
* blip is not. Only the former may stop a poll — misclassifying a transient
* failure would silently freeze a healthy session.
*
- * Match the gateway's 4001 code when the error carries one. Codeless errors
+ * Canonical authority errors share code 4001; their reason is authoritative.
+ * Only reason `not_found` means the binding is gone. Legacy errors without a
+ * reason retain the gateway's 4001 contract. Codeless errors
* (the frame's structure was lost across the IPC bridge or a wrapped rethrow)
* are accepted only with a bare "session not found" body — a tool or report
* string that merely mentions the phrase must not latch a live runtime. */
export function isSessionGoneForBackgroundPolling(error: unknown): boolean {
+ const data = error && typeof error === 'object' ? (error as { data?: unknown }).data : undefined
+ const reason = data && typeof data === 'object' ? (data as { reason?: unknown }).reason : undefined
+
+ if (typeof reason === 'string') {
+ return reason === 'not_found'
+ }
+
if (error instanceof JsonRpcGatewayError && typeof error.code === 'number') {
return error.code === GATEWAY_SESSION_NOT_FOUND_CODE
}