Skip to content

The agent that grows with you

fix(desktop): harden the dead-session poll guard per #94950 review

Tekniuma7ea15647007
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
index a2a4cf133a7..142b8cd43d1 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -16,6 +16,7 @@ import {
resumeDesktopBootForRetry,
setDesktopBootStep
} from '@/store/boot'
+import { resetBackgroundPollingGuard } from '@/store/composer-status'
import {
$gateway,
activeGatewayConnectionId,
@@ -344,6 +345,11 @@ export function useGatewayBoot({
resetTileRuntimeBindings(
primaryRuntimeConnectionId(conn) ?? { liveConnectionIds: liveSecondaryConnectionIds() }
)
+ // The status-stack poll guard latches session ids the OLD runtime
+ // reported gone (4001). A respawned backend re-mints runtimes, so
+ // those ids may be live again after re-resume — clear the latch with
+ // the same lifetime as the runtime bindings it shadows.
+ resetBackgroundPollingGuard()
// Same staleness, other half: pre-reconnect busy flags are keyed by
// those dead runtime ids and would never receive their terminal
// busy:false — clear them or the sidebar running arc lies forever
diff --git a/apps/desktop/src/store/composer-status.test.ts b/apps/desktop/src/store/composer-status.test.ts
index 65bb0447497..f85c3ff7c41 100644
--- a/apps/desktop/src/store/composer-status.test.ts
+++ b/apps/desktop/src/store/composer-status.test.ts
@@ -1,5 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { JsonRpcGatewayError } from '@hermes/shared'
+
import {
$backgroundStatusBySession,
dismissBackgroundProcess,
@@ -263,3 +265,55 @@ describe('refreshBackgroundProcesses dead-session guard', () => {
expect(request).toHaveBeenCalledTimes(2)
})
})
+
+// ── Review-thread hardenings on the guard (#94950) ───────────────────────────
+describe('refreshBackgroundProcesses dead-session guard hardenings', () => {
+ beforeEach(() => {
+ $backgroundStatusBySession.set({})
+ resetBackgroundPollingGuard()
+ })
+
+ afterEach(() => {
+ $gateway.set(null as never)
+ resetBackgroundPollingGuard()
+ })
+
+ it('matches the structured 4001 code, not a message substring, when a code is present', () => {
+ // Structured gateway rejection: the code decides, both directions.
+ expect(isSessionGoneForBackgroundPolling(new JsonRpcGatewayError('session not found', { code: 4001 }))).toBe(true)
+ expect(isSessionGoneForBackgroundPolling(new JsonRpcGatewayError('gone', { code: 4001 }))).toBe(true)
+
+ // An unrelated coded error whose text merely MENTIONS the phrase must not
+ // latch — that would freeze the status stack on a healthy session.
+ expect(
+ isSessionGoneForBackgroundPolling(
+ new JsonRpcGatewayError('tool failed: upstream said session not found', { code: 5007 })
+ )
+ ).toBe(false)
+
+ // Codeless errors keep the message fallback (legacy frames).
+ expect(isSessionGoneForBackgroundPolling(new JsonRpcGatewayError('session not found'))).toBe(true)
+ })
+
+ it('a full guard reset (runtime re-mint) resumes polling every latched session', async () => {
+ const request = vi.fn(async () => {
+ throw new JsonRpcGatewayError('session not found', { code: 4001 })
+ })
+
+ $gateway.set({ request } as never)
+
+ await refreshBackgroundProcesses(SID)
+ await refreshBackgroundProcesses('sess-2')
+ await refreshBackgroundProcesses(SID)
+ await refreshBackgroundProcesses('sess-2')
+ expect(request).toHaveBeenCalledTimes(2)
+
+ // Gateway reconnect re-mints runtimes: the no-arg reset (wired at the
+ // reconnect seams) must clear every latched id, not just one.
+ resetBackgroundPollingGuard()
+ await refreshBackgroundProcesses(SID)
+ await refreshBackgroundProcesses('sess-2')
+
+ expect(request).toHaveBeenCalledTimes(4)
+ })
+})
diff --git a/apps/desktop/src/store/composer-status.ts b/apps/desktop/src/store/composer-status.ts
index 4b627fe73fb..6960f19230c 100644
--- a/apps/desktop/src/store/composer-status.ts
+++ b/apps/desktop/src/store/composer-status.ts
@@ -1,5 +1,7 @@
import { atom, computed } from 'nanostores'
+import { JsonRpcGatewayError } from '@hermes/shared'
+
import { translateNow } from '@/i18n'
import { stableArray } from '@/lib/stable-array'
import type { TodoItem, TodoStatus } from '@/lib/todos'
@@ -387,10 +389,23 @@ export function reconcileBackgroundProcesses(sid: string, procs: GatewayProcessE
* (#94219 fallout). Latch the id here and skip it until something rebinds it. */
const goneSessions = new Set<string>()
+/** Gateway JSON-RPC code for "session not found" (tui_gateway _sess_nowait). */
+const GATEWAY_SESSION_NOT_FOUND_CODE = 4001
+
/** A gone session is unrecoverable for THIS runtime id; a timeout or transport
* blip is not. Only the former may stop the poll — misclassifying a transient
- * failure would silently freeze the status stack on a healthy session. */
+ * failure would silently freeze the status stack on a healthy session.
+ *
+ * Match the gateway's 4001 code when the error carries one (JsonRpcGatewayError
+ * from a structured RPC rejection) — a message substring alone could latch on
+ * an unrelated error class that merely mentions "session not found" (e.g. a
+ * wrapped tool/report string). The message fallback survives only for errors
+ * with no numeric code at all, where the frame's structure was lost. */
export function isSessionGoneForBackgroundPolling(error: unknown): boolean {
+ if (error instanceof JsonRpcGatewayError && typeof error.code === 'number') {
+ return error.code === GATEWAY_SESSION_NOT_FOUND_CODE
+ }
+
const message = error instanceof Error ? error.message : String(error ?? '')
return /session not found/i.test(message)
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 3aa53d2caa5..570eaba70ff 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -450,11 +450,15 @@ async function openSecondary(entry: Secondary): Promise<void> {
if (reopening) {
try {
const { reconcileBusyStatesOnReconnect, resetTileRuntimeBindings } = await import('@/store/session-states')
+ const { resetBackgroundPollingGuard } = await import('@/store/composer-status')
resetTileRuntimeBindings({
connectionId: entry.connectionId || 'local',
profile: entry.profile
})
+ // Runtime re-mint also invalidates the status-stack gone-latch: ids
+ // the dead runtime 4001'd may be live again once tiles re-resume.
+ resetBackgroundPollingGuard()
reconcileBusyAfterOpen = () => reconcileBusyStatesOnReconnect(entry.scope)
} catch {
// Best effort for partial test/HMR graphs. Production always loads the