fix(sandbox-control): Codex broker review fixes (M1-M4, L5-L8)

- M1: a permanent refresh failure revokes only the login it refreshed (CAS on updated_at).
- M2: dead logins (refresh_token_expired/reused/invalidated) are never retried; a new login clears it.
- M3: the Codex account route only carries the token to /backend-api/codex/*, the plugin catalog and
  wham/usage (GET), seen live; plain segments only.
- M4: one atomic INSERT enforces one live broker per person (409 broker_in_progress) and 6 starts/hour;
  broker sandboxes use 2 vCPUs.
- L5: auth.json is read only after Codex exited 0.
- L6: the 401 hook respects the refresher's backoff and dead logins.
- L7: a cancel while the broker is starting wins: the sandbox goes and POST answers 409 cancelled.
- L8: orca-sandbox-env writes the marker before the swap, recognises any Orca placeholder, and never
  overwrites a login already set aside.
This commit is contained in:
Neil
2026-11-10 05:22:43 -08:00
parent 4af8f7fe04
commit 1bca45b536
8 changed files with 217 additions and 40 deletions
File diff suppressed because one or more lines are too long
@@ -128,17 +128,24 @@ codex_auth="$HOME/.codex/auth.json"
codex_saved="$HOME/.codex/auth.json.orca-saved"
codex_marker="$HOME/.codex/.orca-account-placeholder"
codex_ours=''
if [ -f "$codex_marker" ] && [ -f "$codex_auth" ] && grep -qF "\"$(cat "$codex_marker")\"" "$codex_auth"; then codex_ours=1; fi
# Ours: the marker's placeholder, or any Orca placeholder (all end in `.orca-placeholder`; a real token never does).
if [ -f "$codex_auth" ] && { { [ -f "$codex_marker" ] && grep -qF "\"$(cat "$codex_marker")\"" "$codex_auth"; } || grep -q '\.orca-placeholder"' "$codex_auth"; }; then
codex_ours=1
fi
if [ -n "$codex_placeholder" ]; then
mkdir -p "$HOME/.codex"
if [ -f "$codex_auth" ] && [ -z "$codex_ours" ]; then mv -f "$codex_auth" "$codex_saved"; fi
if [ -f "$codex_auth" ] && [ -z "$codex_ours" ]; then
# Never overwrite a login already set aside: a second one gets its own name.
if [ -e "$codex_saved" ]; then mv -f "$codex_auth" "$codex_saved.$(date -u +%Y%m%d%H%M%S)"; else mv -f "$codex_auth" "$codex_saved"; fi
fi
# Marker first: a run cut short after the swap still recognises the file as Orca's.
printf '%s' "$codex_placeholder" >"$codex_marker"
codex_tmp="$(mktemp "$HOME/.codex/.auth.XXXXXX")"
printf '{\n "OPENAI_API_KEY": null,\n "tokens": {\n "id_token": "%s",\n "access_token": "%s",\n "refresh_token": "",\n "account_id": null\n },\n "last_refresh": "%s"\n}\n' \
"$codex_placeholder" "$codex_placeholder" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" >"$codex_tmp"
chmod 600 "$codex_tmp"
mv -f "$codex_tmp" "$codex_auth"
printf '%s' "$codex_placeholder" >"$codex_marker"
elif [ -f "$codex_marker" ]; then
elif [ -f "$codex_marker" ] || [ -n "$codex_ours" ]; then
if [ -n "$codex_ours" ]; then rm -f "$codex_auth"; fi
rm -f "$codex_marker"
if [ -f "$codex_saved" ] && [ ! -f "$codex_auth" ]; then mv -f "$codex_saved" "$codex_auth"; fi
@@ -19,6 +19,8 @@ export type AccountProvider = {
header: string
placeholder(row: Pick<SandboxRow, 'id'>): string
format(credential: string): string
/** Narrows the account route to these requests (Codex: its ChatGPT backend APIs, not all of chatgpt.com). */
pathAllowed?(method: string, path: string): boolean
/** Extra headers derived from the real credential (e.g. Codex's ChatGPT account id). */
extraHeaders?(credential: string): Record<string, string>
/**
+37 -21
View File
@@ -10,7 +10,6 @@
import { storeCodexLogin, parseCodexAuthJson } from './codex-credentials'
import type { SandboxCaller } from './auth'
import { goldenFor, type LifecycleDeps } from './lifecycle'
import { SANDBOX_VCPUS } from './machine-size'
import { compileNetworkPolicy } from './network-policy'
import type { Keyring } from './secrets-crypto'
@@ -20,6 +19,8 @@ const BROKER_SANDBOX_TIMEOUT_MS = 20 * 60_000
/** A completion that died mid-way (Worker evicted) is retried after this. */
const COMPLETING_STALE_MS = 2 * 60_000
const STARTS_PER_HOUR = 6
/** A device login needs almost nothing: the smallest size (verified live in pdx1). */
const BROKER_VCPUS = 2
const START_TIMEOUT_MS = 60_000
/** Codex's device login talks to auth.openai.com only; chatgpt.com serves the post-login account checks. */
export const BROKER_EGRESS_HOSTS: readonly string[] = ['auth.openai.com', 'chatgpt.com']
@@ -65,7 +66,7 @@ export type BrokerRow = {
export class CodexBrokerError extends Error {
constructor(
readonly status: 400 | 404 | 429 | 502 | 503,
readonly status: 400 | 404 | 409 | 429 | 502 | 503,
readonly code: string
) {
super(code)
@@ -145,13 +146,6 @@ export async function startCodexBroker(
if (live?.verification_url && live.user_code) {
return { brokerId: live.id, verificationUrl: live.verification_url, userCode: live.user_code, expiresAt: live.expires_at }
}
const recent = await db
.prepare('SELECT COUNT(*) AS n FROM codex_broker_sessions WHERE user_id = ? AND created_at > ?')
.bind(caller.userId, now - 60 * 60_000)
.first<{ n: number }>()
if ((recent?.n ?? 0) >= STARTS_PER_HOUR) {
throw new CodexBrokerError(429, 'rate_limited')
}
const region = config.defaultRegion
const snapshotId = goldenFor(deps.life, region)
if (!snapshotId) {
@@ -173,19 +167,34 @@ export async function startCodexBroker(
expires_at: now + BROKER_CODE_TTL_MS,
destroyed_at: null
}
await db
// One statement decides both limits, so two racing POSTs can't both start a broker: at most one live
// broker per person (a second POST while one is still starting gets 409) and STARTS_PER_HOUR an hour.
const inserted = await db
.prepare(
`INSERT INTO codex_broker_sessions (id, user_id, account_id, provider_name, status, created_at, updated_at, expires_at)
VALUES (?, ?, ?, ?, 'starting', ?, ?, ?)`
SELECT ?, ?, ?, ?, 'starting', ?, ?, ?
WHERE NOT EXISTS (SELECT 1 FROM codex_broker_sessions WHERE user_id = ? AND destroyed_at IS NULL
AND status IN ('starting', 'pending', 'completing') AND expires_at > ?)
AND (SELECT COUNT(*) FROM codex_broker_sessions WHERE user_id = ? AND created_at > ?) < ?`
)
.bind(row.id, row.user_id, row.account_id, row.provider_name, now, now, row.expires_at)
.bind(row.id, row.user_id, row.account_id, row.provider_name, now, now, row.expires_at, caller.userId, now, caller.userId, now - 60 * 60_000, STARTS_PER_HOUR)
.run()
if (inserted.meta.changes !== 1) {
const busy = await db
.prepare(
`SELECT 1 AS hit FROM codex_broker_sessions WHERE user_id = ? AND destroyed_at IS NULL
AND status IN ('starting', 'pending', 'completing') AND expires_at > ?`
)
.bind(caller.userId, now)
.first<{ hit: number }>()
throw busy ? new CodexBrokerError(409, 'broker_in_progress') : new CodexBrokerError(429, 'rate_limited')
}
let output: string
try {
const state = await provider.createSandbox({
name: row.provider_name,
snapshotId,
vcpus: SANDBOX_VCPUS,
vcpus: BROKER_VCPUS,
region,
timeoutMs: BROKER_SANDBOX_TIMEOUT_MS,
ports: [],
@@ -211,7 +220,11 @@ export async function startCodexBroker(
await fail(deps, row, reason)
throw new CodexBrokerError(502, reason)
}
await update(db, id, { status: 'pending', verification_url: code.verificationUrl, user_code: code.userCode }, deps.life.now(), 'starting')
if (!(await update(db, id, { status: 'pending', verification_url: code.verificationUrl, user_code: code.userCode }, deps.life.now(), 'starting'))) {
// Cancelled while it was starting: the code is never handed out.
await destroy(deps, row)
throw new CodexBrokerError(409, 'cancelled')
}
log('codex_broker_started', { brokerId: id, userId: caller.userId, ms: deps.life.now() - now })
return { brokerId: id, ...code, expiresAt: row.expires_at }
}
@@ -239,21 +252,24 @@ export async function advanceCodexBroker(deps: BrokerDeps, row: BrokerRow): Prom
if (!row.session_id) {
return fail(deps, row, 'broker_lost')
}
let auth: string | null
// Codex writes auth.json before it exits; only an exit status of 0 means the file is complete and final.
let exit: string | null
let auth: string | null = null
try {
auth = await provider.readFile(row.session_id, BROKER_AUTH_PATH)
exit = auth ? null : await provider.readFile(row.session_id, BROKER_EXIT_PATH)
exit = await provider.readFile(row.session_id, BROKER_EXIT_PATH)
if (exit?.trim() === '0') {
auth = await provider.readFile(row.session_id, BROKER_AUTH_PATH)
}
} catch {
// The broker sandbox is gone (timed out or deleted) before the login finished.
return now > row.expires_at ? fail(deps, row, 'expired') : fail(deps, row, 'broker_lost')
}
if (!auth) {
if (exit !== null) {
return fail(deps, row, 'login_failed')
}
if (exit === null) {
return now > row.expires_at ? fail(deps, row, 'expired') : row
}
if (!auth) {
return fail(deps, row, 'login_failed')
}
// Claim the completion: exactly one caller stores this login (a second store would put back a refresh
// token the refresher may already have spent).
if (!(await update(db, row.id, { status: 'completing' }, now, row.status))) {
@@ -11,7 +11,7 @@
// (that is what `refresh_token_reused` punishes).
import { decodeJwt } from 'jose'
import type { AccountProvider } from './account-providers'
import { credentialBinding, markAgentCredentialRevoked, putAgentCredential, readUsableAgentCredential } from './agent-credentials'
import { credentialBinding, putAgentCredential, readUsableAgentCredential } from './agent-credentials'
import type { Keyring } from './secrets-crypto'
import type { SandboxRow } from './store'
@@ -118,6 +118,32 @@ function formatCodex(credential: string): string {
return `Bearer ${stored ? stored.access_token : credential}`
}
/**
* The ChatGPT backend calls Codex makes with its login (seen live, Codex 0.166): the Codex API itself, its
* plugin catalog and usage. Nothing else on chatgpt.com (conversations, memories, connectors) gets the token.
*/
const CODEX_PREFIXES = ['/backend-api/codex/']
const CODEX_EXACT_GETS = new Set([
'/backend-api/ps/plugins/list',
'/backend-api/ps/plugins/installed',
'/backend-api/ps/plugins/suggested/codex',
'/backend-api/plugins/featured',
'/backend-api/plugins/export/curated',
'/backend-api/wham/usage'
])
export function codexPathAllowed(method: string, path: string): boolean {
const pathname = path.split('?')[0] ?? ''
// Plain segments only: no encoded or dot segments that could walk out of an allowed prefix.
if (!/^(\/[A-Za-z0-9_~-][A-Za-z0-9._~-]*)+\/?$/.test(pathname) || /(^|\/)\.\.?(\/|$)/.test(pathname)) {
return false
}
if (CODEX_PREFIXES.some((prefix) => pathname.startsWith(prefix))) {
return true
}
return method.toUpperCase() === 'GET' && CODEX_EXACT_GETS.has(pathname)
}
export const codexAccount: AccountProvider = {
agent: 'codex',
hosts: CODEX_ACCOUNT_HOSTS,
@@ -125,6 +151,7 @@ export const codexAccount: AccountProvider = {
sessionEnv: (row) => ({ [CODEX_PLACEHOLDER_ENV]: codexPlaceholder(row) }),
header: 'authorization',
format: formatCodex,
pathAllowed: codexPathAllowed,
// Codex sends its ChatGPT workspace id with a real login; the placeholder file can't know it.
extraHeaders: (credential): Record<string, string> => {
const account = credential.startsWith('{') ? parseStoredCodex(credential)?.account_id : null
@@ -133,8 +160,15 @@ export const codexAccount: AccountProvider = {
// A 401 from chatgpt.com: refresh now instead of revoking (the refresher revokes a dead login itself).
// At most once per UNAUTHORIZED_REFRESH_MS, so a burst of 401s never spends token after token.
onUnauthorized: async ({ db, keyring, userId, now, waitUntil }) => {
const lease = await db.prepare('SELECT refreshed_at FROM codex_leases WHERE user_id = ?').bind(userId).first<{ refreshed_at: number | null }>()
if (lease?.refreshed_at && now - lease.refreshed_at < UNAUTHORIZED_REFRESH_MS) {
const lease = await db
.prepare('SELECT refreshed_at, next_attempt_at, last_error FROM codex_leases WHERE user_id = ?')
.bind(userId)
.first<{ refreshed_at: number | null; next_attempt_at: number | null; last_error: string | null }>()
if (
(lease?.refreshed_at && now - lease.refreshed_at < UNAUTHORIZED_REFRESH_MS) ||
(lease?.next_attempt_at && lease.next_attempt_at > now) ||
(lease?.last_error && DEAD_LOGIN.has(lease.last_error))
) {
return
}
log('codex_token_unauthorized', { userId })
@@ -206,6 +240,7 @@ async function dueUsers(db: D1Database, now: number): Promise<string[]> {
JOIN agent_credentials c ON c.user_id = l.user_id AND c.agent = 'codex' AND c.state = 'active'
WHERE (l.access_expires_at IS NULL OR l.access_expires_at <= ? OR l.refreshed_at IS NULL OR l.refreshed_at <= ?)
AND (l.next_attempt_at IS NULL OR l.next_attempt_at <= ?)
AND (l.last_error IS NULL OR l.last_error NOT IN (${DEAD_SQL}))
AND (l.lease_until IS NULL OR l.lease_until <= ?)
ORDER BY l.access_expires_at LIMIT ?`
)
@@ -267,8 +302,18 @@ async function setExpiry(db: D1Database, userId: string, expectedUpdatedAt: numb
.run()
}
async function revokeIf(db: D1Database, userId: string, expectedUpdatedAt: number, now: number): Promise<void> {
await db
.prepare(`UPDATE agent_credentials SET state = 'revoked', updated_at = ? WHERE user_id = ? AND agent = 'codex' AND updated_at = ?`)
.bind(Math.max(now, expectedUpdatedAt + 1), userId, expectedUpdatedAt)
.run()
}
/** Token-endpoint error codes that end a login for good (the person must sign in again). */
const DEAD_LOGIN = new Set(['refresh_token_expired', 'refresh_token_reused', 'refresh_token_invalidated', 'invalid_grant'])
const DEAD_LOGIN_CODES = ['refresh_token_expired', 'refresh_token_reused', 'refresh_token_invalidated', 'invalid_grant'] as const
const DEAD_LOGIN: ReadonlySet<string> = new Set(DEAD_LOGIN_CODES)
/** SQL list of the codes above: a dead login is never retried (a new broker login clears last_error). */
const DEAD_SQL = DEAD_LOGIN_CODES.map((code) => `'${code}'`).join(', ')
export type TokenResult =
| { ok: true; tokens: Partial<CodexTokens> }
@@ -348,10 +393,11 @@ export async function refreshCodexCredential(deps: RefreshDeps, userId: string):
const lease = await db.prepare('SELECT * FROM codex_leases WHERE user_id = ?').bind(userId).first<LeaseRow>()
const failures = (lease?.failures ?? 0) + 1
if (result.permanent) {
// Only the login that was refreshed: a newer broker login written meanwhile stays untouched.
if (result.code === 'refresh_token_expired') {
await setExpiry(db, userId, snapshot.updatedAt, now)
} else {
await markAgentCredentialRevoked(db, userId, 'codex', now)
await revokeIf(db, userId, snapshot.updatedAt, now)
}
await release(db, userId, owner, { failures, last_error: result.code, next_attempt_at: null })
log('codex_token_dead', { userId, code: result.code })
+1
View File
@@ -323,6 +323,7 @@ function egressRoutes(env: Env, deps: Deps): (row: SandboxRow, host: string) =>
header: provider.header,
placeholders: [provider.format(provider.placeholder(row))],
label: `account:${agent}`,
...(provider.pathAllowed ? { pathAllowed: provider.pathAllowed } : {}),
credential: async () => {
const now = (deps.now ?? Date.now)()
// The session was set up for this owner; a workspace handed to someone else never uses
@@ -32,6 +32,12 @@ Follow these steps to sign in with ChatGPT using device code authorization:
\u001b[94mABCD-12EF\u001b[0m
`
/** Codex wrote auth.json and exited 0. */
function finishLogin(box: { files: Map<string, string> }) {
box.files.set(BROKER_AUTH_PATH, AUTH_JSON)
box.files.set(BROKER_EXIT_PATH, '0\n')
}
let logs: string[] = []
beforeEach(() => {
logs = []
@@ -91,7 +97,7 @@ async function setup(overrides: Record<string, string> = {}) {
const as = (sub: string, org?: string, role?: string) => h.token(sub, org, 'sandbox', role ? { orgRole: role } : {})
const brokerBox = () => [...h.vercel.boxes.values()].find((box) => box.params.tags.orcaCodexBroker)
const lastEnv = (command: string) => decode(h.vercel.commands.filter((c) => c.command === command).at(-1)?.env.ORCA_SECRET_ENV)
async function proxied(sandboxId: string, host: string, headers: Record<string, string>) {
async function proxied(sandboxId: string, host: string, headers: Record<string, string>, path = '/backend-api/codex/responses', method = 'POST') {
const oidc = await new SignJWT({ team_id: 'team_orca', project_id: 'prj_orca', sandbox_name: `orca-test-${sandboxId}` })
.setProtectedHeader({ alg: 'RS256', kid: 'v1' })
.setIssuer('https://oidc.vercel.com/team_orca')
@@ -100,14 +106,14 @@ async function setup(overrides: Record<string, string> = {}) {
.setExpirationTime('5m')
.sign(privateKey)
const response = await h.send(
new Request(`${PROXY_URL}/backend-api/codex/responses`, {
method: 'POST',
body: '{}',
new Request(`${PROXY_URL}${path}`, {
method,
body: method === 'GET' ? undefined : '{}',
headers: {
'vercel-forwarded-host': host,
'vercel-forwarded-scheme': 'https',
'vercel-forwarded-port': '443',
'vercel-forwarded-path': '/backend-api/codex/responses',
'vercel-forwarded-path': path,
'vercel-sandbox-oidc-token': oidc,
...headers
}
@@ -119,7 +125,7 @@ async function setup(overrides: Record<string, string> = {}) {
async function signIn(bearer: string) {
const started = await h.call('POST', '/v1/agent-credentials/codex/broker', { bearer })
expect(started.status).toBe(200)
brokerBox()!.files.set(BROKER_AUTH_PATH, AUTH_JSON)
finishLogin(brokerBox()!)
const done = await h.call('GET', `/v1/agent-credentials/codex/broker/${started.body.brokerId}`, { bearer })
expect(done.body).toEqual({ status: 'active' })
return started.body.brokerId as string
@@ -175,6 +181,7 @@ describe('Codex broker (flag S2)', () => {
// Infra: tagged for its own sweep, no workspace tag (orphan sweep, billing), no snapshot, only sign-in hosts.
expect(box.params.tags).toEqual({ orcaCodexBroker: started.body.brokerId, orcaEnv: 'test', orcaInfra: 'codex-broker' })
expect(box.params.persistent).toBe(false)
expect(box.params.vcpus).toBe(2)
expect(Object.keys(box.policy.allow).sort()).toEqual(['auth.openai.com', 'chatgpt.com'])
expect((await h.env.SANDBOX_DB.prepare('SELECT COUNT(*) AS n FROM sandboxes').first<{ n: number }>())!.n).toBe(0)
// A second POST while the code is live reuses it (no second VM).
@@ -186,7 +193,10 @@ describe('Codex broker (flag S2)', () => {
// Someone else's broker id is not theirs to read.
expect((await h.call('GET', path, { bearer: await as('bob', 'acme', 'member') })).status).toBe(404)
// auth.json alone (Codex still running) is not a finished login.
box.files.set(BROKER_AUTH_PATH, AUTH_JSON)
expect((await h.call('GET', path, { bearer: mia })).body).toEqual({ status: 'pending' })
finishLogin(box)
const done = await h.call('GET', path, { bearer: mia })
expect(done.body).toEqual({ status: 'active' })
expect(brokerBox()).toBeUndefined()
@@ -236,11 +246,51 @@ describe('Codex broker (flag S2)', () => {
expect(brokerBox()).toBeUndefined()
})
it('one live broker per person, even for racing POSTs; a cancel while starting never hands out the code', async () => {
const { h, as } = await setup()
const mia = await as('mia')
const [a, b] = await Promise.all([
h.call('POST', '/v1/agent-credentials/codex/broker', { bearer: mia }),
h.call('POST', '/v1/agent-credentials/codex/broker', { bearer: mia })
])
// Either the second sees the first's code (reused) or is refused while it starts; never two VMs.
expect(a.status === 409 || b.status === 409 || a.body.brokerId === b.body.brokerId).toBe(true)
expect([...h.vercel.boxes.values()].filter((box) => box.params.tags.orcaCodexBroker)).toHaveLength(1)
const live = a.status === 200 ? a : b
// Another POST while one is still starting (no code yet) is refused, not a second VM.
h.raw.exec(`UPDATE codex_broker_sessions SET status = 'starting'`)
expect(await h.call('POST', '/v1/agent-credentials/codex/broker', { bearer: mia })).toEqual({ status: 409, body: { error: 'broker_in_progress' } })
h.raw.exec(`UPDATE codex_broker_sessions SET status = 'pending'`)
await h.call('DELETE', `/v1/agent-credentials/codex/broker/${live.body.brokerId}`, { bearer: mia })
// The person cancels while the broker is still starting (between create and the device code).
h.vercel.commandHandler = (params) => {
if (params.command === 'bash' && params.args?.[1]?.includes('--device-auth')) {
h.raw.exec(`UPDATE codex_broker_sessions SET status = 'failed', error = 'cancelled' WHERE status = 'starting'`)
return { exitCode: 0, stdout: DEVICE_OUTPUT, stderr: '' }
}
return undefined
}
const raced = await h.call('POST', '/v1/agent-credentials/codex/broker', { bearer: await as('mia') })
expect(raced).toEqual({ status: 409, body: { error: 'cancelled' } })
expect([...h.vercel.boxes.values()].filter((box) => box.params.tags.orcaCodexBroker)).toHaveLength(0)
// At most 6 starts an hour per person (2 so far).
h.vercel.commandHandler = (params) =>
params.command === 'bash' && params.args?.[1]?.includes('--device-auth') ? { exitCode: 0, stdout: DEVICE_OUTPUT, stderr: '' } : undefined
for (let i = 0; i < 4; i++) {
const started = await h.call('POST', '/v1/agent-credentials/codex/broker', { bearer: mia })
expect(started.status).toBe(200)
await h.call('DELETE', `/v1/agent-credentials/codex/broker/${started.body.brokerId}`, { bearer: mia })
}
expect(await h.call('POST', '/v1/agent-credentials/codex/broker', { bearer: mia })).toEqual({ status: 429, body: { error: 'rate_limited' } })
})
it('the cron completes a login nobody polls, and deletes broker sandboxes D1 lost track of', async () => {
const { h, as, brokerBox } = await setup()
const mia = await as('mia')
await h.call('POST', '/v1/agent-credentials/codex/broker', { bearer: mia })
brokerBox()!.files.set(BROKER_AUTH_PATH, AUTH_JSON)
finishLogin(brokerBox()!)
await codexTick(h.env, false, h.deps)
expect(brokerBox()).toBeUndefined()
expect((await h.call('GET', '/v1/agent-credentials', { bearer: mia })).body.codex.state).toBe('active')
@@ -273,6 +323,21 @@ describe('Codex account in workspaces (D10, D13)', () => {
expect(upstream.at(-1)!.url).toBe('https://chatgpt.com/backend-api/codex/responses')
expect(upstream.at(-1)!.headers.get('authorization')).toBe(`Bearer ${ACCESS}`)
expect(upstream.at(-1)!.headers.get('chatgpt-account-id')).toBe('acct-1')
// Only Codex's own ChatGPT backend calls get the token: not conversations, memories or connectors.
const auth = { authorization: `Bearer ${placeholder}` }
expect((await proxied(id, 'chatgpt.com', auth, '/backend-api/codex/models?client_version=0.166.0', 'GET')).status).toBe(200)
expect((await proxied(id, 'chatgpt.com', auth, '/backend-api/wham/usage', 'GET')).status).toBe(200)
expect((await proxied(id, 'chatgpt.com', auth, '/backend-api/ps/plugins/list?scope=GLOBAL', 'GET')).status).toBe(200)
for (const [path, method] of [
['/backend-api/conversation', 'POST'],
['/backend-api/memories', 'GET'],
['/backend-api/wham/apps', 'GET'],
['/backend-api/codex/../conversation', 'POST'],
['/backend-api/codex/%2e%2e/conversation', 'POST'],
['/backend-api/wham/usage', 'POST']
]) {
expect((await proxied(id, 'chatgpt.com', auth, path!, method!)).body).toBe('path_not_allowed')
}
// Another sandbox's placeholder (a clone's, D14) is not this one's.
expect((await proxied(id, 'chatgpt.com', { authorization: `Bearer ${codexPlaceholder({ id: 'sbx_other' })}` })).status).toBe(403)
})
@@ -356,6 +421,27 @@ describe('Codex lease refresher', () => {
expect(await refreshCodexCredential(await refreshDeps(), 'mia')).toBe('revoked')
expect(await view()).toBe('revoked')
// A dead login (refresh_token_expired) is not retried every tick.
await signIn(await as('mia'))
h.advance(8 * 86_400_000)
setTokenReply(() => Response.json({ error: 'refresh_token_expired' }, { status: 401 }))
expect(await refreshCodexCredential(await refreshDeps(), 'mia')).toBe('revoked')
expect(await view()).toBe('expired')
h.advance(10 * 60_000)
expect((await refreshCodexLeases(await refreshDeps())).due).toBe(0)
// A permanent failure never revokes a newer login written while the refresh was in flight.
await signIn(await as('mia'))
h.advance(8 * 86_400_000)
setTokenReply(() => Response.json({ error: { code: 'refresh_token_reused' } }, { status: 401 }))
const racing = await refreshDeps()
const newerLogin: typeof fetch = async (input, init) => {
await h.env.SANDBOX_DB.prepare(`UPDATE agent_credentials SET updated_at = updated_at + 5 WHERE agent = 'codex'`).run()
return racing.fetch!(input, init)
}
expect(await refreshCodexCredential({ ...racing, fetch: newerLogin }, 'mia')).toBe('revoked')
expect(await view()).toBe('active')
// A new login, then DELETE while a refresh is in flight: the refreshed tokens are dropped.
await signIn(await as('mia'))
h.advance(8 * 86_400_000)
@@ -1,7 +1,7 @@
// image/vm/orca-sandbox-env run for real (bash), against a scratch HOME: the Codex account placeholder
// login (contract D11) and the managed ~/.codex/config.toml staying stable across refreshes.
import { execFileSync } from 'node:child_process'
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync, mkdirSync } from 'node:fs'
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
@@ -66,11 +66,30 @@ describe('orca-sandbox-env: Codex via the Orca account', () => {
expect(file('.codex/config.toml')).not.toContain('orca_chatgpt')
})
it('never overwrites a login already set aside, and recognises its own placeholder without the marker', () => {
const { home, run, file } = scratch()
mkdirSync(join(home, '.codex'))
const saved = '{"tokens":{"access_token":"first"}}\n'
const second = '{"tokens":{"access_token":"second"}}\n'
writeFileSync(join(home, '.codex', 'auth.json.orca-saved'), saved)
writeFileSync(join(home, '.codex', 'auth.json'), second)
run({ ORCA_CODEX_AUTH_PLACEHOLDER: codexPlaceholder({ id: 'sbx_1' }) })
expect(file('.codex/auth.json.orca-saved')).toBe(saved)
const aside = readdirSync(join(home, '.codex')).filter((name) => name.startsWith('auth.json.orca-saved.'))
expect(aside.map((name) => file(`.codex/${name}`))).toEqual([second])
// A run cut short before the marker: the placeholder is still Orca's, never set aside as a login.
rmSync(join(home, '.codex', '.orca-account-placeholder'))
run({ ORCA_CODEX_AUTH_PLACEHOLDER: codexPlaceholder({ id: 'sbx_2' }) })
expect(file('.codex/auth.json.orca-saved')).toBe(saved)
run({})
expect(file('.codex/auth.json')).toBe(saved)
})
it('leaves config.toml byte-identical across refreshes (no blank-line growth) and keeps user settings', () => {
const { home, run, file } = scratch()
mkdirSync(join(home, '.codex'))
writeFileSync(join(home, '.codex', 'config.toml'), 'model = "gpt-x"\n\n[profiles.mine]\nmodel = "y"\n')
for (const secrets of [{ ORCA_CODEX_AUTH_PLACEHOLDER: codexPlaceholder({ id: 'sbx_1' }) }, { OPENAI_BASE_URL: 'https://gw.example.com/v1' }, {}]) {
for (const secrets of [{ ORCA_CODEX_AUTH_PLACEHOLDER: codexPlaceholder({ id: 'sbx_1' }) }, { OPENAI_BASE_URL: 'https://gw.example.com/v1' }, {}] as Record<string, string>[]) {
run(secrets)
const first = file('.codex/config.toml')
run(secrets)