From aea8274e8f047e1cf779b82e0c3e921bd2dc820f Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 12 Nov 2026 22:51:31 -0800 Subject: [PATCH] =?UTF-8?q?refactor(cloud):=20one=20stale=5Ftoken=20re-min?= =?UTF-8?q?t=20path=20=E2=80=94=20drop=20the=20request-level=20retry=20in?= =?UTF-8?q?=20favour=20of=20the=20control-plane=20fetch=20(after=20#598)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cloud-secrets-client.test.ts | 28 ++++++++++----- .../hosted-sandbox/hosted-sandbox-request.ts | 34 +------------------ 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/src/main/cloud-secrets/cloud-secrets-client.test.ts b/src/main/cloud-secrets/cloud-secrets-client.test.ts index 54c68298f37..eb0724fe430 100644 --- a/src/main/cloud-secrets/cloud-secrets-client.test.ts +++ b/src/main/cloud-secrets/cloud-secrets-client.test.ts @@ -1,4 +1,6 @@ import { describe, expect, it } from 'vitest' +import { EnvironmentProviderError } from '../environment-providers/environment-provider-driver' +import { createOrgCheckedFetch } from '../hosted-sandbox/hosted-sandbox-org-fetch' import { cloudSecretsFailureCode, createCloudSecretsClient } from './cloud-secrets-client' type Seen = { method: string; url: string; body: string | null; authorization: string | null } @@ -35,20 +37,28 @@ const VIEW = { describe('createCloudSecretsClient', () => { it('re-mints once on 401 stale_token (admin write window), and not for other 401s', async () => { + // The single re-mint path is the control-plane fetch (hosted-sandbox-org-fetch), as wired by + // cloud-secrets-ipc; the request layer itself never re-mints. const tokens = ['old', 'new'] let invalidated = 0 const seen: string[] = [] let answers: Response[] = [] + const getAccessToken = async (): Promise => tokens[Math.min(invalidated, 1)] ?? 'new' const client = createCloudSecretsClient({ baseUrl: 'https://control.test', - getAccessToken: async () => tokens[Math.min(invalidated, 1)]!, - invalidateAccessToken: () => { - invalidated++ - }, - fetch: async (_input, init) => { - seen.push(new Headers(init?.headers).get('authorization') ?? '') - return answers.shift()! - } + getAccessToken, + fetch: createOrgCheckedFetch({ + fetchImpl: async (_input, init) => { + seen.push(new Headers(init?.headers).get('authorization') ?? '') + return answers.shift() ?? Response.json({}, { status: 500 }) + }, + orgHeader: () => 'org-1', + getAccessToken, + invalidateAccessToken: () => { + invalidated++ + }, + orgSwitchError: () => new EnvironmentProviderError('conflict', 'switch failed') + }) }) answers = [ Response.json({ error: 'unauthorized', reason: 'stale_token' }, { status: 401 }), @@ -58,12 +68,14 @@ describe('createCloudSecretsClient', () => { expect(seen).toEqual(['Bearer old', 'Bearer new']) expect(invalidated).toBe(1) + // Still stale after the re-mint: exactly one re-mint, then the refusal surfaces. answers = [ Response.json({ error: 'unauthorized', reason: 'stale_token' }, { status: 401 }), Response.json({ error: 'unauthorized', reason: 'stale_token' }, { status: 401 }) ] await expect(client.remove({ scope: 'org' }, 'DATABASE_URL')).rejects.toThrow() expect(invalidated).toBe(2) + expect(seen).toHaveLength(4) answers = [Response.json({ error: 'unauthorized' }, { status: 401 })] await expect(client.remove({ scope: 'org' }, 'DATABASE_URL')).rejects.toThrow() diff --git a/src/main/hosted-sandbox/hosted-sandbox-request.ts b/src/main/hosted-sandbox/hosted-sandbox-request.ts index 57bf1f0cb8e..7ec3d4c0223 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-request.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-request.ts @@ -12,11 +12,6 @@ export type HostedSandboxControlPlaneClientOptions = { baseUrl: string /** Mints an Orca Cloud token with aud=orca-sandbox; never a provider credential. */ getAccessToken: (signal?: AbortSignal) => Promise - /** - * Drops the cached token. When set, a 401 `stale_token` (the token predates a role change, or is too - * old for an admin-only write) is retried once with a freshly minted token. - */ - invalidateAccessToken?: () => void fetch?: typeof fetch /** Per-call ceilings; create/resume/rollover include a VM boot and orcad start. */ timeouts?: Partial> @@ -46,7 +41,7 @@ export function createHostedSandboxRequest( const doFetch = options.fetch ?? fetch const timeouts = { ...DEFAULT_TIMEOUTS, ...options.timeouts } - const attempt = async function attempt( + return async function call( method: 'GET' | 'POST' | 'PUT' | 'DELETE', path: string, schema: z.ZodType, @@ -103,33 +98,6 @@ export function createHostedSandboxRequest( } return parsed.data } - - return async function call( - method: 'GET' | 'POST' | 'PUT' | 'DELETE', - path: string, - schema: z.ZodType, - init: Parameters[3] - ): Promise { - try { - return await attempt(method, path, schema, init) - } catch (error) { - if (!options.invalidateAccessToken || !isStaleToken(error)) { - throw error - } - // The control plane refused this token as older than a role change or the write window; the - // request did nothing, so one retry with a newly minted token is safe. - options.invalidateAccessToken() - return attempt(method, path, schema, init) - } - } -} - -function isStaleToken(error: unknown): boolean { - return ( - error instanceof HostedSandboxRefusalError && - error.httpStatus === 401 && - error.reason === 'stale_token' - ) } function parseBaseUrl(value: string): URL {