Skip to content

The agent that grows with you

fix(desktop): surface unsafe gateway paths without re-ensuring

David Dudok de Wit104b46845672
diff --git a/apps/desktop/electron/local-gateway-unsafe-path.test.ts b/apps/desktop/electron/local-gateway-unsafe-path.test.ts
new file mode 100644
index 00000000000..cdbe1d662e9
--- /dev/null
+++ b/apps/desktop/electron/local-gateway-unsafe-path.test.ts
@@ -0,0 +1,27 @@
+import { expect, test, vi } from 'vitest'
+
+import { redialLocalGateway } from './local-gateway'
+
+test('an unsafe control path surfaces without forgetting and re-ensuring the same owner', async () => {
+ const failure = new Error('Unsafe gateway control path')
+ const ensure = vi.fn(async () => 'owner')
+ const forget = vi.fn()
+ const use = vi.fn(async () => { throw failure })
+
+ await expect(redialLocalGateway({ ensure, forget, use })).rejects.toBe(failure)
+ expect(ensure).toHaveBeenCalledOnce()
+ expect(use).toHaveBeenCalledOnce()
+ expect(forget).not.toHaveBeenCalled()
+})
+
+test.each(['Gateway ticket control socket missing', 'Invalid gateway control pointer', 'Noncanonical gateway profile'])(
+ 'a stale %s still refreshes its endpoint once', async message => {
+ const ensure = vi.fn(async () => 'owner')
+ const forget = vi.fn()
+ const use = vi.fn().mockRejectedValueOnce(new Error(message)).mockResolvedValue('fresh ticket')
+
+ await expect(redialLocalGateway({ ensure, forget, use })).resolves.toBe('fresh ticket')
+ expect(ensure).toHaveBeenCalledTimes(2)
+ expect(forget).toHaveBeenCalledOnce()
+ }
+)
diff --git a/apps/desktop/electron/local-gateway.test.ts b/apps/desktop/electron/local-gateway.test.ts
index 56e63b2f2b7..4ed5b1f6051 100644
--- a/apps/desktop/electron/local-gateway.test.ts
+++ b/apps/desktop/electron/local-gateway.test.ts
@@ -352,7 +352,7 @@ test.skipIf(process.platform === 'win32')('a group-accessible control socket is
const failure = await mintLocalGatewayTicket(endpoint).catch(error => error)
expect(failure).toBeInstanceOf(Error)
expect(failure.message).toBe('Unsafe gateway control path')
- expect(isStaleLocalGatewayError(failure)).toBe(true)
+ expect(isStaleLocalGatewayError(failure)).toBe(false)
expect(isStaleLocalGatewayError(new Error('EACCES: permission denied'))).toBe(false)
} finally {
await new Promise<void>(resolve => server.close(() => resolve()))
diff --git a/apps/desktop/electron/local-gateway.ts b/apps/desktop/electron/local-gateway.ts
index a72f02fa84d..9ab512dafa7 100644
--- a/apps/desktop/electron/local-gateway.ts
+++ b/apps/desktop/electron/local-gateway.ts
@@ -188,10 +188,10 @@ export async function ensureLocalGateway(
// update restart) every ticket mint against the stale control socket fails and
// the renderer's reconnect backoff would loop on the dead descriptor forever.
// Forget the cached descriptor exactly once and re-run the canonical ensure;
-// a second failure is a real error and surfaces to the caller. Every control-path
-// refusal mintLocalGatewayTicket raises qualifies: a pointer mid-rewrite or left by
-// an earlier owner, and a profile home moved since the descriptor was cached.
-const STALE_CONTROL_ERRORS = new Set(['Invalid gateway ticket response', 'Unsafe gateway control path', 'Invalid gateway control pointer', 'Noncanonical gateway profile'])
+// a second failure is a real error and surfaces to the caller. A pointer mid-rewrite
+// or left by an earlier owner, and a profile home moved since caching, may recover.
+// Unsafe path permissions or types require repair, not another ensure of the same home.
+const STALE_CONTROL_ERRORS = new Set(['Invalid gateway ticket response', 'Invalid gateway control pointer', 'Noncanonical gateway profile'])
export function isStaleLocalGatewayError(error: unknown): boolean {
const message = error instanceof Error ? error.message : String(error ?? '')