Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): retry a sandbox-ui mint rejected for a stale RPC token
Diego Zacariasfccb06ac62ea
diff --git a/desktop-app/src/__tests__/appService.sandboxUiRpcTokenRetry.test.ts b/desktop-app/src/__tests__/appService.sandboxUiRpcTokenRetry.test.tsnew file mode 100644index 000000000..bd9cd928e--- /dev/null+++ b/desktop-app/src/__tests__/appService.sandboxUiRpcTokenRetry.test.ts@@ -0,0 +1,151 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import { AppService } from '../appService.js'++// The RPC-token retry around the sandbox-ui calls is exercised through the REAL+// AppService and the REAL RpcProxyClient: the errors the retry inspects are the+// ones the client actually throws for rpc-proxy's HTTP answers, fed through+// `fetch`. Only the RPC-token issuer (control-api via external-rest-api) and+// the Electron runtime are faked.++const mocks = vi.hoisted(() => ({+ getOrIssue: vi.fn(),+ clear: vi.fn(),+ openExternal: vi.fn(),+}))++vi.mock('electron', () => ({+ app: { getPath: vi.fn(() => '/tmp/clerum-desktop-test') },+ safeStorage: {+ isEncryptionAvailable: vi.fn(() => false),+ encryptString: vi.fn(),+ decryptString: vi.fn(),+ },+ shell: { openExternal: mocks.openExternal },+}))++vi.mock('../config.js', () => ({+ getActiveEnvKey: () => 'test-env',+ config: {+ rpcProxyBaseUrl: 'https://rpc.example',+ externalRestApiBaseUrl: 'http://rest',+ desktopProfileUiBaseUrl: 'https://profile.example.com',+ desktopProfileUiBaseUrlExplicit: false,+ enableDevLoginUi: false,+ requestTimeoutMs: 60000,+ appName: 'test',+ },+}))++vi.mock('../rpcTokenManager.js', () => ({+ RpcTokenManager: class {+ getOrIssue = mocks.getOrIssue+ clear = mocks.clear+ getMetadata = vi.fn(() => ({ expiresAtMs: null, scopes: [], hostRefs: [] }))+ },+}))++vi.mock('../authClient.js', () => ({+ AuthClient: class {+ health = vi.fn().mockResolvedValue({ status: 'ok' })+ getMe = vi.fn()+ },+}))++vi.mock('../tokenStore.js', () => ({+ TokenStore: class {+ getSessionToken = vi.fn().mockResolvedValue(null)+ setSessionToken = vi.fn()+ clearSessionToken = vi.fn()+ },+}))++// rpc-proxy answers with Express `res.status(n).json(body)`. The bodies are the+// ones its auth middleware (`requireScope`) and sandbox-ui routes emit.+function rpcProxyJson(status: number, body: unknown): Response {+ return new Response(JSON.stringify(body), {+ status,+ headers: { 'content-type': 'application/json; charset=utf-8' },+ })+}+const UNAUTHORIZED = () => rpcProxyJson(401, { error: 'Unauthorized' })+const MISSING_SCOPE = () => rpcProxyJson(403, { error: 'Forbidden: missing scope' })+const ACL_DENIED = () => rpcProxyJson(403, { error: 'recipe_acl_denied' })++const SESSION_COOKIE =+ 'clerum_sandbox_ui_session=tok; Path=/api/v1/sandbox-ui/sandbox-recipes/crm/; HttpOnly'+const mintOk = () => new Response(null, { status: 204, headers: { 'set-cookie': SESSION_COOKIE } })++const fetchMock = vi.fn<(input: string | URL | Request, init?: RequestInit) => Promise<Response>>()++function bearersSent(): string[] {+ return fetchMock.mock.calls.map(([, init]) =>+ String((init?.headers as Record<string, string> | undefined)?.authorization)+ )+}++function makeService(): AppService {+ const svc = new AppService()+ ;(svc as unknown as { sessionToken: string }).sessionToken = 'session-token'+ return svc+}++beforeEach(() => {+ vi.clearAllMocks()+ fetchMock.mockReset()+ mocks.getOrIssue.mockReset()+ mocks.getOrIssue+ .mockResolvedValueOnce({ token: 'stale' })+ .mockResolvedValueOnce({ token: 'fresh' })+ .mockResolvedValue({ token: 'unexpected-third' })+ vi.stubGlobal('fetch', fetchMock)+})++afterEach(() => {+ vi.unstubAllGlobals()+})++describe('AppService.mintSandboxUiSession retries once with a fresh RPC token', () => {+ it('re-mints after a 401 and returns the session cookie', async () => {+ fetchMock.mockResolvedValueOnce(UNAUTHORIZED()).mockResolvedValueOnce(mintOk())++ await expect(makeService().mintSandboxUiSession('sandbox-recipes', 'crm')).resolves.toEqual({+ setCookie: SESSION_COOKIE,+ })++ expect(bearersSent()).toEqual(['Bearer stale', 'Bearer fresh'])+ expect(mocks.clear).toHaveBeenCalledOnce()+ })++ it('re-mints after a 403 for a missing scope', async () => {+ fetchMock.mockResolvedValueOnce(MISSING_SCOPE()).mockResolvedValueOnce(mintOk())++ await expect(makeService().mintSandboxUiSession('sandbox-recipes', 'crm')).resolves.toEqual({+ setCookie: SESSION_COOKIE,+ })++ expect(bearersSent()).toEqual(['Bearer stale', 'Bearer fresh'])+ expect(mocks.clear).toHaveBeenCalledOnce()+ })++ it('does not retry a recipe ACL denial', async () => {+ fetchMock.mockResolvedValueOnce(ACL_DENIED())++ await expect(makeService().mintSandboxUiSession('sandbox-recipes', 'crm')).rejects.toThrow(+ /\(403\)/+ )++ expect(bearersSent()).toEqual(['Bearer stale'])+ expect(mocks.clear).not.toHaveBeenCalled()+ })++ it('retries only once when the fresh token is rejected too', async () => {+ fetchMock.mockResolvedValueOnce(UNAUTHORIZED()).mockResolvedValueOnce(UNAUTHORIZED())++ await expect(makeService().mintSandboxUiSession('sandbox-recipes', 'crm')).rejects.toThrow(+ /\(401\)/+ )++ expect(bearersSent()).toEqual(['Bearer stale', 'Bearer fresh'])+ expect(mocks.clear).toHaveBeenCalledOnce()+ })+})diff --git a/desktop-app/src/__tests__/httpClient.upstreamErrors.test.ts b/desktop-app/src/__tests__/httpClient.upstreamErrors.test.tsindex d2d0dbaf4..6321d268b 100644--- a/desktop-app/src/__tests__/httpClient.upstreamErrors.test.ts+++ b/desktop-app/src/__tests__/httpClient.upstreamErrors.test.ts@@ -259,8 +259,10 @@ describe('bounded upstream error excerpts at every remaining call site', () => {)expect(error).toBeInstanceOf(SandboxUiSessionError)+ expect(error).toBeInstanceOf(ApiError)expect((error as SandboxUiSessionError).status).toBe(500)expect((error as SandboxUiSessionError).body).toBe(HUGE)+ expect((error as SandboxUiSessionError).bodyText).toBe(HUGE)})})diff --git a/desktop-app/src/rpcProxyClient.ts b/desktop-app/src/rpcProxyClient.tsindex d118284c8..0bd4f5f9d 100644--- a/desktop-app/src/rpcProxyClient.ts+++ b/desktop-app/src/rpcProxyClient.ts@@ -448,17 +448,23 @@ function url(path: string): string {* 409 → app is updating; try again* 500 / 502 → generic open failure*/-export class SandboxUiSessionError extends Error {- readonly status: number- readonly body: string+export class SandboxUiSessionError extends ApiError {+ // An ApiError so `AppService.shouldRefreshRpcToken` sees a 401 or a+ // missing-scope 403 and re-mints with a fresh RPC token. Such a 401 comes+ // from the short-lived RPC token, never from the user's session: it must+ // not be read as a reason to sign the user out.constructor(status: number, body: string) {super(- `sandbox-ui session mint failed (${status}): ${boundedErrorExcerpt(body) || '<empty body>'}`+ `sandbox-ui session mint failed (${status}): ${boundedErrorExcerpt(body) || '<empty body>'}`,+ status,+ body)- this.status = status- this.body = bodythis.name = 'SandboxUiSessionError'}++ get body(): string {+ return this.bodyText+ }}async function readErrorBody(response: Response): Promise<string> {