Skip to content

The agent that grows with you

fix(desktop): require an open socket before publishing a secondary gateway route

ygd587e0b535610fe
diff --git a/apps/desktop/src/store/gateway-secondary-open-check.test.ts b/apps/desktop/src/store/gateway-secondary-open-check.test.ts
new file mode 100644
index 00000000000..237faacbfd4
--- /dev/null
+++ b/apps/desktop/src/store/gateway-secondary-open-check.test.ts
@@ -0,0 +1,159 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+// Regression for issue #92265: a transient first-dial WebSocket failure
+// (e.g. ECONNRESET before the socket reaches `open`) must not let Desktop
+// publish the closed gateway as the active route. entry.connection is set
+// BEFORE the dial completes in openSecondary(), so checking only its
+// truthiness previously let a failed activation still "succeed" and
+// publish -- the next chat RPC then failed with "Hermes gateway is not
+// connected" even though the UI had already switched to that route.
+
+const gatewayMocks = vi.hoisted(() => ({
+ connect: vi.fn(async (_wsUrl: string): Promise<void> => undefined),
+ setConnection: vi.fn(),
+ setGatewayState: vi.fn()
+}))
+
+vi.mock('@/hermes', () => ({
+ setApiRequestConnection: vi.fn(),
+ HermesGateway: class {
+ connectionState = 'closed'
+ connect = async (wsUrl: string): Promise<void> => {
+ // Unlike gateway-agent-scope.test.ts's always-succeeds mock, this
+ // one propagates gatewayMocks.connect's outcome -- letting tests
+ // below simulate a rejected first dial without flipping
+ // connectionState to 'open'.
+ await gatewayMocks.connect(wsUrl)
+ this.connectionState = 'open'
+ }
+ close = (): void => {
+ this.connectionState = 'closed'
+ }
+ onEvent = vi.fn(() => () => {})
+ onState = vi.fn(() => () => {})
+ }
+}))
+vi.mock('@/store/session', () => ({
+ setConnection: gatewayMocks.setConnection,
+ setGatewayState: gatewayMocks.setGatewayState
+}))
+vi.mock('@/store/notify-baseline', () => ({ markNativeNotifyBaseline: vi.fn() }))
+
+const {
+ $gateway,
+ activeGateway,
+ closeSecondaryGateways,
+ configureGatewayRegistry,
+ ensureGatewayForAgent,
+ ensureGatewayForProfile,
+ isActivePrimary,
+ setPrimaryGateway
+} = await import('./gateway')
+
+interface DesktopStub {
+ getConnection: ReturnType<typeof vi.fn>
+ getConnectionFor: ReturnType<typeof vi.fn>
+}
+
+function installDesktop(stub: DesktopStub): void {
+ ;(window as unknown as { hermesDesktop: unknown }).hermesDesktop = stub
+}
+
+function makePrimary(): { connectionState: string } {
+ return { connectionState: 'open' }
+}
+
+const agentConn = {
+ authMode: 'token',
+ baseUrl: 'https://homelab.invalid',
+ mode: 'remote',
+ profile: 'research',
+ token: 'fake-test-token',
+ wsUrl: 'wss://homelab.invalid/api/ws?token=fake-test-token'
+}
+
+function installAgentDesktop(): DesktopStub {
+ const stub: DesktopStub = {
+ getConnection: vi.fn(async () => agentConn),
+ getConnectionFor: vi.fn(async () => agentConn)
+ }
+
+ installDesktop(stub)
+
+ return stub
+}
+
+beforeEach(() => {
+ configureGatewayRegistry({ onEvent: vi.fn() })
+})
+
+afterEach(() => {
+ closeSecondaryGateways()
+ vi.clearAllMocks()
+ delete (window as unknown as { hermesDesktop?: unknown }).hermesDesktop
+})
+
+describe('secondary activation requires an open socket, not just a connection descriptor (issue #92265)', () => {
+ it('ensureGatewayForAgent: a transient first-dial failure does not activate or publish the closed gateway', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ await ensureGatewayForProfile('default')
+ const publishedPrimary = $gateway.get()
+ installAgentDesktop()
+
+ gatewayMocks.connect.mockRejectedValueOnce(new Error('ECONNRESET'))
+
+ const activated = await ensureGatewayForAgent('homelab', 'research')
+
+ expect(activated).toBe(false)
+ // The exact reported symptom: the UI must NOT have switched away from
+ // the primary onto the closed secondary.
+ expect(isActivePrimary()).toBe(true)
+ expect(activeGateway()).toBe(primary)
+ expect($gateway.get()).toBe(publishedPrimary)
+ })
+
+ it('ensureGatewayForAgent: a successful dial still activates and publishes normally', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ installAgentDesktop()
+
+ const activated = await ensureGatewayForAgent('homelab', 'research')
+
+ expect(activated).toBe(true)
+ expect(isActivePrimary()).toBe(false)
+ expect(activeGateway()).not.toBe(primary)
+ expect($gateway.get()).not.toBe(primary)
+ })
+
+ it('ensureGatewayForProfile: a transient first-dial failure does not publish the closed gateway', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ await ensureGatewayForProfile('default')
+ const publishedPrimary = $gateway.get()
+ installDesktop({
+ getConnection: vi.fn(async () => agentConn),
+ getConnectionFor: vi.fn(async () => agentConn)
+ })
+
+ gatewayMocks.connect.mockRejectedValueOnce(new Error('ECONNRESET'))
+
+ await ensureGatewayForProfile('research')
+
+ // Must still be on the primary -- the closed secondary was never
+ // published as the active route.
+ expect(isActivePrimary()).toBe(true)
+ expect($gateway.get()).toBe(publishedPrimary)
+ })
+
+ it('ensureGatewayForProfile: a successful dial still activates and publishes normally', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ installAgentDesktop()
+
+ await ensureGatewayForProfile('research')
+
+ expect(isActivePrimary()).toBe(false)
+ expect($gateway.get()).not.toBe(primary)
+ })
+})
diff --git a/apps/desktop/src/store/gateway-shared-remote.test.ts b/apps/desktop/src/store/gateway-shared-remote.test.ts
index c294cabe200..face9eac772 100644
--- a/apps/desktop/src/store/gateway-shared-remote.test.ts
+++ b/apps/desktop/src/store/gateway-shared-remote.test.ts
@@ -41,7 +41,6 @@ const {
$gateway,
closeSecondaryGateways,
configureGatewayRegistry,
- ensureActiveGatewayOpen,
ensureGatewayForProfile,
setPrimaryGateway
} = await import('./gateway')
@@ -136,7 +135,7 @@ describe('ensureGatewayForProfile under a shared global remote', () => {
// No activation was published for the dead dial — $connection keeps the
// primary's descriptor (set by setPrimaryGateway), never the unreachable
- // secondary's.
+ // secondary's. (Also the #92265 shape: publish requires an OPEN socket.)
expect(gatewayMocks.setConnection).not.toHaveBeenCalled()
// Once the backend is reachable again, retrying the switch activates and
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 27d246e72af..737c6c0d4d5 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -1178,11 +1178,17 @@ export async function ensureGatewayForAgent(
}
// A source edit/remove may dispose this entry while its dial is still in
- // flight. Only the still-registered, still-owned activation may publish.
+ // flight. Only the still-registered, still-owned activation may publish --
+ // and only when the WebSocket actually reached open: entry.connection is
+ // set BEFORE the dial completes in openSecondary, so a transient first-dial
+ // failure (caught above, left for scheduleReconnect) must not count as a
+ // successful activation just because a connection descriptor exists
+ // (issue #92265).
const activated =
entry.wantOpen &&
g.secondaries.get(scope) === entry &&
Boolean(entry.connection) &&
+ isOpen(entry.gateway) &&
applyActive(scope, activationEpoch)
if (activated && entry.connection) {
@@ -1251,7 +1257,16 @@ export async function ensureGatewayForProfile(profile: string): Promise<void> {
entry.activationLeaseUntil = 0
}
- if (entry.wantOpen && g.secondaries.get(key) === entry && applyActive(key, activationEpoch) && entry.connection) {
+ // Only publish when the WebSocket actually reached open -- entry.connection
+ // is set before the dial completes, so a transient first-dial failure must
+ // not count as a successful activation (issue #92265).
+ if (
+ entry.wantOpen &&
+ g.secondaries.get(key) === entry &&
+ isOpen(entry.gateway) &&
+ applyActive(key, activationEpoch) &&
+ entry.connection
+ ) {
publishActiveConnection(entry.connection)
}
}