The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): probe live secondaries on forced wake instead of skipping them
psama915f0b3b2bb
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.tsindex 9e77961cb03..51fd4ec77c6 100644--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts@@ -1020,6 +1020,14 @@ export function useGatewayBoot({let lastForcedWakeReconnectAt = 0const forceReconnectNow = () => {+ // reconnectNow no-ops while boot is incomplete or a gateway switch is in+ // flight; stamping the holdoff then would burn the window and drop the+ // next 'online' (often the one with the network actually back), leaving+ // recovery to the backoff loops. Stamp only when it will proceed.+ if (cancelled || !bootCompleted || $gatewaySwitching.get()) {+ return+ }+const now = Date.now()if (now - lastForcedWakeReconnectAt < WAKE_RECONNECT_HOLDOFF_MS) {diff --git a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts b/apps/desktop/src/store/gateway-connection-lifecycle.test.tsindex 26d569d2530..fba1e336427 100644--- a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts+++ b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts@@ -1,3 +1,4 @@+import { JsonRpcGatewayError } from '@hermes/shared'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'// Connection lifecycle for registry-scoped secondary gateways:@@ -13,7 +14,11 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'// the entry instead of retrying forever.const gatewayMocks = vi.hoisted(() => {- const instances: { close: ReturnType<typeof vi.fn>; connectionState: string }[] = []+ const instances: {+ close: ReturnType<typeof vi.fn>+ request: ReturnType<typeof vi.fn>+ connectionState: string+ }[] = []return {connect: vi.fn(async (_wsUrl: string): Promise<void> => undefined),@@ -38,6 +43,7 @@ vi.mock('@/hermes', () => ({await gatewayMocks.connect(wsUrl)this.connectionState = 'open'}+ request = vi.fn(async (_method: string, _params: Record<string, unknown>) => ({}))onEvent = vi.fn((handler: (event: unknown) => void) => {gatewayMocks.eventHandlers.push(handler)@@ -469,6 +475,54 @@ describe('reconnectSecondaryGateways', () => {expect(gatewayMocks.instances[0].connectionState).toBe('open')expect(getConnectionFor).toHaveBeenCalledTimes(1)})++ it('closes a live-in-use secondary on a forced wake only when its liveness probe fails', async () => {+ // A half-open socket never fires a close event, so skipping it would strand+ // an in-flight request until its per-call timeout (30 min for+ // prompt.submit). The wake path probes instead: a dead transport is+ // closed; a healthy one — including a version-skewed backend answering+ // -32601 — keeps its socket (#94769 review).+ configureGatewayRegistry({+ onEvent: vi.fn(),+ foregroundScopes: () => new Set(['conn:homelab::default'])+ } as never)++ const getConnectionFor = vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>+ descriptorFor(connectionId, profile)+ )++ installDesktop({ getConnectionFor })++ await ensureGatewayForAgent('homelab', 'default')+ const socket = gatewayMocks.instances[0]+ expect(socket.connectionState).toBe('open')++ // Healthy version-skewed backend: -32601 (method not found) is a live+ // answer, not a dead socket — the same carve-out the primary's probe+ // makes. The socket stays open.+ socket.request = vi.fn(async () => {+ throw new JsonRpcGatewayError('Method not found', { code: -32601 })+ })++ reconnectSecondaryGateways({ forceOpenSockets: true })+ await Promise.resolve()+ await Promise.resolve()++ expect(socket.close).not.toHaveBeenCalled()+ expect(socket.connectionState).toBe('open')++ // Dead transport: the probe rejects with a non-RPC error (timeout family)+ // and the socket is torn down so its reconnect backoff can heal it.+ socket.request = vi.fn(async () => {+ throw new Error('probe timeout')+ })++ reconnectSecondaryGateways({ forceOpenSockets: true })+ await vi.waitFor(() => {+ expect(socket.close).toHaveBeenCalledOnce()+ })+ expect(socket.connectionState).toBe('closed')+ })})describe('reconnect fail-stop on a removed connection', () => {diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.tsindex 4e3f43adb62..31876970603 100644--- a/apps/desktop/src/store/gateway.ts+++ b/apps/desktop/src/store/gateway.ts@@ -2,6 +2,7 @@ import {type ConnectionState,type GatewayEvent,isGatewayReauthRequired,+ JsonRpcGatewayError,reconnectBackoffDelayMs,registryBackendScopeKey,resolveGatewayWsUrl,@@ -1906,6 +1907,41 @@ const ACTIVE_GATEWAY_OPEN_WAIT_MS = 8_000// (#94769 prune ↔ redial race).const SECONDARY_MIN_LIFETIME_MS = 30_000+// Wake-path liveness probe budget for a live-in-use secondary: mirrors+// GATEWAY_LIVENESS_PROBE_TIMEOUT_MS in use-gateway-boot (the primary's probe).+const SECONDARY_WAKE_PROBE_TIMEOUT_MS = 5_000++// Probe a live-in-use secondary instead of blind-closing it on a forced wake,+// and close it only when the probe proves it not alive. A half-open TCP+// connection (sleep/wake, silent network drop) reports connectionState+// 'open' forever and fires no close event, so without this close an in-flight+// request rides a dead transport until its per-call timeout — prompt.submit's+// is 30 minutes. Closing arms the entry's ordinary reconnect backoff via its+// onState('closed') handler; a healthy-but-busy backend answers the ping and+// keeps its socket (#94769 review).+function probeSecondaryLiveness(entry: Secondary): void {+ if (typeof entry.gateway.request !== 'function') {+ return+ }++ void entry.gateway.request('ping', {}, SECONDARY_WAKE_PROBE_TIMEOUT_MS).catch((error: unknown) => {+ // -32601 (method not found) = a version-skewed but HEALTHY backend that+ // predates the ping method — the same compatibility carve-out the+ // primary's probe makes in use-gateway-boot.+ if (error instanceof JsonRpcGatewayError && error.code === -32601) {+ return+ }++ // The entry may have been pruned or redialed while the probe was+ // pending; only the very same socket may be torn down.+ if (g.secondaries.get(entry.scope) !== entry || !isOpen(entry.gateway)) {+ return+ }++ entry.gateway.close()+ })+}+// Recovery signal: nudge every live secondary back open. Power-resume/network// signals can force sockets that still report open to retire before redialing.export function reconnectSecondaryGateways({ forceOpenSockets = false }: { forceOpenSockets?: boolean } = {}): void {@@ -1931,10 +1967,14 @@ export function reconnectSecondaryGateways({ forceOpenSockets = false }: { force// secondary socket before redialing. Closing one that is mid-use detaches// its runtime → the backend orphan-reaps it → `session.reclaimed` → the// surface re-resumes on a fresh socket the same signal may close again:- // the #94769 flicker loop. Only force-redial quiescent sockets; live- // ones ride through and ordinary close/reconnect still heals them if- // the wake genuinely dropped them.+ // the #94769 flicker loop. But a live socket also cannot simply be+ // SKIPPED: a half-open socket never fires a close event, so an in-flight+ // request would hang until its per-call timeout. Probe liveness instead —+ // a healthy-but-busy backend answers and keeps its socket; a dead+ // transport is closed and healed by the ordinary reconnect backoff.if (entry.activeRequests > 0 || foregroundPinned(entry)) {+ probeSecondaryLiveness(entry)+continue}