fix(desktop): reuse registry primary for owned session RPCs
This commit is contained in:
@@ -29,6 +29,7 @@ import {
|
||||
reconnectSecondaryGateways,
|
||||
reportPrimaryGatewayState,
|
||||
setPrimaryGateway,
|
||||
setPrimaryGatewayConnection,
|
||||
touchSecondaryGateways
|
||||
} from '@/store/gateway'
|
||||
import { registerGatewayReconnect } from '@/store/gateway-reconnect'
|
||||
@@ -281,6 +282,8 @@ export function useGatewayBoot({
|
||||
'Timed out reconnecting to Hermes backend'
|
||||
)
|
||||
|
||||
setPrimaryGatewayConnection(conn)
|
||||
|
||||
if (cancelled) {
|
||||
return
|
||||
}
|
||||
@@ -521,6 +524,7 @@ export function useGatewayBoot({
|
||||
}
|
||||
|
||||
publish(conn)
|
||||
setPrimaryGatewayConnection(conn)
|
||||
|
||||
// Bounded for the same reason as attemptReconnect() (#93454): a wedged
|
||||
// ticket mint would otherwise hang the gateway switch forever.
|
||||
@@ -828,6 +832,7 @@ export function useGatewayBoot({
|
||||
progress: 95
|
||||
})
|
||||
publish(conn)
|
||||
setPrimaryGatewayConnection(conn)
|
||||
|
||||
// Seed the workspace BEFORE the gateway opens: every session-restore
|
||||
// path is gated on gatewayState === 'open', so nothing can be active yet
|
||||
@@ -942,6 +947,7 @@ export function useGatewayBoot({
|
||||
|
||||
if (survivor?.connection) {
|
||||
publish(survivor.connection)
|
||||
setPrimaryGatewayConnection(survivor.connection)
|
||||
}
|
||||
|
||||
const profile = survivor?.profile ?? $activeGatewayProfile.get()
|
||||
|
||||
@@ -58,7 +58,8 @@ const {
|
||||
requestGatewayForAgent,
|
||||
requestGatewayForProfile,
|
||||
retainGatewayForAgent,
|
||||
setPrimaryGateway
|
||||
setPrimaryGateway,
|
||||
setPrimaryGatewayConnection
|
||||
} = await import('./gateway')
|
||||
|
||||
function installDesktop(getConnection: ReturnType<typeof vi.fn>): void {
|
||||
@@ -191,6 +192,70 @@ describe('requestGatewayForProfile', () => {
|
||||
})
|
||||
|
||||
describe('requestGatewayForAgent', () => {
|
||||
it('reuses the active primary socket when its registry connection owns the session', async () => {
|
||||
const primary = makePrimary()
|
||||
|
||||
const getConnectionFor = vi.fn(async ({ connectionId, profile }) => ({
|
||||
connectionId,
|
||||
port: 5151,
|
||||
profile,
|
||||
token: 'secondary-token'
|
||||
}))
|
||||
|
||||
setPrimaryGateway(primary as never, 'default')
|
||||
setPrimaryGatewayConnection({ connectionId: 'remote-primary' })
|
||||
|
||||
;(window as unknown as { hermesDesktop: unknown }).hermesDesktop = {
|
||||
getConnection: vi.fn(),
|
||||
getConnectionFor,
|
||||
getGatewayWsUrlFor: vi.fn(async () => ({ ok: true as const, wsUrl: 'wss://remote.invalid/api/ws' })),
|
||||
touchBackend: vi.fn(async () => undefined)
|
||||
}
|
||||
await ensureGatewayForProfile('default')
|
||||
|
||||
const result = await requestGatewayForAgent('remote-primary', 'default', 'session.resume', {
|
||||
session_id: 'stored-session'
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
method: 'session.resume',
|
||||
params: { session_id: 'stored-session' }
|
||||
})
|
||||
expect(primary.request).toHaveBeenCalledWith('session.resume', { session_id: 'stored-session' })
|
||||
expect(getConnectionFor).not.toHaveBeenCalled()
|
||||
expect(secondaryGateways).toHaveLength(0)
|
||||
expect($gateway.get()).toBe(primary)
|
||||
})
|
||||
|
||||
it('keeps another profile on the same registry source isolated from the primary', async () => {
|
||||
const primary = makePrimary()
|
||||
|
||||
const getConnectionFor = vi.fn(async ({ connectionId, profile }) => ({
|
||||
connectionId,
|
||||
port: 5151,
|
||||
profile,
|
||||
token: 'secondary-token'
|
||||
}))
|
||||
|
||||
setPrimaryGateway(primary as never, 'default')
|
||||
setPrimaryGatewayConnection({ connectionId: 'remote-primary' })
|
||||
|
||||
;(window as unknown as { hermesDesktop: unknown }).hermesDesktop = {
|
||||
getConnection: vi.fn(),
|
||||
getConnectionFor,
|
||||
getGatewayWsUrlFor: vi.fn(async () => ({ ok: true as const, wsUrl: 'wss://remote.invalid/api/ws' })),
|
||||
touchBackend: vi.fn(async () => undefined)
|
||||
}
|
||||
|
||||
await requestGatewayForAgent('remote-primary', 'research', 'session.resume', {
|
||||
session_id: 'research-session'
|
||||
})
|
||||
|
||||
expect(getConnectionFor).toHaveBeenCalledWith({ connectionId: 'remote-primary', profile: 'research' })
|
||||
expect(primary.request).not.toHaveBeenCalled()
|
||||
expect(secondaryGateways).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('leases separate registry sockets for duplicate profile names without changing the active gateway', async () => {
|
||||
const primary = makePrimary()
|
||||
const getConnection = vi.fn(async (profile: null | string) => ({ port: 4242, profile, token: 'legacy-token' }))
|
||||
@@ -273,6 +338,7 @@ describe('requestGatewayForAgent', () => {
|
||||
|
||||
it('evicts registry sockets when their source is edited or removed', async () => {
|
||||
const primary = makePrimary()
|
||||
|
||||
const getConnectionFor = vi.fn(async ({ connectionId, profile }) => ({ connectionId, port: 5151, profile }))
|
||||
const onActiveConnectionInvalidated = vi.fn()
|
||||
|
||||
|
||||
@@ -100,6 +100,8 @@ const ACTIVATION_LEASE_MS = 30_000
|
||||
interface GatewayRegistryState {
|
||||
config: RegistryConfig | null
|
||||
primaryGateway: HermesGateway | null
|
||||
/** Registry source currently served by primaryGateway, when known. */
|
||||
primaryConnectionId: null | string
|
||||
primaryProfile: string
|
||||
activeKey: string
|
||||
activationEpoch: number
|
||||
@@ -114,6 +116,7 @@ function createRegistryState(): GatewayRegistryState {
|
||||
return {
|
||||
config: null,
|
||||
primaryGateway: null,
|
||||
primaryConnectionId: null,
|
||||
primaryProfile: 'default',
|
||||
activeKey: 'default',
|
||||
activationEpoch: 0,
|
||||
@@ -186,10 +189,19 @@ export function emitLocalGatewayEvent(event: GatewayEvent): void {
|
||||
}
|
||||
|
||||
export function setPrimaryGateway(gateway: HermesGateway | null, profile = 'default'): void {
|
||||
if (g.primaryGateway !== gateway) {
|
||||
g.primaryConnectionId = null
|
||||
}
|
||||
|
||||
g.primaryGateway = gateway
|
||||
g.primaryProfile = normKey(profile)
|
||||
}
|
||||
|
||||
/** Publish the registry source owned by the window primary socket. */
|
||||
export function setPrimaryGatewayConnection(connection: Pick<HermesConnection, 'connectionId'> | null): void {
|
||||
g.primaryConnectionId = connection?.connectionId?.trim() || null
|
||||
}
|
||||
|
||||
export function isActivePrimary(): boolean {
|
||||
return g.activeKey === g.primaryProfile
|
||||
}
|
||||
@@ -668,6 +680,22 @@ export async function requestGatewayForAgent<T>(
|
||||
return requestGatewayForProfile<T>(key, method, params, timeoutMs, signal)
|
||||
}
|
||||
|
||||
// A primary remote selected from the connection registry carries its source
|
||||
// id in the active connection descriptor. Requests for that exact
|
||||
// (connection, profile) already have an owning socket: the window primary.
|
||||
// Dialing a registry secondary here can resolve the same public endpoint to a
|
||||
// different backend/profile route, so durable session.resume reports
|
||||
// "session not found" while REST history from the primary remains visible.
|
||||
// Require both owner identities to agree before collapsing the route; a
|
||||
// different source or profile must retain its isolated secondary.
|
||||
if (
|
||||
key === g.primaryProfile &&
|
||||
Boolean(g.primaryConnectionId) &&
|
||||
g.primaryConnectionId === String(connectionId).trim()
|
||||
) {
|
||||
return requestGatewayForProfile<T>(key, method, params, timeoutMs, signal)
|
||||
}
|
||||
|
||||
if (!window.hermesDesktop?.getConnectionFor) {
|
||||
throw new Error('This Desktop build cannot dial registry connections. Update Hermes Desktop.')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user