Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
chore: merge origin/dev after Codex image input (#660)
Alfredo Lopeza94854e0907b
diff --cc codex-llm-proxy/src/config.tsindex 114ad903d,2aa9c3e08..ccf498677--- a/codex-llm-proxy/src/config.ts+++ b/codex-llm-proxy/src/config.ts@@@ -1,17 -1,13 +1,19 @@@+ import { LIMITS } from '@clerum/llm-provider-attempt-contract'+import { STREAM_LIMITS } from './requestLimits.js'+import { DEFAULT_HEARTBEAT_INTERVAL_MS } from './sseHeartbeat.js'export type CodexLlmProxyConfig = {runtimePort: numberadminPort: numberprobePort: numbermaxBodyBytes: number+ maxVisualBodyBytes: numbermaxStreamDurationMs: numbermaxDeadlineMs: number+ /** Lowers STREAM_LIMITS.upstreamIdleTimeoutMs; the transport never raises it. */+ upstreamIdleTimeoutMs: number+ /** Interval between SSE keepalive comments once the redeem succeeded. */+ heartbeatIntervalMs: numberjwtIssuer: stringjwtPublicKey: stringexecutionEnabled: booleandiff --cc codex-llm-proxy/src/requestLimits.tsindex 84e9c4242,2356e88f4..bb88e8dee--- a/codex-llm-proxy/src/requestLimits.ts+++ b/codex-llm-proxy/src/requestLimits.ts@@@ -3,16 -3,21 +3,28 @@@ import { LIMITS as CONTRACT_LIMITS } frexport const STREAM_LIMITS = {maxConcurrentStreams: 8,maxQueuedRequests: 16,- maxStreamDurationMs: 300_000,+ maxStreamDurationMs: 1_800_000,+ // Longest time a request may wait for a stream slot. Bounded so that queue+ // wait + redeem + the first keepalive stays below the Host HTTP client's+ // 300 s header timeout.+ maxQueueWaitMs: 60_000,+ // Longest silence tolerated while waiting on the upstream (response headers+ // or the next SSE chunk). Matches the Codex CLI stream idle timeout.+ upstreamIdleTimeoutMs: 300_000,} as const+ /**+ * Admission for a body whose Content-Length exceeds the ordinary cap.+ * The 256Mi pod cannot hold the ordinary 8-stream gate across a 24 MiB image,+ * so those requests are a tighter sibling and a V2 request keeps the slot+ * until the stream ends. Small bodies, including every valid V1, must not+ * enter this gate. Do not raise proxy memory to widen it.+ */+ export const VISUAL_STREAM_LIMITS = {+ maxConcurrentStreams: 2,+ maxQueuedRequests: 8,+ } as const+export class RequestLimitError extends Error {readonly code = 'provider_unavailable'constructor(message: string) {diff --cc codex-llm-proxy/src/server.tsindex 33f7464ba,5b4cf9abc..269d2a9ee--- a/codex-llm-proxy/src/server.ts+++ b/codex-llm-proxy/src/server.ts@@@ -18,15 -17,23 +18,24 @@@ import { ControlApiClient, ControlApiClimport { logger } from './logger.js'import { createProxyMetrics } from './metrics.js'import {- defaultAddressLookup,OriginDeniedError,type OriginPolicyOptions,+ defaultAddressLookup,} from './originPolicy.js'- import { RequestLimitError, streamGate } from './requestLimits.js'+ import {+ LIMITS,+ SCHEMA_VERSION_V2,+ measureNonImageCompletionBytes,+ requestBodyLimitBytes,+ } from '@clerum/llm-provider-attempt-contract'+ import { RequestLimitError, streamGate, visualStreamGate } from './requestLimits.js'+import { startSseHeartbeat } from './sseHeartbeat.js'+ type GatedRequest = Request & { codexStreamRelease?: () => void }+const COMPLETION_KEYS = new Set(['executionTicket', 'requestHash', 'request', 'deadlineMs'])const ADMIN_KEYS = new Set(['accessToken'])+ const COMPLETION_PATH = '/internal/runtime/v1/codex/completions'const completionBodySchema = z.object({@@@ -174,12 -283,20 +285,24 @@@ export function createProxyAppsconst attemptStarted = Date.now()let toolCalls = 0let textChunks = 0+ let heartbeats = 0+ // Set once the redeem succeeded. Every exit path below calls it before+ // it ends the response: a tick after `res.end()` would write after end.+ let stopHeartbeat: (() => void) | undefinedtry {- release = await streamGate.acquire(abort.signal)+ // A visual slot exists only when Content-Length exceeded the ordinary+ // cap. Keep it for the ChatGPT stream only when the parsed V2 body still+ // exceeds that cap (image bytes stay resident). Padding, V1, and small+ // V2 release it and take the 8-wide gate.+ if (visualRequest && wholeBodyBytes > config.maxBodyBytes) {+ release = gated.codexStreamRelease+ gated.codexStreamRelease = undefined+ } else {+ releaseAdmission()+ }+ if (!release) {+ release = await streamGate.acquire(abort.signal)+ }res.status(200)res.setHeader('content-type', 'text/event-stream')res.setHeader('cache-control', 'no-cache')diff --cc codex-llm-proxy/test/codexTransport.conformance.test.tsindex 48907d81a,b557f0f34..d2d73a88d--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts@@@ -69,6 -77,132 +77,134 @@@ function sseResponse}describe('streamCodexCompletion', () => {+ it('maps an over-dimension image to payload_too_large before redeem', async () => {+ const request = {+ schemaVersion: 'codex-completion-request.v2' as const,+ requestId: 'req-over-dimension',+ idempotencyKey: 'idem-over-dimension',+ provider: 'codex-subscription' as const,+ model: 'gpt-5.1',+ messages: [+ {+ role: 'user' as const,+ content: 'look',+ contentParts: [+ { type: 'text' as const, text: 'look' },+ {+ type: 'image' as const,+ mimeType: 'image/png' as const,+ data: declaredHeaderPng(3000, 3000).toString('base64'),+ source: { kind: 'attachment' as const, attachmentId: 'att-1', messageId: 'msg-1' },+ },+ ],+ },+ ],+ }+ const redeem = vi.fn()+ await expect(+ streamCodexCompletion({++ maxDeadlineMs: 1_800_000,+ executionTicket: 'visual-ticket',+ requestHash: 'a'.repeat(64),+ request,+ ticket: {+ jti: 'visual-ticket',+ hostRef: 'research-host',+ model: request.model,+ requestHash: 'a'.repeat(64),+ providerAttemptId: 'visual-attempt',+ },+ redeem,+ finalize: vi.fn(),+ fetchFn: vi.fn(),+ })+ ).rejects.toMatchObject({+ code: 'payload_too_large',+ message: expect.stringMatching(/image dimension exceeds 2048/),+ })+ expect(redeem).not.toHaveBeenCalled()+ })++ it.each(['png', 'jpeg'] as const)(+ 'projects authorized %s parts without leaking provenance upstream',+ async format => {+ const fixtures = JSON.parse(+ readFileSync(+ new URL(+ '../../packages/llm-provider-attempt-contract/fixtures/visual-requests.json',+ import.meta.url+ ),+ 'utf8'+ )+ ) as Record<+ string,+ {+ messages: Array<{+ contentParts: Array<{+ type: string+ text?: string+ mimeType?: string+ data?: string+ }>+ }>+ }+ >+ const parsed = parseCodexCompletionRequest({ ...fixtures[format], model: REQUEST.model })+ expect(parsed.ok).toBe(true)+ if (!parsed.ok) throw new Error(parsed.message)+ const request = parsed.value+ const requestHash = hashCodexCompletionRequest(request)+ const fetchFn = vi.fn<typeof fetch>(async () =>+ sseResponse(['data: {"type":"response.completed"}\n\n'])+ )+ const redeem = vi.fn(async () => redeemSuccess())+ const finalize = vi.fn(async () => ({+ providerAttemptId: 'visual-attempt',+ outcome: 'success' as const,+ duplicate: false,+ }))+ const input: StreamCodexCompletionInput = {++ maxDeadlineMs: 1_800_000,+ executionTicket: 'visual-ticket',+ requestHash,+ request,+ ticket: {+ jti: 'visual-ticket',+ hostRef: 'research-host',+ model: request.model,+ requestHash,+ providerAttemptId: 'visual-attempt',+ },+ redeem,+ finalize,+ fetchFn,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ }+ await expect(streamCodexCompletion({ ...input, deadlineMs: 1000 })).rejects.toMatchObject({+ code: 'invalid_request',+ })+ expect(redeem).not.toHaveBeenCalled()+ expect(fetchFn).not.toHaveBeenCalled()+ await streamCodexCompletion(input)+ const body = JSON.parse(String(fetchFn.mock.calls[0]?.[1]?.body))+ const sourceParts = fixtures[format].messages[0].contentParts+ expect(body.input[0].content).toEqual(+ sourceParts.map(+ (part: { type: string; text?: string; mimeType?: string; data?: string }) =>+ part.type === 'text'+ ? { type: 'input_text', text: part.text }+ : {+ type: 'input_image',+ image_url: `data:${part.mimeType};base64,${part.data}`,+ detail: 'high',+ }+ )+ )+ expect(JSON.stringify(body)).not.toContain('attachmentId')+ expect(finalize).toHaveBeenCalledOnce()+ }+ )+it('does not read further upstream bytes while the frame consumer is back-pressured', async () => {const encoder = new TextEncoder()const events = [diff --cc codex-llm-proxy/test/requestLimits.test.tsindex deb988815,483d6e66c..c7e9fe13b--- a/codex-llm-proxy/test/requestLimits.test.ts+++ b/codex-llm-proxy/test/requestLimits.test.ts@@@ -1,23 -1,10 +1,33 @@@import { describe, expect, it } from 'vitest'+import { CONTROL_API_REQUEST_TIMEOUT_MS } from '../src/controlApiClient.js'- import { RequestLimitError, STREAM_LIMITS, StreamGate } from '../src/requestLimits.js'+ import {+ RequestLimitError,+ STREAM_LIMITS,+ StreamGate,+ VISUAL_STREAM_LIMITS,+ } from '../src/requestLimits.js'+import { DEFAULT_HEARTBEAT_INTERVAL_MS } from '../src/sseHeartbeat.js'++// undici's default headersTimeout and bodyTimeout in the Node 24.16.0 image the+// Host runs; the Host sets neither for its proxy calls.+const HOST_UNDICI_TIMEOUT_MS = 300_000++describe('stream timing invariant', () => {+ it('reaches the first keepalive before the Host HTTP client times out', () => {+ expect(STREAM_LIMITS.maxStreamDurationMs).toBe(1_800_000)+ expect(STREAM_LIMITS.maxQueueWaitMs).toBe(60_000)+ // Nothing is written while a request waits for a slot or for the redeem,+ // so both must end, and one heartbeat interval pass, inside the timeout.++ // A visual request can wait twice: on visualStreamGate before the body is++ // parsed, then on streamGate when the parsed body no longer needs the++ // visual slot. Both gates share the maxQueueWaitMs default.+ expect(- STREAM_LIMITS.maxQueueWaitMs + CONTROL_API_REQUEST_TIMEOUT_MS + DEFAULT_HEARTBEAT_INTERVAL_MS++ 2 * STREAM_LIMITS.maxQueueWaitMs +++ CONTROL_API_REQUEST_TIMEOUT_MS +++ DEFAULT_HEARTBEAT_INTERVAL_MS+ ).toBeLessThan(HOST_UNDICI_TIMEOUT_MS)+ })+})describe('StreamGate', () => {it('admits a queued waiter once a running stream releases', async () => {diff --cc codex-llm-proxy/test/runtimePath.hermetic.e2e.test.tsindex 13529acd4,bbaf486d5..3f45596fc--- a/codex-llm-proxy/test/runtimePath.hermetic.e2e.test.ts+++ b/codex-llm-proxy/test/runtimePath.hermetic.e2e.test.ts@@@ -80,10 -80,9 +80,11 @@@ function config(): CodexLlmProxyConfigadminPort: 0,probePort: 0,maxBodyBytes: 1_048_576,+ maxVisualBodyBytes: 24 * 1024 * 1024,maxStreamDurationMs: 30_000,maxDeadlineMs: 30_000,+ upstreamIdleTimeoutMs: 300_000,+ heartbeatIntervalMs: 15_000,jwtIssuer: 'control-api',jwtPublicKey: publicKey,executionEnabled: true,diff --cc codex-llm-proxy/test/server.security.test.tsindex f299a0a26,cdf01142f..d392f17cc--- a/codex-llm-proxy/test/server.security.test.ts+++ b/codex-llm-proxy/test/server.security.test.ts@@@ -32,10 -31,9 +32,11 @@@ function config(overrides: Partial<CodeadminPort: 8081,probePort: 9090,maxBodyBytes: 1024,+ maxVisualBodyBytes: 24 * 1024 * 1024,- maxStreamDurationMs: 300_000,- maxDeadlineMs: 300_000,+ maxStreamDurationMs: 1_800_000,+ maxDeadlineMs: 1_800_000,+ upstreamIdleTimeoutMs: 300_000,+ heartbeatIntervalMs: 15_000,jwtIssuer: 'control-api',jwtPublicKey: publicKey,executionEnabled: true,@@@ -295,57 -383,23 +386,73 @@@ describe('codex-llm-proxy security surfCODEX_LLM_PROXY_MAX_STREAM_DURATION_MS: String(Number.MAX_SAFE_INTEGER),})).toThrow(/bounded positive integer/)+ expect(() =>+ loadConfig({+ CODEX_LLM_PROXY_JWT_PUBLIC_KEY: publicKey,+ CODEX_LLM_PROXY_UPSTREAM_IDLE_TIMEOUT_MS: '0',+ })+ ).toThrow(/CODEX_LLM_PROXY_UPSTREAM_IDLE_TIMEOUT_MS must be a finite integer greater than zero/)+ })++ it('defaults the upstream idle timeout to the published STREAM_LIMITS value', () => {+ const loaded = loadConfig({+ CODEX_LLM_PROXY_JWT_PUBLIC_KEY: publicKey,+ CODEX_LLM_PROXY_CONTROL_API_URL: 'http://control-api:8080',+ CODEX_LLM_PROXY_CONTROL_API_TOKEN: 'service-token',+ })+ expect(loaded.upstreamIdleTimeoutMs).toBe(300_000)+ expect(+ loadConfig({+ CODEX_LLM_PROXY_JWT_PUBLIC_KEY: publicKey,+ CODEX_LLM_PROXY_CONTROL_API_URL: 'http://control-api:8080',+ CODEX_LLM_PROXY_CONTROL_API_TOKEN: 'service-token',+ CODEX_LLM_PROXY_UPSTREAM_IDLE_TIMEOUT_MS: '45000',+ }).upstreamIdleTimeoutMs+ ).toBe(45_000)+ })++ it('defaults the total stream cap and the deadline ceiling to 30 min', () => {+ const loaded = loadConfig({+ CODEX_LLM_PROXY_JWT_PUBLIC_KEY: publicKey,+ CODEX_LLM_PROXY_CONTROL_API_URL: 'http://control-api:8080',+ CODEX_LLM_PROXY_CONTROL_API_TOKEN: 'service-token',+ })+ expect(loaded.maxStreamDurationMs).toBe(1_800_000)+ expect(loaded.maxDeadlineMs).toBe(1_800_000)+ })++ it('defaults the SSE heartbeat to 15 s and rejects a non-positive interval', () => {+ const env = {+ CODEX_LLM_PROXY_JWT_PUBLIC_KEY: publicKey,+ CODEX_LLM_PROXY_CONTROL_API_URL: 'http://control-api:8080',+ CODEX_LLM_PROXY_CONTROL_API_TOKEN: 'service-token',+ }+ expect(loadConfig(env).heartbeatIntervalMs).toBe(15_000)+ expect(+ loadConfig({ ...env, CODEX_LLM_PROXY_HEARTBEAT_INTERVAL_MS: '5000' }).heartbeatIntervalMs+ ).toBe(5_000)+ for (const raw of ['0', '-1']) {+ expect(() => loadConfig({ ...env, CODEX_LLM_PROXY_HEARTBEAT_INTERVAL_MS: raw })).toThrow(+ /CODEX_LLM_PROXY_HEARTBEAT_INTERVAL_MS must be a finite integer greater than zero/+ )+ }})++ it('refuses a visual envelope budget below the shared contract', () => {+ const required = {+ CODEX_LLM_PROXY_JWT_PUBLIC_KEY: publicKey,+ CODEX_LLM_PROXY_CONTROL_API_URL:+ 'http://control-api-rpc-gateway.control-plane.svc.cluster.local:8090/api/v1',+ CODEX_LLM_PROXY_CONTROL_API_TOKEN: 'dev-codex-llm-proxy-token',+ }+ expect(() =>+ loadConfig({+ ...required,+ CODEX_LLM_PROXY_MAX_VISUAL_BODY_BYTES: '1024',+ })+ ).toThrow(/shared envelope byte budget/)+ expect(loadConfig(required).maxVisualBodyBytes).toBe(24 * 1024 * 1024)+ })})describe('codex-llm-proxy execution kill switch', () => {diff --cc codex-llm-proxy/test/sseBackpressure.test.tsindex b6c8ef609,dae86e6e5..76efcb319--- a/codex-llm-proxy/test/sseBackpressure.test.ts+++ b/codex-llm-proxy/test/sseBackpressure.test.ts@@@ -90,10 -90,9 +90,11 @@@ function config(): CodexLlmProxyConfigadminPort: 0,probePort: 0,maxBodyBytes: 1_048_576,+ maxVisualBodyBytes: 24 * 1024 * 1024,maxStreamDurationMs: 60_000,maxDeadlineMs: 60_000,+ upstreamIdleTimeoutMs: 300_000,+ heartbeatIntervalMs: 15_000,jwtIssuer: 'control-api',jwtPublicKey: publicKey,executionEnabled: true,diff --cc codex-llm-proxy/test/streamGate.handoff.test.tsindex 000000000,9abda5974..cd9dee4a6mode 000000,100644..100644--- a/codex-llm-proxy/test/streamGate.handoff.test.ts+++ b/codex-llm-proxy/test/streamGate.handoff.test.ts@@@ -1,0 -1,373 +1,375 @@@+ import { generateKeyPairSync } from 'node:crypto'+ import { createServer } from 'node:http'+ import { connect as connectTcp } from 'node:net'+ import jwt from 'jsonwebtoken'+ import { afterEach, describe, expect, it } from 'vitest'+ import {+ hashCodexCompletionRequest,+ parseCodexCompletionRequest,+ } from '@clerum/llm-provider-attempt-contract'+ import { type CodexLlmProxyConfig } from '../src/config.js'+ import { createProxyApps } from '../src/server.js'+ import {+ STREAM_LIMITS,+ VISUAL_STREAM_LIMITS,+ streamGate,+ visualStreamGate,+ } from '../src/requestLimits.js'++ const { privateKey, publicKey } = generateKeyPairSync('rsa', {+ modulusLength: 2048,+ publicKeyEncoding: { type: 'spki', format: 'pem' },+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' },+ })++ function config(overrides: Partial<CodexLlmProxyConfig> = {}): CodexLlmProxyConfig {+ return {+ runtimePort: 8080,+ adminPort: 8081,+ probePort: 9090,+ maxBodyBytes: 1_048_576,+ maxVisualBodyBytes: 24 * 1024 * 1024,+ maxStreamDurationMs: 300_000,+ maxDeadlineMs: 300_000,++ upstreamIdleTimeoutMs: 300_000,++ heartbeatIntervalMs: 15_000,+ jwtIssuer: 'control-api',+ jwtPublicKey: publicKey,+ executionEnabled: true,+ controlApiBaseUrl: '',+ controlApiServiceName: 'codex-llm-proxy',+ controlApiServiceToken: '',+ ...overrides,+ }+ }++ function sign(payload: Record<string, unknown>, audience: string): string {+ return jwt.sign(payload, privateKey, {+ algorithm: 'RS256',+ issuer: 'control-api',+ audience,+ expiresIn: 60,+ })+ }++ function platformToken(): string {+ return sign(+ {+ sub: 'default/research-host',+ hostRefs: ['research-host'],+ workflowControlScopes: ['llm:codex:execute'],+ scope: 'workflow:approval:request',+ },+ 'workflow-approvals'+ )+ }++ let ticketSeq = 1++ function ticket(requestHash: string): string {+ ticketSeq += 1+ return sign(+ {+ jti: `11111111-1111-4111-8111-${String(ticketSeq).padStart(12, '0')}`,+ typ: 'codex-execution-ticket',+ hostRef: 'research-host',+ model: 'gpt-5.1',+ requestHash,+ providerAttemptId: `att-${ticketSeq}`,+ },+ 'codex-llm-proxy'+ )+ }++ function completionRequest(+ schemaVersion: 'codex-completion-request.v1' | 'codex-completion-request.v2',+ content = 'hi'+ ) {+ ticketSeq += 1+ return {+ schemaVersion,+ requestId: `req-gate-${ticketSeq}`,+ idempotencyKey: `idem-gate-${ticketSeq}`,+ provider: 'codex-subscription',+ model: 'gpt-5.1',+ messages: [{ role: 'user', content }],+ }+ }++ function hangStream() {+ let releaseHeld!: () => void+ const held = new Promise<void>(resolve => {+ releaseHeld = resolve+ })+ return {+ release: () => releaseHeld(),+ impl: async () => {+ await held+ return { outcome: 'canceled' as const }+ },+ }+ }++ async function waitFor(predicate: () => boolean, label: string): Promise<void> {+ const started = Date.now()+ while (!predicate()) {+ if (Date.now() - started > 5_000) throw new Error(label)+ await new Promise(resolve => setTimeout(resolve, 10))+ }+ }++ function completionPayload(+ schemaVersion: 'codex-completion-request.v1' | 'codex-completion-request.v2',+ content = 'hi'+ ): { token: string; body: string } {+ const raw = completionRequest(schemaVersion, content)+ const parsed = parseCodexCompletionRequest(raw)+ if (!parsed.ok) throw new Error(parsed.message)+ const requestHash = hashCodexCompletionRequest(parsed.value)+ return {+ token: platformToken(),+ body: JSON.stringify({+ executionTicket: ticket(requestHash),+ requestHash,+ request: raw,+ }),+ }+ }++ async function postCompletion(+ port: number,+ schemaVersion: 'codex-completion-request.v1' | 'codex-completion-request.v2',+ content = 'hi'+ ): Promise<Response> {+ const { token, body } = completionPayload(schemaVersion, content)+ return fetch(`http://127.0.0.1:${port}/internal/runtime/v1/codex/completions`, {+ method: 'POST',+ headers: {+ Authorization: `Bearer ${token}`,+ 'content-type': 'application/json',+ },+ body,+ })+ }++ function postChunked(port: number, token: string): Promise<number> {+ return new Promise((resolve, reject) => {+ const socket = connectTcp(port, '127.0.0.1', () => {+ const chunk = Buffer.from('{}')+ socket.write(+ `POST /internal/runtime/v1/codex/completions HTTP/1.1\r\n` ++ `Host: 127.0.0.1:${port}\r\n` ++ `Authorization: Bearer ${token}\r\n` ++ `Content-Type: application/json\r\n` ++ `Connection: close\r\n` ++ `Transfer-Encoding: chunked\r\n` ++ `\r\n` ++ `${chunk.length.toString(16)}\r\n`+ )+ socket.write(chunk)+ socket.write('\r\n0\r\n\r\n')+ })+ const chunks: Buffer[] = []+ const finish = () => {+ const raw = Buffer.concat(chunks).toString('utf8')+ const match = /^HTTP\/1\.1 (\d+)/.exec(raw)+ if (!match) {+ reject(new Error(`no status in ${raw.slice(0, 180)}`))+ return+ }+ resolve(Number(match[1]))+ }+ socket.setTimeout(2_000, () => {+ socket.destroy()+ finish()+ })+ socket.on('data', data => {+ chunks.push(data)+ if (Buffer.concat(chunks).toString('utf8').includes('\r\n\r\n')) {+ socket.destroy()+ finish()+ }+ })+ socket.on('error', err => {+ if ((err as NodeJS.ErrnoException).code !== 'ECONNRESET') reject(err)+ })+ socket.on('close', finish)+ })+ }++ describe('visual stream-gate handoff', () => {+ const hangs: Array<{ release: () => void }> = []+ const serversToClose: Array<{ close: () => Promise<void> }> = []+ const listeners: Array<ReturnType<typeof createServer>> = []++ afterEach(async () => {+ for (const hang of hangs.splice(0)) hang.release()+ for (const server of serversToClose.splice(0)) await server.close()+ await Promise.all(+ listeners.splice(0).map(+ listener =>+ new Promise<void>((resolve, reject) =>+ listener.close(err => (err ? reject(err) : resolve()))+ )+ )+ )+ await waitFor(+ () =>+ visualStreamGate.snapshot().running === 0 &&+ visualStreamGate.snapshot().queued === 0 &&+ streamGate.snapshot().running === 0 &&+ streamGate.snapshot().queued === 0,+ 'gates did not drain after release'+ )+ })++ it('keeps small V1 text off the visual gate while large V2 streams hold it', async () => {+ expect(VISUAL_STREAM_LIMITS.maxConcurrentStreams).toBe(2)+ expect(STREAM_LIMITS.maxConcurrentStreams).toBe(8)++ const small = completionPayload('codex-completion-request.v1')+ const maxBodyBytes = Buffer.byteLength(small.body) + 512+ const largeContent = 'x'.repeat(maxBodyBytes)+ const large = completionPayload('codex-completion-request.v2', largeContent)+ expect(Buffer.byteLength(large.body)).toBeGreaterThan(maxBodyBytes)++ const hang = hangStream()+ hangs.push(hang)+ const servers = createProxyApps(config({ maxBodyBytes, maxVisualBodyBytes: 1024 * 1024 }), {+ streamCompletion: hang.impl,+ })+ serversToClose.push(servers)+ const listener = createServer(servers.runtimeApp).listen(0)+ listeners.push(listener)+ const address = listener.address()+ if (!address || typeof address === 'string') throw new Error('listener has no port')+ const port = address.port++ const firstVisual = postCompletion(port, 'codex-completion-request.v2', largeContent)+ const secondVisual = postCompletion(port, 'codex-completion-request.v2', largeContent)+ await waitFor(+ () => visualStreamGate.snapshot().running === 2 && streamGate.snapshot().running === 0,+ 'large V2 did not hold the visual gate'+ )++ const v1 = postCompletion(port, 'codex-completion-request.v1')+ await waitFor(+ () => streamGate.snapshot().running === 1 && visualStreamGate.snapshot().running === 2,+ 'small V1 waited on the visual gate instead of the ordinary stream gate'+ )++ const queued = Array.from({ length: VISUAL_STREAM_LIMITS.maxQueuedRequests }, () =>+ postCompletion(port, 'codex-completion-request.v2', largeContent)+ )+ await waitFor(+ () => visualStreamGate.snapshot().queued === VISUAL_STREAM_LIMITS.maxQueuedRequests,+ 'visual queue did not fill to 8'+ )+ const overflow = await postCompletion(port, 'codex-completion-request.v2', largeContent)+ expect(overflow.status).toBe(503)+ expect(await overflow.json()).toEqual({ error: 'provider_unavailable' })++ const textWhileSaturated = postCompletion(port, 'codex-completion-request.v1')+ await waitFor(+ () => streamGate.snapshot().running === 2 && visualStreamGate.snapshot().running === 2,+ 'a small V1 was rejected or queued behind saturated image streams'+ )++ hang.release()+ await Promise.all([v1, textWhileSaturated, firstVisual, secondVisual, ...queued])+ })++ it('does not queue a chunked platform body behind saturated image streams', async () => {+ const small = completionPayload('codex-completion-request.v1')+ const maxBodyBytes = Buffer.byteLength(small.body) + 512+ const largeContent = 'x'.repeat(maxBodyBytes)+ const hang = hangStream()+ hangs.push(hang)+ const servers = createProxyApps(config({ maxBodyBytes, maxVisualBodyBytes: 1024 * 1024 }), {+ streamCompletion: hang.impl,+ })+ serversToClose.push(servers)+ const listener = createServer(servers.runtimeApp).listen(0)+ listeners.push(listener)+ const address = listener.address()+ if (!address || typeof address === 'string') throw new Error('listener has no port')+ const port = address.port++ const firstVisual = postCompletion(port, 'codex-completion-request.v2', largeContent)+ const secondVisual = postCompletion(port, 'codex-completion-request.v2', largeContent)+ await waitFor(+ () => visualStreamGate.snapshot().running === 2 && visualStreamGate.snapshot().queued === 0,+ 'large V2 did not fill the visual gate'+ )++ const status = await postChunked(port, platformToken())+ expect(status).toBe(400)+ expect(visualStreamGate.snapshot()).toEqual({ running: 2, queued: 0 })++ hang.release()+ await Promise.all([firstVisual, secondVisual])+ })++ it('rejects a between-cap V1 with 413 and leaves the visual gate empty', async () => {+ const small = completionPayload('codex-completion-request.v1')+ const maxBodyBytes = Buffer.byteLength(small.body) + 512+ const largeContent = 'x'.repeat(maxBodyBytes)+ const large = completionPayload('codex-completion-request.v1', largeContent)+ expect(Buffer.byteLength(large.body)).toBeGreaterThan(maxBodyBytes)++ const servers = createProxyApps(config({ maxBodyBytes, maxVisualBodyBytes: 1024 * 1024 }))+ serversToClose.push(servers)+ const listener = createServer(servers.runtimeApp).listen(0)+ listeners.push(listener)+ const address = listener.address()+ if (!address || typeof address === 'string') throw new Error('listener has no port')+ const port = address.port++ const response = await fetch(`http://127.0.0.1:${port}/internal/runtime/v1/codex/completions`, {+ method: 'POST',+ headers: {+ Authorization: `Bearer ${large.token}`,+ 'content-type': 'application/json',+ },+ body: large.body,+ })+ expect(response.status).toBe(413)+ expect(await response.json()).toEqual({ error: 'payload_too_large' })+ expect(visualStreamGate.snapshot()).toEqual({ running: 0, queued: 0 })+ expect(streamGate.snapshot()).toEqual({ running: 0, queued: 0 })+ })++ it('releases the visual slot for a whitespace-padded small V2', async () => {+ const small = completionPayload('codex-completion-request.v2')+ const maxBodyBytes = Buffer.byteLength(small.body) + 64+ const padded = `${small.body}${' '.repeat(maxBodyBytes - Buffer.byteLength(small.body) + 1)}`+ expect(Buffer.byteLength(padded)).toBeGreaterThan(maxBodyBytes)++ const hang = hangStream()+ hangs.push(hang)+ const servers = createProxyApps(config({ maxBodyBytes, maxVisualBodyBytes: 1024 * 1024 }), {+ streamCompletion: hang.impl,+ })+ serversToClose.push(servers)+ const listener = createServer(servers.runtimeApp).listen(0)+ listeners.push(listener)+ const address = listener.address()+ if (!address || typeof address === 'string') throw new Error('listener has no port')+ const port = address.port++ const response = fetch(`http://127.0.0.1:${port}/internal/runtime/v1/codex/completions`, {+ method: 'POST',+ headers: {+ Authorization: `Bearer ${small.token}`,+ 'content-type': 'application/json',+ },+ body: padded,+ })+ await waitFor(+ () => streamGate.snapshot().running === 1 && visualStreamGate.snapshot().running === 0,+ 'padded small V2 kept the visual slot instead of the ordinary stream gate'+ )+ hang.release()+ await response+ })+ })diff --cc codex-llm-proxy/test/streamLimitsFreeze.test.tsindex 62c568c3b,000000000..542460ddcmode 100644,000000..100644--- a/codex-llm-proxy/test/streamLimitsFreeze.test.ts+++ b/codex-llm-proxy/test/streamLimitsFreeze.test.ts@@@ -1,73 -1,0 +1,109 @@@+import { describe, expect, it } from 'vitest'+import { readdirSync, readFileSync } from 'node:fs'+import { STREAM_LIMITS } from '../src/requestLimits.js'++const fixturePath = new URL(+ '../../tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json',+ import.meta.url+)+const srcDir = new URL('../src/', import.meta.url)+- // Every construction of a transport error in src, with its first code argument.- // UpstreamTimeoutError takes its metric kind first and its wire code second.- const TRANSPORT_ERROR_SITE =- /new (?:CodexTransportError|UpstreamTimeoutError)\(\s*(?:'(?:idle|total)',\s*)?([^,)\s]+)/g++// Every construction of a transport error in src. UpstreamTimeoutError takes++// its metric kind first and its wire code second.++const TRANSPORT_ERROR_SITE = /new (CodexTransportError|UpstreamTimeoutError)\(/g++const CODE_LITERAL = /^'([a-z][a-z0-9_]+)'$/++// A code chosen between two literals, e.g. `kind === 'size' ? 'a' : 'b'`.++const CODE_TERNARY = /^[^?]+\?\s*'([a-z][a-z0-9_]+)'\s*:\s*'([a-z][a-z0-9_]+)'$/++++// Splits the call's top-level arguments, skipping over string contents and++// nested brackets, and stops at the closing parenthesis of the call.++function callArguments(source: string, openParen: number): string[] {++ const args: string[] = []++ let depth = 0++ let start = openParen + 1++ for (let i = start; i < source.length; i += 1) {++ const ch = source[i]!++ if (ch === "'" || ch === '"' || ch === '`') {++ for (i += 1; source[i] !== ch; i += 1) if (source[i] === '\\') i += 1++ } else if (ch === '(' || ch === '[' || ch === '{') {++ depth += 1++ } else if (depth > 0 && (ch === ')' || ch === ']' || ch === '}')) {++ depth -= 1++ } else if (depth === 0 && (ch === ',' || ch === ')')) {++ args.push(source.slice(start, i).trim())++ if (ch === ')') return args++ start = i + 1++ }++ }++ throw new Error(`unterminated call at offset ${openParen}`)++}++function emittedTransportCodes(): { codes: Set<string>; sites: number; constructions: number } {+ const codes = new Set<string>()+ let sites = 0+ let constructions = 0+ for (const name of readdirSync(srcDir).filter(file => file.endsWith('.ts'))) {+ const source = readFileSync(new URL(name, srcDir), 'utf8')+ constructions +=+ source.split('new CodexTransportError(').length - 1 ++ source.split('new UpstreamTimeoutError(').length - 1+ for (const match of source.matchAll(TRANSPORT_ERROR_SITE)) {- const literal = /^'([a-z][a-z0-9_]+)'$/.exec(match[1]!)- // A computed code cannot be checked against the taxonomy, so it fails here.- expect(literal, `${name}: transport error code must be a string literal, got ${match[1]}`)- .not.toBeNull()- codes.add(literal![1]!)++ const args = callArguments(source, match.index! + match[0].length - 1)++ const code = args[match[1] === 'UpstreamTimeoutError' ? 1 : 0] ?? ''++ const literal = CODE_LITERAL.exec(code)++ const ternary = CODE_TERNARY.exec(code)++ // Any other computed code cannot be checked against the taxonomy, so it fails here.++ expect(++ literal ?? ternary,++ `${name}: transport error code must be a string literal or a choice of two, got ${code}`++ ).not.toBeNull()++ if (literal) codes.add(literal[1]!)++ if (ternary) codes.add(ternary[1]!).add(ternary[2]!)+ sites += 1+ }+ }+ return { codes, sites, constructions }+}++describe('codex-subscription stream limits freeze', () => {+ it('pins the proxy STREAM_LIMITS to the limits the fixture publishes', () => {+ // StreamGate and the upstream deadlines enforce these published bounds+ // from the proxy's own constant, not from the contract package, so the+ // Codex freeze gate in tests/e2e cannot see them.+ const { limits } = JSON.parse(readFileSync(fixturePath, 'utf8')) as {+ limits: Record<string, unknown>+ }+ const streamLimits: Record<string, number> = { ...STREAM_LIMITS }+ expect(Object.keys(streamLimits).sort()).toEqual([+ 'maxConcurrentStreams',+ 'maxQueueWaitMs',+ 'maxQueuedRequests',+ 'maxStreamDurationMs',+ 'upstreamIdleTimeoutMs',+ ])+ for (const name of Object.keys(streamLimits)) {+ expect({ [name]: limits[name] }).toEqual({ [name]: streamLimits[name] })+ }+ })++ it('publishes every transport error code the proxy emits in the fixture errorTaxonomy', () => {+ const { errorTaxonomy } = JSON.parse(readFileSync(fixturePath, 'utf8')) as {+ errorTaxonomy: string[]+ }+ const { codes, sites, constructions } = emittedTransportCodes()+ // Liveness witness: the pattern read the code of every construction a- // plain substring count finds, including both upstream timeout codes.++ // plain substring count finds, including both upstream timeout codes and++ // payload_too_large, which src only emits from a two-literal choice.+ expect(constructions).toBeGreaterThanOrEqual(15)+ expect(sites).toBe(constructions)+ expect([...codes]).toEqual(- expect.arrayContaining(['provider_unavailable', 'stream_duration_exceeded'])++ expect.arrayContaining([++ 'provider_unavailable',++ 'stream_duration_exceeded',++ 'payload_too_large',++ ])+ )+ const unpublished = [...codes].filter(code => !errorTaxonomy.includes(code)).sort()+ expect(unpublished, 'emitted transport codes missing from errorTaxonomy').toEqual([])+ })+})diff --cc control-api/package-lock.jsonindex 026eff3ee,716800e60..0056e37ad--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@@ -1,12 -1,12 +1,12 @@@{"name": "clerum-control-api",- "version": "0.1.602",- "version": "0.1.601",++ "version": "0.1.603","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.602",- "version": "0.1.601",++ "version": "0.1.603","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --cc control-api/package.jsonindex 8ec051e38,a4c6f4544..4bcfb5ef1--- a/control-api/package.json+++ b/control-api/package.json@@@ -1,6 -1,6 +1,6 @@@{"name": "clerum-control-api",- "version": "0.1.602",- "version": "0.1.601",++ "version": "0.1.603","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --cc control-api/test/services.llmProviderAttemptAuthorization.realPostgres.integration.test.tsindex b5891d505,8b5e30497..f5ca923ea--- a/control-api/test/services.llmProviderAttemptAuthorization.realPostgres.integration.test.ts+++ b/control-api/test/services.llmProviderAttemptAuthorization.realPostgres.integration.test.ts@@@ -224,70 -225,133 +226,200 @@@ describeRealPostgres('Codex provider-atexpect(leftover.rows[0]?.count).toBe('0')})+ it('reserves a danger-zone token budget for the whole attempt lifetime', async () => {+ const current = await getSafeCodexSubscriptionConnection(pool)+ // remaining (1000) < max_task_amount (2000) puts the budget in the danger+ // zone, and min_start 0 lets the attempt through with a reservation.+ const budget = await pool.query<{ id: string }>(+ `INSERT INTO token_budgets+ (name, scope, unit, limit_amount, period, timezone,+ min_start_amount, max_task_amount, enforcement)+ VALUES ('attempt-envelope', '{}'::jsonb, 'tokens', 1000, 'monthly', 'UTC', 0, 2000, 'block')+ RETURNING id`+ )+ const budgetId = budget.rows[0]!.id+ __resetBudgetCheckCache()+ try {+ const invocationId = `invocation-envelope-${randomUUID()}`+ const authorized = await authorizeLlmProviderAttempt(+ claims(),+ {+ request: { ...REQUEST, requestId: 'req-pg-3', idempotencyKey: 'idem-pg-3' },+ invocationId,+ attemptGeneration: 1,+ providerAttemptIndex: 1,+ policyRevision: current!.catalogRevision,+ policyHash: computeCodexPolicyHash({+ model: REQUEST.model,+ catalogRevision: current!.catalogRevision,+ credentialRevision: current!.credentialRevision,+ connectionKey: current!.connectionKey,+ }),+ },+ testDeps()+ )+ expect(authorized.providerAttemptId).toEqual(expect.any(String))++ // created_at and expires_at come from the same statement's NOW(), so+ // their difference is exactly the TTL the reservation was written with.+ const reservations = await pool.query<{+ id: string+ task_ref: string+ ttl_seconds: number+ }>(+ `SELECT id, task_ref,+ EXTRACT(EPOCH FROM (expires_at - created_at))::int AS ttl_seconds+ FROM budget_pending_reservations+ WHERE budget_id = $1`,+ [budgetId]+ )+ expect(reservations.rows).toEqual([+ {+ id: expect.any(String),+ task_ref: `${invocationId}:1:1`,+ ttl_seconds: CODEX_ATTEMPT_RESERVATION_TTL_SECONDS,+ },+ ])+ expect(CODEX_ATTEMPT_RESERVATION_TTL_SECONDS).toBe(2160)++ const attempt = await pool.query<{ budget_reservation_id: string }>(+ `SELECT budget_reservation_id FROM llm_provider_attempts WHERE invocation_id = $1`,+ [invocationId]+ )+ expect(attempt.rows).toEqual([{ budget_reservation_id: reservations.rows[0]!.id }])+ } finally {+ await pool.query('DELETE FROM token_budgets WHERE id = $1', [budgetId])+ __resetBudgetCheckCache()+ }+ })+++ it.each([false, true])(+ 'rolls back an oversized signed V2 envelope without losing an existing reservation (presented=%s)',+ async presented => {+ const current = await getSafeCodexSubscriptionConnection(pool)+ const invocationId = `visual-envelope-${randomUUID()}`+ const request = {+ ...REQUEST,+ schemaVersion: 'codex-completion-request.v2',+ messages: [{ role: 'user', content: '' }],+ }+ const budgetId = randomUUID()+ const existingReservationId = randomUUID()+ // Force the real danger-zone path without relying on rollup timing. The+ // configured maximum task is greater than the remaining period allowance.+ await pool.query(+ `INSERT INTO token_budgets+ (id, name, scope, unit, limit_amount, period, min_start_amount, max_task_amount, enforcement)+ VALUES ($1, 'visual envelope rollback', $2::jsonb, 'tokens', 100, 'daily', 1, 200, 'block')`,+ [budgetId, JSON.stringify({ provider: ['codex-subscription'], model: [REQUEST.model] })]+ )+ await pool.query(+ `INSERT INTO budget_pending_reservations+ (id, budget_id, est_amount, task_ref, host_ref, expires_at)+ VALUES ($1, $2, 5, 'preexisting-visual-task', 'research-host', NOW() + INTERVAL '15 minutes')`,+ [existingReservationId, budgetId]+ )+ __resetBudgetCheckCache()+ const payload = {+ request,+ invocationId,+ attemptGeneration: 1,+ providerAttemptIndex: 1,+ policyRevision: current!.catalogRevision,+ policyHash: computeCodexPolicyHash({+ model: REQUEST.model,+ catalogRevision: current!.catalogRevision,+ credentialRevision: current!.credentialRevision,+ connectionKey: current!.connectionKey,+ }),+ ...(presented ? { budgetReservationId: existingReservationId } : {}),+ }+ request.messages[0].content = 'x'.repeat(+ LIMITS.maxRequestBodyBytes - Buffer.byteLength(JSON.stringify(payload)) - 16+ )+ const counts = () =>+ pool.query(`SELECT+ (SELECT count(*)::text FROM llm_provider_attempts) AS attempts,+ (SELECT count(*)::text FROM llm_provider_attempt_tickets) AS tickets,+ (SELECT count(*)::text FROM budget_pending_reservations) AS reservations`)+ const readExisting = () =>+ pool.query('SELECT * FROM budget_pending_reservations WHERE id = $1', [+ existingReservationId,+ ])+ const before = (await counts()).rows[0]+ const existingBefore = (await readExisting()).rows[0]+ let observedReservationId: string | undefined+ let signed = false+ try {+ await expect(+ authorizeLlmProviderAttempt(+ claims(),+ payload,+ testDeps({+ issueTicket: async (db, input) => {+ observedReservationId = input.budgetReservationId+ const active = await db.query(+ 'SELECT id FROM budget_pending_reservations WHERE budget_id = $1',+ [budgetId]+ )+ expect(active.rows).toHaveLength(presented ? 1 : 2)+ if (presented) expect(input.budgetReservationId).toBe(existingReservationId)+ else {+ expect(input.budgetReservationId).not.toBe('unbudgeted')+ expect(input.budgetReservationId).not.toBe(existingReservationId)+ }+ const issued = await issueRegisteredCodexExecutionTicket(db, input)+ const registered = await db.query(+ 'SELECT jti FROM llm_provider_attempt_tickets WHERE provider_attempt_id = $1',+ [input.providerAttemptId]+ )+ expect(registered.rows).toHaveLength(1)+ signed = true+ // The request's text budget stays at 1 MiB. Inject an oversized+ // return value only after a real ticket/reservation exists in+ // this transaction, to prove the final envelope guard rolls+ // back database writes. This value never leaves the test.+ return {+ ...issued,+ executionTicket:+ issued.executionTicket + 'x'.repeat(LIMITS.maxVisualRequestBodyBytes),+ }+ },+ })+ )+ ).rejects.toMatchObject({ code: 'payload_too_large' })+ expect(signed).toBe(true)+ expect((await counts()).rows[0]).toEqual(before)+ expect((await readExisting()).rows[0]).toEqual(existingBefore)+ if (!presented) {+ expect(observedReservationId).toBeDefined()+ expect(+ (+ await pool.query('SELECT id FROM budget_pending_reservations WHERE id = $1', [+ observedReservationId,+ ])+ ).rows+ ).toHaveLength(0)+ }+ const leftover = await pool.query(+ 'SELECT id FROM llm_provider_attempts WHERE invocation_id = $1',+ [invocationId]+ )+ expect(leftover.rows).toHaveLength(0)+ // A failed size check must not burn the invocation/attempt identity.+ // Retrying that same binding with a request that fits must authorize.+ request.messages[0].content = 'A bounded request after rollback'+ const retried = await authorizeLlmProviderAttempt(claims(), payload, testDeps())+ const committed = await pool.query(+ 'SELECT id FROM llm_provider_attempts WHERE invocation_id = $1',+ [invocationId]+ )+ expect(committed.rows).toEqual([{ id: retried.providerAttemptId }])+ expect((await readExisting()).rows[0]).toEqual(existingBefore)+ } finally {+ await pool.query('DELETE FROM token_budgets WHERE id = $1', [budgetId])+ __resetBudgetCheckCache()+ }+ }+ )})diff --cc control-api/test/services.llmProviderAttemptAuthorizer.test.tsindex b9adcdad9,a0a84d9ca..472732659--- a/control-api/test/services.llmProviderAttemptAuthorizer.test.ts+++ b/control-api/test/services.llmProviderAttemptAuthorizer.test.ts@@@ -153,6 -202,330 +202,332 @@@ function deps(}describe('authorizeLlmProviderAttempt', () => {+ // In-process integration, not browser E2E or live OAuth evidence. The Host,+ // both clients, authorizer, parsers/hash and proxy projection are real.+ // DB/grant custody and the external model are injected seams.+ it.each([+ ['png', 'attachment', false],+ ['jpeg', 'attachment', true],+ ['png', 'tool', true],+ ['jpeg', 'tool', false],+ ['png', 'attachment', false, 10 * 1024 * 1024],+ ['png', 'tool', true, 5 * 1024 * 1024],+ ['jpeg', 'attachment', true, 5 * 1024 * 1024],+ ['jpeg', 'tool', true, 5 * 1024 * 1024],+ ] as const)(+ 'carries %s from %s (tools=%s) through Host authorization and proxy projection',+ async (format, origin, withTools, imageBytes = 0) => {+ const fixtures = loadVisualFixtures()+ let authorized: Awaited<ReturnType<typeof authorizeLlmProviderAttempt>> | undefined+ const authorizer = new ProviderAttemptAuthorizer({+ authorizeUrl: 'http://test-control/authorize',+ readPlatformJwt: () => FIXTURE_ACCESS_VALUE,+ fetchFn: async (_url, init) => {+ authorized = await authorizeLlmProviderAttempt(+ claims(),+ JSON.parse(String(init?.body)),+ deps()+ )+ return Response.json(authorized)+ },+ })+ let projectedBody: Record<string, unknown> | undefined+ const finalize = vi.fn(async () => ({+ providerAttemptId: authorized!.providerAttemptId,+ outcome: 'success' as const,+ duplicate: false,+ }))+ const proxy = new CodexLlmProxyClient({+ runtimeUrl: 'http://test-proxy/completions',+ readPlatformJwt: () => FIXTURE_ACCESS_VALUE,+ fetchFn: async (_url, init) => {+ expect(authorized).toBeDefined()+ const envelope = JSON.parse(String(init?.body))+ const frames: unknown[] = []+ const result = await streamCodexCompletion({+ ...envelope,++ maxDeadlineMs: 1_800_000,+ ticket: {+ jti: 'fixture-ticket',+ hostRef: 'research-host',+ model: REQUEST.model,+ requestHash: authorized!.requestHash,+ providerAttemptId: authorized!.providerAttemptId,+ },+ redeem: async () => ({+ accessToken: FIXTURE_ACCESS_VALUE,+ chatgptAccountId: 'fixture-account',+ attemptReceipt: 'a'.repeat(64),+ expiryClass: 'short_lived',+ transport: {+ protocolVersion: 'codex-subscription-transport.v1',+ completionsOrigin: CODEX_COMPLETIONS_ORIGIN,+ catalogOrigin: CODEX_CATALOG_ORIGIN,+ operation: 'completion_stream',+ servedModel: REQUEST.model,+ maxStreamDurationMs: 1000,+ },+ }),+ finalize,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ fetchFn: async (_upstream, requestInit) => {+ projectedBody = JSON.parse(String(requestInit?.body))+ const parts = (+ projectedBody!.input as Array<{ role?: string; content: Array<{ type: string }> }>+ ).find(item => item.role === 'user')!.content+ expect(parts.some(part => part.type === 'input_image')).toBe(true)+ return new Response(+ 'data: {"type":"response.output_text.delta","delta":"image received"}\n\ndata: {"type":"response.completed"}\n\n'+ )+ },+ onFrame: frame => frames.push(frame),+ })+ frames.push({ type: 'done', ...result })+ return new Response(frames.map(frame => `data: ${JSON.stringify(frame)}\n\n`).join(''))+ },+ })+ const provider = new CodexSubscriptionProvider(REQUEST.model, {+ authorizer,+ proxy,+ attemptContext: () => ({+ policyRevision: 4,+ policyHash: body().policyHash,+ hostRef: 'research-host',+ }),+ })+ const image = fixtures[format].messages[0].contentParts.find(+ (part: { type: string }) => part.type === 'image'+ )+ if (imageBytes > 0) {+ image.data =+ format === 'png'+ ? padPngToSize(image.data, imageBytes).toString('base64')+ : jpegOfSize(imageBytes).toString('base64')+ expect(Buffer.from(image.data, 'base64')).toHaveLength(imageBytes)+ }+ let messages = fixtures[format].messages+ if (origin === 'tool') {+ image.source = { kind: 'tool', attachmentId: 'fixture-image', toolCallId: 'fixture-call' }+ messages = [+ {+ role: 'assistant',+ content: '',+ tool_calls: [{ id: 'fixture-call', name: 'screenshot', arguments: {} }],+ },+ {+ role: 'tool',+ content: 'Screenshot ready',+ tool_call_id: 'fixture-call',+ name: 'screenshot',+ },+ ...messages,+ ]+ }+ const result = withTools+ ? await provider.completeSingleTurnWithTools(messages, [+ {+ name: 'screenshot',+ description: 'Take a screenshot',+ parameters: { type: 'object', properties: {} },+ },+ ])+ : await provider.completeSingleTurn(messages)+ expect(+ (projectedBody!.input as Array<{ role?: string; content: unknown[] }>).find(+ item => item.role === 'user'+ )!.content+ ).toContainEqual({+ type: 'input_image',+ image_url: `data:${image.mimeType};base64,${image.data}`,+ detail: 'high',+ })+ if (origin === 'tool') {+ expect((projectedBody!.input as Array<{ type?: string }>).map(item => item.type)).toEqual([+ 'function_call',+ 'function_call_output',+ undefined,+ ])+ }+ expect(result.content).toBe('image received')+ expect(finalize).toHaveBeenCalledOnce()+ },+ 60_000+ )++ it.each([+ {+ name: '10MiB PNG + 5MiB JPEG',+ images: [+ { format: 'png' as const, bytes: 10 * MIB },+ { format: 'jpeg' as const, bytes: 5 * MIB },+ ],+ },+ {+ name: 'three 5MiB PNGs',+ images: [+ { format: 'png' as const, bytes: 5 * MIB },+ { format: 'png' as const, bytes: 5 * MIB },+ { format: 'png' as const, bytes: 5 * MIB },+ ],+ },+ ])(+ 'carries $name through Host authorization and proxy projection',+ async ({ images }) => {+ const fixtures = loadVisualFixtures()+ const parts = images.map((item, index) => {+ const template = fixtures[item.format].messages[0].contentParts.find(+ part => part.type === 'image'+ )+ expect(template).toBeDefined()+ const data =+ item.format === 'png'+ ? padPngToSize(template!.data, item.bytes).toString('base64')+ : jpegOfSize(item.bytes).toString('base64')+ expect(Buffer.from(data, 'base64')).toHaveLength(item.bytes)+ return {+ ...template!,+ data,+ source: {+ kind: 'attachment' as const,+ attachmentId: `att-${index}`,+ messageId: 'msg-1',+ },+ }+ })+ let authorized: Awaited<ReturnType<typeof authorizeLlmProviderAttempt>> | undefined+ const authorizer = new ProviderAttemptAuthorizer({+ authorizeUrl: 'http://test-control/authorize',+ readPlatformJwt: () => FIXTURE_ACCESS_VALUE,+ fetchFn: async (_url, init) => {+ authorized = await authorizeLlmProviderAttempt(+ claims(),+ JSON.parse(String(init?.body)),+ deps()+ )+ return Response.json(authorized)+ },+ })+ let projectedBody: Record<string, unknown> | undefined+ const finalize = vi.fn(async () => ({+ providerAttemptId: authorized!.providerAttemptId,+ outcome: 'success' as const,+ duplicate: false,+ }))+ const proxy = new CodexLlmProxyClient({+ runtimeUrl: 'http://test-proxy/completions',+ readPlatformJwt: () => FIXTURE_ACCESS_VALUE,+ fetchFn: async (_url, init) => {+ expect(authorized).toBeDefined()+ const envelope = JSON.parse(String(init?.body))+ const frames: unknown[] = []+ const result = await streamCodexCompletion({+ ...envelope,++ maxDeadlineMs: 1_800_000,+ ticket: {+ jti: 'fixture-ticket',+ hostRef: 'research-host',+ model: REQUEST.model,+ requestHash: authorized!.requestHash,+ providerAttemptId: authorized!.providerAttemptId,+ },+ redeem: async () => ({+ accessToken: FIXTURE_ACCESS_VALUE,+ chatgptAccountId: 'fixture-account',+ attemptReceipt: 'a'.repeat(64),+ expiryClass: 'short_lived',+ transport: {+ protocolVersion: 'codex-subscription-transport.v1',+ completionsOrigin: CODEX_COMPLETIONS_ORIGIN,+ catalogOrigin: CODEX_CATALOG_ORIGIN,+ operation: 'completion_stream',+ servedModel: REQUEST.model,+ maxStreamDurationMs: 1000,+ },+ }),+ finalize,+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],+ fetchFn: async (_upstream, requestInit) => {+ projectedBody = JSON.parse(String(requestInit?.body))+ return new Response(+ 'data: {"type":"response.output_text.delta","delta":"image received"}\n\ndata: {"type":"response.completed"}\n\n'+ )+ },+ onFrame: frame => frames.push(frame),+ })+ frames.push({ type: 'done', ...result })+ return new Response(frames.map(frame => `data: ${JSON.stringify(frame)}\n\n`).join(''))+ },+ })+ const provider = new CodexSubscriptionProvider(REQUEST.model, {+ authorizer,+ proxy,+ attemptContext: () => ({+ policyRevision: 4,+ policyHash: body().policyHash,+ hostRef: 'research-host',+ }),+ })+ const result = await provider.completeSingleTurn([+ {+ role: 'user',+ content: 'Describe the attached image',+ contentParts: [{ type: 'text', text: 'Describe the attached image' }, ...parts],+ },+ ])+ const projectedParts = (+ projectedBody!.input as Array<{ role?: string; content: unknown[] }>+ ).find(item => item.role === 'user')!.content+ for (const part of parts) {+ expect(projectedParts).toContainEqual({+ type: 'input_image',+ image_url: `data:${part.mimeType};base64,${part.data}`,+ detail: 'high',+ })+ }+ expect(result.content).toBe('image received')+ expect(finalize).toHaveBeenCalledOnce()+ },+ 60_000+ )++ it('checks the V2 proxy envelope inside the transaction, after signing and before commit', async () => {+ const transactionEvents: string[] = []+ const request = {+ ...REQUEST,+ schemaVersion: 'codex-completion-request.v2',+ messages: [{ role: 'user', content: '' }],+ }+ const payload = body({ request })+ request.messages[0].content = 'x'.repeat(+ LIMITS.maxRequestBodyBytes - Buffer.byteLength(JSON.stringify(payload)) - 16+ )+ expect(Buffer.byteLength(JSON.stringify(payload))).toBeLessThan(LIMITS.maxRequestBodyBytes)+ const current = deps({+ withTransaction: async work => {+ transactionEvents.push('begin')+ try {+ const result = await work({ query: vi.fn() } as never)+ transactionEvents.push('commit')+ return result+ } catch (error) {+ transactionEvents.push('rollback')+ throw error+ }+ },+ // Fault injection at the signing seam: exercise the post-signing envelope+ // guard without exceeding the unchanged 1 MiB non-image request budget.+ issueTicket: vi.fn().mockResolvedValue({+ executionTicket: 't'.repeat(LIMITS.maxVisualRequestBodyBytes),+ expiresAt: new Date('2026-09-16T23:00:00Z'),+ }),+ })+ await expect(authorizeLlmProviderAttempt(claims(), payload, current)).rejects.toMatchObject({+ code: 'payload_too_large',+ })+ expect(current.issueTicket).toHaveBeenCalledOnce()+ expect(transactionEvents).toEqual(['begin', 'rollback'])+ })+let current: LlmProviderAttemptAuthorizerDepsbeforeEach(() => {diff --cc deploy/base/control-plane/codex-llm-proxy.yamlindex 41630ac67,d8cf25cb2..ae8f33b41--- a/deploy/base/control-plane/codex-llm-proxy.yaml+++ b/deploy/base/control-plane/codex-llm-proxy.yaml@@@ -19,8 -19,11 +19,11 @@@ dataCODEX_LLM_PROXY_ADMIN_PORT: "8081"CODEX_LLM_PROXY_PROBE_PORT: "9090"CODEX_LLM_PROXY_MAX_BODY_BYTES: "1048576"+ # V2 visual envelope. Keep this equal to LIMITS.maxVisualRequestBodyBytes.+ # Ordinary and anonymous requests still use CODEX_LLM_PROXY_MAX_BODY_BYTES.+ CODEX_LLM_PROXY_MAX_VISUAL_BODY_BYTES: "25165824"- CODEX_LLM_PROXY_MAX_STREAM_DURATION_MS: "300000"- CODEX_LLM_PROXY_MAX_DEADLINE_MS: "300000"+ CODEX_LLM_PROXY_MAX_STREAM_DURATION_MS: "1800000"+ CODEX_LLM_PROXY_MAX_DEADLINE_MS: "1800000"CODEX_LLM_PROXY_JWT_ISSUER: control-apiCODEX_LLM_PROXY_EXECUTION_ENABLED: "false"CODEX_LLM_PROXY_CONTROL_API_URL: http://control-api-rpc-gateway.control-plane.svc.cluster.local:8090/api/v1diff --cc docs/architecture/codex-subscription-transport-contract.mdindex 48979ed38,811f2f205..c8f9b792e--- a/docs/architecture/codex-subscription-transport-contract.md+++ b/docs/architecture/codex-subscription-transport-contract.md@@@ -48,30 -144,18 +144,31 @@@ OAuth scopes: `openid`, `profile`, `emaAll values are finite and greater than zero. `maxRetriesPerAttempt` is `1`:one physical execution per ticket. A retry or fallback must mint a new attempt.--| Limit | Value |-| -------------------- | ------- |-| maxRequestBodyBytes | 1048576 |-| maxMessages | 1024 |-| maxToolCalls | 256 |-| maxOutputTokens | 16384 |-| maxStreamDurationMs | 300000 |-| maxDeadlineMs | 300000 |-| maxConcurrentStreams | 8 |-| maxQueuedRequests | 16 |-| maxRetriesPerAttempt | 1 |+The value is not a counter the proxy consults; it is a property of the+single-use redeem in control-api, enforced in three layers inside one+transaction: the ticket row is locked and must still be `issued`, the attempt+must still be `authorized`, and the consuming `UPDATE` matches only+`status = 'issued'`. A second redeem of the same ticket fails with+`ticket_replayed` and leaves the ledger unchanged. The real-PostgreSQL tests+`services.llmProviderAttemptRedemption.realPostgres.integration.test.ts`+(sequential replay and 20 concurrent redeems) and+`services.grokProviderAttemptRedemption.refresh.realPostgres.integration.test.ts`+(Grok replay) pin this behaviour.+- | Limit | Value |- | --------------------- | ------- |- | maxRequestBodyBytes | 1048576 |- | maxMessages | 1024 |- | maxToolCalls | 256 |- | maxOutputTokens | 16384 |- | maxStreamDurationMs | 1800000 |- | maxDeadlineMs | 1800000 |- | maxConcurrentStreams | 8 |- | maxQueuedRequests | 16 |- | maxQueueWaitMs | 60000 |- | upstreamIdleTimeoutMs | 300000 |- | maxRetriesPerAttempt | 1 |++| Limit | Value |++| ------------------------- | -------- |++| maxRequestBodyBytes | 1048576 |++| maxVisualRequestBodyBytes | 25165824 |++| maxMessages | 1024 |++| maxToolCalls | 256 |++| maxOutputTokens | 16384 |++| maxStreamDurationMs | 1800000 |++| maxDeadlineMs | 1800000 |++| maxConcurrentStreams | 8 |++| maxQueuedRequests | 16 |++| maxQueueWaitMs | 60000 |++| upstreamIdleTimeoutMs | 300000 |++| maxRetriesPerAttempt | 1 |Tool definitions have no independent count ceiling in the Evenfire requestcontract. The entire serialized request, including all definitions, remainsdiff --cc mcp-host/package-lock.jsonindex 31e677e92,cd947ff9e..7920f127f--- a/mcp-host/package-lock.json+++ b/mcp-host/package-lock.json@@@ -1,12 -1,12 +1,12 @@@{"name": "clerum-mcp-host",- "version": "0.1.369",- "version": "0.1.368",++ "version": "0.1.370","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-mcp-host",- "version": "0.1.369",- "version": "0.1.368",++ "version": "0.1.370","license": "SEE LICENSE IN LICENSE","dependencies": {"@anthropic-ai/sdk": "^0.32.0",diff --cc mcp-host/package.jsonindex 826596dc8,3dc925d7b..fbb6fd5d6--- a/mcp-host/package.json+++ b/mcp-host/package.json@@@ -1,6 -1,6 +1,6 @@@{"name": "clerum-mcp-host",- "version": "0.1.369",- "version": "0.1.368",++ "version": "0.1.370","description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude","main": "dist/main.js","engines": {diff --cc tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.jsonindex c061d25ab,03c3997e4..3c534fd81--- a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json+++ b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json@@@ -63,10 -62,9 +64,12 @@@"ticket_invalid","ticket_replayed","request_hash_mismatch",+ "invalid_request",+ "image_source_invalid",+ "payload_too_large",- "tool_call_limit_exceeded"+ "tool_call_limit_exceeded",+ "sse_buffer_exceeded",+ "stream_duration_exceeded"],"termsAndTestAccount": {"automationAuthorized": true,diff --cc tests/e2e/fixtures/package-lock.jsonindex 87f7ab185,3a9bac504..78c5189ed--- a/tests/e2e/fixtures/package-lock.json+++ b/tests/e2e/fixtures/package-lock.json@@@ -1,12 -1,12 +1,12 @@@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.91",- "version": "1.0.90",++ "version": "1.0.92","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "@clerum/e2e-test-fixtures",- "version": "1.0.91",- "version": "1.0.90",++ "version": "1.0.92","dependencies": {"@kubernetes/client-node": "^1.0.0"},diff --cc tests/e2e/fixtures/package.jsonindex db0b424af,91a0be7f9..aa7639ef1--- a/tests/e2e/fixtures/package.json+++ b/tests/e2e/fixtures/package.json@@@ -1,6 -1,6 +1,6 @@@{"name": "@clerum/e2e-test-fixtures",- "version": "1.0.91",- "version": "1.0.90",++ "version": "1.0.92","description": "E2E test fixtures and utilities for Clerum testing","type": "module","main": "index.ts",diff --cc tests/e2e/integration/codex-subscription-contract-freeze.test.tsindex 3d67eb793,c975fb76b..609709cf8--- a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts+++ b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts@@@ -8,9 -8,9 +8,10 @@@*/import { describe, expect, it } from 'vitest'import { existsSync, readFileSync } from 'node:fs'+ import { createRequire } from 'node:module'import { dirname, join } from 'node:path'import { fileURLToPath } from 'node:url'+import { LIMITS } from '@clerum/llm-provider-attempt-contract'const here = dirname(fileURLToPath(import.meta.url))const repoRoot = join(here, '../../..')@@@ -50,24 -65,6 +68,25 @@@ const REQUIRED_LIMIT_KEYS ='maxRetriesPerAttempt',] as const+// Runtime `LIMITS` keys that the fixture also publishes. Each must carry the+// same value on both sides.+const PUBLISHED_RUNTIME_LIMIT_KEYS = [+ 'maxDeadlineMs',+ 'maxMessages',+ 'maxOutputTokens',+ 'maxRequestBodyBytes',+ 'maxToolCalls',++ 'maxVisualRequestBodyBytes',+] as const++// Runtime bounds the published contract deliberately does not describe. Every+// key of the runtime `LIMITS` must be in exactly one of these two lists, so+// adding a key to `LIMITS` fails this suite until someone decides which.+// Publishing it takes four edits: the fixture's `limits`, REQUIRED_LIMIT_KEYS,+// PUBLISHED_RUNTIME_LIMIT_KEYS and the architecture doc table. Keeping it+// runtime-only takes one: add it here.+const RUNTIME_ONLY_LIMIT_KEYS = ['maxIdLength', 'maxNestingDepth'] as const+const SENSITIVE_VALUE_PATTERN =/^(?:sk-[A-Za-z0-9]+|Bearer\s+\S+|eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+|(?!https?:\/\/)[^;\s]+=[^;\s]+(?:;|$))/@@@ -232,23 -210,8 +286,24 @@@ describe('codex-subscription contract fexpect(limits?.maxToolCalls).toBe(256)expect(limits?.maxMessages).toBe(1024)expect(limits?.maxRequestBodyBytes).toBe(1048576)+ expect(limits?.maxVisualRequestBodyBytes).toBe(25165824)+ // The architecture doc publishes the same limits as a table; a row that+ // drifts from the fixture misdescribes what the runtime enforces. The table+ // is parsed as a whole so a duplicated, extra or missing row also fails.+ const docLimits = parseLimitsTable(architectureDoc)+ const docKeys = docLimits.map(([key]) => key)+ expect(+ [...docKeys].sort(),+ 'architecture doc limits table must list each fixture limit exactly once'+ ).toEqual([...REQUIRED_LIMIT_KEYS].sort())+ for (const [key, value] of docLimits) {+ expect(+ { [key]: value },+ `architecture doc limits table must list ${key} = ${String(limits?.[key])}`+ ).toEqual({ [key]: String(limits?.[key]) })+ }+const errors = contract.errorTaxonomyexpect(Array.isArray(errors) && (errors as unknown[]).length > 0).toBe(true)for (const code of errors as unknown[]) {diff --cc tests/e2e/package-lock.jsonindex 358a86daf,36c4b726b..c8fa4d356--- a/tests/e2e/package-lock.json+++ b/tests/e2e/package-lock.json@@@ -1,14 -1,13 +1,14 @@@{"name": "clerum-e2e-tests",- "version": "0.1.92",- "version": "0.1.90",++ "version": "0.1.93","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-e2e-tests",- "version": "0.1.92",- "version": "0.1.90",++ "version": "0.1.93","devDependencies": {+ "@clerum/llm-provider-attempt-contract": "file:../../packages/llm-provider-attempt-contract","@types/jsonwebtoken": "^9.0.10","@types/node": "^22.0.0","jsonwebtoken": "^9.0.2",diff --cc tests/e2e/package.jsonindex 2f5d506d8,561f40264..573ff5dd0--- a/tests/e2e/package.json+++ b/tests/e2e/package.json@@@ -1,6 -1,6 +1,6 @@@{"name": "clerum-e2e-tests",- "version": "0.1.92",- "version": "0.1.90",++ "version": "0.1.93","private": true,"type": "module","scripts": {