Skip to content

The agent that grows with you

fix(desktop): harden stale session RPC guard

Shinji Kaneko0a80adc127d0
diff --git a/apps/desktop/src/store/composer-status.test.ts b/apps/desktop/src/store/composer-status.test.ts
index 5173f8aa237..547d269c26b 100644
--- a/apps/desktop/src/store/composer-status.test.ts
+++ b/apps/desktop/src/store/composer-status.test.ts
@@ -13,6 +13,9 @@ import {
import { $gateway } from './gateway'
import { markSessionGone } from './runtime-gone'
+vi.mock('./notifications', () => ({ notifyError: vi.fn() }))
+import { notifyError } from './notifications'
+
const SID = 'sess-1'
const running = (id: string, command = `cmd ${id}`) => ({ command, session_id: id, status: 'running' })
@@ -286,6 +289,17 @@ describe('refreshBackgroundProcesses dead-session guard', () => {
expect(items()).toEqual([])
})
+ it('keeps the row and reports failure when the gateway is disconnected', async () => {
+ reconcileBackgroundProcesses(SID, [running('unreachable')])
+ $gateway.set(null as never)
+ vi.mocked(notifyError).mockClear()
+
+ await stopBackgroundProcess(SID, 'unreachable')
+
+ expect(items()).toEqual([expect.objectContaining({ id: 'unreachable', state: 'running' })])
+ expect(notifyError).toHaveBeenCalledWith(expect.any(Error), 'Could not stop the process')
+ })
+
it('dismisses and latches when Stop discovers the runtime is gone', async () => {
const request = vi.fn(async () => {
throw new Error('session not found')
diff --git a/apps/desktop/src/store/composer-status.ts b/apps/desktop/src/store/composer-status.ts
index ea2d7ae04c3..a260a0926a9 100644
--- a/apps/desktop/src/store/composer-status.ts
+++ b/apps/desktop/src/store/composer-status.ts
@@ -466,6 +466,8 @@ export async function stopBackgroundProcess(sid: string, id: string): Promise<vo
}
if (!gateway) {
+ notifyError(new Error('Gateway is not connected'), 'Could not stop the process')
+
return
}
diff --git a/apps/desktop/src/store/native-notifications.test.ts b/apps/desktop/src/store/native-notifications.test.ts
index c1fd51d30f7..7bbb5cd888c 100644
--- a/apps/desktop/src/store/native-notifications.test.ts
+++ b/apps/desktop/src/store/native-notifications.test.ts
@@ -16,6 +16,7 @@ import {
import { __resetNativeNotifyBaselineForTests, markNativeNotifyBaseline } from './notify-baseline'
import { $approvalRequest, setApprovalRequest } from './prompts'
import { $activeSessionId, setActiveSessionId } from './session'
+import { markSessionGone, resetBackgroundPollingGuard } from './runtime-gone'
const desktopWindow = window as unknown as { hermesDesktop?: Window['hermesDesktop'] }
const initialHermesDesktop = desktopWindow.hermesDesktop
@@ -47,6 +48,7 @@ beforeEach(() => {
}
setActiveSessionId(null)
+ resetBackgroundPollingGuard()
setWindowState({ focused: false, hidden: true })
__resetNativeNotifyBaselineForTests()
})
@@ -59,6 +61,8 @@ afterEach(() => {
} else {
delete desktopWindow.hermesDesktop
}
+
+ resetBackgroundPollingGuard()
})
describe('dispatchNativeNotification focus gating', () => {
@@ -339,4 +343,12 @@ describe('respondToApprovalAction', () => {
await respondToApprovalAction('bg', 'approve')
expect(request).not.toHaveBeenCalled()
})
+
+ it('does not retry an approval action for a runtime already marked gone', async () => {
+ markSessionGone('bg')
+
+ await respondToApprovalAction('bg', 'approve')
+
+ expect(request).not.toHaveBeenCalled()
+ })
})
diff --git a/apps/desktop/src/store/native-notifications.ts b/apps/desktop/src/store/native-notifications.ts
index 341d31a1eaf..cfe87ce0c5e 100644
--- a/apps/desktop/src/store/native-notifications.ts
+++ b/apps/desktop/src/store/native-notifications.ts
@@ -7,7 +7,7 @@ import { $gateway } from './gateway'
import { withinNativeNotifyBaseline } from './notify-baseline'
import { clearApprovalRequest } from './prompts'
import { $activeSessionId } from './session'
-import { isSessionGoneForBackgroundPolling, markSessionGone } from './runtime-gone'
+import { isSessionGoneForBackgroundPolling, isSessionGone, markSessionGone } from './runtime-gone'
import { requestForOwnedSession } from './session-states'
export type { HermesOpenTarget }
@@ -354,6 +354,10 @@ export async function respondToApprovalAction(sessionId: null | string, actionId
return
}
+ if (sessionId && isSessionGone(sessionId)) {
+ return
+ }
+
const gateway = $gateway.get()
if (!gateway) {
diff --git a/apps/desktop/src/store/prompts.test.ts b/apps/desktop/src/store/prompts.test.ts
index d1e8360f912..015a47542df 100644
--- a/apps/desktop/src/store/prompts.test.ts
+++ b/apps/desktop/src/store/prompts.test.ts
@@ -146,6 +146,15 @@ describe('approval prompt store', () => {
expect($approvalRequest.get()).toBeNull()
})
+ it('propagates transient approval replay failures without latching the runtime', async () => {
+ const request = vi.fn(async () => {
+ throw new Error('gateway timed out')
+ })
+
+ await expect(replayPendingApproval({ request }, 'transient-runtime')).rejects.toThrow('gateway timed out')
+ expect(isSessionGone('transient-runtime')).toBe(false)
+ })
+
it('keeps approval receipt failures contained and marks the runtime gone', async () => {
const request = vi.fn(async () => {
throw new JsonRpcGatewayError('session not found', { code: 4001 })
diff --git a/apps/desktop/src/store/prompts.ts b/apps/desktop/src/store/prompts.ts
index ae34ee2db0f..419de6b3a7b 100644
--- a/apps/desktop/src/store/prompts.ts
+++ b/apps/desktop/src/store/prompts.ts
@@ -159,9 +159,11 @@ export async function replayPendingApproval(gateway: ApprovalGateway | null, ses
} catch (error) {
if (isSessionGoneForBackgroundPolling(error)) {
markSessionGone(sessionId)
+
+ return
}
- return
+ throw error
}
const result =
diff --git a/apps/desktop/src/store/session-request-router.test.ts b/apps/desktop/src/store/session-request-router.test.ts
index 10268403e87..9bd5a6d0ad2 100644
--- a/apps/desktop/src/store/session-request-router.test.ts
+++ b/apps/desktop/src/store/session-request-router.test.ts
@@ -206,6 +206,39 @@ describe('requestForSessionProfile', () => {
expect(isSessionGone('rt-rebound')).toBe(true)
})
+ it('clears a dead-runtime latch through the bare profile owner route', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ installDesktop()
+ const ambient = vi.fn(async () => ({ ambient: true }))
+
+ markSessionGone('profile-rebound')
+
+ await requestForSessionProfile('loki', ambient as never, 'session.resume', {
+ session_id: 'profile-rebound'
+ })
+
+ expect(isSessionGone('profile-rebound')).toBe(false)
+ })
+
+ it('clears a dead-runtime latch through an explicit connection owner route', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ installDesktop()
+ const ambient = vi.fn(async () => ({ ambient: true }))
+
+ markSessionGone('connection-rebound')
+
+ await requestForSessionProfile(
+ { connectionId: 'source-a', profile: 'default' },
+ ambient as never,
+ 'session.activate',
+ { session_id: 'connection-rebound' }
+ )
+
+ expect(isSessionGone('connection-rebound')).toBe(false)
+ })
+
it('keeps routing a bare profile owner through its legacy profile pool when a connection registry exists', async () => {
// A profile pick on the primary or the explicit `local` source takes the
// legacy profile-only door (store/profile activateOnCurrentSource), so a
diff --git a/apps/desktop/src/store/session-request-router.ts b/apps/desktop/src/store/session-request-router.ts
index e328bd6f891..91cb5d2fdbb 100644
--- a/apps/desktop/src/store/session-request-router.ts
+++ b/apps/desktop/src/store/session-request-router.ts
@@ -109,7 +109,7 @@ async function withRoutedTurnLease<T>(
const sessionId = promptSessionId(method, params)
if (!sessionId) {
- return request()
+ return requestWithRebindGuard(method, params, request)
}
const release = await retainGatewayForSessionTurn(connectionId, profile, sessionId)