Skip to content

The agent that grows with you

fix(desktop): keep a stale primary recovery from overwriting the foreground descriptor

teknium1ffe3d823fafa
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-request.test.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-request.test.ts
index 37324b326ca..3411b7ff733 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-request.test.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-request.test.ts
@@ -385,6 +385,46 @@ describe('useGatewayRequest', () => {
expect(desktop.getConnection).toHaveBeenCalledTimes(1)
})
+ it.each(['resolves', 'rejects'] as const)(
+ 'keeps the newer foreground descriptor when a stale primary recovery lookup %s',
+ async outcome => {
+ const desktop = installRemoteDesktop()
+ const primary = makePrimaryGateway()
+ const failure = new Error('connection closed')
+ primary.connectionState = 'closed'
+ primary.request.mockRejectedValueOnce(failure)
+ let settleLookup!: () => void
+ const localConnection = await desktop.getConnection()
+ desktop.getConnection.mockClear()
+ desktop.getConnection.mockImplementationOnce(
+ () =>
+ new Promise((resolve, reject) => {
+ settleLookup = () =>
+ outcome === 'resolves' ? resolve(localConnection) : reject(new Error('local backend unavailable'))
+ })
+ )
+ setPrimaryGateway(primary as unknown as HermesGateway, 'default')
+ $gateway.set(primary as unknown as HermesGateway)
+ $gatewayState.set('closed')
+ const { result } = renderHook(() => useGatewayRequest())
+ const pending = result.current.requestGateway('prompt.submit', { text: 'local-only' }).catch(error => error)
+ await vi.waitFor(() => expect(desktop.getConnection).toHaveBeenCalledTimes(1))
+ await act(async () => {
+ await ensureGatewayForAgent('home', 'default')
+ })
+ const foreground = { ...localConnection, baseUrl: 'https://home.example.test', connectionId: 'home' }
+ $connection.set(foreground as never)
+ await act(async () => {
+ settleLookup()
+ })
+
+ expect(await pending).toBe(failure)
+ expect($connection.get()).toBe(foreground)
+ expect(result.current.connectionRef.current).toBeNull()
+ expect(primary.connect).not.toHaveBeenCalled()
+ }
+ )
+
it('does not reconnect for a non-transport request failure', async () => {
const { desktop, gateway } = await activateRemoteGateway()
const failure = Object.assign(new Error('request rejected'), { code: 'EVALIDATION' })
diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-request.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-request.ts
index f462e490c4a..2d5a503ef6d 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-request.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-request.ts
@@ -11,6 +11,7 @@ import {
activeGatewayConnectionId,
activeGatewayProfileKey,
ensureActiveGatewayOpen,
+ gatewayActivationEpoch,
isActivePrimary
} from '@/store/gateway'
import { $gatewayState, setConnection } from '@/store/session'
@@ -84,6 +85,14 @@ export function useGatewayRequest() {
reauthErrorRef.current = null
+ // Main resolves the profile-less lookup through the window's CURRENT
+ // route. If another source took the foreground meanwhile, the answer may
+ // describe that source, and either outcome is no longer ours to publish
+ // over the newer descriptor. The boot hook's own-route reconnect loop
+ // still restores the primary socket.
+ const activationEpoch = gatewayActivationEpoch()
+ const ownsForeground = () => isActivePrimary() && gatewayActivationEpoch() === activationEpoch
+
try {
// This path recovers only the window primary (requestGateway routes
// secondaries to ensureActiveGatewayOpen). Call getConnection() with no
@@ -102,6 +111,10 @@ export function useGatewayRequest() {
'Timed out reconnecting to Hermes backend'
)
+ if (!ownsForeground()) {
+ return null
+ }
+
connectionRef.current = conn
setConnection(conn)
@@ -125,8 +138,10 @@ export function useGatewayRequest() {
reauthErrorRef.current = error
}
- connectionRef.current = null
- setConnection(null)
+ if (ownsForeground()) {
+ connectionRef.current = null
+ setConnection(null)
+ }
return null
} finally {