Skip to content

The agent that grows with you

fix(desktop): local & remote profiles reuse the default socket after 0.20.1

Brooklyn Nicholson4d6f4a6fe7b5
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 64d076d714d..a4593d9336e 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -8103,7 +8103,11 @@ async function ensureBackend(profile) {
// A shared backend still owes the caller its profile scope, so renderer-side
// WebSocket, filesystem, and cache routing target the selected profile.
- return route.descriptorProfile ? { ...connection, profile: route.descriptorProfile } : connection
+ // `sharedPrimary` marks this as the shared-primary route: pooled backends
+ // also carry `profile`, so only this descriptor gets the flag.
+ return route.descriptorProfile
+ ? { ...connection, profile: route.descriptorProfile, sharedPrimary: true }
+ : connection
}
const existing = backendPool.get(key)
diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts
index e64f0d426a9..fdce5560787 100644
--- a/apps/desktop/src/global.d.ts
+++ b/apps/desktop/src/global.d.ts
@@ -537,6 +537,10 @@ export interface HermesConnection {
// Set for pool (non-primary) backends so the renderer knows which profile a
// connection belongs to.
profile?: string
+ // True only when `profile` is a request scope on the shared primary backend.
+ // A pooled backend also carries `profile`, so presence alone cannot identify
+ // the shared-primary routing case.
+ sharedPrimary?: boolean
windowButtonPosition: { x: number; y: number } | null
}
diff --git a/apps/desktop/src/store/gateway-shared-remote.test.ts b/apps/desktop/src/store/gateway-shared-remote.test.ts
index a16fe376cd0..9d1481ae07c 100644
--- a/apps/desktop/src/store/gateway-shared-remote.test.ts
+++ b/apps/desktop/src/store/gateway-shared-remote.test.ts
@@ -1,19 +1,26 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
// The global-remote share (backend routing case 3): every profile is served
-// by the PRIMARY backend over one host, and getConnection() tags the shared
-// descriptor with `profile`. Dialing a second WebSocket at that descriptor
+// by the PRIMARY backend over one host, and getConnection() explicitly tags
+// the shared descriptor with `sharedPrimary`. Dialing a second WebSocket at it
// used to fail over SSH (per-backend tunnel/ticket) and poison the active
// gateway with a closed socket — "Hermes gateway is not connected" for every
-// profile except the primary. These tests pin the fix: a profile routed to
-// the shared primary activates the primary socket instead of dialing.
+// profile except the primary. Pooled backends (own-remote override, local
+// named profile) also carry `profile` for WS URL minting, so `profile` alone
+// cannot identify the shared-primary route. These tests pin the fix: only a
+// `sharedPrimary` descriptor activates the primary socket; a pooled descriptor
+// that also carries `profile` must still dial its own socket.
+
+const gatewayMocks = vi.hoisted(() => ({
+ connect: vi.fn(async (_wsUrl: string): Promise<void> => {
+ throw new Error('dialed a socket for a shared-primary profile')
+ })
+}))
vi.mock('@/hermes', () => ({
HermesGateway: class {
connectionState = 'closed'
- connect = vi.fn(async () => {
- throw new Error('dialed a socket for a shared-primary profile')
- })
+ connect = gatewayMocks.connect
onEvent = vi.fn(() => () => {})
onState = vi.fn(() => () => {})
}
@@ -47,32 +54,45 @@ afterEach(() => {
})
describe('ensureGatewayForProfile under a shared global remote', () => {
- it('activates the primary socket for a profile tagged onto the shared descriptor', async () => {
+ it('activates the primary socket for an explicitly shared-primary descriptor', async () => {
const primary = makePrimary()
setPrimaryGateway(primary as never, 'default')
installDesktop({
- // Shared descriptor: primary connection tagged with the profile.
- getConnection: vi.fn(async () => ({ port: 4242, profile: 'venture', token: 't' }))
+ // Shared descriptor: primary connection tagged with the profile scope
+ // AND the explicit sharedPrimary marker.
+ getConnection: vi.fn(async () => ({ port: 4242, profile: 'venture', sharedPrimary: true, token: 't' }))
})
await ensureGatewayForProfile('venture')
+ expect(gatewayMocks.connect).not.toHaveBeenCalled()
expect($gateway.get()).toBe(primary)
})
- it('still pools a socket for profiles with their own descriptor (untagged)', async () => {
+ it('dials the exact WebSocket URL for a pooled profile descriptor that carries profile', async () => {
const primary = makePrimary()
+ const remoteWsUrl = 'wss://remote.invalid/api/ws?token=fake-test-token'
+
setPrimaryGateway(primary as never, 'default')
installDesktop({
- // Own descriptor: no profile tag → normal pooled path (dial attempted).
- getConnection: vi.fn(async () => ({ port: 5151, token: 't2' }))
+ // Pooled descriptor: carries `profile` for WS URL minting but is NOT
+ // shared-primary (no marker) — it must dial its own socket, not reuse
+ // the primary. This is the local named / own-remote profile case.
+ getConnection: vi.fn(async () => ({
+ authMode: 'token',
+ baseUrl: 'https://remote.invalid',
+ mode: 'remote',
+ profile: 'worker',
+ token: 'fake-test-token',
+ wsUrl: remoteWsUrl
+ }))
})
+ gatewayMocks.connect.mockResolvedValueOnce(undefined)
await ensureGatewayForProfile('worker')
- // The pooled path dialed (our stub throws, so the socket stays closed and
- // reconnect is scheduled) — the important part is it did NOT silently
- // reuse the primary.
+ expect(gatewayMocks.connect).toHaveBeenCalledOnce()
+ expect(gatewayMocks.connect).toHaveBeenCalledWith(remoteWsUrl)
expect($gateway.get()).not.toBe(primary)
})
})
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 0a64b3da1f2..6c815a78834 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -248,12 +248,14 @@ function createSecondary(profile: string): Secondary {
}
// True when `profile`'s backend route resolves to the SHARED primary backend
-// (global-remote case 3 in resolveProfileBackendRoute): the descriptor comes
-// back as the primary connection tagged with `profile`. Own-remote-override
-// and local pooled descriptors are never tagged. Dialing a second socket at
-// that descriptor is wrong — over SSH the second dial fails (tunnel/token are
-// per-backend) and the closed socket poisons the active gateway with
-// "not connected" even though the primary is open right next to it.
+// (global-remote case 3 in resolveProfileBackendRoute). Both shared-primary and
+// pooled descriptors carry `profile` so WebSocket URL minting targets the right
+// profile. `sharedPrimary` is the explicit discriminator; treating every tagged
+// descriptor as shared strands local/own-remote pooled profiles on the default
+// socket. Dialing a second socket at the shared descriptor is wrong — over SSH
+// the second dial fails (tunnel/token are per-backend) and the closed socket
+// poisons the active gateway with "not connected" even though the primary is
+// open right next to it.
async function sharedPrimaryRoute(profile: string): Promise<boolean> {
const desktop = window.hermesDesktop
@@ -264,7 +266,7 @@ async function sharedPrimaryRoute(profile: string): Promise<boolean> {
try {
const conn = await desktop.getConnection(profile)
- return Boolean(conn && typeof conn === 'object' && (conn as { profile?: string }).profile)
+ return Boolean(conn && typeof conn === 'object' && (conn as { sharedPrimary?: boolean }).sharedPrimary === true)
} catch {
return false
}