Skip to content

The agent that grows with you

fix(desktop): fall back to the cached socket URL when a native ticket mint fails

emozilla08650aef50ac
diff --git a/apps/desktop/src/lib/browser-desktop-bridge.test.ts b/apps/desktop/src/lib/browser-desktop-bridge.test.ts
index 5c9244de5cd..268a624ddec 100644
--- a/apps/desktop/src/lib/browser-desktop-bridge.test.ts
+++ b/apps/desktop/src/lib/browser-desktop-bridge.test.ts
@@ -7,6 +7,7 @@ import { clearWebappSession, seedWebappSession } from '@/test/webapp-session'
import { installBrowserDesktopBridge } from './browser-desktop-bridge'
import { desktopGit } from './desktop-git'
import { openLink } from './external-link'
+import { resolveDesktopGatewayWsUrl } from './gateway-ws-url'
import { downloadGatewayMediaFile, resolveMediaPlaybackSrc } from './media'
type MutableWindow = Window & {
@@ -1052,6 +1053,20 @@ describe('browser-hosted Desktop bridge', () => {
)
})
+ it("rejects a dial whose ticket mint fails instead of dialing the Webapp's credential-free socket URL", async () => {
+ const win = mutableWindow()
+ seedWebappSession('served-token')
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn().mockResolvedValue(new Response('{"detail":"gateway unavailable"}', { status: 503 }))
+ )
+ expect(installBrowserDesktopBridge()).toBe(true)
+ const desktop = win.hermesDesktop!
+ const connection = await desktop.getConnection('worker-a')
+
+ await expect(resolveDesktopGatewayWsUrl(desktop, connection)).rejects.toThrow('503')
+ })
+
it('bounds the ticket mint by the ordinary request timeout, not a gateway cold start', async () => {
vi.useFakeTimers()
diff --git a/apps/desktop/src/lib/browser-transport.ts b/apps/desktop/src/lib/browser-transport.ts
index 5784afea4f2..cdfde517620 100644
--- a/apps/desktop/src/lib/browser-transport.ts
+++ b/apps/desktop/src/lib/browser-transport.ts
@@ -1,4 +1,4 @@
-import { buildHermesWebSocketUrl, normalizeBasePath } from '@hermes/shared'
+import { BROWSER_NATIVE_DIAL, buildHermesWebSocketUrl, normalizeBasePath } from '@hermes/shared'
import type { HermesApiRequest } from '@/global'
import { consumeWebappSession, WEBAPP_LAUNCH_REQUIRED } from '@/lib/browser-launch-session'
@@ -285,7 +285,7 @@ export async function gatewayWebsocketUrl(bootstrap: BrowserBootstrap, profile?:
/** The credential-free canonical socket URL a browser connection descriptor reports. */
export function gatewaySocketUrl(bootstrap: BrowserBootstrap, profile?: null | string): string {
const url = new URL(websocketUrl(bootstrap.basePath, '/api/ws', {}, profile))
- url.searchParams.set('native_dial', 'browser')
+ url.searchParams.set('native_dial', BROWSER_NATIVE_DIAL)
return url.toString()
}
diff --git a/apps/desktop/src/lib/gateway-ws-url.test.ts b/apps/desktop/src/lib/gateway-ws-url.test.ts
index 75af31dcef2..ac45b09ac7c 100644
--- a/apps/desktop/src/lib/gateway-ws-url.test.ts
+++ b/apps/desktop/src/lib/gateway-ws-url.test.ts
@@ -173,10 +173,9 @@ describe('resolveGatewayWsUrl', () => {
})
})
- // A native dial (local Electron's gateway, the Webapp's serving gateway) carries no credential in
- // its descriptor: every dial needs a fresh ticket, so a failed mint is the reason to show, never a
- // fallback to a URL that can only fail later without one.
- describe('native dial', () => {
+ // The Webapp's socket descriptor carries no credential: every dial needs a fresh ticket, so a
+ // failed mint is the reason to show, never a fallback to a URL that can only fail later without one.
+ describe('Webapp native dial', () => {
const nativeConn = { authMode: 'token' as const, wsUrl: 'ws://host/api/ws?native_dial=browser' }
it("surfaces the mint's own failure instead of dialing the credential-free URL", async () => {
@@ -199,6 +198,31 @@ describe('resolveGatewayWsUrl', () => {
await expect(resolveGatewayWsUrl({ getGatewayWsUrl }, nativeConn)).resolves.toBe(fresh)
})
})
+
+ // Local Electron dials its own gateway through main: a failed mint falls back to the descriptor
+ // URL, so the reconnect loop owns the retry.
+ describe('local Electron native dial', () => {
+ const localConn = { authMode: 'native' as const, wsUrl: 'ws://127.0.0.1:8642/api/ws?native_dial=unminted' }
+
+ it('falls back to the descriptor URL when the mint rejects', async () => {
+ const getGatewayWsUrl = vi.fn().mockRejectedValue(new Error('control socket busy'))
+
+ await expect(resolveGatewayWsUrl({ getGatewayWsUrl }, localConn)).resolves.toBe(localConn.wsUrl)
+ })
+
+ it('falls back to the descriptor URL when the mint refuses', async () => {
+ const getGatewayWsUrl = vi.fn().mockResolvedValue({ error: 'gateway restarting', ok: false })
+
+ await expect(resolveGatewayWsUrl({ getGatewayWsUrl }, localConn)).resolves.toBe(localConn.wsUrl)
+ })
+
+ it('dials the freshly minted URL', async () => {
+ const fresh = 'ws://127.0.0.1:8642/api/ws?native_dial=0b6c&ticket=t'
+ const getGatewayWsUrl = vi.fn().mockResolvedValue({ ok: true, wsUrl: fresh })
+
+ await expect(resolveGatewayWsUrl({ getGatewayWsUrl }, localConn)).resolves.toBe(fresh)
+ })
+ })
})
describe('isGatewayReauthRequired', () => {
diff --git a/apps/shared/src/index.ts b/apps/shared/src/index.ts
index 3e93f36fa73..b653088776f 100644
--- a/apps/shared/src/index.ts
+++ b/apps/shared/src/index.ts
@@ -197,6 +197,7 @@ export {
export { type UpdateDebt, updateDebt, type UpdateDebtReceipt, type UpdateDebtStep } from './update-debt'
export { mergeUsage, usageFromTick, type UsageTickPayload } from './usage-tick'
export {
+ BROWSER_NATIVE_DIAL,
buildHermesWebSocketUrl,
type GatewayAuthMode,
GatewayReauthRequiredError,
diff --git a/apps/shared/src/websocket-url.ts b/apps/shared/src/websocket-url.ts
index 9c4e1a3e32c..b66ae816a24 100644
--- a/apps/shared/src/websocket-url.ts
+++ b/apps/shared/src/websocket-url.ts
@@ -78,13 +78,14 @@ export async function resolveGatewayWsUrl(deps: ResolveGatewayWsUrlDeps, conn: G
}
}
- // A native dial's descriptor URL carries no credential of its own, so a failed mint IS the answer
+ // The Webapp's descriptor URL carries no credential of its own, so a failed mint IS the answer
// (a gateway that cannot attach says why); falling back to that URL only fails later, nameless.
- const nativeDial = hasNativeDial(conn.wsUrl)
+ // Every other descriptor falls back to its cached URL and leaves the retry to the reconnect loop.
+ const mintRequired = nativeDialOf(conn.wsUrl) === BROWSER_NATIVE_DIAL
if (mint) {
const fresh = await mint(profile).catch((error: unknown) => {
- if (nativeDial) {
+ if (mintRequired) {
throw error
}
@@ -99,7 +100,7 @@ export async function resolveGatewayWsUrl(deps: ResolveGatewayWsUrlDeps, conn: G
return fresh.wsUrl
}
- if (fresh && nativeDial) {
+ if (fresh && mintRequired) {
throw new Error(fresh.error || 'Could not mint a gateway WebSocket ticket.')
}
}
@@ -107,11 +108,14 @@ export async function resolveGatewayWsUrl(deps: ResolveGatewayWsUrlDeps, conn: G
return conn.wsUrl
}
-function hasNativeDial(wsUrl: string): boolean {
+/** The `native_dial` value on the browser-hosted Desktop's own socket URL. */
+export const BROWSER_NATIVE_DIAL = 'browser'
+
+function nativeDialOf(wsUrl: string): null | string {
try {
- return new URL(wsUrl).searchParams.has('native_dial')
+ return new URL(wsUrl).searchParams.get('native_dial')
} catch {
- return false
+ return null
}
}