Skip to content

The agent that grows with you

fix(desktop): update flow nudges a gateway reconnect; wake path probes instead of blind-closing

Tekniumb90289b04661
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
index 73c79fc88ab..9949831bf68 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx
@@ -545,12 +545,20 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect(powerResume).not.toBeNull()
// macOS can discard the TCP connection during sleep without updating the
- // renderer WebSocket object. Leave readyState OPEN and emit only resume.
+ // renderer WebSocket object. Leave readyState OPEN, swallow the liveness
+ // ping (a half-open socket never answers), and emit only resume. The wake
+ // path no longer blind-closes an open-looking socket — it probes first and
+ // closes only when the probe times out.
+ FakeWebSocket.pingMode = 'silent'
act(() => powerResume?.())
- await flushAsync()
- await flushAsync()
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(5_100)
+ })
expect(staleSocket.readyState).toBe(FakeWebSocket.CLOSED)
+ // The probe-driven close schedules the regular backoff reconnect, which
+ // revalidates the (possibly dead) remote descriptor before re-dialing.
+ await advanceBackoff()
expect(desktop.revalidateConnection).toHaveBeenCalledOnce()
expect(FakeWebSocket.instances).toHaveLength(2)
expect($gatewayState.get()).toBe('open')
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 d3dd0195abd..c4953469e66 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -353,14 +353,10 @@ export function useGatewayBoot({
reconnectSecondaryGateways({ forceOpenSockets: forceOpenSocket })
// Browser WebSocket state can remain OPEN after sleep even though the OS
- // discarded the underlying TCP connection. Strong recovery signals must
- // retire that half-open socket before the normal reconnect path can run.
- if (forceOpenSocket && gatewayOpen()) {
- gateway.close()
- // close() publishes `closed`, which schedules the regular backoff.
- // This path reconnects immediately, so remove that redundant timer.
- clearReconnectTimer()
- }
+ // discarded the underlying TCP connection. Strong recovery signals used
+ // to blind-close here, but that churned perfectly healthy connections on
+ // every wake/online blip — the liveness probe below now decides, closing
+ // only a socket that is provably dead.
if (!gatewayOpen()) {
await attemptReconnect()
diff --git a/apps/desktop/src/store/updates.test.ts b/apps/desktop/src/store/updates.test.ts
index 043aa2a8d6a..9c1bf44937c 100644
--- a/apps/desktop/src/store/updates.test.ts
+++ b/apps/desktop/src/store/updates.test.ts
@@ -52,6 +52,16 @@ vi.mock('@/hermes', () => ({
getActionStatus: (...args: unknown[]) => getActionStatusSpy(...args)
}))
+// A successful backend apply must nudge the gateway reconnect handler — the
+// update restarted the gateway process, and over tunnels the old socket dies
+// without a close event (users force-quit to recover). Mock the tiny registry
+// module so the assertion is direct.
+const reconnectGatewaySpy = vi.fn().mockResolvedValue(undefined)
+
+vi.mock('@/store/gateway-reconnect', () => ({
+ reconnectGateway: (...args: unknown[]) => reconnectGatewaySpy(...args)
+}))
+
const {
maybeNotifyUpdateAvailable,
checkBackendUpdates,
@@ -614,6 +624,24 @@ describe('client nudge after a backend update', () => {
const ids = notifySpy.mock.calls.map(call => (call[0] as { id?: string }).id)
expect(ids).not.toContain('client-update-after-backend')
})
+
+ it('nudges a gateway reconnect after the backend caught up (tunnel socket may be dead)', async () => {
+ checkClientMock.mockResolvedValue(status({ behind: 0, updateAvailable: false }))
+ reconnectGatewaySpy.mockClear()
+
+ await applyBackendUpdate()
+
+ expect(reconnectGatewaySpy).toHaveBeenCalledTimes(1)
+ })
+
+ it('does not nudge a reconnect when the backend apply failed', async () => {
+ reconnectGatewaySpy.mockClear()
+ getActionStatusSpy.mockReset().mockResolvedValue({ lines: [], running: false, exit_code: 1 })
+
+ await applyBackendUpdate()
+
+ expect(reconnectGatewaySpy).not.toHaveBeenCalled()
+ })
})
describe('applyUpdates terminal state', () => {
diff --git a/apps/desktop/src/store/updates.ts b/apps/desktop/src/store/updates.ts
index 9dd9e60c957..f6e977f9757 100644
--- a/apps/desktop/src/store/updates.ts
+++ b/apps/desktop/src/store/updates.ts
@@ -18,6 +18,7 @@ import { checkHermesUpdate, getActionStatus, updateHermes } from '@/hermes'
import { translateNow } from '@/i18n'
import { persistString, storedString } from '@/lib/storage'
import { $connectionsRegistry, refreshConnectionsRegistry } from '@/store/connections'
+import { reconnectGateway } from '@/store/gateway-reconnect'
import { dismissNotification, notify } from '@/store/notifications'
import { $connection } from '@/store/session'
import type { BackendUpdateCheckResponse } from '@/types/hermes'
@@ -539,6 +540,14 @@ function finishBackendApply(returned: boolean): DesktopUpdateApplyResult {
$backendUpdateApply.set(IDLE)
setUpdateOverlayOpen(false)
void checkBackendUpdates()
+ // The update restarted the gateway process, which strands this window's
+ // WebSocket: over SSH/tailscale tunnels the old TCP connection often dies
+ // without a close event, so connectionState still reads 'open' while every
+ // RPC hangs — users force-quit the app to recover. Nudge the registered
+ // reconnect handler (forceReconnectNow), which retires the half-open
+ // socket and re-dials with a fresh ticket. Best-effort: local installs
+ // whose socket survived treat it as a cheap probe.
+ void reconnectGateway().catch(() => undefined)
// The backend caught up, but the CLIENT may still be behind — the exact
// gap that strands remote-mode users on an old GUI forever (every update
// affordance in remote mode targets the backend, so nothing ever told