mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
refactor(cloud): one stale_token re-mint path — drop the request-level retry in favour of the control-plane fetch (after #598)
This commit is contained in:
@@ -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<string> => 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()
|
||||
|
||||
@@ -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<string>
|
||||
/**
|
||||
* 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<Record<'boot' | 'control', number>>
|
||||
@@ -46,7 +41,7 @@ export function createHostedSandboxRequest(
|
||||
const doFetch = options.fetch ?? fetch
|
||||
const timeouts = { ...DEFAULT_TIMEOUTS, ...options.timeouts }
|
||||
|
||||
const attempt = async function attempt<T>(
|
||||
return async function call<T>(
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
||||
path: string,
|
||||
schema: z.ZodType<T>,
|
||||
@@ -103,33 +98,6 @@ export function createHostedSandboxRequest(
|
||||
}
|
||||
return parsed.data
|
||||
}
|
||||
|
||||
return async function call<T>(
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
|
||||
path: string,
|
||||
schema: z.ZodType<T>,
|
||||
init: Parameters<HostedSandboxRequest>[3]
|
||||
): Promise<T> {
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user