The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): liveness probe no longer closes a healthy socket behind a slow RPC
Tekniuma55806910490
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.tsindex 838cc2c325b..9be66465c74 100644--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts@@ -19,8 +19,8 @@ import { translateNow } from '@/i18n'import { desktopDefaultCwd } from '@/lib/desktop-fs'import {decideLivenessForceClose,- LIVENESS_PROBE_TIMEOUT_MS,- LIVENESS_REPROBE_DELAY_MS+ LIVENESS_REPROBE_DELAY_MS,+ probeGatewayLiveness} from '@/lib/gateway-liveness-policy'import { resolveDesktopGatewayWsUrl } from '@/lib/gateway-ws-url'import { hostHas } from '@/lib/host-capabilities'@@ -699,7 +699,7 @@ export function useGatewayBoot({// one inconclusive probe DEFERS the teardown behind a bounded re-probe;// only an exhausted streak (or no in-flight work) closes.try {- await gateway.request('ping', {}, LIVENESS_PROBE_TIMEOUT_MS)+ await probeGatewayLiveness(gateway)livenessProbeFailures = 0} catch (probeErr) {// A version-skewed backend that predates the ping method answers@@ -1305,7 +1305,7 @@ export function useGatewayBoot({// work in flight still closes and re-dials, so a restart that DID take// this socket down is recovered here and now.try {- await gateway.request('ping', {}, LIVENESS_PROBE_TIMEOUT_MS)+ await probeGatewayLiveness(gateway)livenessProbeFailures = 0returndiff --git a/apps/desktop/src/lib/gateway-liveness-policy.test.ts b/apps/desktop/src/lib/gateway-liveness-policy.test.tsindex b81fb3778c3..a570e5059dc 100644--- a/apps/desktop/src/lib/gateway-liveness-policy.test.ts+++ b/apps/desktop/src/lib/gateway-liveness-policy.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest'-import { decideLivenessForceClose, LIVENESS_PROBE_FAILURE_STREAK } from './gateway-liveness-policy'+import { decideLivenessForceClose, LIVENESS_PROBE_FAILURE_STREAK, probeGatewayLiveness } from './gateway-liveness-policy'describe('decideLivenessForceClose', () => {it('keeps the socket on the first timeout while a turn is in flight (#95327)', () => {@@ -56,3 +56,17 @@ describe('decideLivenessForceClose', () => {})})})++describe('probeGatewayLiveness', () => {+ // The backend answers `gateway.ping` on its socket reader; every other RPC, `ping` included,+ // waits behind the connection's serial control lane. A probe stuck behind a slow control RPC+ // closed a healthy socket (#145737).+ it('is answered while the control lane is busy', async () => {+ const gateway = {+ request: (method: string) =>+ method === 'gateway.ping' ? Promise.resolve({ ok: true }) : new Promise<unknown>(() => undefined)+ }++ await expect(probeGatewayLiveness(gateway)).resolves.toEqual({ ok: true })+ })+})diff --git a/apps/desktop/src/lib/gateway-liveness-policy.ts b/apps/desktop/src/lib/gateway-liveness-policy.tsindex 3f219a433b3..92252defa6f 100644--- a/apps/desktop/src/lib/gateway-liveness-policy.ts+++ b/apps/desktop/src/lib/gateway-liveness-policy.ts@@ -37,6 +37,21 @@/** Ping budget for a liveness probe (primary and secondary sockets share it). */export const LIVENESS_PROBE_TIMEOUT_MS = 5_000+/**+ * The probe's RPC. The backend's socket reader answers `gateway.ping` itself, while any other method+ * waits behind the connection's serial control lane: probing with `ping` timed out behind a slow RPC+ * (compression, a large resume) and closed a healthy socket (#145737). A backend older than the+ * heartbeat answers -32601, which every probe site already treats as alive.+ */+const LIVENESS_PROBE_METHOD = 'gateway.ping'++/** Send one liveness probe on `gateway`; resolves when the backend answers within the budget. */+export function probeGatewayLiveness(gateway: {+ request: (method: string, params: Record<string, unknown>, timeoutMs: number) => Promise<unknown>+}): Promise<unknown> {+ return gateway.request(LIVENESS_PROBE_METHOD, {}, LIVENESS_PROBE_TIMEOUT_MS)+}+/** Consecutive unanswered probes tolerated while work is in flight. */export const LIVENESS_PROBE_FAILURE_STREAK = 2diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.tsindex 9886b651884..1c1da94caef 100644--- a/apps/desktop/src/store/gateway.ts+++ b/apps/desktop/src/store/gateway.ts@@ -17,8 +17,8 @@ import { translateNow } from '@/i18n'import { UnservedProfileError } from '@/lib/browser-served-profiles'import {decideLivenessForceClose,- LIVENESS_PROBE_TIMEOUT_MS,- LIVENESS_REPROBE_DELAY_MS+ LIVENESS_REPROBE_DELAY_MS,+ probeGatewayLiveness} from '@/lib/gateway-liveness-policy'import { isMissingRpcMethod } from '@/lib/gateway-rpc'import { traceIdentityChange } from '@/lib/identity-trace'@@ -2049,7 +2049,7 @@ function clearSecondaryLivenessReprobe(entry: Secondary): void {// onState('closed') handler; a healthy-but-busy backend answers the ping and// keeps its socket (#94769 review).function probeSecondaryLiveness(entry: Secondary): void {- void entry.gateway.request('ping', {}, LIVENESS_PROBE_TIMEOUT_MS).then(+ void probeGatewayLiveness(entry.gateway).then(() => {entry.livenessProbeFailures = 0clearSecondaryLivenessReprobe(entry)diff --git a/apps/desktop/src/test/fake-gateway-socket.ts b/apps/desktop/src/test/fake-gateway-socket.tsindex 1639ef2f110..9b90cf5d3d8 100644--- a/apps/desktop/src/test/fake-gateway-socket.ts+++ b/apps/desktop/src/test/fake-gateway-socket.ts@@ -63,7 +63,8 @@ export class FakeWebSocket {return}- if (frame.method !== 'ping') {+ // `ping` for tests calling it directly; the liveness probe sends `gateway.ping`.+ if (frame.method !== 'ping' && frame.method !== 'gateway.ping') {return}