Skip to content

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 authorize-url rejected for a stale RPC token

Diego Zacarias4b146671d806
diff --git a/desktop-app/src/__tests__/appService.sandboxUiRpcTokenRetry.test.ts b/desktop-app/src/__tests__/appService.sandboxUiRpcTokenRetry.test.ts
index bd9cd928e..6606c2053 100644
--- a/desktop-app/src/__tests__/appService.sandboxUiRpcTokenRetry.test.ts
+++ b/desktop-app/src/__tests__/appService.sandboxUiRpcTokenRetry.test.ts
@@ -149,3 +149,55 @@ describe('AppService.mintSandboxUiSession retries once with a fresh RPC token',
expect(mocks.clear).toHaveBeenCalledOnce()
})
})
+
+const AUTHORIZE_URL = 'https://accounts.example.com/o/oauth2/auth?client_id=x'
+const authorizeOk = () => rpcProxyJson(200, { authorizeUrl: AUTHORIZE_URL })
+
+describe('AppService.requestSandboxUiOauthAuthorize retries once with a fresh RPC token', () => {
+ it('re-requests after a 401 and opens the returned authorize URL', async () => {
+ fetchMock.mockResolvedValueOnce(UNAUTHORIZED()).mockResolvedValueOnce(authorizeOk())
+
+ await expect(
+ makeService().requestSandboxUiOauthAuthorize('sandbox-recipes', 'crm', 'salesforce')
+ ).resolves.toBeUndefined()
+
+ expect(mocks.openExternal).toHaveBeenCalledWith(AUTHORIZE_URL)
+ expect(bearersSent()).toEqual(['Bearer stale', 'Bearer fresh'])
+ expect(mocks.clear).toHaveBeenCalledOnce()
+ })
+
+ it('re-requests after a 403 for a missing scope', async () => {
+ fetchMock.mockResolvedValueOnce(MISSING_SCOPE()).mockResolvedValueOnce(authorizeOk())
+
+ await expect(
+ makeService().requestSandboxUiOauthAuthorize('sandbox-recipes', 'crm', 'salesforce')
+ ).resolves.toBeUndefined()
+
+ expect(mocks.openExternal).toHaveBeenCalledWith(AUTHORIZE_URL)
+ expect(bearersSent()).toEqual(['Bearer stale', 'Bearer fresh'])
+ })
+
+ it('does not retry a recipe ACL denial', async () => {
+ fetchMock.mockResolvedValueOnce(ACL_DENIED())
+
+ await expect(
+ makeService().requestSandboxUiOauthAuthorize('sandbox-recipes', 'crm', 'salesforce')
+ ).rejects.toThrow(/\(403\)/)
+
+ expect(bearersSent()).toEqual(['Bearer stale'])
+ expect(mocks.clear).not.toHaveBeenCalled()
+ expect(mocks.openExternal).not.toHaveBeenCalled()
+ })
+
+ it('retries only once when the fresh token is rejected too', async () => {
+ fetchMock.mockResolvedValueOnce(UNAUTHORIZED()).mockResolvedValueOnce(UNAUTHORIZED())
+
+ await expect(
+ makeService().requestSandboxUiOauthAuthorize('sandbox-recipes', 'crm', 'salesforce')
+ ).rejects.toThrow(/\(401\)/)
+
+ expect(bearersSent()).toEqual(['Bearer stale', 'Bearer fresh'])
+ expect(mocks.clear).toHaveBeenCalledOnce()
+ expect(mocks.openExternal).not.toHaveBeenCalled()
+ })
+})
diff --git a/desktop-app/src/__tests__/rpcProxyClient.test.ts b/desktop-app/src/__tests__/rpcProxyClient.test.ts
index b8483b021..a1183212c 100644
--- a/desktop-app/src/__tests__/rpcProxyClient.test.ts
+++ b/desktop-app/src/__tests__/rpcProxyClient.test.ts
@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { ApiError } from '../httpClient.js'
import { RpcProxyClient, SANDBOX_UI_MINT_TIMEOUT_MS } from '../rpcProxyClient.js'
// Mock the config module so the module-level url() helper uses a fixed base URL
@@ -801,9 +802,16 @@ describe('RpcProxyClient.requestSandboxUiOauthAuthorizeUrl', () => {
text: async () => '{"error":"recipe_not_found"}',
})
)
- await expect(client.requestSandboxUiOauthAuthorizeUrl('t', 'ns', 'r', 'cid')).rejects.toThrow(
- /authorize-url request failed \(404\)/
+ const error = await client.requestSandboxUiOauthAuthorizeUrl('t', 'ns', 'r', 'cid').then(
+ () => null,
+ (e: unknown) => e
)
+ expect(error).toBeInstanceOf(ApiError)
+ expect(error).toMatchObject({
+ message: 'sandbox-ui authorize-url request failed (404): {"error":"recipe_not_found"}',
+ status: 404,
+ bodyText: '{"error":"recipe_not_found"}',
+ })
})
it('throws when the JSON response is missing authorizeUrl', async () => {
diff --git a/desktop-app/src/rpcProxyClient.ts b/desktop-app/src/rpcProxyClient.ts
index 0bd4f5f9d..6bdae4ae8 100644
--- a/desktop-app/src/rpcProxyClient.ts
+++ b/desktop-app/src/rpcProxyClient.ts
@@ -735,8 +735,12 @@ export class RpcProxyClient {
)
if (!response.ok) {
const body = await response.text()
- throw new Error(
- `sandbox-ui authorize-url request failed (${response.status}): ${boundedErrorExcerpt(body) || '<empty>'}`
+ // An ApiError so `AppService.shouldRefreshRpcToken` can retry a 401 or a
+ // missing-scope 403 with a fresh RPC token.
+ throw new ApiError(
+ `sandbox-ui authorize-url request failed (${response.status}): ${boundedErrorExcerpt(body) || '<empty>'}`,
+ response.status,
+ body
)
}
const json = (await response.json()) as { authorizeUrl?: unknown }