Skip to content

The agent that grows with you

fix(desktop): fail-open attached shared-remote probe; honest ensure/open (#96493)

Paula Rossi89c28ccf7b79
diff --git a/apps/desktop/src/store/gateway-profile-request.test.ts b/apps/desktop/src/store/gateway-profile-request.test.ts
index fd4d99230ce..b16cb95d293 100644
--- a/apps/desktop/src/store/gateway-profile-request.test.ts
+++ b/apps/desktop/src/store/gateway-profile-request.test.ts
@@ -635,4 +635,46 @@ describe('attached shared-remote group turns (#96493)', () => {
expect(secondaryGateways).toHaveLength(1)
expect(primary.request).not.toHaveBeenCalled()
})
+
+ it('reuses the primary when the shared-remote probe fails instead of dialing a ghost secondary', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ setPrimaryGatewayConnection({ connectionId: 'homelab' })
+ ;(window as unknown as { hermesDesktop: unknown }).hermesDesktop = {
+ getConnection: vi.fn(async (profile: null | string) => ({ port: 4242, profile, token: 't' })),
+ getConnectionFor: vi.fn(async () => {
+ throw new Error('Timed out connecting to profile "voter"')
+ }),
+ getGatewayWsUrlFor: vi.fn(async () => ({ ok: true as const, wsUrl: 'ws://homelab/voter' })),
+ touchBackend: vi.fn(async () => undefined)
+ }
+ await ensureGatewayForProfile('default')
+
+ await requestGatewayForAgent('homelab', 'voter', 'session.create', { title: 'g' })
+
+ expect(secondaryGateways).toHaveLength(0)
+ expect(primary.request).toHaveBeenCalledOnce()
+ })
+
+ it('openGatewayForAgent and ensureGatewayForAgent do not dial a secondary', async () => {
+ const primary = makePrimary()
+ setPrimaryGateway(primary as never, 'default')
+ setPrimaryGatewayConnection({ connectionId: 'homelab' })
+ installAttachedSharedRemote()
+ await ensureGatewayForProfile('default')
+
+ await openGatewayForAgent('homelab', 'voter')
+ expect(await ensureGatewayForAgent('homelab', 'voter')).toBe(true)
+ expect(secondaryGateways).toHaveLength(0)
+ })
+
+ it('ensureGatewayForAgent is false when the attached primary socket is closed', async () => {
+ const primary = { connectionState: 'closed', request: vi.fn() }
+ setPrimaryGateway(primary as never, 'default')
+ setPrimaryGatewayConnection({ connectionId: 'homelab' })
+ installAttachedSharedRemote()
+
+ expect(await ensureGatewayForAgent('homelab', 'voter')).toBe(false)
+ expect(secondaryGateways).toHaveLength(0)
+ })
})
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 3c8088c01a1..60c5fada83f 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -329,7 +329,11 @@ async function isAttachedSharedRemote(connectionId: null | string, profile: stri
return Boolean(conn && typeof conn === 'object' && (conn as { sharedRemote?: boolean }).sharedRemote === true)
} catch {
- return false
+ // Probe failed. A secondary at this already-attached source is the #96493
+ // ghost WebSocket (accept/close, messages=1). Prefer the primary until a
+ // later probe can prove isolation (`sharedRemote: false`). Isolated SSH
+ // still dials its own socket when getConnectionFor succeeds.
+ return true
}
}
@@ -341,7 +345,7 @@ async function requestOnPrimaryGateway<T>(
): Promise<T> {
const gateway = g.primaryGateway
- if (!gateway) {
+ if (!gateway || !isOpen(gateway)) {
throw new Error('Hermes gateway unavailable')
}
@@ -1332,6 +1336,10 @@ export async function openGatewayForAgent(
}
if (await isAttachedSharedRemote(connectionId, profile)) {
+ if (!isOpen(g.primaryGateway)) {
+ throw new Error('Hermes gateway unavailable')
+ }
+
return
}
@@ -1382,7 +1390,7 @@ export async function ensureGatewayForAgent(
}
if (await isAttachedSharedRemote(connectionId, profile)) {
- return !signal?.aborted
+ return Boolean(isOpen(g.primaryGateway) && !signal?.aborted)
}
if (!window.hermesDesktop?.getConnectionFor) {