Skip to content

The agent that grows with you

fix(desktop): drop a primary reconnect lookup a connection apply replaced

unsupportedpastels97565aeac59e
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 57a48014d15..4da1e247f73 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
@@ -2808,6 +2808,56 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect(FakeWebSocket.instances.at(-1)?.url).toBe(secondary.wsUrl)
}
)
+
+ it('a reconnect lookup that resolves after a connection apply cannot re-own the primary', async () => {
+ const oldPrimary = { ...remotePrimaryConn, registryScoped: true }
+ const newPrimary = { ...coderConn, mode: 'remote' as const, profile: 'default', registryScoped: true }
+ const heldOldLookup = deferred<typeof oldPrimary>()
+ let applied = false
+
+ const desktop = {
+ ...fakeDesktop(),
+ // Main resolves a profile-less request from the applied window route.
+ getConnection: vi.fn(async () => (applied ? newPrimary : oldPrimary)),
+ getConnectionFor: vi.fn(({ connectionId }: { connectionId: string; profile: string }) =>
+ connectionId === newPrimary.connectionId ? Promise.resolve(newPrimary) : heldOldLookup.promise
+ ),
+ getGatewayWsUrlFor: vi.fn(async ({ connectionId }: { connectionId: string; profile: string }) =>
+ connectionId === newPrimary.connectionId ? newPrimary.wsUrl : oldPrimary.wsUrl
+ )
+ }
+
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+ render(<Harness />)
+ await flushAsync()
+ expect(FakeWebSocket.instances[0].url).toBe(oldPrimary.wsUrl)
+
+ // The old primary drops and its reconnect parks on the route lookup.
+ act(() => FakeWebSocket.instances[0].drop())
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(1_000)
+ })
+ expect(desktop.getConnectionFor).toHaveBeenCalledWith(
+ expect.objectContaining({ connectionId: oldPrimary.connectionId })
+ )
+
+ applied = true
+ act(() => connectionApplied?.())
+ await flushAsync()
+ expect(FakeWebSocket.instances.at(-1)?.url).toBe(newPrimary.wsUrl)
+ expect($gatewayState.get()).toBe('open')
+
+ await act(async () => {
+ heldOldLookup.resolve(oldPrimary)
+ await vi.advanceTimersByTimeAsync(0)
+ })
+ expect($connection.get()?.connectionId).toBe(newPrimary.connectionId)
+
+ act(() => FakeWebSocket.instances.at(-1)!.drop())
+ await advanceBackoff()
+ expect(FakeWebSocket.instances.at(-1)?.url).toBe(newPrimary.wsUrl)
+ expect($connection.get()?.connectionId).toBe(newPrimary.connectionId)
+ })
})
describe('window-state IPC before the first connection publishes (#108641)', () => {
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 0657a3f0100..63ae00eb9c7 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -316,9 +316,13 @@ export function useGatewayBoot({
// signals that fire around wake (power resume, network online, the window
// becoming visible).
let primaryConnection: HermesConnection | null = null
+ // Bumped whenever the primary route is recorded, so a reconnect whose
+ // lookup started before a boot/connection apply cannot re-own the primary.
+ let primaryRouteRevision = 0
const recordPrimaryConnection = (connection: HermesConnection) => {
primaryConnection = connection
+ primaryRouteRevision += 1
setPrimaryGatewayConnection(connection)
}
@@ -468,6 +472,8 @@ export function useGatewayBoot({
// 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 lookupRevision = primaryRouteRevision
+
const conn = await withTimeout(
primaryConnection?.registryScoped && primaryConnection.connectionId
? (desktop.getConnectionFor?.({
@@ -479,12 +485,15 @@ export function useGatewayBoot({
'Timed out reconnecting to Hermes backend'
)
- recordPrimaryConnection(conn)
-
- if (cancelled) {
+ // A boot/connection apply that recorded a newer primary route during
+ // the lookup owns the socket; recording, publishing or dialing the old
+ // route would undo it and pin later reconnects to the old gateway.
+ if (cancelled || lookupRevision !== primaryRouteRevision) {
return
}
+ recordPrimaryConnection(conn)
+
// Only publish the primary descriptor when the primary is active.
// Otherwise a background-profile view would inherit the primary's
// mode/baseUrl and break image.attach / fs / media routing (#46651).