Skip to content

The agent that grows with you

fix(desktop): keepalive-touch only secondaries with an open socket

kshitijk4poorb07163bdad90
diff --git a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
index edd0c112154..182f4ff3730 100644
--- a/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
+++ b/apps/desktop/src/store/gateway-connection-lifecycle.test.ts
@@ -53,6 +53,7 @@ vi.mock('@/store/session-states', () => reconnectStateMocks)
const {
activeGateway,
+ touchSecondaryGateways,
closeLegacySecondaryGateways,
closeSecondaryGateways,
configureGatewayRegistry,
@@ -532,3 +533,34 @@ describe('reconnect fail-stop on a removed connection', () => {
expect((result as unknown as { connectionState: string }).connectionState).toBe('open')
})
})
+
+describe('touchSecondaryGateways', () => {
+ it('pings only secondaries whose socket is open, so a backend nobody reaches can idle-reap (#103375)', async () => {
+ const getConnectionFor = vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) =>
+ descriptorFor(connectionId, profile)
+ )
+ const touchBackend = vi.fn(async () => ({ ok: true }))
+
+ installDesktop({ getConnectionFor, touchBackend })
+
+ await ensureGatewayForAgent('homelab', 'default')
+ await ensureGatewayForAgent('office', 'default')
+ // openSecondary pings once per successful dial; only the keepalive sweep
+ // is under test here.
+ touchBackend.mockClear()
+
+ touchSecondaryGateways()
+ expect(touchBackend).toHaveBeenCalledTimes(2)
+
+ // The office socket drops and sits in reconnect backoff: still wantOpen,
+ // but nothing on this window uses that backend until it reopens.
+ const office = gatewayMocks.instances[1] as unknown as { connectionState: string }
+ office.connectionState = 'closed'
+ touchBackend.mockClear()
+
+ touchSecondaryGateways()
+
+ expect(touchBackend).toHaveBeenCalledTimes(1)
+ expect(touchBackend).not.toHaveBeenCalledWith(expect.stringContaining('office'))
+ })
+})
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index f9ce195cb10..ece50692a31 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -1665,11 +1665,15 @@ export function openSecondaryCount(): number {
// Keep the idle reaper from killing a backend we still need: ping every live
// secondary. The active one is pinged separately (touchActiveGatewayBackend).
+// "Live" means the socket is OPEN: a wantOpen entry stuck in its reconnect
+// backoff has no consumer on that backend, and pinging it anyway kept a
+// tile-pinned backend keepalive-fresh forever, so LRU eviction and the idle
+// reaper never freed its pool slot (#103375).
export function touchSecondaryGateways(): void {
const desktop = window.hermesDesktop
for (const entry of g.secondaries.values()) {
- if (entry.wantOpen) {
+ if (entry.wantOpen && isOpen(entry.gateway)) {
void desktop?.touchBackend?.(entry.scope).catch(() => undefined)
}
}