Skip to content

The agent that grows with you

test(desktop): split the scoped ws-url cases and explain the recovery route

kshitijk4poorfde4997f580c
diff --git a/apps/desktop/e2e/peer-device-reset.spec.ts b/apps/desktop/e2e/peer-device-reset.spec.ts
index 70963841485..53ca2a1699c 100644
--- a/apps/desktop/e2e/peer-device-reset.spec.ts
+++ b/apps/desktop/e2e/peer-device-reset.spec.ts
@@ -44,6 +44,9 @@ function pythonBinary(): string {
// Do not inherit profile selection, provider secrets, a dev-server URL, or
// native account state. buildAppEnv supplies the suite's desktop launch flags.
+// The temp-dir variables ARE inherited: a sandboxed runner points them at a
+// private writable directory, and Electron plus the spawned gateways fall back
+// to a possibly unwritable system /tmp without them.
function isolatedEnv(sandbox: Sandbox): Record<string, string> {
const defaults = buildAppEnv(sandbox)
const env: Record<string, string> = {}
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 cea023681be..1850e8e0671 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-request.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-request.ts
@@ -71,15 +71,17 @@ export function useGatewayRequest() {
reauthErrorRef.current = null
try {
- // This recovery path serves the window primary; secondaries reconnect
- // through the registry below. Omit the profile so main resolves the
- // sender's full route, not a remote profile name in the local pool.
- // Both awaits below are IPC round-trips
- // into the main process with no timeout of their own (#93454) — a
- // wedged main-process round-trip otherwise hangs this await forever,
- // latching reconnectingRef.current so every later requestGateway() call
- // returns the same never-settling promise. Bound the same way
- // use-gateway-boot.ts bounds the primary boot/soft-switch equivalents.
+ // This path recovers only the window primary (requestGateway routes
+ // secondaries to ensureActiveGatewayOpen). Call getConnection() with no
+ // profile so main resolves the sender's full route — passing the
+ // profile name would look it up in the LOCAL pool, which fails for a
+ // profile that exists only on a remote gateway (peer windows).
+ // Both awaits below are IPC round-trips into the main process with no
+ // timeout of their own (#93454) — a wedged main-process round-trip
+ // otherwise hangs this await forever, latching reconnectingRef.current
+ // so every later requestGateway() call returns the same never-settling
+ // promise. Bound the same way use-gateway-boot.ts bounds the primary
+ // boot/soft-switch equivalents.
const conn = await withTimeout(
desktop.getConnection(),
RECONNECT_ATTEMPT_TIMEOUT_MS,
diff --git a/apps/desktop/src/lib/gateway-ws-url.test.ts b/apps/desktop/src/lib/gateway-ws-url.test.ts
index 566573a0768..32b049cf6a1 100644
--- a/apps/desktop/src/lib/gateway-ws-url.test.ts
+++ b/apps/desktop/src/lib/gateway-ws-url.test.ts
@@ -9,44 +9,74 @@ const oauthConn = { authMode: 'oauth' as const, wsUrl: 'ws://host/api/ws?ticket=
const tokenConn = { authMode: 'token' as const, wsUrl: 'ws://host/api/ws?token=abc' }
describe('desktop connection scope', () => {
- it.each(['token', 'oauth'] as const)(
- 'preserves legacy aliases and never falls back to legacy minting for a registered %s route',
- async authMode => {
- const connection = {
- authMode,
- connectionId: 'remote-device',
- profile: 'client-alias',
- wsUrl: 'wss://remote.invalid/api/ws?token=cached'
- } as HermesConnection
-
- const desktop = {
- getGatewayWsUrl: vi.fn(async () => 'wss://legacy.invalid/api/ws?token=fresh'),
- getGatewayWsUrlFor: vi.fn(async () => 'wss://remote.invalid/api/ws?ticket=fresh')
- } as unknown as Window['hermesDesktop']
-
- await expect(resolveDesktopGatewayWsUrl(desktop, connection)).resolves.toContain('legacy.invalid')
- expect(desktop.getGatewayWsUrl).toHaveBeenCalledWith('client-alias')
- expect(desktop.getGatewayWsUrlFor).not.toHaveBeenCalled()
- vi.mocked(desktop.getGatewayWsUrl!).mockClear()
-
- const registered = { ...connection, profile: 'remote-profile', registryScoped: true }
- await expect(resolveDesktopGatewayWsUrl(desktop, registered)).resolves.toContain('remote.invalid')
- expect(desktop.getGatewayWsUrlFor).toHaveBeenCalledWith({
- connectionId: 'remote-device',
- profile: 'remote-profile'
- })
+ const authModes = ['token', 'oauth'] as const
+
+ function aliasConnection(authMode: (typeof authModes)[number]) {
+ return {
+ authMode,
+ connectionId: 'remote-device',
+ profile: 'client-alias',
+ wsUrl: 'wss://remote.invalid/api/ws?token=cached'
+ } as HermesConnection
+ }
+
+ function registeredConnection(authMode: (typeof authModes)[number]) {
+ return { ...aliasConnection(authMode), profile: 'remote-profile', registryScoped: true } as HermesConnection
+ }
+
+ function fakeDesktop(withScopedMint = true) {
+ return {
+ getGatewayWsUrl: vi.fn(async () => 'wss://legacy.invalid/api/ws?token=fresh'),
+ ...(withScopedMint ? { getGatewayWsUrlFor: vi.fn(async () => 'wss://remote.invalid/api/ws?ticket=fresh') } : {})
+ } as unknown as Window['hermesDesktop']
+ }
+
+ it.each(authModes)('an inferred connectionId keeps the legacy profile-alias mint (%s)', async authMode => {
+ const desktop = fakeDesktop()
+
+ await expect(resolveDesktopGatewayWsUrl(desktop, aliasConnection(authMode))).resolves.toContain('legacy.invalid')
+ expect(desktop.getGatewayWsUrl).toHaveBeenCalledWith('client-alias')
+ expect(desktop.getGatewayWsUrlFor).not.toHaveBeenCalled()
+ })
+
+ it.each(authModes)('a registry-scoped route mints against its owning connection (%s)', async authMode => {
+ const desktop = fakeDesktop()
+
+ await expect(resolveDesktopGatewayWsUrl(desktop, registeredConnection(authMode))).resolves.toContain(
+ 'remote.invalid'
+ )
+ expect(desktop.getGatewayWsUrlFor).toHaveBeenCalledWith({
+ connectionId: 'remote-device',
+ profile: 'remote-profile'
+ })
+ expect(desktop.getGatewayWsUrl).not.toHaveBeenCalled()
+ })
- delete desktop.getGatewayWsUrlFor
+ it('a registry-scoped flag without a connectionId still takes the legacy mint', async () => {
+ const desktop = fakeDesktop()
+ const scopedWithoutId = { ...registeredConnection('token'), connectionId: undefined } as HermesConnection
- if (authMode === 'oauth') {
- await expect(resolveDesktopGatewayWsUrl(desktop, registered)).rejects.toThrow('cannot refresh OAuth')
- } else {
- await expect(resolveDesktopGatewayWsUrl(desktop, registered)).resolves.toBe(registered.wsUrl)
- }
+ await expect(resolveDesktopGatewayWsUrl(desktop, scopedWithoutId)).resolves.toContain('legacy.invalid')
+ expect(desktop.getGatewayWsUrl).toHaveBeenCalledWith('remote-profile')
+ expect(desktop.getGatewayWsUrlFor).not.toHaveBeenCalled()
+ })
+
+ it('a registry-scoped OAuth route without the scoped mint bridge fails instead of dialing the legacy gateway', async () => {
+ const desktop = fakeDesktop(false)
+
+ await expect(resolveDesktopGatewayWsUrl(desktop, registeredConnection('oauth'))).rejects.toThrow(
+ 'cannot refresh OAuth'
+ )
+ expect(desktop.getGatewayWsUrl).not.toHaveBeenCalled()
+ })
+
+ it('a registry-scoped token route without the scoped mint bridge reuses its cached URL', async () => {
+ const desktop = fakeDesktop(false)
+ const registered = registeredConnection('token')
- expect(desktop.getGatewayWsUrl).not.toHaveBeenCalled()
- }
- )
+ await expect(resolveDesktopGatewayWsUrl(desktop, registered)).resolves.toBe(registered.wsUrl)
+ expect(desktop.getGatewayWsUrl).not.toHaveBeenCalled()
+ })
})
describe('resolveGatewayWsUrl', () => {
diff --git a/apps/desktop/src/lib/gateway-ws-url.ts b/apps/desktop/src/lib/gateway-ws-url.ts
index bb21f0ea380..ca8247d81de 100644
--- a/apps/desktop/src/lib/gateway-ws-url.ts
+++ b/apps/desktop/src/lib/gateway-ws-url.ts
@@ -6,8 +6,9 @@ export function resolveDesktopGatewayWsUrl(
desktop: Window['hermesDesktop'],
connection: HermesConnection
): Promise<string> {
- // An inferred connectionId can still carry a legacy profile alias. Only an
- // explicitly registry-scoped descriptor is safe to send to the *For bridge.
+ // Only a registry-scoped descriptor may use the *For bridge (see
+ // HermesConnection.registryScoped); an absent bridge fails closed rather
+ // than minting the peer's URL against the local pool.
const { connectionId, profile, registryScoped } = connection
if (!registryScoped || !connectionId) {