Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(codex): complete the Desktop Responses stream path
Alfredo Lopez01c25ab4b15c
diff --git a/codex-llm-proxy/src/chatgptUpstreamHeaders.ts b/codex-llm-proxy/src/chatgptUpstreamHeaders.tsnew file mode 100644index 000000000..21d796e17--- /dev/null+++ b/codex-llm-proxy/src/chatgptUpstreamHeaders.ts@@ -0,0 +1,51 @@+const CHATGPT_AUTH_CLAIM = 'https://api.openai.com/auth'++/** Identifies this Evenfire client; do not impersonate `codex_cli_rs`. */+export const CODEX_UPSTREAM_ORIGINATOR = 'evenfire'+export const CODEX_UPSTREAM_VERSION = '1.0.0'+export const CODEX_OPENAI_BETA = 'responses=v1'++export function chatgptAccountIdFromAccessToken(accessToken: string): string | undefined {+ const parts = accessToken.split('.')+ if (parts.length < 2) return undefined+ try {+ const payload = JSON.parse(Buffer.from(parts[1]!, 'base64url').toString('utf8')) as Record<+ string,+ unknown+ >+ const nested = payload[CHATGPT_AUTH_CLAIM]+ if (nested && typeof nested === 'object') {+ const accountId = (nested as { chatgpt_account_id?: unknown }).chatgpt_account_id+ if (typeof accountId === 'string' && accountId.trim()) return accountId.trim()+ }+ if (typeof payload.chatgpt_account_id === 'string' && payload.chatgpt_account_id.trim()) {+ return payload.chatgpt_account_id.trim()+ }+ const auth = payload.auth+ if (auth && typeof auth === 'object') {+ const accountId = (auth as { chatgpt_account_id?: unknown }).chatgpt_account_id+ if (typeof accountId === 'string' && accountId.trim()) return accountId.trim()+ }+ } catch {+ return undefined+ }+ return undefined+}++export function chatgptUpstreamHeaders(+ accessToken: string,+ extra: Record<string, string> = {}+): Record<string, string> {+ const headers: Record<string, string> = {+ authorization: `Bearer ${accessToken}`,+ originator: CODEX_UPSTREAM_ORIGINATOR,+ 'user-agent': 'evenfire-codex-subscription',+ 'oai-product-sku': 'codex',+ 'openai-beta': CODEX_OPENAI_BETA,+ version: CODEX_UPSTREAM_VERSION,+ ...extra,+ }+ const accountId = chatgptAccountIdFromAccessToken(accessToken)+ if (accountId) headers['chatgpt-account-id'] = accountId+ return headers+}diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.tsindex e986253c9..ab6945112 100644--- a/codex-llm-proxy/src/codexTransport.ts+++ b/codex-llm-proxy/src/codexTransport.ts@@ -10,12 +10,12 @@ import {CODEX_COMPLETIONS_ORIGIN,OriginDeniedError,assertAllowedUpstreamUrl,- assertRedirectLocation,- assertResolvedUpstream,+ fetchFrozenOrigin,type OriginPolicyOptions,} from './originPolicy.js'import { assertBoundedDeadline } from './requestLimits.js'import { parseSafeUsage, type SafeUsage } from './usage.js'+import { chatgptUpstreamHeaders } from './chatgptUpstreamHeaders.js'export type StreamFrame =| { type: 'text'; text: string }@@ -111,6 +111,7 @@ export async function streamCodexCompletion(const streamed = await readUpstreamStream({request,accessToken,+ chatgptAccountId: redeemed.chatgptAccountId,deadlineMs,signal: input.signal,fetchFn: input.fetchFn,@@ -179,6 +180,7 @@ async function finalizeQuietly(async function readUpstreamStream(input: {request: CodexCompletionRequestV1accessToken: string+ chatgptAccountId?: stringdeadlineMs: numbersignal?: AbortSignalfetchFn: typeof fetch@@ -186,48 +188,116 @@ async function readUpstreamStream(input: {onFrame?: (frame: StreamFrame) => void}): Promise<StreamCodexCompletionResult> {const url = assertAllowedUpstreamUrl(CODEX_COMPLETIONS_ORIGIN, 'completions')- await assertResolvedUpstream(url, input.lookup)const timeout = AbortSignal.timeout(input.deadlineMs)const signal = input.signal ? AbortSignal.any([input.signal, timeout]) : timeout- const response = await input.fetchFn(url.href, {- method: 'POST',- redirect: 'manual',- signal,- headers: {- authorization: `Bearer ${input.accessToken}`,- 'content-type': 'application/json',- accept: 'text/event-stream',- },- body: JSON.stringify(toUpstreamPayload(input.request)),+ const headers = chatgptUpstreamHeaders(input.accessToken, {+ 'content-type': 'application/json',+ accept: 'text/event-stream',+ session_id: input.request.requestId,+ ...(input.chatgptAccountId ? { 'chatgpt-account-id': input.chatgptAccountId } : {}),})- if (response.status >= 300 && response.status < 400) {- const location = response.headers.get('location') || ''- assertRedirectLocation(location, url)- throw new OriginDeniedError('origin_denied')+ if (!headers['chatgpt-account-id']) {+ throw new CodexTransportError(+ 'connection_unavailable',+ 'Codex access token is missing ChatGPT account id'+ )}+ const response = await fetchFrozenOrigin({+ url,+ fetchFn: input.fetchFn,+ lookup: input.lookup,+ init: {+ method: 'POST',+ signal,+ headers,+ body: JSON.stringify(toUpstreamPayload(input.request)),+ },+ })if (!response.ok || !response.body) {+ logger.warn(+ {+ event: 'codex_upstream_http',+ operation: 'completion_stream',+ status: response.status,+ accountHeader: Boolean(headers['chatgpt-account-id']),+ },+ 'Codex completions upstream returned a non-success status'+ )+ if (response.status === 400) {+ throw new CodexTransportError('invalid_request', 'upstream rejected the Codex request')+ }+ if (response.status === 401 || response.status === 403) {+ throw new CodexTransportError(+ 'connection_unavailable',+ 'upstream rejected the Codex credential'+ )+ }throw new CodexTransportError('provider_unavailable', 'upstream completion failed')}return consumeSse(response.body, input.onFrame, signal)}function toUpstreamPayload(request: CodexCompletionRequestV1): Record<string, unknown> {- return {+ const instructions = request.messages+ .filter(message => message.role === 'system' && message.content.trim())+ .map(message => message.content)+ .join('\n\n')+ const input: Record<string, unknown>[] = []+ for (const message of request.messages) {+ if (message.role === 'system') continue+ if (message.role === 'tool') {+ input.push({+ type: 'function_call_output',+ call_id: message.toolCallId || message.name || 'tool',+ output: message.content,+ })+ continue+ }+ if (message.role === 'assistant' && message.toolCalls && message.toolCalls.length > 0) {+ if (message.content.trim()) {+ input.push({ role: 'assistant', content: message.content })+ }+ for (const call of message.toolCalls) {+ input.push({+ type: 'function_call',+ call_id: call.id,+ name: call.name,+ arguments: JSON.stringify(call.arguments ?? {}),+ })+ }+ continue+ }+ input.push({ role: message.role, content: message.content })+ }+ const payload: Record<string, unknown> = {model: request.model,stream: true,- input: request.messages.map(message => ({- role: message.role,- content: message.content,- })),- tools: request.tools?.map(tool => ({+ store: false,+ input,+ }+ if (instructions) payload.instructions = instructions+ if (request.tools && request.tools.length > 0) {+ payload.tools = request.tools.map(tool => ({type: 'function',name: tool.name,description: tool.description,parameters: tool.parameters,- })),- max_output_tokens: request.generation?.maxOutputTokens,- temperature: request.generation?.temperature,+ }))+ payload.parallel_tool_calls = true+ }+ if (request.generation?.maxOutputTokens !== undefined) {+ payload.max_output_tokens = request.generation.maxOutputTokens+ }+ if (request.generation?.temperature !== undefined) {+ payload.temperature = request.generation.temperature+ }+ if (request.generation?.toolChoice) {+ payload.tool_choice = request.generation.toolChoice+ }+ if (request.transportHints?.promptCacheKey) {+ payload.prompt_cache_key = request.transportHints.promptCacheKey}+ return payload}async function consumeSse(@@ -237,8 +307,10 @@ async function consumeSse(): Promise<StreamCodexCompletionResult> {const reader = body.getReader()const decoder = new TextDecoder()+ const pending = new Map<string, PendingToolCall>()let buffer = ''let completed = false+ let failed = falselet usage: SafeUsage | undefinedtry {while (!signal.aborted) {@@ -261,25 +333,46 @@ async function consumeSse(} catch {continue}- const mapped = mapUpstreamEvent(parsed)+ const mapped = mapUpstreamEvent(parsed, pending)if (mapped.frame) onFrame?.(mapped.frame)if (mapped.usage) usage = mapped.usageif (mapped.completed) completed = true+ if (mapped.failed) failed = true}if (signal.aborted) break}} finally {reader.releaseLock()}+ for (const call of pending.values()) {+ if (call.emitted) continue+ const args = parseToolArguments(call.arguments)+ onFrame?.({ type: 'tool_call', id: call.id, name: call.name, arguments: args })+ call.emitted = true+ }if (signal.aborted) return { outcome: 'canceled', usage }+ if (failed) {+ throw new CodexTransportError('provider_unavailable', 'upstream response failed')+ }if (completed) return { outcome: 'success', usage }return { outcome: 'unknown', usage }}-function mapUpstreamEvent(event: unknown): {+type PendingToolCall = {+ id: string+ name: string+ arguments: string+ emitted: boolean+}++function mapUpstreamEvent(+ event: unknown,+ pending: Map<string, PendingToolCall>+): {frame?: StreamFrameusage?: SafeUsagecompleted?: boolean+ failed?: boolean} {if (!event || typeof event !== 'object') return {}const row = event as Record<string, unknown>@@ -288,23 +381,84 @@ function mapUpstreamEvent(event: unknown): {return { frame: { type: 'text', text: row.delta } }}if (type === 'response.output_item.added' && isPlainObject(row.item) && row.item.type === 'function_call') {- const args = parseToolArguments(row.item.arguments)- return {- frame: {- type: 'tool_call',- id: String(row.item.id || 'tool'),- name: String(row.item.name || 'tool'),- arguments: args,- },- }+ const call = upsertPendingTool(pending, row.item)+ if (isCompleteJson(call.arguments)) return { frame: emitToolCall(call) }+ return {}+ }+ if (type === 'response.function_call_arguments.delta') {+ upsertPendingTool(pending, {+ id: row.item_id,+ item_id: row.item_id,+ arguments: typeof row.delta === 'string' ? row.delta : '',+ append: true,+ })+ return {}+ }+ if (+ type === 'response.function_call_arguments.done' ||+ (type === 'response.output_item.done' && isPlainObject(row.item) && row.item.type === 'function_call')+ ) {+ const source = isPlainObject(row.item) ? row.item : row+ const call = upsertPendingTool(pending, source)+ if (call && !call.emitted) return { frame: emitToolCall(call) }+ return {}}if (type === 'response.completed') {const response = isPlainObject(row.response) ? row.response : rowreturn { completed: true, usage: parseSafeUsage(response.usage) }}+ if (type === 'response.failed' || type === 'response.incomplete' || type === 'error') {+ return { failed: true }+ }return {}}+function upsertPendingTool(+ pending: Map<string, PendingToolCall>,+ source: Record<string, unknown> & { append?: boolean }+): PendingToolCall {+ const key = String(source.item_id || source.id || source.call_id || 'tool')+ const current =+ pending.get(key) ??+ ({+ id: String(source.call_id || source.id || key),+ name: 'tool',+ arguments: '',+ emitted: false,+ } satisfies PendingToolCall)+ if (typeof source.call_id === 'string' && source.call_id.trim()) current.id = source.call_id+ if (typeof source.name === 'string' && source.name.trim()) current.name = source.name+ const rawArgs = source.arguments+ if (typeof rawArgs === 'string') {+ current.arguments = source.append ? `${current.arguments}${rawArgs}` : rawArgs || current.arguments+ } else if (isPlainObject(rawArgs) && !source.append) {+ current.arguments = JSON.stringify(rawArgs)+ }+ pending.set(key, current)+ return current+}++function emitToolCall(call: PendingToolCall): StreamFrame {+ call.emitted = true+ return {+ type: 'tool_call',+ id: call.id,+ name: call.name,+ arguments: parseToolArguments(call.arguments),+ }+}++function isCompleteJson(raw: string): boolean {+ const trimmed = raw.trim()+ if (!trimmed) return false+ try {+ JSON.parse(trimmed)+ return true+ } catch {+ return false+ }+}+function parseToolArguments(raw: unknown): Record<string, unknown> {if (isPlainObject(raw)) return rawif (typeof raw !== 'string' || raw.length === 0) return {}@@ -326,21 +480,28 @@ export async function listCodexModels(input: {lookup?: OriginPolicyOptions['lookup']}): Promise<{ outcome: 'ready' | 'auth-rejected' | 'unavailable'; models: Array<{ model: string; displayName?: string }> }> {const url = assertAllowedUpstreamUrl(CODEX_CATALOG_ORIGIN, 'catalog')- await assertResolvedUpstream(url, input.lookup)- const response = await input.fetchFn(url.href, {- method: 'GET',- redirect: 'manual',- headers: {- authorization: `Bearer ${input.accessToken}`,- accept: 'application/json',+ const headers = chatgptUpstreamHeaders(input.accessToken, { accept: 'application/json' })+ const response = await fetchFrozenOrigin({+ url,+ fetchFn: input.fetchFn,+ lookup: input.lookup,+ init: {+ method: 'GET',+ headers,},})- if (response.status >= 300 && response.status < 400) {- assertRedirectLocation(response.headers.get('location') || '', url)- throw new OriginDeniedError('origin_denied')- }if (response.status === 401 || response.status === 403) return { outcome: 'auth-rejected', models: [] }- if (!response.ok) return { outcome: 'unavailable', models: [] }+ if (!response.ok) {+ logger.warn(+ {+ event: 'codex_catalog_upstream',+ status: response.status,+ accountHeader: Boolean(headers['chatgpt-account-id']),+ },+ 'Codex catalog upstream returned a non-success status'+ )+ return { outcome: 'unavailable', models: [] }+ }const body = (await response.json()) as unknownreturn { outcome: 'ready', models: normalizeModels(body) }}diff --git a/codex-llm-proxy/src/controlApiClient.ts b/codex-llm-proxy/src/controlApiClient.tsindex 31deaed11..282dae779 100644--- a/codex-llm-proxy/src/controlApiClient.ts+++ b/codex-llm-proxy/src/controlApiClient.ts@@ -9,6 +9,7 @@ export type RedeemOperation = 'completion_stream' | 'completion_cancel' | 'conneexport type RedeemAttemptSuccess = {accessToken: string+ chatgptAccountId?: stringtransport: {protocolVersion: typeof CODEX_TRANSPORT_PROTOCOLcompletionsOrigin: typeof CODEX_COMPLETIONS_ORIGIN@@ -140,6 +141,9 @@ function parseRedeem(body: unknown): RedeemAttemptSuccess {}return {accessToken: body.accessToken,+ ...(typeof body.chatgptAccountId === 'string' && body.chatgptAccountId.trim()+ ? { chatgptAccountId: body.chatgptAccountId.trim() }+ : {}),transport: {protocolVersion: CODEX_TRANSPORT_PROTOCOL,completionsOrigin: CODEX_COMPLETIONS_ORIGIN,diff --git a/codex-llm-proxy/src/originPolicy.ts b/codex-llm-proxy/src/originPolicy.tsindex c06ac1b14..c53de137b 100644--- a/codex-llm-proxy/src/originPolicy.ts+++ b/codex-llm-proxy/src/originPolicy.ts@@ -1,5 +1,7 @@export const CODEX_COMPLETIONS_ORIGIN = 'https://chatgpt.com/backend-api/codex/responses'-export const CODEX_CATALOG_ORIGIN = 'https://chatgpt.com/backend-api/codex/models'+export const CODEX_CATALOG_CLIENT_VERSION = '1.0.0'+export const CODEX_CATALOG_ORIGIN =+ `https://chatgpt.com/backend-api/codex/models?client_version=${CODEX_CATALOG_CLIENT_VERSION}`export const CODEX_TRANSPORT_PROTOCOL = 'codex-subscription-transport.v1' as constexport type UpstreamKind = 'completions' | 'catalog'@@ -34,7 +36,7 @@ export function assertAllowedUpstreamUrl(raw: string, kind: UpstreamKind): URL {if (parsed.protocol !== 'https:') throw new OriginDeniedError('origin_denied')if (parsed.username || parsed.password) throw new OriginDeniedError('origin_denied')if (parsed.hash) throw new OriginDeniedError('origin_denied')- if (parsed.search) throw new OriginDeniedError('origin_denied')+ if (parsed.search !== frozen.search) throw new OriginDeniedError('origin_denied')if (parsed.origin !== frozen.origin || parsed.pathname !== frozen.pathname) {throw new OriginDeniedError('origin_denied')}@@ -56,6 +58,28 @@ export async function assertResolvedUpstream(}}+/**+ * POST/GET the frozen origin with redirects disabled. Follow at most one+ * same-origin/same-path/same-search HTTPS redirect; anything else is denied.+ */+export async function fetchFrozenOrigin(input: {+ url: URL+ init: RequestInit+ fetchFn: typeof fetch+ lookup?: OriginPolicyOptions['lookup']+}): Promise<Response> {+ await assertResolvedUpstream(input.url, input.lookup)+ const init: RequestInit = { ...input.init, redirect: 'manual' }+ const first = await input.fetchFn(input.url.href, init)+ if (first.status < 300 || first.status >= 400) return first+ const next = assertRedirectLocation(first.headers.get('location') || '', input.url)+ const second = await input.fetchFn(next.href, init)+ if (second.status >= 300 && second.status < 400) {+ throw new OriginDeniedError('origin_denied')+ }+ return second+}+export function assertRedirectLocation(location: string, from: URL): URL {let next: URLtry {@@ -69,6 +93,7 @@ export function assertRedirectLocation(location: string, from: URL): URL {throw new OriginDeniedError('origin_denied')}if (next.pathname !== from.pathname) throw new OriginDeniedError('origin_denied')+ if (next.search !== from.search) throw new OriginDeniedError('origin_denied')return next}diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.tsindex 7d57a77e9..d58e3ca24 100644--- a/codex-llm-proxy/src/server.ts+++ b/codex-llm-proxy/src/server.ts@@ -120,16 +120,21 @@ export function createProxyApps(config: CodexLlmProxyConfig, deps: ProxyRuntimeDreject(res, 403, 'ticket_invalid')return}+ if (!platform.hostRefs.includes('*') && !platform.hostRefs.includes(ticket.hostRef)) {+ reject(res, 403, 'host_binding_mismatch')+ return+ }if (!config.executionEnabled) {reject(res, 404, 'disabled')return}void (async () => {- const release = await streamGate.acquire()+ let release: (() => void) | undefinedconst abort = new AbortController()req.on('close', () => abort.abort())try {+ release = await streamGate.acquire()res.status(200)res.setHeader('content-type', 'text/event-stream')res.setHeader('cache-control', 'no-cache')@@ -139,7 +144,7 @@ export function createProxyApps(config: CodexLlmProxyConfig, deps: ProxyRuntimeDrequestHash: parsed.data.requestHash,request: parsed.data.request,deadlineMs: parsed.data.deadlineMs,- maxDeadlineMs: config.maxDeadlineMs,+ maxDeadlineMs: Math.min(config.maxDeadlineMs, config.maxStreamDurationMs),ticket: {jti: ticket.jti,hostRef: ticket.hostRef,@@ -155,16 +160,23 @@ export function createProxyApps(config: CodexLlmProxyConfig, deps: ProxyRuntimeDres.write(`data: ${JSON.stringify(frame)}\n\n`)},})- res.write(`data: ${JSON.stringify({ type: 'done', outcome: result.outcome })}\n\n`)+ res.write(+ `data: ${JSON.stringify({ type: 'done', outcome: result.outcome, ...(result.usage ? { usage: result.usage } : {}) })}\n\n`+ )metrics.observeAttempt(result.outcome, 'completion_stream')metrics.observeStream(Date.now() - started)res.end()} catch (err) {const mapped = mapError(err)metrics.observeAttempt('error', 'completion_stream')+ if (res.headersSent) {+ res.write(`data: ${JSON.stringify({ type: 'error', code: mapped.code })}\n\n`)+ res.end()+ return+ }reject(res, mapped.status, mapped.code)} finally {- release()+ release?.()}})()})@@ -267,15 +279,23 @@ function mapError(err: unknown): { status: number; code: string } {if (err instanceof OriginDeniedError) return { status: 403, code: 'origin_denied' }if (err instanceof RequestLimitError) return { status: 503, code: 'provider_unavailable' }if (err instanceof CodexTransportError || err instanceof ControlApiClientError) {- const status =- err.code === 'request_hash_mismatch' || err.code === 'ticket_invalid' || err.code === 'model_not_allowed'- ? 403- : err.code === 'disabled'- ? 404- : err.code === 'ticket_replayed'- ? 409- : 503- return { status, code: err.code }+ const statusByCode: Record<string, number> = {+ invalid_request: 400,+ Unauthorized: 401,+ origin_denied: 403,+ request_hash_mismatch: 403,+ ticket_invalid: 403,+ ticket_expired: 403,+ no_grant: 403,+ host_binding_mismatch: 403,+ model_not_allowed: 403,+ insufficient_scope: 403,+ disabled: 404,+ ticket_replayed: 409,+ connection_unavailable: 503,+ provider_unavailable: 503,+ }+ return { status: statusByCode[err.code] ?? 503, code: err.code }}return { status: 503, code: 'provider_unavailable' }}diff --git a/codex-llm-proxy/test/chatgptUpstreamHeaders.test.ts b/codex-llm-proxy/test/chatgptUpstreamHeaders.test.tsnew file mode 100644index 000000000..931ef0399--- /dev/null+++ b/codex-llm-proxy/test/chatgptUpstreamHeaders.test.ts@@ -0,0 +1,39 @@+import { describe, expect, it } from 'vitest'+import {+ chatgptAccountIdFromAccessToken,+ chatgptUpstreamHeaders,+ CODEX_UPSTREAM_ORIGINATOR,+} from '../src/chatgptUpstreamHeaders.js'++function jwtWithPayload(payload: Record<string, unknown>): string {+ const encoded = Buffer.from(JSON.stringify(payload)).toString('base64url')+ return `hdr.${encoded}.sig`+}++describe('chatgptUpstreamHeaders', () => {+ it('attaches ChatGPT-Account-ID from the OpenAI auth claim without leaking the token', () => {+ const token = jwtWithPayload({+ sub: 'user-1',+ 'https://api.openai.com/auth': { chatgpt_account_id: 'acct_live_1' },+ })+ const headers = chatgptUpstreamHeaders(token, { accept: 'application/json' })+ expect(headers['chatgpt-account-id']).toBe('acct_live_1')+ expect(headers.originator).toBe(CODEX_UPSTREAM_ORIGINATOR)+ expect(headers.originator).not.toBe('codex_cli_rs')+ expect(headers['openai-beta']).toBe('responses=v1')+ expect(headers.version).toBe('1.0.0')+ expect(JSON.stringify(headers)).not.toMatch(/sk-|refresh-secret/i)+ })++ it('omits the account header when the access token is not a JWT', () => {+ expect(chatgptAccountIdFromAccessToken('opaque-token')).toBeUndefined()+ const headers = chatgptUpstreamHeaders('opaque-token')+ expect(headers['chatgpt-account-id']).toBeUndefined()+ expect(headers.authorization).toBe('Bearer opaque-token')+ })++ it('also reads chatgpt_account_id from a nested auth object', () => {+ const token = jwtWithPayload({ auth: { chatgpt_account_id: 'acct_nested' } })+ expect(chatgptAccountIdFromAccessToken(token)).toBe('acct_nested')+ })+})diff --git a/codex-llm-proxy/test/codexTransport.conformance.test.ts b/codex-llm-proxy/test/codexTransport.conformance.test.tsindex 3d666dbf0..89fcbb483 100644--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts@@ -15,13 +15,23 @@ const REQUEST = {const REQUEST_HASH = hashCodexCompletionRequestV1(REQUEST)+function accessTokenFor(label: string): string {+ const encoded = Buffer.from(+ JSON.stringify({+ sub: label,+ 'https://api.openai.com/auth': { chatgpt_account_id: 'acct_live_1' },+ })+ ).toString('base64url')+ return `hdr.${encoded}.sig`+}+function redeemSuccess(overrides: Partial<RedeemAttemptSuccess> = {}): RedeemAttemptSuccess {return {- accessToken: 'tok-live',+ accessToken: accessTokenFor('live'),transport: {protocolVersion: 'codex-subscription-transport.v1',completionsOrigin: CODEX_COMPLETIONS_ORIGIN,- catalogOrigin: 'https://chatgpt.com/backend-api/codex/models',+ catalogOrigin: 'https://chatgpt.com/backend-api/codex/models?client_version=1.0.0',operation: 'completion_stream',servedModel: 'gpt-5.1',maxStreamDurationMs: 300_000,@@ -91,7 +101,67 @@ describe('streamCodexCompletion', () => {receipt: expect.objectContaining({ outcome: 'success', requestHash: REQUEST_HASH }),}))- expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain('tok-live')+ expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(accessTokenFor('live'))+ expect(fetchFn.mock.calls[0]?.[1]?.headers?.['originator']).toBe('evenfire')+ expect(fetchFn.mock.calls[0]?.[1]?.headers?.['openai-beta']).toBe('responses=v1')+ expect(fetchFn.mock.calls[0]?.[1]?.headers?.['session_id']).toBe('req-001')+ expect(fetchFn.mock.calls[0]?.[1]?.headers?.['chatgpt-account-id']).toBe('acct_live_1')+ expect(String(fetchFn.mock.calls[0]?.[1]?.body)).toContain('"store":false')+ })++ it('uses the redeemed ChatGPT account id when the access token is opaque', async () => {+ const fetchFn = vi.fn(async () =>+ sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])+ )+ await streamCodexCompletion({+ executionTicket: 'ticket-1',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-1',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-1',+ },+ redeem: async () =>+ redeemSuccess({ accessToken: 'opaque-token', chatgptAccountId: 'acct_from_id_token' }),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'success',+ duplicate: false,+ })),+ fetchFn,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ })+ expect(fetchFn.mock.calls[0]?.[1]?.headers?.['chatgpt-account-id']).toBe('acct_from_id_token')+ })++ it('refuses to fetch completions when the access token has no ChatGPT account id', async () => {+ const fetchFn = vi.fn()+ await expect(+ streamCodexCompletion({+ executionTicket: 'ticket-1',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-1',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-1',+ },+ redeem: async () => redeemSuccess({ accessToken: 'opaque-token' }),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'error',+ duplicate: false,+ })),+ fetchFn,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ })+ ).rejects.toMatchObject({ code: 'connection_unavailable' })+ expect(fetchFn).not.toHaveBeenCalled()})it('rejects a mutated requestHash before redeem', async () => {@@ -161,6 +231,57 @@ describe('streamCodexCompletion', () => {).rejects.toMatchObject({ code: 'origin_denied' })})+ it('follows one frozen same-origin redirect then streams', async () => {+ const fetchFn = vi.fn(async () => {+ if (fetchFn.mock.calls.length === 1) {+ return new Response(null, {+ status: 307,+ headers: { location: CODEX_COMPLETIONS_ORIGIN },+ })+ }+ return sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])+ })+ const result = await streamCodexCompletion({+ executionTicket: 'ticket-1',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-1',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-1',+ },+ redeem: async () => redeemSuccess(),+ finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'success', duplicate: false })),+ fetchFn,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ })+ expect(result.outcome).toBe('success')+ expect(fetchFn).toHaveBeenCalledTimes(2)+ })++ it('maps upstream 401 to connection_unavailable instead of origin_denied', async () => {+ await expect(+ streamCodexCompletion({+ executionTicket: 'ticket-1',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-1',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-1',+ },+ redeem: async () => redeemSuccess(),+ finalize: vi.fn(async () => ({ providerAttemptId: 'att-1', outcome: 'error', duplicate: false })),+ fetchFn: vi.fn(async () => new Response('denied', { status: 401 })),+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ })+ ).rejects.toMatchObject({ code: 'connection_unavailable' })+ })+it('finalizes canceled after the first frame and treats finalize 500 as idempotent retry', async () => {const abort = new AbortController()const finalize = vi@@ -211,7 +332,7 @@ describe('streamCodexCompletion', () => {const tokens: string[] = []const redeem = vi.fn(async (input: { executionTicket: string }) => {tokens.push(input.executionTicket)- return redeemSuccess({ accessToken: `tok-${input.executionTicket}` })+ return redeemSuccess({ accessToken: accessTokenFor(input.executionTicket) })})const fetchFn = vi.fn(async () =>sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])@@ -244,8 +365,8 @@ describe('streamCodexCompletion', () => {lookup: async () => [{ address: '1.2.3.4', family: 4 }],})expect(tokens).toEqual(['t-a', 't-b'])- expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain('tok-t-a')- expect(String(fetchFn.mock.calls[1]?.[1]?.headers?.['authorization'])).toContain('tok-t-b')+ expect(String(fetchFn.mock.calls[0]?.[1]?.headers?.['authorization'])).toContain(accessTokenFor('t-a'))+ expect(String(fetchFn.mock.calls[1]?.[1]?.headers?.['authorization'])).toContain(accessTokenFor('t-b'))})it('does not retry after an ambiguous upstream response', async () => {@@ -269,4 +390,96 @@ describe('streamCodexCompletion', () => {expect(result.outcome).toBe('unknown')expect(fetchFn).toHaveBeenCalledOnce()})++ it('maps system, tool history, and generation hints into the Responses payload', async () => {+ const request = {+ schemaVersion: 'codex-completion-request.v1' as const,+ requestId: 'req-001',+ idempotencyKey: 'idem-001',+ provider: 'codex-subscription' as const,+ model: 'gpt-5.1',+ messages: [+ { role: 'system' as const, content: 'be brief' },+ { role: 'user' as const, content: 'hello' },+ {+ role: 'assistant' as const,+ content: '',+ toolCalls: [{ id: 'call-1', name: 'echo', arguments: { x: 1 } }],+ },+ { role: 'tool' as const, content: 'ok', toolCallId: 'call-1' },+ ],+ tools: [{ name: 'echo', description: 'echo', parameters: {} }],+ generation: { toolChoice: 'auto' as const },+ transportHints: { promptCacheKey: 'sess-1' },+ }+ const requestHash = hashCodexCompletionRequestV1(request)+ const fetchFn = vi.fn(async () =>+ sseResponse(['data: {"type":"response.completed","response":{"usage":{}}}\n\n'])+ )+ await streamCodexCompletion({+ executionTicket: 'ticket-1',+ requestHash,+ request,+ ticket: {+ jti: 'jti-1',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash,+ providerAttemptId: 'att-1',+ },+ redeem: async () => redeemSuccess(),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'success',+ duplicate: false,+ })),+ fetchFn,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ })+ const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body)) as Record<string, unknown>+ expect(body.instructions).toBe('be brief')+ expect(body.store).toBe(false)+ expect(body.tool_choice).toBe('auto')+ expect(body.prompt_cache_key).toBe('sess-1')+ expect(body.input).toEqual([+ { role: 'user', content: 'hello' },+ { type: 'function_call', call_id: 'call-1', name: 'echo', arguments: '{"x":1}' },+ { type: 'function_call_output', call_id: 'call-1', output: 'ok' },+ ])+ })++ it('emits a tool call only after argument deltas complete', async () => {+ const frames: unknown[] = []+ const fetchFn = vi.fn(async () =>+ sseResponse([+ 'data: {"type":"response.output_item.added","item":{"type":"function_call","id":"item-1","call_id":"call-9","name":"lookup","arguments":""}}\n\n',+ 'data: {"type":"response.function_call_arguments.delta","item_id":"item-1","delta":"{\\"q\\":"}}\n\n',+ 'data: {"type":"response.function_call_arguments.delta","item_id":"item-1","delta":"\\"x\\"}"}\n\n',+ 'data: {"type":"response.function_call_arguments.done","item_id":"item-1","arguments":"{\\"q\\":\\"x\\"}"}\n\n',+ 'data: {"type":"response.completed","response":{"usage":{}}}\n\n',+ ])+ )+ await streamCodexCompletion({+ executionTicket: 'ticket-1',+ requestHash: REQUEST_HASH,+ request: REQUEST,+ ticket: {+ jti: 'jti-1',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash: REQUEST_HASH,+ providerAttemptId: 'att-1',+ },+ redeem: async () => redeemSuccess(),+ finalize: vi.fn(async () => ({+ providerAttemptId: 'att-1',+ outcome: 'success',+ duplicate: false,+ })),+ fetchFn,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ onFrame: frame => frames.push(frame),+ })+ expect(frames).toEqual([{ type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } }])+ })})diff --git a/codex-llm-proxy/test/controlApiClient.test.ts b/codex-llm-proxy/test/controlApiClient.test.tsindex 6e70d8ceb..bd6a0f57c 100644--- a/codex-llm-proxy/test/controlApiClient.test.ts+++ b/codex-llm-proxy/test/controlApiClient.test.ts@@ -27,7 +27,7 @@ function listen(handler: (req: IncomingMessage, body: unknown, res: Server) => vtransport: {protocolVersion: 'codex-subscription-transport.v1',completionsOrigin: 'https://chatgpt.com/backend-api/codex/responses',- catalogOrigin: 'https://chatgpt.com/backend-api/codex/models',+ catalogOrigin: 'https://chatgpt.com/backend-api/codex/models?client_version=1.0.0',operation: 'completion_stream',servedModel: 'gpt-5.1',maxStreamDurationMs: 300000,diff --git a/codex-llm-proxy/test/originPolicy.test.ts b/codex-llm-proxy/test/originPolicy.test.tsindex 3aa059d87..e3bcff3e5 100644--- a/codex-llm-proxy/test/originPolicy.test.ts+++ b/codex-llm-proxy/test/originPolicy.test.ts@@ -5,6 +5,7 @@ import {OriginDeniedError,assertAllowedUpstreamUrl,assertRedirectLocation,+ fetchFrozenOrigin,} from '../src/originPolicy.js'const LOOPBACK_V4 = ['127', '0', '0', '1'].join('.')@@ -31,6 +32,15 @@ describe('originPolicy', () => {expect(() => assertAllowedUpstreamUrl('https://chatgpt.com/backend-api/codex/responses?hijack=1', 'completions')).toThrow(OriginDeniedError)+ expect(() =>+ assertAllowedUpstreamUrl('https://chatgpt.com/backend-api/codex/models', 'catalog')+ ).toThrow(OriginDeniedError)+ expect(() =>+ assertAllowedUpstreamUrl(+ 'https://chatgpt.com/backend-api/codex/models?client_version=1.0.0&hijack=1',+ 'catalog'+ )+ ).toThrow(OriginDeniedError)})it('rejects cross-origin and private-address redirects', () => {@@ -51,6 +61,38 @@ describe('originPolicy', () => {).toBe(CODEX_COMPLETIONS_ORIGIN)})+ it('follows one frozen same-origin redirect and denies a second hop', async () => {+ let hops = 0+ const allowed = await fetchFrozenOrigin({+ url: new URL(CODEX_COMPLETIONS_ORIGIN),+ init: { method: 'POST' },+ fetchFn: (async () => {+ hops += 1+ if (hops === 1) {+ return new Response(null, {+ status: 307,+ headers: { location: CODEX_COMPLETIONS_ORIGIN },+ })+ }+ return new Response('ok', { status: 200 })+ }) as unknown as typeof fetch,+ })+ expect(allowed.status).toBe(200)+ expect(hops).toBe(2)++ await expect(+ fetchFrozenOrigin({+ url: new URL(CODEX_COMPLETIONS_ORIGIN),+ init: { method: 'POST' },+ fetchFn: (async () =>+ new Response(null, {+ status: 302,+ headers: { location: CODEX_COMPLETIONS_ORIGIN },+ })) as unknown as typeof fetch,+ })+ ).rejects.toBeInstanceOf(OriginDeniedError)+ })+it('rejects DNS rebinding onto private or metadata addresses', async () => {const { assertResolvedUpstream } = await import('../src/originPolicy.js')await expect(diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.tsindex cb494c4fd..fbe9ba6b7 100644--- a/codex-llm-proxy/test/server.security.test.ts+++ b/codex-llm-proxy/test/server.security.test.ts@@ -141,6 +141,28 @@ describe('codex-llm-proxy security surface', () => {expect(res.status).toBe(413)})+ it('rejects a platform JWT whose hostRefs do not bind the ticket hostRef', async () => {+ const { runtimeApp } = createProxyApps(config())+ const foreign = sign(+ {+ sub: 'default/other-host',+ hostRefs: ['other-host'],+ workflowControlScopes: ['llm:codex:execute'],+ },+ 'workflow-approvals'+ )+ const res = await request(runtimeApp)+ .post('/internal/runtime/v1/codex/completions')+ .set('Authorization', `Bearer ${foreign}`)+ .send({+ executionTicket: ticket(),+ requestHash: 'a'.repeat(64),+ request: {},+ })+ expect(res.status).toBe(403)+ expect(res.body.error).toBe('host_binding_mismatch')+ })+it('rejects missing, zero, and unbounded config', () => {expect(() => loadConfig({ CODEX_LLM_PROXY_RUNTIME_PORT: '0' })).toThrow(/greater than zero/)expect(() =>diff --git a/control-api/src/db.ts b/control-api/src/db.tsindex abaaa770a..914ac06c3 100644--- a/control-api/src/db.ts+++ b/control-api/src/db.ts@@ -5,7 +5,10 @@ import {createBoundedPgPoolForConnection,} from './boundedPgPool.js'import { config } from './config.js'-import { applyCodexSubscriptionConnectionSchema } from './services/codexSubscriptionConnection.js'+import {+ applyCodexChatgptAccountIdSchema,+ applyCodexSubscriptionConnectionSchema,+} from './services/codexSubscriptionConnection.js'import { applyCodexSubscriptionOAuthStateSchema } from './services/codexSubscriptionOAuthState.js'import {applyGfsUploadCleanupSchema,@@ -5936,6 +5939,10 @@ export const CONTROL_API_MIGRATIONS: DbMigration[] = [version: '00a3_llm_provider_attempt_tickets',apply: applyLlmProviderAttemptTicketSchema,},+ {+ version: '00a4_codex_chatgpt_account_id',+ apply: applyCodexChatgptAccountIdSchema,+ },{version: '0100_seed_minimax_allowed_model',apply: async db => {diff --git a/control-api/src/services/chatgptAccountId.ts b/control-api/src/services/chatgptAccountId.tsnew file mode 100644index 000000000..fc1c19525--- /dev/null+++ b/control-api/src/services/chatgptAccountId.ts@@ -0,0 +1,33 @@+const CHATGPT_AUTH_CLAIM = 'https://api.openai.com/auth'++/**+ * ChatGPT Responses requires ChatGPT-Account-ID. The OAuth access token is+ * sometimes opaque, so the gateway also reads the id_token at grant time.+ */+export function chatgptAccountIdFromJwt(token: string | null | undefined): string | undefined {+ if (!token) return undefined+ const parts = token.split('.')+ if (parts.length < 2) return undefined+ try {+ const payload = JSON.parse(Buffer.from(parts[1]!, 'base64url').toString('utf8')) as Record<+ string,+ unknown+ >+ const nested = payload[CHATGPT_AUTH_CLAIM]+ if (nested && typeof nested === 'object') {+ const accountId = (nested as { chatgpt_account_id?: unknown }).chatgpt_account_id+ if (typeof accountId === 'string' && accountId.trim()) return accountId.trim()+ }+ if (typeof payload.chatgpt_account_id === 'string' && payload.chatgpt_account_id.trim()) {+ return payload.chatgpt_account_id.trim()+ }+ const auth = payload.auth+ if (auth && typeof auth === 'object') {+ const accountId = (auth as { chatgpt_account_id?: unknown }).chatgpt_account_id+ if (typeof accountId === 'string' && accountId.trim()) return accountId.trim()+ }+ } catch {+ return undefined+ }+ return undefined+}diff --git a/control-api/src/services/codexSubscriptionCatalog.ts b/control-api/src/services/codexSubscriptionCatalog.tsindex 086034f44..a5ba45280 100644--- a/control-api/src/services/codexSubscriptionCatalog.ts+++ b/control-api/src/services/codexSubscriptionCatalog.ts@@ -188,14 +188,23 @@ export function createCodexProxyCatalogTransport(deps: {return {async listModels(input: { accessToken: string }): Promise<CodexCatalogTransportResult> {const base = deps.adminBaseUrl.replace(/\/+$/, '')- const response = await fetchFn(`${base}/internal/admin/v1/codex/models`, {- method: 'POST',- headers: {- 'content-type': 'application/json',- authorization: `Bearer ${signPermit('catalog_list')}`,- },- body: JSON.stringify({ accessToken: input.accessToken }),- })+ let response: Response+ try {+ response = await fetchFn(`${base}/internal/admin/v1/codex/models`, {+ method: 'POST',+ headers: {+ 'content-type': 'application/json',+ authorization: `Bearer ${signPermit('catalog_list')}`,+ },+ body: JSON.stringify({ accessToken: input.accessToken }),+ })+ } catch (err) {+ log.warn(+ { event: 'codex_catalog_proxy_unreachable', err },+ 'Codex catalog proxy unreachable'+ )+ return { outcome: 'unavailable' }+ }if (!response.ok) {if (response.status === 401 || response.status === 403) return { outcome: 'auth-rejected' }return { outcome: 'unavailable' }diff --git a/control-api/src/services/codexSubscriptionConnection.ts b/control-api/src/services/codexSubscriptionConnection.tsindex 2f5dd539f..6ce1f9a86 100644--- a/control-api/src/services/codexSubscriptionConnection.ts+++ b/control-api/src/services/codexSubscriptionConnection.ts@@ -36,6 +36,7 @@ export type CodexSubscriptionCredentialWrite = {refreshToken: stringaccessToken?: string | nullaccessTokenExpiresAt?: Date | null+ chatgptAccountId?: string | nullaccountFingerprint: stringstatus?: Extract<CodexSubscriptionConnectionStatus, 'connected' | 'reauth_required'>}@@ -44,6 +45,7 @@ export type CodexSubscriptionSecrets = {refreshToken: stringaccessToken: string | nullaccessTokenExpiresAt: Date | null+ chatgptAccountId: string | nullcredentialRevision: number}@@ -140,6 +142,14 @@ export async function applyCodexSubscriptionConnectionSchema(db: DbClient): PromGRANT SELECT, INSERT, UPDATE ON TABLE codex_subscription_connections TO control_api_runtime;REVOKE DELETE, TRUNCATE, REFERENCES, TRIGGER ON TABLE codex_subscription_connections FROM control_api_runtime;`)+ await applyCodexChatgptAccountIdSchema(db)+}++export async function applyCodexChatgptAccountIdSchema(db: DbClient): Promise<void> {+ await db.query(`+ ALTER TABLE codex_subscription_connections+ ADD COLUMN IF NOT EXISTS chatgpt_account_id_encrypted TEXT+ `)}export async function getSafeCodexSubscriptionConnection(@@ -165,6 +175,7 @@ export async function insertInitialCodexSubscriptionConnection(input.accessToken == null || input.accessToken === ''? null: encryptOAuthSecret(encryptionKey, input.accessToken)+ const chatgptAccountIdEncrypted = encryptOptionalSecret(encryptionKey, input.chatgptAccountId)const result = await db.query(`INSERT INTO codex_subscription_connections (connection_key,@@ -172,12 +183,13 @@ export async function insertInitialCodexSubscriptionConnection(refresh_token_encrypted,access_token_encrypted,access_token_expires_at,+ chatgpt_account_id_encrypted,credential_revision,catalog_revision,account_fingerprint,catalog_status,last_auth_at- ) VALUES ($1, $2, $3, $4, $5, 1, 0, $6, 'never_synced', now())+ ) VALUES ($1, $2, $3, $4, $5, $6, 1, 0, $7, 'never_synced', now())RETURNING ${SAFE_CONNECTION_COLUMNS}`,[CODEX_SUBSCRIPTION_CONNECTION_KEY,@@ -185,6 +197,7 @@ export async function insertInitialCodexSubscriptionConnection(refreshTokenEncrypted,accessTokenEncrypted,input.accessTokenExpiresAt ?? null,+ chatgptAccountIdEncrypted,input.accountFingerprint,])@@ -202,25 +215,28 @@ export async function rotateCodexSubscriptionCredentials(input.accessToken == null || input.accessToken === ''? null: encryptOAuthSecret(encryptionKey, input.accessToken)+ const chatgptAccountIdEncrypted = encryptOptionalSecret(encryptionKey, input.chatgptAccountId)const result = await db.query(`UPDATE codex_subscription_connectionsSET refresh_token_encrypted = $1,access_token_encrypted = $2,access_token_expires_at = $3,- account_fingerprint = $4,- status = $5,+ chatgpt_account_id_encrypted = COALESCE($4, chatgpt_account_id_encrypted),+ account_fingerprint = $5,+ status = $6,credential_revision = credential_revision + 1,last_refresh_at = now(),last_auth_at = now(),revoked_at = NULL,updated_at = now()- WHERE connection_key = $6- AND credential_revision = $7+ WHERE connection_key = $7+ AND credential_revision = $8RETURNING ${SAFE_CONNECTION_COLUMNS}`,[refreshTokenEncrypted,accessTokenEncrypted,input.accessTokenExpiresAt ?? null,+ chatgptAccountIdEncrypted,input.accountFingerprint,input.status ?? 'connected',CODEX_SUBSCRIPTION_CONNECTION_KEY,@@ -241,6 +257,7 @@ export async function revokeCodexSubscriptionConnection(refresh_token_encrypted = NULL,access_token_encrypted = NULL,access_token_expires_at = NULL,+ chatgpt_account_id_encrypted = NULL,account_fingerprint = NULL,credential_revision = credential_revision + 1,refresh_lock_token = NULL,@@ -303,6 +320,7 @@ export async function loadCodexSubscriptionSecrets(`SELECT refresh_token_encrypted,access_token_encrypted,access_token_expires_at,+ chatgpt_account_id_encrypted,credential_revisionFROM codex_subscription_connectionsWHERE connection_key = $1@@ -314,6 +332,7 @@ export async function loadCodexSubscriptionSecrets(refresh_token_encrypted: string | nullaccess_token_encrypted: string | nullaccess_token_expires_at: Date | string | null+ chatgpt_account_id_encrypted: string | nullcredential_revision: string | number}| undefined@@ -324,10 +343,74 @@ export async function loadCodexSubscriptionSecrets(? decryptOAuthSecret(encryptionKey, row.access_token_encrypted): null,accessTokenExpiresAt: asDate(row.access_token_expires_at),+ chatgptAccountId: row.chatgpt_account_id_encrypted+ ? decryptOAuthSecret(encryptionKey, row.chatgpt_account_id_encrypted)+ : null,credentialRevision: Number(row.credential_revision),}}+export async function updateCodexAccessTokenInPlace(+ db: DbClient,+ encryptionKey: Buffer,+ expectedRevision: number,+ input: {+ accessToken: string+ accessTokenExpiresAt?: Date | null+ refreshToken?: string | null+ chatgptAccountId?: string | null+ }+): Promise<CodexSubscriptionSafeConnection> {+ const result = await db.query(+ `UPDATE codex_subscription_connections+ SET access_token_encrypted = $1,+ access_token_expires_at = $2,+ refresh_token_encrypted = COALESCE($3, refresh_token_encrypted),+ chatgpt_account_id_encrypted = COALESCE($4, chatgpt_account_id_encrypted),+ last_refresh_at = now(),+ updated_at = now()+ WHERE connection_key = $5+ AND credential_revision = $6+ AND revoked_at IS NULL+ RETURNING ${SAFE_CONNECTION_COLUMNS}`,+ [+ encryptOAuthSecret(encryptionKey, input.accessToken),+ input.accessTokenExpiresAt ?? null,+ encryptOptionalSecret(encryptionKey, input.refreshToken),+ encryptOptionalSecret(encryptionKey, input.chatgptAccountId),+ CODEX_SUBSCRIPTION_CONNECTION_KEY,+ expectedRevision,+ ]+ )+ const row = result.rows[0] as SafeConnectionRow | undefined+ if (!row) throw new CodexSubscriptionStaleRevisionError()+ return toSafeConnection(row)+}++export async function persistCodexChatgptAccountId(+ db: DbClient,+ encryptionKey: Buffer,+ chatgptAccountId: string+): Promise<void> {+ await db.query(+ `UPDATE codex_subscription_connections+ SET chatgpt_account_id_encrypted = $1,+ updated_at = now()+ WHERE connection_key = $2+ AND revoked_at IS NULL+ AND chatgpt_account_id_encrypted IS NULL`,+ [encryptOAuthSecret(encryptionKey, chatgptAccountId), CODEX_SUBSCRIPTION_CONNECTION_KEY]+ )+}++function encryptOptionalSecret(+ encryptionKey: Buffer,+ value: string | null | undefined+): string | null {+ if (value == null || value === '') return null+ return encryptOAuthSecret(encryptionKey, value)+}+export async function recordCodexCatalogOutcome(db: DbClient,input: {diff --git a/control-api/src/services/codexSubscriptionOAuth.ts b/control-api/src/services/codexSubscriptionOAuth.tsindex 243eb4ad5..8a7dd4890 100644--- a/control-api/src/services/codexSubscriptionOAuth.ts+++ b/control-api/src/services/codexSubscriptionOAuth.ts@@ -1,6 +1,7 @@import { createHash, randomBytes } from 'node:crypto'import type { DbClient } from '../db.js'import { rootLogger } from '../observability/logger.js'+import { chatgptAccountIdFromJwt } from './chatgptAccountId.js'import {type CodexSubscriptionSafeConnection,CodexSubscriptionStaleRevisionError,@@ -8,9 +9,11 @@ import {getSafeCodexSubscriptionConnection,insertInitialCodexSubscriptionConnection,loadCodexSubscriptionSecrets,+ persistCodexChatgptAccountId,releaseCodexSubscriptionRefreshLock,revokeCodexSubscriptionConnection,rotateCodexSubscriptionCredentials,+ updateCodexAccessTokenInPlace,} from './codexSubscriptionConnection.js'import {type CodexSubscriptionOAuthIntent,@@ -39,6 +42,7 @@ const DEVICE_CODE_TIMEOUT_SECONDS = 15 * 60const TOKEN_TIMEOUT_MS = 15_000const REFRESH_LOCK_TTL_MS = 30_000const BROWSER_STATE_TTL_MS = 10 * 60_000+const ACCESS_TOKEN_REFRESH_SKEW_MS = 5 * 60_000export type CodexOAuthErrorCode =| 'disabled'@@ -277,6 +281,7 @@ export async function refreshCodexSubscriptionConnection(refreshToken: token.refreshToken ?? secrets.refreshToken,accessToken: token.accessToken,accessTokenExpiresAt: token.expiresAt,+ chatgptAccountId: token.chatgptAccountId,accountFingerprint: token.accountFingerprint,})@@ -335,6 +340,7 @@ async function persistGrantedTokens(refreshToken: parsed.refreshToken ?? '',accessToken: parsed.accessToken,accessTokenExpiresAt: parsed.expiresAt,+ chatgptAccountId: parsed.chatgptAccountId,accountFingerprint: parsed.accountFingerprint,status: 'connected' as const,}@@ -370,6 +376,60 @@ type ParsedCodexToken = {refreshToken: string | nullexpiresAt: Date | nullaccountFingerprint: string+ chatgptAccountId?: string+}++/**+ * Refresh the ChatGPT access token without bumping credential_revision.+ * Authorize tickets bind that revision; rotating it after issue invalidates them.+ */+export async function ensureFreshCodexAccessToken(deps: CodexOAuthDeps): Promise<void> {+ requireEnabled(deps)+ const secrets = await loadCodexSubscriptionSecrets(deps.db, deps.encryptionKey)+ if (!secrets) {+ throw new CodexSubscriptionOAuthError('no_grant', 'encrypted refresh token missing')+ }+ let accountId = secrets.chatgptAccountId || chatgptAccountIdFromJwt(secrets.accessToken)+ const expiring =+ secrets.accessTokenExpiresAt != null &&+ secrets.accessTokenExpiresAt.getTime() - Date.now() < ACCESS_TOKEN_REFRESH_SKEW_MS+ if (secrets.accessToken && !expiring && accountId) {+ if (!secrets.chatgptAccountId) {+ await persistCodexChatgptAccountId(deps.db, deps.encryptionKey, accountId)+ }+ return+ }++ const lockToken = randomBytes(16).toString('hex')+ const locked = await acquireCodexSubscriptionRefreshLock(deps.db, lockToken, REFRESH_LOCK_TTL_MS)+ if (!locked) {+ await new Promise(resolve => setTimeout(resolve, 400))+ return+ }+ try {+ const latest = await loadCodexSubscriptionSecrets(deps.db, deps.encryptionKey)+ if (!latest) {+ throw new CodexSubscriptionOAuthError('no_grant', 'encrypted refresh token missing')+ }+ const token = await exchangeRefreshToken(deps, latest.refreshToken)+ accountId = token.chatgptAccountId || chatgptAccountIdFromJwt(token.accessToken) || accountId+ await updateCodexAccessTokenInPlace(deps.db, deps.encryptionKey, latest.credentialRevision, {+ accessToken: token.accessToken,+ accessTokenExpiresAt: token.expiresAt,+ refreshToken: token.refreshToken,+ chatgptAccountId: accountId,+ })+ } catch (err) {+ if (err instanceof CodexSubscriptionStaleRevisionError) {+ throw new CodexSubscriptionOAuthError(+ 'stale_revision',+ 'in-place refresh lost the credential race'+ )+ }+ throw err+ } finally {+ await releaseCodexSubscriptionRefreshLock(deps.db, lockToken)+ }}async function exchangeAuthorizationCode(@@ -495,12 +555,15 @@ function parseTokenResponse(body: Record<string, unknown>): ParsedCodexToken {const accessToken = requiredString(body.access_token, 'access_token')const refreshToken = typeof body.refresh_token === 'string' ? body.refresh_token : nullconst expiresIn = typeof body.expires_in === 'number' ? body.expires_in : null- const subject = subjectFromIdToken(typeof body.id_token === 'string' ? body.id_token : null)+ const idToken = typeof body.id_token === 'string' ? body.id_token : null+ const subject = subjectFromIdToken(idToken)+ const chatgptAccountId = chatgptAccountIdFromJwt(idToken) || chatgptAccountIdFromJwt(accessToken)return {accessToken,refreshToken,expiresAt: expiresIn ? new Date(Date.now() + expiresIn * 1000) : null,accountFingerprint: fingerprintAccount(subject),+ ...(chatgptAccountId ? { chatgptAccountId } : {}),}}diff --git a/control-api/src/services/llmProviderAttemptAuthorizer.ts b/control-api/src/services/llmProviderAttemptAuthorizer.tsindex 964448d7b..4337d5006 100644--- a/control-api/src/services/llmProviderAttemptAuthorizer.ts+++ b/control-api/src/services/llmProviderAttemptAuthorizer.ts@@ -1,6 +1,6 @@-import { createHash } from 'node:crypto'import {LIMITS,+ computeCodexPolicyHash,hashCodexCompletionRequestV1,parseCodexCompletionRequestV1,} from '@clerum/llm-provider-attempt-contract'@@ -9,7 +9,6 @@ import { type DbClient, pool, withTransaction } from '../db.js'import { rootLogger } from '../observability/logger.js'import type { McpHostAccessClaims } from '../utils/auth/mcpHostJwtToken.js'import { isPlainObject } from '../utils/isPlainObject.js'-import { stableStringify } from '../utils/stableStringify.js'import { evaluateBudgetCheck } from './budgets/check.js'import { getActiveReservation } from './budgets/reservations.js'import { getSafeCodexSubscriptionConnection } from './codexSubscriptionConnection.js'@@ -96,22 +95,7 @@ const defaultDeps = (): LlmProviderAttemptAuthorizerDeps => ({issueTicket: issueRegisteredCodexExecutionTicket,})-export function computeCodexPolicyHash(input: {- model: string- catalogRevision: number- credentialRevision: number-}): string {- return createHash('sha256')- .update(- stableStringify({- catalogRevision: input.catalogRevision,- credentialRevision: input.credentialRevision,- model: input.model,- provider: PROVIDER,- })- )- .digest('hex')-}+export { computeCodexPolicyHash }function firstUnknownKey(body: Record<string, unknown>): string | null {for (const key of Object.keys(body)) {diff --git a/control-api/src/services/llmProviderAttemptRedemption.ts b/control-api/src/services/llmProviderAttemptRedemption.tsindex 53de7b46f..60bc4a492 100644--- a/control-api/src/services/llmProviderAttemptRedemption.ts+++ b/control-api/src/services/llmProviderAttemptRedemption.ts@@ -1,12 +1,17 @@import { createHash } from 'node:crypto'import { config } from '../config.js'-import { withTransaction } from '../db.js'+import { pool, withTransaction } from '../db.js'import { deriveOAuthEncryptionKey } from '../oauth/encryption.js'import { rootLogger } from '../observability/logger.js'+import { chatgptAccountIdFromJwt } from './chatgptAccountId.js'import {getSafeCodexSubscriptionConnection,loadCodexSubscriptionSecrets,} from './codexSubscriptionConnection.js'+import {+ CodexSubscriptionOAuthError,+ ensureFreshCodexAccessToken,+} from './codexSubscriptionOAuth.js'import {loadLlmProviderAttempt,lockLlmProviderAttemptTicket,@@ -17,7 +22,8 @@ import { verifyCodexExecutionTicket } from './llmProviderAttemptTicket.js'const log = rootLogger.child({ module: 'llm-provider-attempt-redemption' })export const CODEX_COMPLETIONS_ORIGIN = 'https://chatgpt.com/backend-api/codex/responses'-export const CODEX_CATALOG_ORIGIN = 'https://chatgpt.com/backend-api/codex/models'+export const CODEX_CATALOG_ORIGIN =+ 'https://chatgpt.com/backend-api/codex/models?client_version=1.0.0'export const CODEX_TRANSPORT_PROTOCOL = 'codex-subscription-transport.v1'export const CODEX_MAX_STREAM_DURATION_MS = 300_000@@ -43,6 +49,7 @@ export class LlmProviderAttemptRedeemError extends Error {export type RedeemAttemptSuccess = {accessToken: string+ chatgptAccountId?: stringtransport: {protocolVersion: typeof CODEX_TRANSPORT_PROTOCOLcompletionsOrigin: typeof CODEX_COMPLETIONS_ORIGIN@@ -78,6 +85,7 @@ export type RedeemAttemptDeps = {withTransaction: typeof withTransactionloadSecrets: typeof loadCodexSubscriptionSecretsencryptionKey: Buffer+ ensureFreshAccessToken?: () => Promise<void>}const defaultRedeemDeps = (): RedeemAttemptDeps => ({@@ -85,6 +93,15 @@ const defaultRedeemDeps = (): RedeemAttemptDeps => ({withTransaction,loadSecrets: loadCodexSubscriptionSecrets,encryptionKey: deriveOAuthEncryptionKey(config.oauthEncryptionKey),+ ensureFreshAccessToken: () =>+ ensureFreshCodexAccessToken({+ db: { query: (text, values) => pool.query(text, values) },+ encryptionKey: deriveOAuthEncryptionKey(config.oauthEncryptionKey),+ fetchFn: fetch,+ clientId: config.codexOAuthClientId,+ redirectUri: 'http://127.0.0.1/codex/oauth/callback',+ enabled: config.codexSubscriptionEnabled,+ }),})export async function redeemLlmProviderAttempt(@@ -111,6 +128,22 @@ export async function redeemLlmProviderAttempt(throw new LlmProviderAttemptRedeemError('ticket_invalid', 'hostRef does not match the ticket')}+ if (deps.ensureFreshAccessToken) {+ try {+ await deps.ensureFreshAccessToken()+ } catch (err) {+ if (err instanceof CodexSubscriptionOAuthError) {+ throw new LlmProviderAttemptRedeemError(+ err.code === 'no_grant' || err.code === 'not_connected'+ ? 'no_grant'+ : 'connection_unavailable',+ 'Codex access token could not be refreshed'+ )+ }+ throw err+ }+ }+return deps.withTransaction(async tx => {const ticket = await lockLlmProviderAttemptTicket(tx, claims.jti)if (!ticket) {@@ -175,6 +208,14 @@ export async function redeemLlmProviderAttempt('no usable access token is available')}+ const chatgptAccountId =+ secrets.chatgptAccountId || chatgptAccountIdFromJwt(secrets.accessToken)+ if (!chatgptAccountId) {+ throw new LlmProviderAttemptRedeemError(+ 'connection_unavailable',+ 'Codex access token is missing ChatGPT account id'+ )+ }const expiryClass =secrets.accessTokenExpiresAt &&@@ -188,6 +229,7 @@ export async function redeemLlmProviderAttempt()return {accessToken: secrets.accessToken,+ chatgptAccountId,transport: {protocolVersion: CODEX_TRANSPORT_PROTOCOL,completionsOrigin: CODEX_COMPLETIONS_ORIGIN,diff --git a/control-api/test/db.codexSubscriptionMigration.test.ts b/control-api/test/db.codexSubscriptionMigration.test.tsindex 67e43c747..928070d25 100644--- a/control-api/test/db.codexSubscriptionMigration.test.ts+++ b/control-api/test/db.codexSubscriptionMigration.test.ts@@ -29,14 +29,22 @@ describe('00a0/00a1 Codex subscription persistence', () => {expect(dbSource).toContain("version: '0099_gfs_upload_finalizing_recovery'")expect(dbSource).toContain("version: '00a0_codex_subscription_connections'")expect(dbSource).toContain("version: '00a1_codex_subscription_oauth_states'")+ expect(dbSource).toContain("version: '00a4_codex_chatgpt_account_id'")expect(dbSource).toContain('apply: applyCodexSubscriptionConnectionSchema')expect(dbSource).toContain('apply: applyCodexSubscriptionOAuthStateSchema')+ expect(dbSource).toContain('apply: applyCodexChatgptAccountIdSchema')expect(dbSource.indexOf("version: '0099_gfs_upload_finalizing_recovery'")).toBeLessThan(dbSource.indexOf("version: '00a0_codex_subscription_connections'"))expect(dbSource.indexOf("version: '00a0_codex_subscription_connections'")).toBeLessThan(dbSource.indexOf("version: '00a1_codex_subscription_oauth_states'"))+ expect(dbSource.indexOf("version: '00a3_llm_provider_attempt_tickets'")).toBeLessThan(+ dbSource.indexOf("version: '00a4_codex_chatgpt_account_id'")+ )+ expect(dbSource.indexOf("version: '00a4_codex_chatgpt_account_id'")).toBeLessThan(+ dbSource.indexOf("version: '0100_seed_minimax_allowed_model'")+ )})it('creates a single active deployment-default connection with ciphertext and revisions', () => {@@ -48,6 +56,8 @@ describe('00a0/00a1 Codex subscription persistence', () => {expect(connectionSource).toContain('credential_revision BIGINT NOT NULL')expect(connectionSource).toContain('catalog_revision BIGINT NOT NULL')expect(connectionSource).toContain('account_fingerprint TEXT')+ expect(connectionSource).toContain('chatgpt_account_id_encrypted TEXT')+ expect(connectionSource).toContain('ADD COLUMN IF NOT EXISTS chatgpt_account_id_encrypted')expect(connectionSource).toContain('CREATE UNIQUE INDEX IF NOT EXISTS codex_subscription_connections_active_key')diff --git a/control-api/test/services.chatgptAccountId.test.ts b/control-api/test/services.chatgptAccountId.test.tsnew file mode 100644index 000000000..501452f7e--- /dev/null+++ b/control-api/test/services.chatgptAccountId.test.ts@@ -0,0 +1,21 @@+import { describe, expect, it } from 'vitest'+import { chatgptAccountIdFromJwt } from '../src/services/chatgptAccountId.js'++function jwtWithPayload(payload: Record<string, unknown>): string {+ const encoded = Buffer.from(JSON.stringify(payload)).toString('base64url')+ return `hdr.${encoded}.sig`+}++describe('chatgptAccountIdFromJwt', () => {+ it('reads chatgpt_account_id from the OpenAI auth claim', () => {+ const token = jwtWithPayload({+ sub: 'user-1',+ 'https://api.openai.com/auth': { chatgpt_account_id: 'acct_live_1' },+ })+ expect(chatgptAccountIdFromJwt(token)).toBe('acct_live_1')+ })++ it('returns undefined for opaque tokens', () => {+ expect(chatgptAccountIdFromJwt('opaque-token')).toBeUndefined()+ })+})diff --git a/control-api/test/services.codexSubscriptionCatalog.test.ts b/control-api/test/services.codexSubscriptionCatalog.test.tsindex 3dbf3beb3..c1224f018 100644--- a/control-api/test/services.codexSubscriptionCatalog.test.ts+++ b/control-api/test/services.codexSubscriptionCatalog.test.ts@@ -174,4 +174,17 @@ describe('Codex proxy catalog transport', () => {})expect(fetchFn).toHaveBeenCalledOnce()})++ it('maps a proxy transport failure to unavailable without throwing', async () => {+ const transport = createCodexProxyCatalogTransport({+ adminBaseUrl: 'http://codex-llm-proxy.control-plane.svc:8081',+ fetchFn: async () => {+ throw new TypeError('fetch failed')+ },+ signPermit: () => 'permit',+ })+ await expect(transport.listModels({ accessToken: 'tok-live' })).resolves.toEqual({+ outcome: 'unavailable',+ })+ })})diff --git a/control-api/test/services.codexSubscriptionOAuth.test.ts b/control-api/test/services.codexSubscriptionOAuth.test.tsindex 59bce0744..c9c731ed7 100644--- a/control-api/test/services.codexSubscriptionOAuth.test.ts+++ b/control-api/test/services.codexSubscriptionOAuth.test.ts@@ -16,6 +16,8 @@ const repos = vi.hoisted(() => ({releaseLock: vi.fn(),loadSecrets: vi.fn(),revokeConnection: vi.fn(),+ updateInPlace: vi.fn(),+ persistAccountId: vi.fn(),}))vi.mock('../src/services/codexSubscriptionOAuthState.js', async () => {@@ -41,6 +43,8 @@ vi.mock('../src/services/codexSubscriptionConnection.js', async () => {releaseCodexSubscriptionRefreshLock: repos.releaseLock,loadCodexSubscriptionSecrets: repos.loadSecrets,revokeCodexSubscriptionConnection: repos.revokeConnection,+ updateCodexAccessTokenInPlace: repos.updateInPlace,+ persistCodexChatgptAccountId: repos.persistAccountId,}})@@ -53,6 +57,7 @@ const {CODEX_OAUTH_DEVICE_VERIFICATION_URI,handleCodexBrowserCallback,pollCodexDevice,+ ensureFreshCodexAccessToken,refreshCodexSubscriptionConnection,revokeCodexSubscription,startCodexBrowserConnect,@@ -61,8 +66,13 @@ const {const KEY = deriveOAuthEncryptionKey('ab'.repeat(32))-function idTokenFor(subject: string): string {- const payload = Buffer.from(JSON.stringify({ sub: subject })).toString('base64url')+function idTokenFor(subject: string, accountId?: string): string {+ const payload = Buffer.from(+ JSON.stringify({+ sub: subject,+ ...(accountId ? { chatgpt_account_id: accountId } : {}),+ })+ ).toString('base64url')return `hdr.${payload}.sig`}@@ -386,6 +396,7 @@ describe('codex subscription OAuth broker', () => {refreshToken: 'old-refresh',accessToken: null,accessTokenExpiresAt: null,+ chatgptAccountId: null,credentialRevision: 7,})const fetchFn = vi.fn().mockResolvedValue({@@ -414,11 +425,45 @@ describe('codex subscription OAuth broker', () => {assertNoLeak(refreshed)})+ it('refreshes an expiring access token in place without rotating credential_revision', async () => {+ repos.loadSecrets.mockResolvedValue({+ refreshToken: 'refresh-secret',+ accessToken: 'stale-access',+ accessTokenExpiresAt: new Date(Date.now() + 30_000),+ chatgptAccountId: null,+ credentialRevision: 3,+ })+ repos.acquireLock.mockResolvedValue(true)+ repos.updateInPlace.mockResolvedValue({+ connectionKey: 'deployment-default',+ status: 'connected',+ credentialRevision: 3,+ accountFingerprint: 'fp',+ })+ const fetchFn = vi.fn().mockResolvedValue({+ ok: true,+ json: async () => ({+ access_token: 'access-secret',+ id_token: idTokenFor('acct_raw_123', 'acct_live_9'),+ }),+ })+ await ensureFreshCodexAccessToken(deps(fetchFn))+ expect(repos.updateInPlace).toHaveBeenCalledWith(+ expect.anything(),+ KEY,+ 3,+ expect.objectContaining({ accessToken: 'access-secret', chatgptAccountId: 'acct_live_9' })+ )+ expect(repos.rotate).not.toHaveBeenCalled()+ expect(repos.releaseLock).toHaveBeenCalled()+ })+it('revokes locally even when upstream revoke fails', async () => {repos.loadSecrets.mockResolvedValue({refreshToken: 'refresh-secret',accessToken: 'access-secret',accessTokenExpiresAt: null,+ chatgptAccountId: null,credentialRevision: 3,})repos.revokeConnection.mockResolvedValue({diff --git a/control-api/test/services.llmProviderAttemptRedemption.realPostgres.integration.test.ts b/control-api/test/services.llmProviderAttemptRedemption.realPostgres.integration.test.tsindex cd52f47e8..d061e4561 100644--- a/control-api/test/services.llmProviderAttemptRedemption.realPostgres.integration.test.ts+++ b/control-api/test/services.llmProviderAttemptRedemption.realPostgres.integration.test.ts@@ -46,6 +46,7 @@ describeRealPostgres('Codex ticket redemption on real PostgreSQL', () => {await insertInitialCodexSubscriptionConnection(pool, KEY, {refreshToken: 'refresh-secret',accessToken: 'access-usable',+ chatgptAccountId: 'acct_test_1',accountFingerprint: 'fp-redeem',})}, 60_000)@@ -128,6 +129,7 @@ describeRealPostgres('Codex ticket redemption on real PostgreSQL', () => {expect(losses).toHaveLength(19)if (wins[0]?.status === 'fulfilled') {expect(wins[0].value.accessToken).toBe('access-usable')+ expect(wins[0].value.chatgptAccountId).toBe('acct_test_1')expect(JSON.stringify(wins[0].value)).not.toContain('refresh-secret')expect(wins[0].value.attemptReceipt).toBe(opaqueAttemptReceipt({diff --git a/control-api/test/services.llmProviderAttemptRedemption.test.ts b/control-api/test/services.llmProviderAttemptRedemption.test.tsindex 856b8d039..a36c42eab 100644--- a/control-api/test/services.llmProviderAttemptRedemption.test.ts+++ b/control-api/test/services.llmProviderAttemptRedemption.test.ts@@ -91,12 +91,14 @@ describe('redeemLlmProviderAttempt', () => {refreshToken: 'refresh-secret',accessToken: 'access-usable',accessTokenExpiresAt: new Date(Date.now() + 120_000),+ chatgptAccountId: 'acct_live_1',credentialRevision: 3,}),encryptionKey: Buffer.alloc(32),})expect(result.accessToken).toBe('access-usable')+ expect(result.chatgptAccountId).toBe('acct_live_1')expect(result.transport.completionsOrigin).toBe('https://chatgpt.com/backend-api/codex/responses')@@ -148,4 +150,24 @@ describe('redeemLlmProviderAttempt', () => {)).rejects.toMatchObject({ code: 'disabled' })})++ it('refuses to redeem when the ChatGPT account id is missing', async () => {+ await expect(+ redeemLlmProviderAttempt(+ { executionTicket: 'ticket', requestHash: CLAIMS.requestHash },+ {+ enabled: true,+ withTransaction: async work => work({ query: vi.fn() } as never),+ loadSecrets: async () => ({+ refreshToken: 'refresh-secret',+ accessToken: 'opaque-token',+ accessTokenExpiresAt: new Date(Date.now() + 120_000),+ chatgptAccountId: null,+ credentialRevision: 3,+ }),+ encryptionKey: Buffer.alloc(32),+ }+ )+ ).rejects.toMatchObject({ code: 'connection_unavailable' })+ })})diff --git a/deploy/base/control-plane/networkpolicies.yaml b/deploy/base/control-plane/networkpolicies.yamlindex dd326cb43..059b05927 100644--- a/deploy/base/control-plane/networkpolicies.yaml+++ b/deploy/base/control-plane/networkpolicies.yaml@@ -625,6 +625,31 @@ spec:- port: 8082protocol: TCP---+# Allow control-api catalog/health admin calls to the Codex LLM proxy.+# deny-all-control-plane blocks intra-namespace traffic; proxy ingress already+# admits control-api:8081, but control-api egress must match.+apiVersion: networking.k8s.io/v1+kind: NetworkPolicy+metadata:+ name: control-api-to-codex-llm-proxy+ namespace: control-plane+ labels:+ clerum.io/policy-type: service+spec:+ podSelector:+ matchLabels:+ app: control-api+ policyTypes:+ - Egress+ egress:+ - to:+ - podSelector:+ matchLabels:+ app: codex-llm-proxy+ ports:+ - port: 8081+ protocol: TCP+---# Ingress side: allow control-api to reach WRCapiVersion: networking.k8s.io/v1kind: NetworkPolicydiff --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.mdindex a013a4c52..f24e6357a 100644--- a/docs/architecture/codex-subscription-transport-contract.md+++ b/docs/architecture/codex-subscription-transport-contract.md@@ -25,7 +25,7 @@ Exact HTTPS origins (no caller-supplied URL or header):- OAuth device callback: `https://auth.openai.com/deviceauth/callback`- OAuth device verification: `https://auth.openai.com/codex/device`- OAuth revoke: `https://auth.openai.com/oauth/revoke`-- Catalog: `https://chatgpt.com/backend-api/codex/models`+- Catalog: `https://chatgpt.com/backend-api/codex/models?client_version=1.0.0`- Completions: `https://chatgpt.com/backend-api/codex/responses`Forbidden examples: `https://api.openai.com/v1/chat/completions` (ordinary APIdiff --git a/mcp-host/src/config.ts b/mcp-host/src/config.tsindex 37d22ddf4..26373d5d6 100644--- a/mcp-host/src/config.ts+++ b/mcp-host/src/config.ts@@ -64,6 +64,8 @@ export interface Config {codexSubscriptionEnabled: boolean// Server-owned Codex proxy URL. Callers cannot override this per request.codexProxyRuntimeBaseUrl: string+ // Explicit authorize override for tests/dev. Host chat ignores an empty hash+ // and binds policyRevision/policyHash from the allowlist ConfigMap instead.codexPolicyRevision: numbercodexPolicyHash: string@@ -655,6 +657,9 @@ export const config: Config = {'CODEX_LLM_PROXY_RUNTIME_URL','http://codex-llm-proxy.control-plane.svc.cluster.local:8080')!,+ // Empty hash is not a grant: Host chat computes the per-model digest from+ // clerum.io/catalog-revision + clerum.io/connection-revision on the allowlist+ // ConfigMap. Set both env vars together only as a test/dev override.codexPolicyRevision: parseInt(getEnv('CODEX_POLICY_REVISION', '1')!, 10),codexPolicyHash: getEnv('CODEX_POLICY_HASH', '')!,diff --git a/mcp-host/src/config/configStore.test.ts b/mcp-host/src/config/configStore.test.tsindex ad40205ec..935a6d296 100644--- a/mcp-host/src/config/configStore.test.ts+++ b/mcp-host/src/config/configStore.test.ts@@ -1,7 +1,12 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { register } from 'prom-client'import { ALL_PROVIDERS, descriptorFor } from '../llm/registryCore'-import { ConfigStore, PROVIDER_ENV_NAMES } from './configStore'+import {+ CATALOG_REVISION_ANNOTATION,+ CONNECTION_REVISION_ANNOTATION,+ ConfigStore,+ PROVIDER_ENV_NAMES,+} from './configStore'const ALLOWLIST_CM = 'clerum-llm-allowed-models'const ALLOWLIST_WATCH_KEY = `/api/v1/namespaces/mcp-host/configmaps|metadata.name=${ALLOWLIST_CM}`@@ -71,9 +76,11 @@ interface FakeCoreApi {function makeFakeCoreApi(initial: {secrets?: Record<string, Record<string, string>>configMaps?: Record<string, Record<string, string>>+ configMapAnnotations?: Record<string, Record<string, string>>}): FakeCoreApi {const secrets = { ...(initial.secrets ?? {}) }const configMaps = { ...(initial.configMaps ?? {}) }+ const configMapAnnotations = { ...(initial.configMapAnnotations ?? {}) }return {readNamespacedSecret: vi.fn(async (req: { name: string }) => {const data = secrets[req.name]@@ -89,7 +96,10 @@ function makeFakeCoreApi(initial: {const err: { code?: number; statusCode?: number } = { code: 404, statusCode: 404 }throw err}- return { data }+ return {+ data,+ metadata: { name: req.name, annotations: configMapAnnotations[req.name] },+ }}),}}@@ -99,10 +109,15 @@ function build(opts: {provider?: 'openai' | 'claude' | 'zai' | 'bailian' | nullsecrets?: Record<string, Record<string, string>>configMaps?: Record<string, Record<string, string>>+ configMapAnnotations?: Record<string, Record<string, string>>allowlistConfigMapName?: string | null}): { store: ConfigStore; watch: FakeWatch; api: FakeCoreApi } {const watch = makeFakeWatch()- const api = makeFakeCoreApi({ secrets: opts.secrets, configMaps: opts.configMaps })+ const api = makeFakeCoreApi({+ secrets: opts.secrets,+ configMaps: opts.configMaps,+ configMapAnnotations: opts.configMapAnnotations,+ })const store = new ConfigStore({namespace: 'mcp-host',hostRef: 'trader',@@ -539,6 +554,51 @@ describe('ConfigStore — allowlist tier (R3)', () => {{ model: 'gpt-5.4', displayName: 'GPT 5.4', contextWindowTokens: 400000, vendor: 'OpenAI' },])expect(store.allowedModels().get('claude')).toEqual([{ model: 'claude-opus-4-8' }])+ expect(store.codexPolicyBinding()).toBeNull()+ })++ it('exposes Codex catalog/credential revisions from allowlist annotations', async () => {+ const built = build({+ provider: 'openai',+ secrets: { 'chatllm-api-keys': { 'openai-api-key': 'sk' } },+ allowlistConfigMapName: ALLOWLIST_CM,+ configMaps: {+ [ALLOWLIST_CM]: { openai: JSON.stringify([{ model: 'gpt-5.4' }]) },+ },+ configMapAnnotations: {+ [ALLOWLIST_CM]: {+ [CATALOG_REVISION_ANNOTATION]: '7',+ [CONNECTION_REVISION_ANNOTATION]: '3',+ },+ },+ })+ store = built.store+ await store.start()++ expect(store.codexPolicyBinding()).toEqual({+ catalogRevision: 7,+ credentialRevision: 3,+ })++ const handle = built.watch.active.get(ALLOWLIST_WATCH_KEY)+ expect(handle).toBeDefined()+ handle!.emit('MODIFIED', {+ metadata: {+ name: ALLOWLIST_CM,+ annotations: {+ [CATALOG_REVISION_ANNOTATION]: '8',+ [CONNECTION_REVISION_ANNOTATION]: '3',+ },+ },+ data: { openai: JSON.stringify([{ model: 'gpt-5.4' }]) },+ })+ expect(store.codexPolicyBinding()).toEqual({+ catalogRevision: 8,+ credentialRevision: 3,+ })++ handle!.emit('DELETED', { metadata: { name: ALLOWLIST_CM } })+ expect(store.codexPolicyBinding()).toBeNull()})it('hot-reloads: a MODIFIED event makes the new allowlist visible + fires allowlistChanged', async () => {diff --git a/mcp-host/src/config/configStore.ts b/mcp-host/src/config/configStore.tsindex f9001af1f..6bd4c7e1d 100644--- a/mcp-host/src/config/configStore.ts+++ b/mcp-host/src/config/configStore.ts@@ -107,6 +107,20 @@ export interface AllowedModelEntry {vendor?: string}+/**+ * Catalog/credential pair projected onto `clerum-llm-allowed-models` by+ * control-api. Host chat hashes this with the selected model to authorize a+ * Codex attempt. Annotation names are a cross-service contract with+ * control-api, HCC, and WRC.+ */+export type CodexPolicyBinding = {+ catalogRevision: number+ credentialRevision: number+}++export const CATALOG_REVISION_ANNOTATION = 'clerum.io/catalog-revision'+export const CONNECTION_REVISION_ANNOTATION = 'clerum.io/connection-revision'+export type ConfigStoreChangeHandler = (change: ConfigStoreChange) => voidexport interface ConfigStoreOptions {@@ -228,6 +242,8 @@ export class ConfigStore {private allowlistDelivered = false/** Dedupe flag for the "CM absent" WARN + metric (fires once per transition). */private allowlistMissingWarned = false+ /** Catalog/credential pair from allowlist CM annotations, or null. */+ private codexBinding: CodexPolicyBinding | null = nullconstructor(opts: ConfigStoreOptions) {this.opts = opts@@ -459,6 +475,16 @@ export class ConfigStore {return this.allowlistDelivered}+ /**+ * Live Codex catalog/credential revisions from the allowlist ConfigMap+ * annotations. Null when the CM is absent, the annotations are missing, or+ * either value is not an integer. Callers must still require catalogRevision+ * >= 1 before authorizing.+ */+ codexPolicyBinding(): CodexPolicyBinding | null {+ return this.codexBinding+ }+// ─── Bootstrap (initial list) ────────────────────────────────────────private async bootstrapLlmSecret(): Promise<void> {@@ -506,7 +532,7 @@ export class ConfigStore {namespace: this.opts.namespace,})const wasAvailable = this.allowlistDelivered- const contentChanged = this.applyAllowlistData(cm.data ?? {})+ const contentChanged = this.applyAllowlistConfigMap(cm)this.allowlistDelivered = truethis.allowlistMissingWarned = falsereturn contentChanged || !wasAvailable@@ -519,6 +545,22 @@ export class ConfigStore {}}+ /**+ * Parse models plus Codex catalog/credential annotations from the allowlist+ * ConfigMap. Returns whether either surface changed so annotation-only+ * catalog bumps still refresh Host chat's authorize binding.+ */+ private applyAllowlistConfigMap(cm: {+ data?: Record<string, string>+ metadata?: { annotations?: Record<string, string> }+ }): boolean {+ const modelsChanged = this.applyAllowlistData(cm.data ?? {})+ const nextBinding = parseCodexPolicyBinding(cm.metadata?.annotations)+ const bindingChanged = !codexBindingsEqual(this.codexBinding, nextBinding)+ this.codexBinding = nextBinding+ return modelsChanged || bindingChanged+ }+/*** Parse the allowlist ConfigMap data — one key per provider, value a JSON* array of {@link AllowedModelEntry}. Per-key fail-loud: a key with invalid@@ -592,6 +634,7 @@ export class ConfigStore {const wasAvailable = this.allowlistDeliveredthis.allowlistDelivered = falsethis.allowedModelsMap = new Map()+ this.codexBinding = nullif (!this.allowlistMissingWarned) {this.allowlistMissingWarned = truellmAllowlistMissingTotal.inc()@@ -701,9 +744,7 @@ export class ConfigStore {}if (type !== 'ADDED' && type !== 'MODIFIED') returnconst wasAvailable = this.allowlistDelivered- const contentChanged = this.applyAllowlistData(- (obj.data as Record<string, string> | undefined) ?? {}- )+ const contentChanged = this.applyAllowlistConfigMap(obj)this.allowlistDelivered = truethis.allowlistMissingWarned = false// Only notify subscribers on a real transition — a re-delivered,@@ -857,10 +898,45 @@ export class ConfigStore {}interface KubeObject {- metadata?: { name?: string }+ metadata?: { name?: string; annotations?: Record<string, string> }data?: Record<string, string>}+function parseOptionalIntegerAnnotation(+ annotations: Record<string, string>,+ key: string+): number | null | 'invalid' {+ if (!(key in annotations)) return null+ const parsed = Number(annotations[key])+ return Number.isInteger(parsed) ? parsed : 'invalid'+}++function parseCodexPolicyBinding(+ annotations: Record<string, string> | undefined+): CodexPolicyBinding | null {+ if (!annotations) return null+ const catalogRevision = parseOptionalIntegerAnnotation(annotations, CATALOG_REVISION_ANNOTATION)+ const credentialRevision = parseOptionalIntegerAnnotation(+ annotations,+ CONNECTION_REVISION_ANNOTATION+ )+ if (+ catalogRevision === null ||+ catalogRevision === 'invalid' ||+ credentialRevision === null ||+ credentialRevision === 'invalid'+ ) {+ return null+ }+ return { catalogRevision, credentialRevision }+}++function codexBindingsEqual(a: CodexPolicyBinding | null, b: CodexPolicyBinding | null): boolean {+ if (a === b) return true+ if (!a || !b) return false+ return a.catalogRevision === b.catalogRevision && a.credentialRevision === b.credentialRevision+}+/*** Deep, order-sensitive equality for two parsed allowlist maps. Used to decide* whether a re-delivered allowlist ConfigMap actually changed (so routine watchdiff --git a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.tsindex c75c38041..8820056ac 100644--- a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts+++ b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts@@ -61,4 +61,48 @@ describe('CodexLlmProxyClient', () => {).rejects.toMatchObject({ code: 'canceled' })expect(client['options'].fetchFn).not.toHaveBeenCalled()})++ it('surfaces the proxy error code with the HTTP status', async () => {+ const client = new CodexLlmProxyClient({+ runtimeUrl: resolveCodexProxyRuntimeUrl(+ 'http://codex-llm-proxy.control-plane.svc.cluster.local:8080'+ ),+ readPlatformJwt: () => 'platform-jwt',+ fetchFn: vi.fn().mockResolvedValue({+ ok: false,+ status: 403,+ json: async () => ({ error: 'origin_denied' }),+ }) as unknown as typeof fetch,+ })+ await expect(+ client.stream({+ executionTicket: 'ticket-123456',+ requestHash: 'a'.repeat(64),+ request: {},+ })+ ).rejects.toMatchObject({+ code: 'origin_denied',+ message: 'proxy stream failed with 403 (origin_denied)',+ })+ })++ it('fails closed when the proxy emits an SSE error frame after headers', async () => {+ const client = new CodexLlmProxyClient({+ runtimeUrl: resolveCodexProxyRuntimeUrl(+ 'http://codex-llm-proxy.control-plane.svc.cluster.local:8080'+ ),+ readPlatformJwt: () => 'platform-jwt',+ fetchFn: vi.fn().mockResolvedValue({+ ok: true,+ body: sse([{ type: 'error', code: 'origin_denied' }]),+ }) as unknown as typeof fetch,+ })+ await expect(+ client.stream({+ executionTicket: 'ticket-123456',+ requestHash: 'a'.repeat(64),+ request: {},+ })+ ).rejects.toMatchObject({ code: 'origin_denied' })+ })})diff --git a/mcp-host/src/llm/__tests__/codexPolicyBinding.test.ts b/mcp-host/src/llm/__tests__/codexPolicyBinding.test.tsnew file mode 100644index 000000000..43c8700ac--- /dev/null+++ b/mcp-host/src/llm/__tests__/codexPolicyBinding.test.ts@@ -0,0 +1,78 @@+import { afterEach, describe, expect, it } from 'vitest'+import { computeCodexPolicyHash } from '@clerum/llm-provider-attempt-contract'+import {+ readLiveCodexPolicyBinding,+ resolveCodexAttemptPolicy,+ setCodexPolicyBindingReader,+} from '../codexPolicyBinding'++describe('resolveCodexAttemptPolicy', () => {+ afterEach(() => {+ setCodexPolicyBindingReader(() => null)+ })++ it('uses a well-formed env override without consulting the live binding', () => {+ const envHash = 'c'.repeat(64)+ expect(+ resolveCodexAttemptPolicy({+ model: 'gpt-5.6-luna',+ envRevision: 1,+ envHash,+ binding: { catalogRevision: 7, credentialRevision: 3 },+ })+ ).toEqual({ policyRevision: 1, policyHash: envHash })+ })++ it('computes the per-model hash from the allowlist catalog/credential pair', () => {+ const binding = { catalogRevision: 7, credentialRevision: 3 }+ const resolved = resolveCodexAttemptPolicy({+ model: 'gpt-5.6-luna',+ envRevision: 1,+ envHash: '',+ binding,+ })+ expect(resolved).toEqual({+ policyRevision: 7,+ policyHash: computeCodexPolicyHash({+ model: 'gpt-5.6-luna',+ catalogRevision: 7,+ credentialRevision: 3,+ }),+ })+ expect(+ resolveCodexAttemptPolicy({+ model: 'gpt-5.6-sol',+ envRevision: 1,+ envHash: '',+ binding,+ })?.policyHash+ ).not.toBe(resolved?.policyHash)+ })++ it('returns null when the catalog revision is missing or not yet synced', () => {+ expect(+ resolveCodexAttemptPolicy({+ model: 'gpt-5.6-luna',+ envRevision: 1,+ envHash: '',+ binding: null,+ })+ ).toBeNull()+ expect(+ resolveCodexAttemptPolicy({+ model: 'gpt-5.6-luna',+ envRevision: 1,+ envHash: '',+ binding: { catalogRevision: 0, credentialRevision: 3 },+ })+ ).toBeNull()+ })++ it('exposes the registered live reader to Host chat construction', () => {+ setCodexPolicyBindingReader(() => ({ catalogRevision: 7, credentialRevision: 3 }))+ expect(readLiveCodexPolicyBinding()).toEqual({+ catalogRevision: 7,+ credentialRevision: 3,+ })+ })+})diff --git a/mcp-host/src/llm/__tests__/codexSubscription.test.ts b/mcp-host/src/llm/__tests__/codexSubscription.test.tsindex d9451f6d0..72f14fdda 100644--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts@@ -1,6 +1,10 @@import { describe, expect, it, vi } from 'vitest'import { LlmErrorCode } from '../../core/errors'-import { CodexSubscriptionProvider } from '../codexSubscription'+import {+ CodexSubscriptionProvider,+ isCodexNativeToolName,+ selectCodexAdvertisedTools,+} from '../codexSubscription'import { CodexAuthorizeError } from '../providerAttemptAuthorizer'import { makeProvider } from '../registry'@@ -28,11 +32,11 @@ function deps(overrides?: {return {authorizer: { authorize },proxy: { stream },- attemptContext: () => ({+ attemptContext: vi.fn(() => ({policyRevision: 1,policyHash: 'b'.repeat(64),hostRef: 'chatllm',- }),+ })),authorize,stream,}@@ -79,6 +83,33 @@ describe('CodexSubscriptionProvider', () => {expect(result.tool_calls).toEqual([{ id: 'c1', name: 'echo', arguments: { x: 1 } }])})+ it('preserves assistant toolCalls and tool results in the authorize request', async () => {+ const wired = deps()+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)+ await provider.completeSingleTurnWithTools(+ [+ { role: 'user', content: 'hi' },+ {+ role: 'assistant',+ content: '',+ tool_calls: [{ id: 'call-1', name: 'echo', arguments: { x: 1 } }],+ },+ { role: 'tool', content: 'ok', tool_call_id: 'call-1', name: 'echo' },+ ],+ [{ name: 'echo', description: 'echo', parameters: {} }]+ )+ const authorizedBody = wired.authorize.mock.calls[0][0]+ expect(authorizedBody.request.messages).toEqual([+ { role: 'user', content: 'hi' },+ {+ role: 'assistant',+ content: '',+ toolCalls: [{ id: 'call-1', name: 'echo', arguments: { x: 1 } }],+ },+ { role: 'tool', content: 'ok', toolCallId: 'call-1', name: 'echo' },+ ])+ })+it('does not call the proxy when aborted before authorize', async () => {const wired = deps()const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)@@ -103,6 +134,27 @@ describe('CodexSubscriptionProvider', () => {)})+ it('passes the selected model into attemptContext', async () => {+ const wired = deps()+ const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)+ await provider.completeSingleTurn([{ role: 'user', content: 'hi' }])+ expect(wired.attemptContext).toHaveBeenCalledWith({ model: 'gpt-5.6-luna' })+ })++ it('refuses to authorize without a catalog policy binding', async () => {+ const wired = deps()+ wired.attemptContext = vi.fn().mockReturnValue({ policyRevision: 0, policyHash: '' })+ const provider = new CodexSubscriptionProvider('gpt-5.6-luna', wired as never)+ await expect(+ provider.completeSingleTurn([{ role: 'user', content: 'hi' }])+ ).rejects.toMatchObject({+ name: 'CodexAuthorizeError',+ code: 'no_grant',+ })+ expect(wired.authorize).not.toHaveBeenCalled()+ expect(wired.stream).not.toHaveBeenCalled()+ })+it('keeps unknown usage unknown when the proxy omits token counts', async () => {const wired = deps()const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)@@ -111,6 +163,77 @@ describe('CodexSubscriptionProvider', () => {expect(result.usage).toEqual({ input_tokens: 0, output_tokens: 0, total_tokens: 0 })})+ it('omits MCP tools and caps natives at the Codex maxTools limit', () => {+ const tools = [+ { name: 'file_read', description: 'read', parameters: {} },+ {+ name: 'mongodb-mcp-stack-mongodb-mcp-server__find',+ description: 'find',+ parameters: {},+ },+ { name: 'clerum__gfs_read', description: 'gfs', parameters: {} },+ {+ name: 'mongodb-mcp-stack-mongodb-mcp-server__aggregate',+ description: 'agg',+ parameters: {},+ },+ ]+ expect(isCodexNativeToolName('file_read')).toBe(true)+ expect(isCodexNativeToolName('clerum__gfs_read')).toBe(true)+ expect(isCodexNativeToolName('mongodb-mcp-stack-mongodb-mcp-server__find')).toBe(false)+ expect(selectCodexAdvertisedTools(tools).map(tool => tool.name)).toEqual([+ 'file_read',+ 'clerum__gfs_read',+ ])++ const overflow = Array.from({ length: 40 }, (_, i) => ({+ name: i < 36 ? `native_${i}` : `mongo-server__tool_${i}`,+ description: 't',+ parameters: {},+ }))+ const advertised = selectCodexAdvertisedTools(overflow)+ expect(advertised).toHaveLength(32)+ expect(advertised.every(tool => isCodexNativeToolName(tool.name))).toBe(true)+ expect(advertised[0]?.name).toBe('native_0')+ expect(advertised[31]?.name).toBe('native_31')+ })++ it('authorizes with native tools only when MCP tools are also offered', async () => {+ const wired = deps()+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)+ await provider.completeSingleTurnWithTools(+ [{ role: 'user', content: 'hi' }],+ [+ { name: 'file_read', description: 'read', parameters: {} },+ {+ name: 'mongodb-mcp-stack-mongodb-mcp-server__find',+ description: 'find',+ parameters: {},+ },+ { name: 'clerum__tool_search', description: 'search', parameters: {} },+ ]+ )+ const authorizedBody = wired.authorize.mock.calls[0][0]+ expect(authorizedBody.request.tools.map((tool: { name: string }) => tool.name)).toEqual([+ 'file_read',+ 'clerum__tool_search',+ ])+ })++ it('does not treat an unknown empty stream as a successful stop', async () => {+ const wired = deps({+ stream: vi.fn().mockResolvedValue({+ text: '',+ toolCalls: [],+ outcome: 'unknown',+ }),+ })+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', wired as never)+ await expect(+ provider.completeSingleTurn([{ role: 'user', content: 'hi' }])+ ).rejects.toMatchObject({ code: 'provider_unavailable' })+ })+it('keeps insufficient_scope distinguishable', () => {const provider = new CodexSubscriptionProvider('gpt-5.3-codex', deps() as never)const classified = provider.classifyError(diff --git a/mcp-host/src/llm/codexLlmProxyClient.ts b/mcp-host/src/llm/codexLlmProxyClient.tsindex b65fdd577..5b23d6c1f 100644--- a/mcp-host/src/llm/codexLlmProxyClient.ts+++ b/mcp-host/src/llm/codexLlmProxyClient.ts@@ -13,7 +13,12 @@ export class CodexProxyError extends Error {export type CodexProxyFrame =| { type: 'text'; text: string }| { type: 'tool_call'; id: string; name: string; arguments: Record<string, unknown> }- | { type: 'done'; outcome: 'success' | 'canceled' | 'error' | 'unknown' }+ | {+ type: 'done'+ outcome: 'success' | 'canceled' | 'error' | 'unknown'+ usage?: { inputTokens: number; outputTokens: number }+ }+ | { type: 'error'; code: string }export type CodexProxyStreamResult = {text: string@@ -65,7 +70,7 @@ export class CodexLlmProxyClient {if (!response.ok) {const payload = (await response.json().catch(() => ({}))) as Record<string, unknown>const code = typeof payload.error === 'string' ? payload.error : 'provider_unavailable'- throw new CodexProxyError(code, `proxy stream failed with ${response.status}`)+ throw new CodexProxyError(code, `proxy stream failed with ${response.status} (${code})`)}if (!response.body) {throw new CodexProxyError('provider_unavailable', 'proxy stream had no body')@@ -87,6 +92,7 @@ async function readProxySse(body: ReadableStream<Uint8Array>): Promise<CodexProxlet text = ''const toolCalls: CodexProxyStreamResult['toolCalls'] = []let outcome: CodexProxyStreamResult['outcome'] = 'unknown'+ let usage: CodexProxyStreamResult['usage']while (true) {const { done, value } = await reader.read()if (done) break@@ -97,10 +103,16 @@ async function readProxySse(body: ReadableStream<Uint8Array>): Promise<CodexProxconst line = part.split('\n').find(entry => entry.startsWith('data: '))if (!line) continueconst frame = JSON.parse(line.slice(6)) as CodexProxyFrame+ if (frame.type === 'error') {+ throw new CodexProxyError(frame.code, `proxy stream failed with ${frame.code}`)+ }if (frame.type === 'text') text += frame.textif (frame.type === 'tool_call') toolCalls.push(frame)- if (frame.type === 'done') outcome = frame.outcome+ if (frame.type === 'done') {+ outcome = frame.outcome+ if (frame.usage) usage = frame.usage+ }}}- return { text, toolCalls, outcome }+ return { text, toolCalls, outcome, ...(usage ? { usage } : {}) }}diff --git a/mcp-host/src/llm/codexPolicyBinding.ts b/mcp-host/src/llm/codexPolicyBinding.tsnew file mode 100644index 000000000..f560f5fee--- /dev/null+++ b/mcp-host/src/llm/codexPolicyBinding.ts@@ -0,0 +1,67 @@+import { computeCodexPolicyHash } from '@clerum/llm-provider-attempt-contract'++const POLICY_HASH_RE = /^[a-f0-9]{64}$/++export type CodexPolicyBinding = {+ catalogRevision: number+ credentialRevision: number+}++export type CodexAttemptPolicy = {+ policyRevision: number+ policyHash: string+}++let reader: () => CodexPolicyBinding | null = () => null++/**+ * Host chat reads catalog/credential revisions from the allowlist ConfigMap+ * (projected by control-api). main.ts registers ConfigStore as the live reader.+ */+export function setCodexPolicyBindingReader(fn: () => CodexPolicyBinding | null): void {+ reader = fn+}++export function readLiveCodexPolicyBinding(): CodexPolicyBinding | null {+ return reader()+}++/**+ * Prefer an explicit env override (tests/dev) when both revision and hash are+ * well-formed. Otherwise bind to the live allowlist catalog/credential pair and+ * hash it for the selected model — the hash is per-model, so a single env hash+ * cannot cover a Host with multiple enabled Codex models.+ */+export function resolveCodexAttemptPolicy(input: {+ model: string+ envRevision: number+ envHash: string+ binding: CodexPolicyBinding | null+}): CodexAttemptPolicy | null {+ const envHash = input.envHash.trim()+ if (+ POLICY_HASH_RE.test(envHash) &&+ Number.isInteger(input.envRevision) &&+ input.envRevision >= 1+ ) {+ return { policyRevision: input.envRevision, policyHash: envHash }+ }+ const binding = input.binding+ if (+ !binding ||+ !Number.isInteger(binding.catalogRevision) ||+ binding.catalogRevision < 1 ||+ !Number.isInteger(binding.credentialRevision) ||+ binding.credentialRevision < 0+ ) {+ return null+ }+ return {+ policyRevision: binding.catalogRevision,+ policyHash: computeCodexPolicyHash({+ model: input.model,+ catalogRevision: binding.catalogRevision,+ credentialRevision: binding.credentialRevision,+ }),+ }+}diff --git a/mcp-host/src/llm/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.tsindex 111b3c696..d2382db60 100644--- a/mcp-host/src/llm/codexSubscription.ts+++ b/mcp-host/src/llm/codexSubscription.ts@@ -1,6 +1,7 @@import { randomUUID } from 'node:crypto'import {type CodexCompletionRequestV1,+ LIMITS,hashCodexCompletionRequestV1,} from '@clerum/llm-provider-attempt-contract'import { LlmErrorCode } from '../core/errors'@@ -55,7 +56,42 @@ function mapCodexUsage(usage?: { inputTokens: number; outputTokens: number }): {export type CodexSubscriptionDeps = {authorizer: ProviderAttemptAuthorizerproxy: CodexLlmProxyClient- attemptContext: () => CodexAttemptContext+ attemptContext: (input: { model: string }) => CodexAttemptContext+}++/**+ * Codex authorize is capped at `LIMITS.maxTools` (32). MCP tools use+ * `serverName__toolName`; native tools are unprefixed or `clerum__*`.+ * Drop MCP tools first so a Host with a large MCP catalog can still+ * authorize. If natives still exceed the cap, keep the leading native+ * slice in registry order.+ */+export function isCodexNativeToolName(name: string): boolean {+ const idx = name.indexOf('__')+ if (idx <= 0) return true+ return name.startsWith('clerum__')+}++function assertTerminalCodexOutcome(result: {+ text: string+ toolCalls: Array<{ id: string; name: string; arguments: Record<string, unknown> }>+ outcome: 'success' | 'canceled' | 'error' | 'unknown'+}): void {+ if (result.outcome === 'error') {+ throw new CodexProxyError('provider_unavailable', 'proxy stream ended with an error outcome')+ }+ if (result.outcome === 'unknown' && !result.text && result.toolCalls.length === 0) {+ throw new CodexProxyError(+ 'provider_unavailable',+ 'proxy stream ended without a terminal outcome'+ )+ }+}++export function selectCodexAdvertisedTools(tools: ToolDefinition[]): ToolDefinition[] {+ const natives = tools.filter(tool => isCodexNativeToolName(tool.name))+ if (natives.length <= LIMITS.maxTools) return natives+ return natives.slice(0, LIMITS.maxTools)}export class CodexSubscriptionProvider implements SingleTurnProvider {@@ -77,7 +113,12 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {classifyError(err: unknown): ClassifiedError {const code =err instanceof CodexAuthorizeError || err instanceof CodexProxyError ? err.code : undefined- if (code === 'insufficient_scope' || code === 'no_grant' || code === 'host_binding_mismatch') {+ if (+ code === 'insufficient_scope' ||+ code === 'no_grant' ||+ code === 'host_binding_mismatch' ||+ code === 'origin_denied'+ ) {return {code: LlmErrorCode.AuthenticationFailed,retryable: false,@@ -117,12 +158,16 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {options?: { max_tokens?: number; temperature?: number; signal?: AbortSignal }): Promise<CompletionResponse> {const result = await this.execute(messages, undefined, options)+ assertTerminalCodexOutcome(result)const usage = mapCodexUsage(result.usage)return {content: result.text,usage: usage.usage,usage_reported: usage.usage_reported,- finish_reason: result.outcome === 'canceled' ? FinishReason.Unknown : FinishReason.Stop,+ finish_reason:+ result.outcome === 'canceled' || result.outcome === 'unknown'+ ? FinishReason.Unknown+ : FinishReason.Stop,}}@@ -137,6 +182,7 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {}): Promise<ToolCompletionResponse> {const result = await this.execute(messages, tools, options)+ assertTerminalCodexOutcome(result)const usage = mapCodexUsage(result.usage)return {content: result.text || null,@@ -150,7 +196,12 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {: null,usage: usage.usage,usage_reported: usage.usage_reported,- finish_reason: result.toolCalls.length > 0 ? FinishReason.ToolUse : FinishReason.Stop,+ finish_reason:+ result.toolCalls.length > 0+ ? FinishReason.ToolUse+ : result.outcome === 'canceled' || result.outcome === 'unknown'+ ? FinishReason.Unknown+ : FinishReason.Stop,}}@@ -169,7 +220,14 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {}const request = this.buildRequest(messages, tools, options)const requestHash = hashCodexCompletionRequestV1(request)- const context = this.deps.attemptContext()+ const context = this.deps.attemptContext({ model: this.model })+ if (+ !Number.isInteger(context.policyRevision) ||+ context.policyRevision < 1 ||+ !/^[a-f0-9]{64}$/.test(context.policyHash)+ ) {+ throw new CodexAuthorizeError('no_grant', 'Codex catalog policy binding is missing')+ }const authorized = await this.deps.authorizer.authorize({request,requestHash,@@ -207,17 +265,34 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {model: this.model,messages: messages.map(message => ({role: message.role,- content: message.content,+ content: message.content ?? '',...(message.name ? { name: message.name } : {}),...(message.tool_call_id ? { toolCallId: message.tool_call_id } : {}),+ ...(message.role === 'assistant' && message.tool_calls && message.tool_calls.length > 0+ ? {+ toolCalls: message.tool_calls.map(call => ({+ id: call.id,+ name: call.name,+ arguments: call.arguments,+ })),+ }+ : {}),})),}if (tools && tools.length > 0) {- request.tools = tools.map(tool => ({- name: tool.name,- description: tool.description,- parameters: tool.parameters,- }))+ const advertised = selectCodexAdvertisedTools(tools)+ if (advertised.length !== tools.length) {+ console.warn(+ `[Codex] advertised ${advertised.length} native tool(s) of ${tools.length} offered (MCP omitted, cap ${LIMITS.maxTools})`+ )+ }+ if (advertised.length > 0) {+ request.tools = advertised.map(tool => ({+ name: tool.name,+ description: tool.description,+ parameters: tool.parameters,+ }))+ }}if (options?.temperature !== undefined ||diff --git a/mcp-host/src/llm/index.ts b/mcp-host/src/llm/index.tsindex f92e862c3..0aa33a459 100644--- a/mcp-host/src/llm/index.ts+++ b/mcp-host/src/llm/index.ts@@ -5,6 +5,7 @@ import { config } from '../config'import { ApiKeys, ModelConfig } from '../types'import { ClaudeProvider } from './claude'import { CodexLlmProxyClient, resolveCodexProxyRuntimeUrl } from './codexLlmProxyClient'+import { readLiveCodexPolicyBinding, resolveCodexAttemptPolicy } from './codexPolicyBinding'import { OpenAIProvider } from './openai'import { ProviderAttemptAuthorizer, resolveCodexAuthorizeUrl } from './providerAttemptAuthorizer'import { makeProvider } from './registry'@@ -34,10 +35,21 @@ function createCodexRuntimeDeps() {runtimeUrl: resolveCodexProxyRuntimeUrl(config.codexProxyRuntimeBaseUrl),readPlatformJwt,}),- attemptContext: () => ({- policyRevision: config.codexPolicyRevision,- policyHash: config.codexPolicyHash,- }),+ attemptContext: ({ model }: { model: string }) => {+ const resolved = resolveCodexAttemptPolicy({+ model,+ envRevision: config.codexPolicyRevision,+ envHash: config.codexPolicyHash,+ binding: readLiveCodexPolicyBinding(),+ })+ if (!resolved) {+ return { policyRevision: 0, policyHash: '' }+ }+ return {+ ...resolved,+ hostRef: config.hostName,+ }+ },}}diff --git a/mcp-host/src/main.ts b/mcp-host/src/main.tsindex d46fda639..6c8bde1ca 100644--- a/mcp-host/src/main.ts+++ b/mcp-host/src/main.ts@@ -61,6 +61,7 @@ import { TaskLifecycle } from './lifecycle/taskLifecycle'import { isTerminal } from './lifecycle/types'import type { TransitionEvent } from './lifecycle/types'import { SingleTurnProvider, apiKeysFromEnv, createLLMProvider } from './llm'+import { setCodexPolicyBindingReader } from './llm/codexPolicyBinding'import { FailoverEngine } from './llm/failover/engine'import { llmFallbackTotal } from './llm/failover/metrics'import { parseLlmPolicy } from './llm/failover/policy'@@ -166,6 +167,7 @@ setOutputDirHostAccessor(() => currentHost)let currentKeys: ApiKeys = {}let currentProvider: SingleTurnProvider | null = nulllet configStore: ConfigStore | null = null+setCodexPolicyBindingReader(() => configStore?.codexPolicyBinding() ?? null)// R5 — provider-fallback. `currentPolicy` is the normalized `spec.llmPolicy`// (null = no failover); `failoverEngine` holds the Host-wide sticky state// (cooldown + served pair) and is (re)built only when the policy changes.diff --git a/packages/llm-provider-attempt-contract/index.cjs b/packages/llm-provider-attempt-contract/index.cjsindex 1575e7c8f..8f2260d63 100644--- a/packages/llm-provider-attempt-contract/index.cjs+++ b/packages/llm-provider-attempt-contract/index.cjs@@ -39,7 +39,8 @@ const ROOT_KEYS = new Set(['deadlineMs','transportHints',])-const MESSAGE_KEYS = new Set(['role', 'content', 'name', 'toolCallId'])+const MESSAGE_KEYS = new Set(['role', 'content', 'name', 'toolCallId', 'toolCalls'])+const TOOL_CALL_KEYS = new Set(['id', 'name', 'arguments'])const TOOL_KEYS = new Set(['name', 'description', 'parameters'])const GENERATION_KEYS = new Set(['temperature', 'maxOutputTokens', 'toolChoice'])const HINT_KEYS = new Set(['promptCacheKey'])@@ -180,6 +181,42 @@ function parseMessages(raw) {if (!isBoundedId(item.toolCallId)) return fail('invalid', `messages[${i}].toolCallId is invalid`)message.toolCallId = item.toolCallId}+ if (item.toolCalls !== undefined) {+ if (item.role !== 'assistant') {+ return fail('invalid', `messages[${i}].toolCalls is only allowed on assistant`)+ }+ if (!Array.isArray(item.toolCalls) || item.toolCalls.length === 0) {+ return fail('invalid', `messages[${i}].toolCalls must be a non-empty array`)+ }+ if (item.toolCalls.length > LIMITS.maxTools) {+ return fail('limit', `messages[${i}].toolCalls exceed ${LIMITS.maxTools}`)+ }+ const toolCalls = []+ for (let j = 0; j < item.toolCalls.length; j++) {+ const call = item.toolCalls[j]+ if (!isPlainObject(call)) {+ return fail('invalid', `messages[${i}].toolCalls[${j}] must be an object`)+ }+ const callExtra = rejectUnknown(call, TOOL_CALL_KEYS, `messages[${i}].toolCalls[${j}]`)+ if (callExtra) return callExtra+ if (!isBoundedId(call.id)) {+ return fail('invalid', `messages[${i}].toolCalls[${j}].id is invalid`)+ }+ if (!isBoundedId(call.name)) {+ return fail('invalid', `messages[${i}].toolCalls[${j}].name is invalid`)+ }+ if (!isPlainObject(call.arguments)) {+ return fail('invalid', `messages[${i}].toolCalls[${j}].arguments must be an object`)+ }+ const finiteArgs = assertFiniteTree(+ call.arguments,+ `messages[${i}].toolCalls[${j}].arguments`+ )+ if (finiteArgs) return finiteArgs+ toolCalls.push({ id: call.id, name: call.name, arguments: call.arguments })+ }+ message.toolCalls = toolCalls+ }messages.push(message)}return ok(messages)@@ -322,6 +359,24 @@ function hashCodexCompletionRequestV1(request) {return createHash('sha256').update(stableStringify(request)).digest('hex')}+/**+ * Grant-binding digest for a Codex authorize attempt. Must match+ * control-api's expected hash: SHA-256 of lexicographic stableStringify of+ * { catalogRevision, credentialRevision, model, provider }.+ */+function computeCodexPolicyHash(input) {+ return createHash('sha256')+ .update(+ stableStringify({+ catalogRevision: input.catalogRevision,+ credentialRevision: input.credentialRevision,+ model: input.model,+ provider: PROVIDER_ID,+ })+ )+ .digest('hex')+}+function requireId(obj, key) {if (!isBoundedId(obj[key])) return fail('invalid', `${key} is invalid`)return null@@ -455,6 +510,7 @@ module.exports = {stableStringify,parseCodexCompletionRequestV1,hashCodexCompletionRequestV1,+ computeCodexPolicyHash,parseCodexExecutionTicketClaims,parseAuthorizeAttemptResponse,parseCodexAttemptReceiptV1,diff --git a/packages/llm-provider-attempt-contract/index.d.ts b/packages/llm-provider-attempt-contract/index.d.tsindex ee8e90fb4..9878bf2e0 100644--- a/packages/llm-provider-attempt-contract/index.d.ts+++ b/packages/llm-provider-attempt-contract/index.d.ts@@ -28,11 +28,18 @@ export type ContractResult<T> =export type CodexMessageRole = 'system' | 'user' | 'assistant' | 'tool'+export interface CodexAssistantToolCallV1 {+ id: string+ name: string+ arguments: Record<string, unknown>+}+export interface CodexMessageV1 {role: CodexMessageRolecontent: stringname?: stringtoolCallId?: string+ toolCalls?: CodexAssistantToolCallV1[]}export interface CodexToolDefinitionV1 {@@ -107,7 +114,7 @@ export type RedeemAttemptResponse = {export type CodexTransportMetadataV1 = {protocolVersion: 'codex-subscription-transport.v1'completionsOrigin: 'https://chatgpt.com/backend-api/codex/responses'- catalogOrigin: 'https://chatgpt.com/backend-api/codex/models'+ catalogOrigin: 'https://chatgpt.com/backend-api/codex/models?client_version=1.0.0'operation: 'completion_stream' | 'completion_cancel' | 'connection_test'servedModel: stringmaxStreamDurationMs: number@@ -130,6 +137,11 @@ export declare function parseCodexCompletionRequestV1(export declare function hashCodexCompletionRequestV1(request: CodexCompletionRequestV1): string+export declare function computeCodexPolicyHash(input: {+ model: string+ catalogRevision: number+ credentialRevision: number+}): stringexport declare function parseCodexExecutionTicketClaims(input: unknown): ContractResult<CodexExecutionTicketClaims>diff --git a/packages/llm-provider-attempt-contract/index.test.cjs b/packages/llm-provider-attempt-contract/index.test.cjsindex c3475ba80..402a29ef7 100644--- a/packages/llm-provider-attempt-contract/index.test.cjs+++ b/packages/llm-provider-attempt-contract/index.test.cjs@@ -111,12 +111,88 @@ test('hash is SHA-256 of lexicographic stableStringify of the projection', () =>assert.equal(contract.hashCodexCompletionRequestV1(parsed.value), expected)})+test('computeCodexPolicyHash is SHA-256 of lexicographic stableStringify of the binding', () => {+ const binding = {+ catalogRevision: 4,+ credentialRevision: 3,+ model: 'gpt-5.1',+ provider: 'codex-subscription',+ }+ const expected = crypto+ .createHash('sha256')+ .update(contract.stableStringify(binding))+ .digest('hex')+ assert.equal(+ contract.computeCodexPolicyHash({+ model: 'gpt-5.1',+ catalogRevision: 4,+ credentialRevision: 3,+ }),+ expected+ )+ assert.equal(+ contract.computeCodexPolicyHash({+ credentialRevision: 3,+ catalogRevision: 4,+ model: 'gpt-5.1',+ }),+ expected+ )+ assert.notEqual(+ contract.computeCodexPolicyHash({+ model: 'gpt-5.6-luna',+ catalogRevision: 4,+ credentialRevision: 3,+ }),+ expected+ )+})+test('stableStringify sorts object keys and drops undefined like control-api', () => {assert.equal(contract.stableStringify({ b: 1, a: 2 }), '{"a":2,"b":1}')assert.equal(contract.stableStringify({ a: undefined, b: 1 }), '{"b":1}')assert.equal(contract.stableStringify([1, undefined, 2]), '[1,null,2]')})+test('assistant toolCalls are hashed into the request and rejected on other roles', () => {+ const withTools = contract.parseCodexCompletionRequestV1({+ ...BASE,+ messages: [+ { role: 'user', content: 'hi' },+ {+ role: 'assistant',+ content: '',+ toolCalls: [{ id: 'call-1', name: 'echo', arguments: { x: 1 } }],+ },+ { role: 'tool', content: 'ok', toolCallId: 'call-1' },+ ],+ })+ assert.equal(withTools.ok, true)+ const digest = contract.hashCodexCompletionRequestV1(withTools.value)+ const without = contract.parseCodexCompletionRequestV1({+ ...BASE,+ messages: [+ { role: 'user', content: 'hi' },+ { role: 'assistant', content: '' },+ { role: 'tool', content: 'ok', toolCallId: 'call-1' },+ ],+ })+ assert.equal(without.ok, true)+ assert.notEqual(contract.hashCodexCompletionRequestV1(without.value), digest)++ const onUser = contract.parseCodexCompletionRequestV1({+ ...BASE,+ messages: [+ {+ role: 'user',+ content: 'hi',+ toolCalls: [{ id: 'call-1', name: 'echo', arguments: {} }],+ },+ ],+ })+ assert.equal(onUser.ok, false)+})+test('ticket and receipt types stay credential-free at the contract boundary', () => {const claims = {jti: 'jti-1',diff --git a/scripts/tests/test-verify-networkpolicies.sh b/scripts/tests/test-verify-networkpolicies.shindex a47bddabf..905d31ebf 100755--- a/scripts/tests/test-verify-networkpolicies.sh+++ b/scripts/tests/test-verify-networkpolicies.sh@@ -153,6 +153,7 @@ echo "PASS: verify-networkpolicies fails on forbidden CIDR"grep -q 'name: codex-llm-proxy-ingress' "${ROOT}/deploy/base/control-plane/networkpolicies.yaml"grep -q 'name: codex-llm-proxy-egress' "${ROOT}/deploy/base/control-plane/networkpolicies.yaml"+grep -q 'name: control-api-to-codex-llm-proxy' "${ROOT}/deploy/base/control-plane/networkpolicies.yaml"grep -A20 'name: control-api-rpc-gateway' "${ROOT}/deploy/base/control-plane/networkpolicies.yaml" \| grep -q 'app: codex-llm-proxy'echo "PASS: Codex proxy NetworkPolicies are declared and the RPC gateway admits the proxy"diff --git a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.jsonindex b31df1f18..aa1c71a23 100644--- a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json+++ b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json@@ -23,7 +23,7 @@"oauthDeviceCallback": "https://auth.openai.com/deviceauth/callback","oauthDeviceVerification": "https://auth.openai.com/codex/device","oauthRevoke": "https://auth.openai.com/oauth/revoke",- "catalog": "https://chatgpt.com/backend-api/codex/models",+ "catalog": "https://chatgpt.com/backend-api/codex/models?client_version=1.0.0","completions": "https://chatgpt.com/backend-api/codex/responses"},"forbiddenOrigins": [diff --git a/tests/e2e/fixtures/package-lock.json b/tests/e2e/fixtures/package-lock.jsonindex 95cd2e2e5..5ca832f24 100644--- a/tests/e2e/fixtures/package-lock.json+++ b/tests/e2e/fixtures/package-lock.json@@ -1,12 +1,12 @@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.65",+ "version": "1.0.66","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "@clerum/e2e-test-fixtures",- "version": "1.0.65",+ "version": "1.0.66","dependencies": {"@kubernetes/client-node": "^1.0.0"},diff --git a/tests/e2e/fixtures/package.json b/tests/e2e/fixtures/package.jsonindex ca73415bc..53f1b16a0 100644--- a/tests/e2e/fixtures/package.json+++ b/tests/e2e/fixtures/package.json@@ -1,6 +1,6 @@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.65",+ "version": "1.0.66","description": "E2E test fixtures and utilities for Clerum testing","type": "module","main": "index.ts",diff --git a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts b/tests/e2e/integration/codex-subscription-contract-freeze.test.tsindex 7f3e30bb3..b0470fe93 100644--- a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts+++ b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts@@ -48,7 +48,7 @@ const REQUIRED_LIMIT_KEYS = [] as constconst SENSITIVE_VALUE_PATTERN =- /^(?:sk-[A-Za-z0-9]+|Bearer\s+\S+|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|[^;\s]+=[^;\s]+(?:;|$))/+ /^(?:sk-[A-Za-z0-9]+|Bearer\s+\S+|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|(?!https?:\/\/)[^;\s]+=[^;\s]+(?:;|$))/type FiniteLimit = numberdiff --git a/tests/e2e/integration/network-policies.test.ts b/tests/e2e/integration/network-policies.test.tsindex 6c6868708..8eedc50ba 100644--- a/tests/e2e/integration/network-policies.test.ts+++ b/tests/e2e/integration/network-policies.test.ts@@ -252,6 +252,7 @@ describe('NetworkPolicy Enforcement (Calico)', () => {)expect(nps).toContain('codex-llm-proxy-ingress')expect(nps).toContain('codex-llm-proxy-egress')+ expect(nps).toContain('control-api-to-codex-llm-proxy')})it('deny-all policies exist in secured namespaces', ctx => {