Skip to content

The agent that grows with you

fix(desktop): record the primary connection while a secondary is foregrounded

teknium1e67255e7e4ba
diff --git a/apps/desktop/src/store/gateway-connection-scope.test.ts b/apps/desktop/src/store/gateway-connection-scope.test.ts
index c043675e1b5..31ae57e675e 100644
--- a/apps/desktop/src/store/gateway-connection-scope.test.ts
+++ b/apps/desktop/src/store/gateway-connection-scope.test.ts
@@ -109,23 +109,25 @@ describe('primary gateway registry scope', () => {
expect(setApiRequestConnection).toHaveBeenLastCalledWith(null)
})
- it('ignores primary connection-id writes while a secondary registry scope is active (#95628 hardening)', async () => {
+ it('records a primary re-dial made while a secondary scope is foregrounded (#95628 keeps ambient routing)', async () => {
setPrimaryGateway({ connectionState: 'open' } as never, 'default')
setPrimaryGatewayConnectionId('primary-vps')
// Foreground Gateway B's composite scope (connectionId 'homelab').
await expect(ensureGatewayForAgent('homelab', 'default')).resolves.toBe(true)
+ vi.mocked(setApiRequestConnection).mockClear()
- // Presentation-layer write while the secondary is foregrounded: the id
- // describes the secondary, not the primary. It must be dropped — accepting
- // it relabels the primary socket and poisons ambient routing.
- setPrimaryGatewayConnectionId('homelab')
+ // A connection apply re-dials the PRIMARY socket to another machine while
+ // the secondary stays in front. Ambient requests still belong to the
+ // secondary, but the primary must stop claiming the machine it left —
+ // owner routing to 'primary-vps' would otherwise land on 'local'.
+ setPrimaryGatewayConnectionId('local')
+ expect(setApiRequestConnection).not.toHaveBeenCalled()
- // Back on the primary route, its registry identity is intact.
await ensureGatewayForProfile('default')
- expect(activeGatewayConnectionId()).toBe('primary-vps')
- expect(setApiRequestConnection).toHaveBeenLastCalledWith('primary-vps')
+ expect(activeGatewayConnectionId()).toBe('local')
+ expect(setApiRequestConnection).toHaveBeenLastCalledWith('local')
})
})
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 9fa64d10d6e..ba1c6e2b637 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -382,10 +382,6 @@ export function dispatchPrimaryServerRequest(request: ServerRequest, profile: st
return dispatchServerRequest(request, profile, g.config?.activeConnectionId?.() ?? null)
}
-/** The source the last primary connect/reconnect named, published or not:
- * diverges from `g.primaryConnectionId` only when the publish was refused. */
-let primaryDialedConnectionId: null | string = null
-
export function setPrimaryGateway(gateway: HermesGateway | null, profile = 'default'): void {
const next = normKey(profile)
@@ -416,25 +412,13 @@ export function setPrimaryGatewayConnectionId(
connectionId: null | string | undefined,
mode: 'local' | 'remote' | null | undefined = undefined
): void {
- // Hardening for #95628: while the active route is a secondary scope, the
- // window is looking at a NON-primary socket — any connection id flowing
- // through presentation-layer code at that moment describes the secondary,
- // not the primary. Accepting it would relabel the primary socket, so every
- // ambient API/WebSocket helper (and new-session routing) silently lands on
- // the wrong backend. The primary's own identity is (re)published by its
- // boot/reconnect path, which runs with the primary route active.
- primaryDialedConnectionId = (connectionId ?? '').trim() || null
-
- if (!isActivePrimary()) {
- traceIdentityChange(
- 'gateway-route',
- 'primary-publish',
- `refused ${primaryDialedConnectionId ?? '-'} keeping=${g.primaryConnectionId ?? '-'} active=${g.activeKey}`
- )
-
- return
- }
-
+ // Every caller publishes the window's OWN primary socket (boot, reconnect,
+ // connection apply, HMR survivor), so record it even while a secondary is
+ // foregrounded. Refusing it there left primaryConnectionId naming the
+ // machine the primary socket had just left, and owner routing then sent
+ // that machine's requests to the other one. What belongs to the active
+ // scope is the AMBIENT request connection (#95628), which moves only while
+ // the primary is in front.
g.primaryConnectionId = (connectionId ?? '').trim() || null
g.primaryConnectionMode = mode === 'local' || mode === 'remote' ? mode : null
@@ -470,7 +454,7 @@ function traceAgentRoute(scope: string, via: string): void {
traceIdentityChange(
'gateway-route',
scope,
- `via=${via} primary=${g.primaryConnectionId ?? '-'}/${g.primaryProfile} dialed=${primaryDialedConnectionId ?? '-'}`
+ `via=${via} primary=${g.primaryConnectionId ?? '-'}/${g.primaryProfile}`
)
}