The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): reconnect the primary socket to its own route, not the foreground
unsupportedpastelsaae0a97a5644
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.tsxindex 30a30467deb..24ed0ff2e76 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@@ -1936,11 +1936,11 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>FakeWebSocket.mode = 'open'await advanceBackoff()expect(desktop.revalidateConnection).toHaveBeenCalledTimes(2)- // The manual reconnect dials the WINDOW-owned primary backend (no profile- // arg) — same contract as the sleep/wake reconnect: passing the active- // profile would retarget the primary socket after a live profile swap.+ // The manual reconnect dials the WINDOW-owned primary backend by its own+ // recorded route — same contract as the sleep/wake reconnect: neither the+ // active profile nor main's foreground route may retarget the primary.const lastCall = desktop.getConnection.mock.calls.at(-1) ?? []- expect(lastCall.length === 0 || lastCall[0] == null || lastCall[0] === '').toBe(true)+ expect(lastCall[0] ?? 'default').toBe('default')expect(desktop.getGatewayWsUrl).toHaveBeenCalledTimes(3)expect(oldPrimary.readyState).toBe(FakeWebSocket.CLOSED)expect(backgroundSocket.readyState).toBe(FakeWebSocket.OPEN)@@ -2176,7 +2176,7 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>const reconnectCalls = desktop.getConnection.mock.calls.slice(callsBeforeDrop)expect(reconnectCalls.some(args => (args[0] ?? '').trim() === 'coder')).toBe(false)- expect(reconnectCalls.some(args => args.length === 0 || args[0] == null || args[0] === '')).toBe(true)+ expect(reconnectCalls.some(args => (args[0] ?? 'default') === 'default')).toBe(true)const primaryReconnectSockets = FakeWebSocket.instances.slice(socketsBeforeDrop)@@ -2787,3 +2787,61 @@ describe('window-state IPC before the first connection publishes (#108641)', ()expect($connection.get()?.windowButtonPosition).toBeNull()})})++it.each([false, true])(+ 'primary reconnect preserves its source while a secondary is foregrounded (registry=%s)',+ async registryScoped => {+ // Keep Electron's Node-only dependency graph outside the renderer TS project.+ const resolverModule = '../../../../electron/desktop-profile'+ const { resolveDesktopConnectionRequest } = await import(resolverModule)+ const primary = { ...remotePrimaryConn, registryScoped }+ const secondary = { ...coderConn, profile: 'default', registryScoped: true }+ let foreground: { connectionId: string | null; profile: string; registryScoped: boolean } | null = null++ const desktop = {+ ...fakeDesktop(),+ setActiveConnectionRoute: vi.fn(route => {+ foreground = route+ }),+ getConnection: vi.fn(async (profile?: null | string) => {+ const route = resolveDesktopConnectionRequest(profile, foreground, 'default')++ return route.connectionId === secondary.connectionId ? secondary : primary+ }),+ getConnectionFor: vi.fn(async ({ connectionId }: { connectionId: string; profile: string }) =>+ connectionId === secondary.connectionId ? secondary : primary+ ),+ getGatewayWsUrlFor: vi.fn(async ({ connectionId }: { connectionId: string; profile: string }) =>+ connectionId === secondary.connectionId ? secondary.wsUrl : primary.wsUrl+ )+ }++ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop+ render(<Harness />)+ await flushAsync()+ const primarySocket = FakeWebSocket.instances[0]+ expect(primarySocket.url).toBe(primary.wsUrl)+ let opening!: Promise<boolean>+ act(() => {+ opening = ensureGatewayForAgent(secondary.connectionId, secondary.profile)+ })+ await flushAsync()+ await opening+ expect(foreground).toMatchObject({ connectionId: secondary.connectionId, registryScoped: true })+ expect(isActivePrimary()).toBe(false)+ act(() => primarySocket.drop())+ await advanceBackoff()+ expect(FakeWebSocket.instances.at(-1)?.url).toBe(primary.wsUrl)+ expect($connection.get()?.connectionId).toBe(secondary.connectionId)+ expect(isActivePrimary()).toBe(false)++ // Explicit connection apply must still follow main's rewritten foreground+ // route, and replace the reconnect target rather than pinning it forever.+ act(() => connectionApplied?.())+ await flushAsync()+ expect(FakeWebSocket.instances.at(-1)?.url).toBe(secondary.wsUrl)+ act(() => FakeWebSocket.instances.at(-1)!.drop())+ await advanceBackoff()+ expect(FakeWebSocket.instances.at(-1)?.url).toBe(secondary.wsUrl)+ }+)diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.tsindex 3d91f4cf084..0657a3f0100 100644--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts@@ -315,6 +315,13 @@ export function useGatewayBoot({// reconnect with backoff, and we nudge a reconnect on the OS/browser// signals that fire around wake (power resume, network online, the window// becoming visible).+ let primaryConnection: HermesConnection | null = null++ const recordPrimaryConnection = (connection: HermesConnection) => {+ primaryConnection = connection+ setPrimaryGatewayConnection(connection)+ }+let bootCompleted = false// The other way a cold boot concludes. Main keeps startHermes() available// after the renderer gave up, and every later getConnection() caller@@ -457,17 +464,22 @@ export function useGatewayBoot({'Timed out revalidating the gateway connection').catch(() => undefined)- // Primary sleep/wake reconnect must dial the WINDOW-owned primary backend- // (same as boot/softSwitch). Passing $activeGatewayProfile would retarget- // this primary socket at a secondary profile's backend after a live swap.- // Secondaries reconnect via reconnectSecondaryGateways().+ // Reconnect the socket's own route, not main's mutable foreground route.+ // Profile-less resolution remains intentional for boot/connection apply.+ // A registry primary needs both identity fields; a legacy primary uses+ // its explicit profile so a foreground secondary cannot retarget it.const conn = await withTimeout(- desktop.getConnection(),+ primaryConnection?.registryScoped && primaryConnection.connectionId+ ? (desktop.getConnectionFor?.({+ connectionId: primaryConnection.connectionId,+ profile: primaryConnection.profile+ }) ?? Promise.reject(new Error('Registry gateway connection is unavailable')))+ : desktop.getConnection(primaryConnection?.profile),RECONNECT_ATTEMPT_TIMEOUT_MS,'Timed out reconnecting to Hermes backend')- setPrimaryGatewayConnection(conn)+ recordPrimaryConnection(conn)if (cancelled) {return@@ -809,7 +821,7 @@ export function useGatewayBoot({}publish(conn)- setPrimaryGatewayConnection(conn)+ recordPrimaryConnection(conn)// Bounded for the same reason as attemptReconnect() (#93454): a wedged// ticket mint would otherwise hang the gateway switch forever.@@ -1450,7 +1462,7 @@ export function useGatewayBoot({progress: 95})publish(conn)- setPrimaryGatewayConnection(conn)+ recordPrimaryConnection(conn)// Seed the workspace BEFORE the gateway opens: every session-restore// path is gated on gatewayState === 'open', so nothing can be active yet@@ -1613,7 +1625,7 @@ export function useGatewayBoot({if (survivor?.connection) {publish(survivor.connection)- setPrimaryGatewayConnection(survivor.connection)+ recordPrimaryConnection(survivor.connection)}const profile = survivor?.profile ?? $activeGatewayProfile.get()