Skip to content

The agent that grows with you

fix(desktop): hold the activation lease for the whole switch dial budget (#120298)

psam9d5789e809b5
diff --git a/apps/desktop/src/store/gateway-activation-prune-lease.test.ts b/apps/desktop/src/store/gateway-activation-prune-lease.test.ts
index c38f2fa41e9..d93f0148eaf 100644
--- a/apps/desktop/src/store/gateway-activation-prune-lease.test.ts
+++ b/apps/desktop/src/store/gateway-activation-prune-lease.test.ts
@@ -57,6 +57,8 @@ const {
SECONDARY_MIN_LIFETIME_MS
} = await import('./gateway')
+const { SOURCE_SWITCH_DIAL_TIMEOUT_MS } = await import('@/lib/with-timeout')
+
function installDesktop(): void {
;(window as unknown as { hermesDesktop: unknown }).hermesDesktop = {
getConnection: vi.fn(async (profile: null | string) =>
@@ -179,9 +181,10 @@ describe('activation lease vs. the live-work pruner (#89622)', () => {
pruneSecondaryGateways(new Set())
expect(secondaryGateways[0].close).not.toHaveBeenCalled()
- // Past the lease window: reclaimed. (Lease is wall-clock bounded so a
- // leaked lease cannot pin a dead entry forever.)
- vi.setSystemTime(Date.now() + SECONDARY_MIN_LIFETIME_MS + 1_000)
+ // Past the lease window — which now spans a whole dial budget (#120298) —
+ // reclaimed. (Lease is wall-clock bounded so a leaked lease cannot pin a
+ // dead entry forever.)
+ vi.setSystemTime(Date.now() + SOURCE_SWITCH_DIAL_TIMEOUT_MS + SECONDARY_MIN_LIFETIME_MS)
pruneSecondaryGateways(new Set())
expect(secondaryGateways[0].close).toHaveBeenCalled()
@@ -212,4 +215,36 @@ describe('activation lease vs. the live-work pruner (#89622)', () => {
pruneSecondaryGateways(new Set())
expect(secondaryGateways[0].close).toHaveBeenCalled()
})
+
+ it('a dial the switch budget still allows is spared for its whole window, not just the first 30 s (#120298)', async () => {
+ vi.useFakeTimers()
+
+ try {
+ let releaseConnect: () => void = () => undefined
+ connectGate = new Promise<void>(resolve => {
+ releaseConnect = resolve
+ })
+
+ const switching = ensureGatewayForProfile('bot')
+ await flushUntilSecondaryRegistered()
+ expect(secondaryGateways).toHaveLength(1)
+
+ // The dial budget covers a whole remote bring-up chain (ssh connect, the
+ // platform/locate/version probes, the remote spawn's ready sentinel, the
+ // forward). A dial late in that window is still a dial the switch is
+ // waiting on, so a recompute landing here must find the target leased —
+ // otherwise the pruner disposes it, the bring-up lands afterwards, and
+ // the switch resolves on a socket that openSecondary has already closed.
+ vi.setSystemTime(Date.now() + SOURCE_SWITCH_DIAL_TIMEOUT_MS - 1_000)
+ pruneSecondaryGateways(new Set())
+ expect(secondaryGateways[0].close).not.toHaveBeenCalled()
+
+ releaseConnect()
+ await switching
+
+ expect(secondaryGateways[0].connectionState).toBe('open')
+ } finally {
+ vi.useRealTimers()
+ }
+ })
})
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 7d40c0b3928..5574343ae21 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -201,10 +201,19 @@ interface Secondary {
activationLeaseUntil: number
}
-// How long a mid-dial activation holds its prune lease: covers a cold pool
-// backend spawn + socket connect with margin, while still letting a leaked
-// lease expire quickly enough for the reaper to reclaim the entry.
-const ACTIVATION_LEASE_MS = 30_000
+// How long a mid-dial activation holds its prune lease: it must outlast every
+// dial the renderer still treats as in flight, or the pruner reaps the switch
+// target mid-dial and the click resolves on a socket that is already closed
+// (#89622's mechanism, one budget change later). The activation dials now carry
+// SOURCE_SWITCH_DIAL_TIMEOUT_MS — the whole remote bring-up chain (ssh connect,
+// the platform/locate/version probes, the remote spawn's ready sentinel, the
+// forward) — so this is DERIVED from it rather than picked independently: at
+// the old 30 s literal, raising the dial budget to 135 s left the switch target
+// prunable for the last ~105 s of a *healthy* dial. The margin covers the
+// settle that releases the lease (the renderer's switch commit). Bounded on
+// purpose: a leaked lease still expires on its own and the reaper reclaims the
+// entry.
+const ACTIVATION_LEASE_MS = SOURCE_SWITCH_DIAL_TIMEOUT_MS + 15_000
// ── HMR-stable module state ─────────────────────────────────────────────────
// All mutable singletons (live sockets, active-profile routing, the event