Skip to content

The agent that grows with you

fix(desktop): stop approval.pending replays against a gone active runtime

kshitijk4poord68739c2fa9e
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts
index df11da34bee..beec7415034 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/index.ts
@@ -11,6 +11,7 @@ import { reconcileSessionCompacting } from '@/store/compaction'
import { $gateway, activeGatewayConnectionId } from '@/store/gateway'
import { $activeGatewayProfile, normalizeProfileKey } from '@/store/profile'
import { replayPendingApproval } from '@/store/prompts'
+import { isSessionGone } from '@/store/session-gone-latch'
import { setSessionProviderWait } from '@/store/provider-wait'
import { setSessionDraftingTool } from '@/store/tool-drafting'
import type { RpcEvent } from '@/types/hermes'
@@ -195,7 +196,10 @@ export function useGatewayEventHandler(deps: GatewayEventDeps) {
const isActiveEvent = !!sessionId && sessionId === activeSessionIdRef.current
- const replaySessionId = approvalReplaySessionId(event.type, activeSessionIdRef.current, sessionId)
+ const replaySessionId = approvalReplaySessionId(event.type, activeSessionIdRef.current, sessionId, {
+ explicit: Boolean(explicitSid),
+ isGone: isSessionGone
+ })
if (replaySessionId) {
void replayPendingApproval($gateway.get(), replaySessionId).catch(() => undefined)
diff --git a/apps/desktop/src/lib/gateway-events.test.ts b/apps/desktop/src/lib/gateway-events.test.ts
index 5ec527d0bcf..b9af988f8c3 100644
--- a/apps/desktop/src/lib/gateway-events.test.ts
+++ b/apps/desktop/src/lib/gateway-events.test.ts
@@ -9,6 +9,18 @@ describe('gateway event routing', () => {
expect(approvalReplaySessionId('message.delta', 'active-1', 'routed-1')).toBeNull()
})
+ it('does not replay against an active runtime the gateway already reported gone', () => {
+ const isGone = (sid: string) => sid === 'dead-1'
+
+ // Unscoped fan-out attributed to a dead active session: skip.
+ expect(approvalReplaySessionId('session.info', 'dead-1', 'dead-1', { explicit: false, isGone })).toBeNull()
+ expect(approvalReplaySessionId('gateway.ready', 'dead-1', null, { explicit: false, isGone })).toBeNull()
+ // A live active session still replays.
+ expect(approvalReplaySessionId('session.info', 'live-1', 'live-1', { explicit: false, isGone })).toBe('live-1')
+ // An explicitly scoped frame is the runtime speaking for itself — never skipped.
+ expect(approvalReplaySessionId('session.info', 'dead-1', 'dead-1', { explicit: true, isGone })).toBe('dead-1')
+ })
+
it('drops only unscoped subagent events (genuinely background work)', () => {
expect(gatewayEventRequiresSessionId('subagent.progress')).toBe(true)
expect(gatewayEventRequiresSessionId('subagent.start')).toBe(true)
diff --git a/apps/desktop/src/lib/gateway-events.ts b/apps/desktop/src/lib/gateway-events.ts
index 6375955510d..7c994a71ce9 100644
--- a/apps/desktop/src/lib/gateway-events.ts
+++ b/apps/desktop/src/lib/gateway-events.ts
@@ -80,20 +80,34 @@ export interface GatewayEventSessionRoute {
sessionId: null | string
}
+/** Which session (if any) to re-pull `approval.pending` for after `eventType`.
+ *
+ * `gateway.ready` and `session.info` are the two rehydration points. An
+ * UNSCOPED `session.info` (the approvals-loop / broadcast fan-out, no
+ * `session_id` on the frame) reaches here attributed to the active session by
+ * the routing fallback; when `isGone(activeSessionId)` — the gateway already
+ * answered 4001 for that runtime — replaying would only re-send the dead id
+ * on every fan-out tick (#100639), so return null. A frame that names the
+ * session explicitly is the runtime speaking for itself and is never gone. */
export function approvalReplaySessionId(
eventType: string | undefined,
activeSessionId: null | string,
- routedSessionId: null | string
+ routedSessionId: null | string,
+ options?: { explicit?: boolean; isGone?: (sessionId: string) => boolean }
): null | string {
+ let target: null | string = null
+
if (eventType === 'gateway.ready') {
- return activeSessionId
+ target = activeSessionId
+ } else if (eventType === 'session.info') {
+ target = routedSessionId
}
- if (eventType === 'session.info') {
- return routedSessionId
+ if (target && !options?.explicit && options?.isGone?.(target)) {
+ return null
}
- return null
+ return target
}
/**