Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

chore(merge): merge origin/dev b86c4474b into fix/731-context-gauge-minified-json

Alfredo Lopez82aedf318ac0
diff --cc codex-llm-proxy/src/codexTransport.ts
index c94f391ae,4ce8cbdbd..ae31219a1
--- a/codex-llm-proxy/src/codexTransport.ts
+++ b/codex-llm-proxy/src/codexTransport.ts
@@@ -234,23 -344,21 +344,26 @@@ async function dispatchUpstreamStream
message.role === 'assistant' ? (message.toolCalls ?? []).map(call => call.name) : []
),
])
- const response = await fetchFrozenOrigin({
- url,
- fetchFn: input.fetchFn,
- lookup: input.lookup,
- init: {
- method: 'POST',
- signal,
- headers,
- body: JSON.stringify(toUpstreamPayload(input.request, names)),
- },
- })
+ const response = await deadline.waitUpstream(
+ fetchFrozenOrigin({
+ url,
+ fetchFn: input.fetchFn,
+ lookup: input.lookup,
+ init: {
+ method: 'POST',
+ signal,
+ headers,
+ body: JSON.stringify(toUpstreamPayload(input.request, names)),
+ },
+ }),
+ signal
+ )
if (!response.ok || !response.body) {
+ const credentialRejected = response.status === 401 || response.status === 403
+ const errorBody =
+ response.ok || credentialRejected
+ ? { read: 'skipped' as const }
+ : await readUpstreamErrorBody(response)
logger.warn(
{
event: 'codex_upstream_http',
@@@ -261,9 -368,6 +374,9 @@@
},
'Codex completions upstream returned a non-success status'
)
+ if (errorBody.read === 'parsed' && errorBody.code === CONTEXT_LENGTH_EXCEEDED) {
- throw new CodexTransportError(CONTEXT_LENGTH_EXCEEDED, 'upstream context window exceeded')
++ throw new CodexTransportError('context_length_exceeded', 'upstream context window exceeded')
+ }
if (response.status === 400) {
throw new CodexTransportError('invalid_request', 'upstream rejected the Codex request')
}
@@@ -275,48 -379,9 +388,48 @@@
}
throw new CodexTransportError('provider_unavailable', 'upstream completion failed')
}
- return consumeSse(response.body, input.onFrame, signal, names)
+ return consumeSse(response.body, input.onFrame, signal, names, deadline)
}
+// R9-6: a non-success completion body is read only to find a context-window
+// refusal. The recorded refusal object is well under 1 KiB; past this bound the
+// body is cancelled and the status mapping stands.
+const UPSTREAM_ERROR_BODY_MAX_BYTES = 16 * 1024
+
+// `read` is a closed set that is safe to log; no upstream text leaves here.
+type UpstreamErrorBody =
+ | { read: 'skipped' | 'absent' | 'oversize' | 'unreadable' | 'unparsable' }
+ | { read: 'parsed'; code: string | undefined }
+
+async function readUpstreamErrorBody(response: Response): Promise<UpstreamErrorBody> {
+ if (!response.body) return { read: 'absent' }
+ const reader = response.body.getReader()
+ const chunks: Uint8Array[] = []
+ let total = 0
+ try {
+ for (;;) {
+ const { done, value } = await reader.read()
+ if (done) break
+ total += value.byteLength
+ if (total > UPSTREAM_ERROR_BODY_MAX_BYTES) {
+ await reader.cancel().catch(() => undefined)
+ return { read: 'oversize' }
+ }
+ chunks.push(value)
+ }
+ } catch {
+ await reader.cancel().catch(() => undefined)
+ return { read: 'unreadable' }
+ }
+ let parsed: unknown
+ try {
+ parsed = JSON.parse(Buffer.concat(chunks).toString('utf8'))
+ } catch {
+ return { read: 'unparsable' }
+ }
+ return { read: 'parsed', code: isPlainObject(parsed) ? upstreamFailureCode(parsed) : undefined }
+}
+
function toUpstreamPayload(
request: CodexCompletionRequest,
names: ToolNameMap
@@@ -469,9 -530,8 +585,9 @@@ async function consumeSse
if (mapped.usage) usage = mapped.usage
if (mapped.completed) completed = true
if (mapped.failed) failed = true
+ if (mapped.failureCode === CONTEXT_LENGTH_EXCEEDED) contextLengthExceeded = true
}
- if (signal.aborted) break
+ signal.throwIfAborted()
}
buffer += decoder.decode().replace(/\r\n/g, '\n').replace(/\r/g, '\n')
if (buffer.trim()) {
@@@ -486,20 -545,15 +602,27 @@@
await reader.cancel().catch(() => undefined)
throw error
} finally {
- reader.releaseLock()
+ try {
+ reader.releaseLock()
+ } catch {
+ // A read that lost the race to an abort may still hold the lock after cancel.
+ }
}
+ // A canceled or failed stream leaves its open call truncated; report the
- // stream's outcome before the flush can refuse those arguments.
- if (signal.aborted) return { outcome: 'canceled', usage }
++ // stream's outcome before the flush can refuse those arguments. A deadline
++ // abort carries its typed UpstreamTimeoutError as the signal reason, so
++ // throwing the reason surfaces stream_duration_exceeded or
++ // provider_unavailable to the route instead of a plain cancel.
++ signal.throwIfAborted()
+ if (failed) {
+ if (contextLengthExceeded) {
- throw new CodexTransportError(CONTEXT_LENGTH_EXCEEDED, 'upstream context window exceeded')
++ throw new CodexTransportError('context_length_exceeded', 'upstream context window exceeded')
+ }
+ throw new CodexTransportError('provider_unavailable', 'upstream response failed')
+ }
for (const call of pending.values()) {
if (call.emitted) continue
- const args = parseToolArguments(call.arguments)
+ const args = parseToolArguments(call.arguments, { closed: false })
await acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })
call.emitted = true
}
diff --cc codex-llm-proxy/src/config.ts
index f64b31611,03097dbf6..32f773920
--- a/codex-llm-proxy/src/config.ts
+++ b/codex-llm-proxy/src/config.ts
@@@ -1,5 -1,6 +1,6 @@@
import { LIMITS } from '@clerum/llm-provider-attempt-contract'
- import { DEFAULT_MAX_BODY_BYTES } from './requestLimits.js'
-import { STREAM_LIMITS } from './requestLimits.js'
++import { DEFAULT_MAX_BODY_BYTES, STREAM_LIMITS } from './requestLimits.js'
+ import { DEFAULT_HEARTBEAT_INTERVAL_MS, MAX_HEARTBEAT_INTERVAL_MS } from './sseHeartbeat.js'
export type CodexLlmProxyConfig = {
runtimePort: number
diff --cc codex-llm-proxy/src/server.ts
index d5576726f,269d2a9ee..2d5b17a20
--- a/codex-llm-proxy/src/server.ts
+++ b/codex-llm-proxy/src/server.ts
@@@ -33,21 -28,10 +34,22 @@@ import
measureNonImageCompletionBytes,
requestBodyLimitBytes,
} from '@clerum/llm-provider-attempt-contract'
-import { RequestLimitError, streamGate, visualStreamGate } from './requestLimits.js'
+import {
+ BODY_READ_DEADLINE_MS,
+ BodyBudget,
+ ENVELOPE_ALLOWANCE_BYTES,
+ IN_FLIGHT_BODY_BUDGET_BODIES,
+ RequestLimitError,
+ streamGate,
+ visualStreamGate,
+} from './requestLimits.js'
+ import { startSseHeartbeat } from './sseHeartbeat.js'
-type GatedRequest = Request & { codexStreamRelease?: () => void }
+type GatedRequest = Request & {
+ codexStreamRelease?: () => void
+ /** Set by the platform gate before body admission (R9-M-B). */
+ codexPlatform?: PlatformJwtClaims
+}
const COMPLETION_KEYS = new Set(['executionTicket', 'requestHash', 'request', 'deadlineMs'])
const ADMIN_KEYS = new Set(['accessToken'])
@@@ -667,11 -533,11 +692,14 @@@ const ATTEMPT_ERROR_STATUS: Record<stri
model_not_allowed: 403,
insufficient_scope: 403,
disabled: 404,
+ // A reserved body that was not read within BODY_READ_DEADLINE_MS (R9-M-B).
+ request_timeout: 408,
ticket_replayed: 409,
tool_call_limit_exceeded: 422,
+ invalid_tool_arguments: 422,
+ // The attempt ran for its whole stream budget. Retrying the same request
+ // would spend the same budget again, so it is a gateway timeout, not 503.
+ stream_duration_exceeded: 504,
connection_unavailable: 503,
provider_unavailable: 503,
sse_buffer_exceeded: 503,
diff --cc codex-llm-proxy/test/bodyAdmission.test.ts
index 02ba0eb6c,000000000..037d4529f
mode 100644,000000..100644
--- a/codex-llm-proxy/test/bodyAdmission.test.ts
+++ b/codex-llm-proxy/test/bodyAdmission.test.ts
@@@ -1,757 -1,0 +1,759 @@@
+/**
+ * #731 R3-2 - body admission before `express.json`.
+ *
+ * At an 8 MiB request cap, the stream gate alone would let 24 bodies in (8
+ * running plus 16 queued). Five bodies in flight already peaked at 574-714 MiB
+ * of RSS, so 24 would not fit the proxy's 768Mi limit by that ratio (not
+ * measured at 24). These tests drive the real runtime app over HTTP and use
+ * the control-api `redeem` call as the witness: it runs only after the whole
+ * body was read, JSON-parsed, contract-parsed and hash-checked, so the number
+ * of attempts held there is the number of bodies in memory.
+ */
+import { describe, expect, it, vi } from 'vitest'
+import jwt from 'jsonwebtoken'
+import { generateKeyPairSync, randomBytes, randomUUID } from 'node:crypto'
+import { request as httpRequest } from 'node:http'
+import type { AddressInfo } from 'node:net'
+import { gzipSync } from 'node:zlib'
+import {
+ LIMITS,
+ hashCodexCompletionRequestV1,
+ parseCodexCompletionRequestV1,
+} from '@clerum/llm-provider-attempt-contract'
+import type { CodexLlmProxyConfig } from '../src/config.js'
+import type {
+ ControlApiClient,
+ FinalizeAttemptSuccess,
+ RedeemAttemptSuccess,
+} from '../src/controlApiClient.js'
+import { logger } from '../src/logger.js'
+import { CODEX_CATALOG_ORIGIN, CODEX_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'
+import {
+ BODY_READ_DEADLINE_MS,
+ DEFAULT_MAX_BODY_BYTES,
+ // The admission budget holds this many bodies of the configured maximum size.
+ IN_FLIGHT_BODY_BUDGET_BODIES as BUDGET_BODIES,
+ STREAM_LIMITS,
+} from '../src/requestLimits.js'
+import { type ProxyRuntimeDeps, createProxyApps } from '../src/server.js'
+
+/** Captured before any test fakes the clock, so waits stay on real time. */
+const realSetTimeout = globalThis.setTimeout
+
+const { privateKey, publicKey } = generateKeyPairSync('rsa', {
+ modulusLength: 2048,
+ publicKeyEncoding: { type: 'spki', format: 'pem' },
+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
+})
+
+const HOST_REF = 'research-host'
+const COMPLETIONS_PATH = '/internal/runtime/v1/codex/completions'
+
+function sign(payload: Record<string, unknown>, audience: string): string {
+ return jwt.sign(payload, privateKey, {
+ algorithm: 'RS256',
+ issuer: 'control-api',
+ audience,
+ expiresIn: 60,
+ })
+}
+
+function config(maxBodyBytes: number): CodexLlmProxyConfig {
+ return {
+ runtimePort: 0,
+ adminPort: 0,
+ probePort: 0,
+ maxBodyBytes,
+ maxVisualBodyBytes: LIMITS.maxVisualRequestBodyBytes,
+ maxStreamDurationMs: 60_000,
+ maxDeadlineMs: 60_000,
++ upstreamIdleTimeoutMs: 300_000,
++ heartbeatIntervalMs: 15_000,
+ jwtIssuer: 'control-api',
+ jwtPublicKey: publicKey,
+ executionEnabled: true,
+ controlApiBaseUrl: 'http://control-api.invalid/api/v1',
+ controlApiServiceName: 'codex-llm-proxy',
+ controlApiServiceToken: 'unused',
+ }
+}
+
+const platformToken = sign(
+ {
+ sub: `default/${HOST_REF}`,
+ hostRefs: [HOST_REF],
+ workflowControlScopes: ['llm:codex:execute'],
+ scope: 'workflow:approval:request',
+ },
+ 'workflow-approvals'
+)
+
+/** A runtime envelope whose request carries one user message of `contentChars`. */
+function completionBody(contentChars: number, attempt: string): string {
+ const raw = {
+ schemaVersion: 'codex-completion-request.v1',
+ requestId: `req-${attempt}`,
+ idempotencyKey: `idem-${attempt}`,
+ provider: 'codex-subscription',
+ model: 'gpt-5.1',
+ messages: [{ role: 'user', content: 'x'.repeat(contentChars) }],
+ }
+ const parsed = parseCodexCompletionRequestV1(raw)
+ if (!parsed.ok) throw new Error(parsed.message)
+ const requestHash = hashCodexCompletionRequestV1(parsed.value)
+ return JSON.stringify({
+ executionTicket: sign(
+ {
+ jti: randomUUID(),
+ typ: 'codex-execution-ticket',
+ hostRef: HOST_REF,
+ model: 'gpt-5.1',
+ requestHash,
+ providerAttemptId: `att-${attempt}`,
+ },
+ 'codex-llm-proxy'
+ ),
+ requestHash,
+ request: raw,
+ })
+}
+
+const adminPermit = sign(
+ { sub: 'admin-1', typ: 'codex-admin-permit', operation: 'catalog_list' },
+ 'codex-llm-proxy-admin'
+)
+
+/**
+ * A proxy whose `redeem` holds every attempt until `releaseAll`, so an attempt
+ * that reached it keeps its parsed body in memory. `hold` holds again.
+ */
+async function heldProxy(maxBodyBytes: number, deps: Pick<ProxyRuntimeDeps, 'bodyReadDeadlineMs'> = {}) {
+ let redeemed = 0
+ let holding = true
+ const held: Array<() => void> = []
+ const client = {
+ async redeem(): Promise<RedeemAttemptSuccess> {
+ redeemed += 1
+ if (holding) await new Promise<void>(resolve => held.push(resolve))
+ return {
+ accessToken: 'test-access-admission',
+ chatgptAccountId: 'acct-admission',
+ transport: {
+ protocolVersion: 'codex-subscription-transport.v1',
+ completionsOrigin: CODEX_COMPLETIONS_ORIGIN,
+ catalogOrigin: CODEX_CATALOG_ORIGIN,
+ operation: 'completion_stream',
+ servedModel: 'gpt-5.1',
+ maxStreamDurationMs: 60_000,
+ },
+ expiryClass: 'short_lived',
+ attemptReceipt: 'b'.repeat(64),
+ }
+ },
+ async finalize(input: {
+ receipt: { providerAttemptId: string; outcome: FinalizeAttemptSuccess['outcome'] }
+ }): Promise<FinalizeAttemptSuccess> {
+ return {
+ providerAttemptId: input.receipt.providerAttemptId,
+ outcome: input.receipt.outcome,
+ duplicate: false,
+ }
+ },
+ } as unknown as ControlApiClient
+ const fetchFn = (async () =>
+ new Response('data: {"type":"response.completed","response":{"usage":{}}}\n\n', {
+ status: 200,
+ headers: { 'content-type': 'text/event-stream' },
+ })) as typeof fetch
+
+ const servers = createProxyApps(config(maxBodyBytes), {
+ controlApiClient: client,
+ fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ ...deps,
+ })
+ await new Promise<void>(resolve => servers.runtime.listen(0, '127.0.0.1', () => resolve()))
+ await new Promise<void>(resolve => servers.admin.listen(0, '127.0.0.1', () => resolve()))
+ const { port } = servers.runtime.address() as AddressInfo
+ const { port: adminPort } = servers.admin.address() as AddressInfo
+ let arrived = 0
+ servers.runtime.on('request', () => {
+ arrived += 1
+ })
+ return {
+ port,
+ adminPort,
+ redeemed: () => redeemed,
+ /** Requests whose headers the runtime listener has received. */
+ arrived: () => arrived,
+ hold: () => {
+ holding = true
+ },
+ releaseAll: () => {
+ holding = false
+ for (const resolve of held.splice(0)) resolve()
+ },
+ close: () => servers.close(),
+ }
+}
+
+type Reply = { status: number; body: string }
+
+/** POST with a declared Content-Length, or with a chunked body of undeclared length. */
+function post(port: number, payload: string, options: { chunked?: boolean } = {}): Promise<Reply> {
+ return new Promise((resolve, reject) => {
+ const req = httpRequest(
+ {
+ host: '127.0.0.1',
+ port,
+ method: 'POST',
+ path: COMPLETIONS_PATH,
+ agent: false,
+ headers: {
+ 'content-type': 'application/json',
+ authorization: `Bearer ${platformToken}`,
+ ...(options.chunked
+ ? { 'transfer-encoding': 'chunked' }
+ : { 'content-length': Buffer.byteLength(payload) }),
+ },
+ },
+ res => {
+ const chunks: Buffer[] = []
+ res.on('data', chunk => chunks.push(chunk as Buffer))
+ res.on('end', () =>
+ resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString('utf8') })
+ )
+ res.on('error', reject)
+ }
+ )
+ req.on('error', reject)
+ req.end(payload)
+ })
+}
+
+/** Waits until `read()` is positive and stops changing for one poll interval. */
+async function settle(read: () => number): Promise<number> {
+ const deadline = Date.now() + 20_000
+ let last = -1
+ while (Date.now() < deadline) {
+ await new Promise(resolve => setTimeout(resolve, 300))
+ const now = read()
+ if (now > 0 && now === last) return now
+ last = now
+ }
+ throw new Error(`count did not settle within 20s (last ${last})`)
+}
+
+function sleep(ms: number): Promise<void> {
+ return new Promise(resolve => realSetTimeout(resolve, ms))
+}
+
+type TimedReply = Reply & { elapsedMs: number }
+
+/** The reply, or status 0 when none arrived within `ms` of real time. */
+function within(reply: Promise<TimedReply>, ms: number): Promise<TimedReply> {
+ return Promise.race([
+ reply,
+ sleep(ms).then(() => ({ status: 0, body: `no reply within ${ms} ms`, elapsedMs: ms })),
+ ])
+}
+
+/**
+ * A POST that declares `declaredBytes`, sends only `sent` and then stalls.
+ * `finish` sends the rest of the body; `destroy` closes the client socket so
+ * the listener can shut down.
+ */
+function stalledPost(
+ port: number,
+ options: { declaredBytes: number; token?: string; path?: string; sent?: string }
+): { reply: Promise<TimedReply>; finish: (rest: string) => void; destroy: () => void } {
+ const started = Date.now()
+ const req = httpRequest({
+ host: '127.0.0.1',
+ port,
+ method: 'POST',
+ path: options.path ?? COMPLETIONS_PATH,
+ agent: false,
+ headers: {
+ 'content-type': 'application/json',
+ 'content-length': options.declaredBytes,
+ ...(options.token ? { authorization: `Bearer ${options.token}` } : {}),
+ },
+ })
+ const reply = new Promise<TimedReply>((resolve, reject) => {
+ req.on('response', res => {
+ const chunks: Buffer[] = []
+ res.on('data', chunk => chunks.push(chunk as Buffer))
+ res.on('end', () =>
+ resolve({
+ status: res.statusCode ?? 0,
+ body: Buffer.concat(chunks).toString('utf8'),
+ elapsedMs: Date.now() - started,
+ })
+ )
+ res.on('error', reject)
+ })
+ req.on('error', reject)
+ })
+ req.write(options.sent ?? '{')
+ return { reply, finish: rest => req.end(rest), destroy: () => req.destroy() }
+}
+
+describe('codex-llm-proxy body admission (#731 R3-2)', () => {
+ it('T-R3-2a holds at most three near-cap bodies at once and admits the rest as they finish', async () => {
+ const proxy = await heldProxy(DEFAULT_MAX_BODY_BYTES)
+ try {
+ const contentChars = Math.floor(LIMITS.maxRequestBodyBytes * 0.9)
+ const payloads = Array.from({ length: 6 }, (_, i) => completionBody(contentChars, `cap-${i}`))
+ const payloadBytes = Buffer.byteLength(payloads[0]!)
+ // Fixture check: three bodies fit the budget and a fourth does not.
+ expect(BUDGET_BODIES * payloadBytes).toBeLessThanOrEqual(
+ BUDGET_BODIES * DEFAULT_MAX_BODY_BYTES
+ )
+ expect((BUDGET_BODIES + 1) * payloadBytes).toBeGreaterThan(
+ BUDGET_BODIES * DEFAULT_MAX_BODY_BYTES
+ )
+
+ const replies = payloads.map(payload => post(proxy.port, payload))
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+
+ // Liveness: the three that waited are admitted once the first finish.
+ proxy.releaseAll()
+ const settled = await Promise.all(replies)
+ expect(settled.map(reply => reply.status)).toEqual([200, 200, 200, 200, 200, 200])
+ for (const reply of settled) expect(reply.body).toContain('"outcome":"success"')
+ expect(proxy.redeemed()).toBe(6)
+ } finally {
+ proxy.releaseAll()
+ await proxy.close()
+ }
+ }, 60_000)
+
+ it('T-R3-2b refuses a body of undeclared length with 411 before reading it', async () => {
+ const proxy = await heldProxy(DEFAULT_MAX_BODY_BYTES)
+ proxy.releaseAll()
+ try {
+ const refused = await post(proxy.port, completionBody(64, 'chunked'), { chunked: true })
+ expect(refused.status).toBe(411)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'length_required' })
+ expect(proxy.redeemed()).toBe(0)
+
+ // Witness: the same request with a declared length is served.
+ const served = await post(proxy.port, completionBody(64, 'declared'))
+ expect(served.status).toBe(200)
+ expect(proxy.redeemed()).toBe(1)
+ } finally {
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-R3-2c answers provider_unavailable once the admission queue is full', async () => {
+ // A small body limit keeps this cheap; the budget scales with it.
+ const maxBodyBytes = 16 * 1024
+ const proxy = await heldProxy(maxBodyBytes)
+ try {
+ const payload = (attempt: string) => completionBody(12_000, attempt)
+ const payloadBytes = Buffer.byteLength(payload('probe'))
+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)
+ expect((BUDGET_BODIES + 1) * payloadBytes).toBeGreaterThan(BUDGET_BODIES * maxBodyBytes)
+
+ const waiting = Array.from({ length: BUDGET_BODIES + STREAM_LIMITS.maxQueuedRequests }, (_, i) =>
+ post(proxy.port, payload(`queued-${i}`))
+ )
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+
+ const overflow = await post(proxy.port, payload('overflow'))
+ expect(overflow.status).toBe(503)
+ expect(JSON.parse(overflow.body)).toEqual({ error: 'provider_unavailable' })
+
+ // Liveness: every queued request is still served once the budget frees.
+ proxy.releaseAll()
+ const settled = await Promise.all(waiting)
+ expect(settled.every(reply => reply.status === 200)).toBe(true)
+ expect(proxy.redeemed()).toBe(BUDGET_BODIES + STREAM_LIMITS.maxQueuedRequests)
+ } finally {
+ proxy.releaseAll()
+ await proxy.close()
+ }
+ }, 60_000)
+
+ it('T-R9-2 drops a queued body whose client left, so the whole budget is free once the holders finish', async () => {
+ // A small body limit keeps this cheap; the budget scales with it. The read
+ // deadline is the production one (10 s), and every check below completes
+ // well inside it, so the deadline cannot reclaim a leaked reservation first.
+ const maxBodyBytes = 16 * 1024
+ const fillingBody = (attempt: string) => completionBody(12_000, attempt)
+ const proxy = await heldProxy(maxBodyBytes)
+ const holders = Array.from({ length: BUDGET_BODIES }, (_, i) =>
+ post(proxy.port, fillingBody(`holder-${i}`))
+ )
+ const refill: Array<Promise<Reply>> = []
+ const body = fillingBody('abandoned')
+ let abandoned: ReturnType<typeof stalledPost> | undefined
+ try {
+ const payloadBytes = Buffer.byteLength(body)
+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)
+ expect((BUDGET_BODIES + 1) * payloadBytes).toBeGreaterThan(BUDGET_BODIES * maxBodyBytes)
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+
+ // The holders fill the budget, so this body waits in the admission queue.
+ abandoned = stalledPost(proxy.port, {
+ declaredBytes: payloadBytes,
+ token: platformToken,
+ sent: body.slice(0, 1),
+ })
+ while (proxy.arrived() < BUDGET_BODIES + 1) await sleep(10)
+ await sleep(50)
+ abandoned.destroy()
+ await expect(abandoned.reply).rejects.toThrow('socket hang up')
+ await sleep(50)
+
+ proxy.releaseAll()
+ for (const reply of await Promise.all(holders)) expect(reply.status).toBe(200)
+
+ // Every reservation is back: bodies that fill the whole budget are all
+ // read and redeemed at once. A reservation granted to the departed
+ // waiter would leave the last of them queued until the read deadline.
+ proxy.hold()
+ for (let i = 0; i < BUDGET_BODIES; i += 1) refill.push(post(proxy.port, fillingBody(`refill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(2 * BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(refill)) {
+ expect(reply.status).toBe(200)
+ expect(reply.body).toContain('"outcome":"success"')
+ }
+ } finally {
+ abandoned?.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled([...holders, ...refill])
+ await proxy.close()
+ }
+ }, 30_000)
+})
+
+/**
+ * R9-M-B — a stalled body must not hold the budget. The platform JWT is
+ * checked before a reservation is taken, and a reserved body that is not read
+ * within the read deadline is answered 408 and releases its reservation.
+ */
+describe('codex-llm-proxy authentication before admission and body-read deadline (R9-M-B)', () => {
+ // A small body limit keeps these cheap; the budget scales with it.
+ const maxBodyBytes = 16 * 1024
+ const fillingBody = (attempt: string) => completionBody(12_000, attempt)
+
+ it('T-MB-0 fixes the production body-read deadline at 10 000 ms', () => {
+ expect(BODY_READ_DEADLINE_MS).toBe(10_000)
+ })
+
+ it('T-MB-1 answers anonymous stalled bodies 401 within 1 s without reserving the budget', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>
+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes })
+ )
+ const filling: Array<Promise<Reply>> = []
+ try {
+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {
+ expect(reply.status).toBe(401)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })
+ }
+ // Zero bytes reserved: authenticated bodies that fill the whole budget
+ // are all read and redeemed at once.
+ const payloadBytes = Buffer.byteLength(fillingBody('probe'))
+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)
+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)
+ } finally {
+ for (const s of anonymous) s.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled(filling)
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-1-admin answers anonymous stalled admin bodies 401 within 1 s without reserving the shared budget', async () => {
+ const deadlineMs = 1_000
+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })
+ const modelsPath = '/internal/admin/v1/codex/models'
+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>
+ stalledPost(proxy.adminPort, { declaredBytes: maxBodyBytes, path: modelsPath })
+ )
+ const permitted = stalledPost(proxy.adminPort, {
+ declaredBytes: maxBodyBytes,
+ path: modelsPath,
+ token: adminPermit,
+ })
+ const filling: Array<Promise<Reply>> = []
+ try {
+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {
+ expect(reply.status).toBe(401)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })
+ }
+ // Witness: a valid admin permit passes the gate and is then bounded by
+ // the read deadline, not refused by the gate.
+ const held = await within(permitted.reply, deadlineMs + 1_000)
+ expect(held.status).toBe(408)
+ expect(JSON.parse(held.body)).toEqual({ error: 'request_timeout' })
+ // The budget is shared with the runtime listener, and all of it is free.
+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)
+ } finally {
+ for (const s of [...anonymous, permitted]) s.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled(filling)
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-2 answers an authenticated stalled body 408 request_timeout at the deadline and frees its reservation', async () => {
+ const deadlineMs = 1_000
+ const warn = vi.spyOn(logger, 'warn')
+ const error = vi.spyOn(logger, 'error')
+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })
+ proxy.releaseAll()
+ const stalled = Array.from({ length: BUDGET_BODIES }, () =>
+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })
+ )
+ const refill: Array<Promise<Reply>> = []
+ try {
+ while (proxy.arrived() < BUDGET_BODIES) await sleep(10)
+ await sleep(50)
+ // The stalled bodies hold the whole budget, so this one waits in the queue.
+ const queued = post(proxy.port, fillingBody('queued'))
+
+ for (const reply of await Promise.all(stalled.map(s => within(s.reply, deadlineMs + 1_000)))) {
+ expect(reply.status).toBe(408)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })
+ expect(reply.elapsedMs).toBeGreaterThanOrEqual(deadlineMs)
+ expect(reply.elapsedMs).toBeLessThan(deadlineMs + 1_000)
+ }
+ // Witness: the queued request is admitted once the reservations are freed.
+ const served = await queued
+ expect(served.status).toBe(200)
+ expect(served.body).toContain('"outcome":"success"')
+ expect(proxy.redeemed()).toBe(1)
+ expect(warn).toHaveBeenCalledWith(
+ { event: 'codex_proxy_denied', code: 'request_timeout' },
+ 'request denied'
+ )
+ expect(error).not.toHaveBeenCalledWith(
+ expect.objectContaining({ event: 'codex_proxy_error' }),
+ expect.anything()
+ )
+
+ // The budget is back to zero: bodies that fill all of it are admitted at once.
+ proxy.hold()
+ for (let i = 0; i < BUDGET_BODIES; i += 1) refill.push(post(proxy.port, fillingBody(`refill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(1 + BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(refill)) expect(reply.status).toBe(200)
+ } finally {
+ for (const s of stalled) s.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled(refill)
+ await proxy.close()
+ warn.mockRestore()
+ error.mockRestore()
+ }
+ }, 30_000)
+
+ it('T-MB-2d arms BODY_READ_DEADLINE_MS when no deadline is injected', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
+ const stalled = stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })
+ try {
+ while (proxy.arrived() < 1) await sleep(10)
+ await sleep(50)
+ vi.advanceTimersByTime(BODY_READ_DEADLINE_MS - 1)
+ expect((await within(stalled.reply, 200)).status).toBe(0)
+ vi.advanceTimersByTime(1)
+ const reply = await within(stalled.reply, 2_000)
+ expect(reply.status).toBe(408)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })
+ } finally {
+ vi.useRealTimers()
+ stalled.destroy()
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-3 starts the deadline at grant, so a body queued past it is still served', async () => {
+ const deadlineMs = 500
+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })
+ const holders = Array.from({ length: BUDGET_BODIES }, (_, i) =>
+ post(proxy.port, fillingBody(`holder-${i}`))
+ )
+ // The queued body sends one byte and holds the rest until half a deadline
+ // after the budget frees, so it can only be served if the clock started at
+ // grant. A fully buffered body would be parsed before any timer fired.
+ const body = fillingBody('queued')
+ let queued: ReturnType<typeof stalledPost> | undefined
+ try {
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+ queued = stalledPost(proxy.port, {
+ declaredBytes: Buffer.byteLength(body),
+ token: platformToken,
+ sent: body.slice(0, 1),
+ })
+ await sleep(3 * deadlineMs)
+ // Witness: the body really waited in the queue for longer than the deadline.
+ expect(proxy.redeemed()).toBe(BUDGET_BODIES)
+ proxy.releaseAll()
+ await sleep(deadlineMs / 2)
+ queued.finish(body.slice(1))
+ const served = await within(queued.reply, 5_000)
+ expect(served.status).toBe(200)
+ expect(served.body).toContain('"outcome":"success"')
+ expect(proxy.redeemed()).toBe(BUDGET_BODIES + 1)
+ for (const reply of await Promise.all(holders)) expect(reply.status).toBe(200)
+ } finally {
+ proxy.releaseAll()
+ queued?.destroy()
+ await Promise.allSettled(holders)
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-4 answers an unauthenticated oversize body 401 without parsing it', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ proxy.releaseAll()
+ // Over the visual ceiling as well, so no parser could accept it.
+ const declaredBytes = LIMITS.maxVisualRequestBodyBytes + 1
+ const anonymous = stalledPost(proxy.port, { declaredBytes })
+ const authenticated = stalledPost(proxy.port, { declaredBytes, token: platformToken })
+ try {
+ const refused = await within(anonymous.reply, 1_000)
+ expect(refused.status).toBe(401)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'Unauthorized' })
+ // Witness: the same body with a platform JWT reaches the size check.
+ const oversize = await within(authenticated.reply, 1_000)
+ expect(oversize.status).toBe(413)
+ expect(JSON.parse(oversize.body)).toEqual({ error: 'payload_too_large' })
+ expect(proxy.redeemed()).toBe(0)
+ } finally {
+ anonymous.destroy()
+ authenticated.destroy()
+ await proxy.close()
+ }
+ }, 30_000)
+})
+
+/**
+ * POST `body` with the given `Content-Encoding`. Keep-alive, so a reply sent
+ * before the body is read is not raced by the client's own write.
+ */
+function postEncoded(
+ port: number,
+ options: { path: string; token: string; body: Buffer; encoding: string }
+): Promise<Reply> {
+ return new Promise((resolve, reject) => {
+ const req = httpRequest(
+ {
+ host: '127.0.0.1',
+ port,
+ method: 'POST',
+ path: options.path,
+ agent: false,
+ headers: {
+ connection: 'keep-alive',
+ 'content-type': 'application/json',
+ 'content-encoding': options.encoding,
+ 'content-length': options.body.length,
+ authorization: `Bearer ${options.token}`,
+ },
+ },
+ res => {
+ const chunks: Buffer[] = []
+ res.on('data', chunk => chunks.push(chunk as Buffer))
+ res.on('end', () => {
+ req.destroy()
+ resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString('utf8') })
+ })
+ res.on('error', reject)
+ }
+ )
+ req.on('error', reject)
+ req.end(options.body)
+ })
+}
+
+/**
+ * R9-1 — the byte budget reserves the declared wire length, so a body the
+ * parser inflated could hold many times its reservation. Every parser refuses
+ * an encoded body before inflating it.
+ */
+describe('codex-llm-proxy encoded bodies (R9-1)', () => {
+ // A small body limit keeps these cheap; the budget scales with it.
+ const maxBodyBytes = 16 * 1024
+
+ it('T-R9-1a refuses a gzip completion body that decodes past its reservation with 415', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ proxy.releaseAll()
+ try {
+ const plain = completionBody(12_000, 'gzip')
+ const gzipped = gzipSync(plain)
+ // Fixture check: the wire length reserved is a small fraction of what
+ // the parser would hold after inflating it, and inflated it fits the cap.
+ expect(10 * gzipped.length).toBeLessThan(Buffer.byteLength(plain))
+ expect(Buffer.byteLength(plain)).toBeLessThanOrEqual(maxBodyBytes)
+ const refused = await postEncoded(proxy.port, {
+ path: COMPLETIONS_PATH,
+ token: platformToken,
+ body: gzipped,
+ encoding: 'gzip',
+ })
+ expect(refused.status).toBe(415)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'unsupported_media_type' })
+ expect(proxy.redeemed()).toBe(0)
+ // Witness: the same request unencoded is read, redeemed and served.
+ const served = await post(proxy.port, completionBody(12_000, 'plain'))
+ expect(served.status).toBe(200)
+ expect(proxy.redeemed()).toBe(1)
+ } finally {
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-R9-1b refuses a gzip body on the visual parser with 415', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ proxy.releaseAll()
+ try {
+ // Random base64 barely compresses, so the wire length stays above the
+ // ordinary cap and selects the visual parser.
+ const gzipped = gzipSync(JSON.stringify({ pad: randomBytes(32 * 1024).toString('base64') }))
+ expect(gzipped.length).toBeGreaterThan(maxBodyBytes)
+ const refused = await postEncoded(proxy.port, {
+ path: COMPLETIONS_PATH,
+ token: platformToken,
+ body: gzipped,
+ encoding: 'gzip',
+ })
+ expect(refused.status).toBe(415)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'unsupported_media_type' })
+ expect(proxy.redeemed()).toBe(0)
+ } finally {
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-R9-1c refuses a gzip admin body with 415', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ try {
+ const gzipped = gzipSync(JSON.stringify({ accessToken: 'x'.repeat(8_000) }))
+ expect(10 * gzipped.length).toBeLessThan(8_000)
+ const refused = await postEncoded(proxy.adminPort, {
+ path: '/internal/admin/v1/codex/models',
+ token: adminPermit,
+ body: gzipped,
+ encoding: 'gzip',
+ })
+ expect(refused.status).toBe(415)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'unsupported_media_type' })
+ } finally {
+ await proxy.close()
+ }
+ }, 30_000)
+})
diff --cc codex-llm-proxy/test/codexTransport.conformance.test.ts
index 3a47159bb,d2d73a88d..ff4b448af
--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts
+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts
@@@ -476,75 -477,6 +483,76 @@@ describe('streamCodexCompletion', () =
expect(emitted).toEqual([])
}
)
+
+ // #731 — an upstream context-window refusal is a property of this request.
+ // The transport keeps the upstream code so the Host does not retry it as an
+ // outage. Each event carries the code in a different place; either is enough.
+ function failingStream(frames: string[], attempt: string) {
+ const finalize = vi.fn(async () => ({
+ providerAttemptId: attempt,
+ outcome: 'error' as const,
+ duplicate: false,
+ }))
+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(frames))
+ const pending = streamCodexCompletion({
+ executionTicket: `ticket-${attempt}`,
+ requestHash: REQUEST_HASH,
+ request: REQUEST,
+ ticket: {
+ jti: `jti-${attempt}`,
+ hostRef: 'research-host',
+ model: REQUEST.model,
+ requestHash: REQUEST_HASH,
+ providerAttemptId: attempt,
+ },
++ maxDeadlineMs: 300_000,
+ redeem: vi.fn(async () => redeemSuccess()),
+ finalize,
+ fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ })
+ return { pending, finalize, fetchFn }
+ }
+
+ it.each([
+ { events: 'the transcribed error + response.failed pair', frames: UPSTREAM_CONTEXT_OVERFLOW_FRAMES },
+ { events: 'the error event alone', frames: [sseFrame(UPSTREAM_CONTEXT_ERROR_EVENT)] },
+ { events: 'the response.failed event alone', frames: [sseFrame(UPSTREAM_CONTEXT_FAILED_EVENT)] },
+ ])('T-R7-2a maps $events to context_length_exceeded', async ({ frames }) => {
+ const { pending, finalize, fetchFn } = failingStream(frames, 'att-context')
+ await expect(pending).rejects.toMatchObject({
+ name: 'CodexTransportError',
+ code: 'context_length_exceeded',
+ })
+ // Witness: the upstream stream was fetched and the attempt finalized as an error.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(finalize).toHaveBeenCalledWith(
+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
+ )
+ })
+
+ // Controls: only `context_length_exceeded` is forwarded. A failure without a
+ // code, or with any other upstream code, stays a provider outage, so an
+ // upstream string never becomes a Host-visible code by itself.
+ it.each([
+ { events: 'response.failed without an error code', event: { type: 'response.failed' } },
+ {
+ events: 'response.failed with another upstream code',
+ event: { type: 'response.failed', response: { error: { code: 'server_error' } } },
+ },
+ {
+ events: 'an error event with another upstream code',
+ event: { type: 'error', error: { code: 'rate_limit_exceeded' } },
+ },
+ ])('T-R7-2b keeps $events as provider_unavailable', async ({ event }) => {
+ const { pending, fetchFn } = failingStream([sseFrame(event)], 'att-other')
+ await expect(pending).rejects.toMatchObject({
+ name: 'CodexTransportError',
+ code: 'provider_unavailable',
+ message: 'upstream response failed',
+ })
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ })
it.each([256, 257])(
'validates the complete %s-call response before emitting executable calls',
async count => {
@@@ -1397,100 -1422,6 +1498,101 @@@
expect(finalize.mock.calls[0]?.[0]?.receipt?.usage).toBeUndefined()
})
+ // R9-6 (L-9): a context-window refusal can also arrive as a plain HTTP error
+ // before any stream starts. The recorded trace carries the SSE events; this
+ // HTTP body wraps the same error object in `{ error }` (not recorded).
+ describe('non-success upstream bodies (R9-6)', () => {
+ const contextErrorBody = JSON.stringify({ error: UPSTREAM_CONTEXT_ERROR_EVENT.error })
+
+ function streamWith(response: () => Response) {
+ const finalize = vi.fn(
+ async (_input: Parameters<StreamCodexCompletionInput['finalize']>[0]) => ({
+ providerAttemptId: 'att-1',
+ outcome: 'error' as const,
+ duplicate: false,
+ })
+ )
+ const pending = 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',
+ },
++ maxDeadlineMs: 300_000,
+ redeem: async () => redeemSuccess(),
+ finalize,
+ fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => response()),
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ })
+ return { pending, finalize }
+ }
+
+ it('T-R9-6a maps an HTTP 400 whose error.code is context_length_exceeded to context_length_exceeded', async () => {
+ const { pending, finalize } = streamWith(
+ () =>
+ new Response(contextErrorBody, {
+ status: 400,
+ headers: { 'content-type': 'application/json' },
+ })
+ )
+ await expect(pending).rejects.toMatchObject({ code: 'context_length_exceeded' })
+ expect(finalize).toHaveBeenCalledWith(
+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
+ )
+ })
+
+ it('T-R9-6b maps the same body on a 5xx to context_length_exceeded', async () => {
+ const { pending } = streamWith(() => new Response(contextErrorBody, { status: 500 }))
+ await expect(pending).rejects.toMatchObject({ code: 'context_length_exceeded' })
+ })
+
+ it('T-R9-6c keeps the status mapping for every other error body', async () => {
+ const other = JSON.stringify({ error: { code: 'rate_limit_exceeded' } })
+ await expect(
+ streamWith(() => new Response(other, { status: 400 })).pending
+ ).rejects.toMatchObject({ code: 'invalid_request' })
+ await expect(
+ streamWith(() => new Response(other, { status: 429 })).pending
+ ).rejects.toMatchObject({ code: 'provider_unavailable' })
+ await expect(
+ streamWith(() => new Response(contextErrorBody, { status: 401 })).pending
+ ).rejects.toMatchObject({ code: 'connection_unavailable' })
+ await expect(
+ streamWith(() => new Response('{"error":', { status: 400 })).pending
+ ).rejects.toMatchObject({ code: 'invalid_request' })
+ })
+
+ it('T-R9-6d reads a bounded prefix of an endless error body and keeps the status mapping', async () => {
+ const chunk = new TextEncoder().encode(`{"pad":"${'x'.repeat(1024)}`)
+ let pulled = 0
+ let canceled = false
+ // highWaterMark 0: nothing is pulled until a reader asks for it.
+ const endless = new ReadableStream<Uint8Array>(
+ {
+ pull(controller) {
+ pulled += chunk.byteLength
+ controller.enqueue(chunk)
+ },
+ cancel() {
+ canceled = true
+ },
+ },
+ { highWaterMark: 0 }
+ )
+ const { pending } = streamWith(() => new Response(endless, { status: 400 }))
+ await expect(pending).rejects.toMatchObject({ code: 'invalid_request' })
+ // Witness: the body was read, and the read stopped at a bound.
+ expect(pulled).toBeGreaterThan(0)
+ expect(canceled).toBe(true)
+ expect(pulled).toBeLessThan(1024 * 1024)
+ })
+ })
+
it('emits a tool call only after argument deltas complete', async () => {
const frames: unknown[] = []
const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
@@@ -1529,263 -1461,167 +1632,427 @@@
{ type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },
])
})
+
+ describe('malformed tool-call arguments fail closed', () => {
+ const sse = (event: Record<string, unknown>) => `data: ${JSON.stringify(event)}\n\n`
+ const textBefore = sse({ type: 'response.output_text.delta', delta: 'before' })
+ const completed = sse({ type: 'response.completed', response: { usage: {} } })
+ const openCall = sse({
+ type: 'response.output_item.added',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: '',
+ },
+ })
+ const truncatedDelta = sse({
+ type: 'response.function_call_arguments.delta',
+ item_id: 'item-1',
+ delta: '{"q":',
+ })
+
+ async function runUpstream(events: string[], options: { abortOnFrame?: boolean } = {}) {
+ const frames: Array<{ type: string }> = []
+ const abort = new AbortController()
+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(events))
+ const finalize = vi.fn(
+ async (input: Parameters<StreamCodexCompletionInput['finalize']>[0]) => ({
+ providerAttemptId: 'att-1',
+ outcome: input.receipt.outcome,
+ duplicate: false,
+ })
+ )
+ const settled = 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',
+ },
+ signal: abort.signal,
++ maxDeadlineMs: 300_000,
+ redeem: async () => redeemSuccess(),
+ finalize,
+ fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ onFrame: frame => {
+ frames.push(frame)
+ if (options.abortOnFrame) abort.abort()
+ },
+ }).then(
+ result => ({ rejected: false as const, result }),
+ (error: unknown) => ({ rejected: true as const, error })
+ )
+ return { settled, frames, fetchFn, finalize }
+ }
+
+ async function expectRefused(events: string[]) {
+ const { settled, frames, fetchFn, finalize } = await runUpstream(events)
+ // Liveness: the upstream was called and its stream was consumed far enough
+ // to deliver the text that precedes the call. Without these, "no tool_call
+ // frame" below would also hold for a transport that never ran.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(frames).toContainEqual({ type: 'text', text: 'before' })
+ // The defect: the malformed call became an executable call with `{}`.
+ expect(frames.filter(frame => frame.type === 'tool_call')).toEqual([])
+ expect(settled).toMatchObject({
+ rejected: true,
+ error: { name: 'CodexTransportError', code: 'invalid_tool_arguments' },
+ })
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('error')
+ }
+
+ it.each([
+ ['truncated JSON', '{"q":'],
+ ['a JSON value that is not an object', '[1,2]'],
+ ])(
+ 'refuses a function_call closed by response.output_item.done with %s as arguments',
+ async (_label, rawArguments) => {
+ await expectRefused([
+ textBefore,
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ completed,
+ ])
+ }
+ )
+
+ it('refuses a call whose argument deltas never complete before the stream ends', async () => {
+ await expectRefused([textBefore, openCall, truncatedDelta, completed])
+ })
+
+ // A closed call is the whole call, so empty `arguments` there mean "no
+ // parameters", not "truncated". An unclosed call gives no such guarantee.
+ async function expectAcceptedWithoutParameters(events: string[]) {
+ const { settled, frames, fetchFn, finalize } = await runUpstream(events)
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(settled).toMatchObject({ rejected: false, result: { outcome: 'success' } })
+ // Exactly one call, with no parameters: the positive frame is the witness.
+ expect(frames).toEqual([
+ { type: 'text', text: 'before' },
+ { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: {} },
+ ])
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('success')
+ }
+
+ it.each([
+ ['an empty string', ''],
+ ['a whitespace-only string', ' \n '],
+ ])(
+ 'accepts a function_call closed by response.output_item.done with %s as arguments as a call with no parameters',
+ async (_label, rawArguments) => {
+ await expectAcceptedWithoutParameters([
+ textBefore,
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ completed,
+ ])
+ }
+ )
+
+ it('accepts a call opened with empty arguments and closed by response.function_call_arguments.done with none', async () => {
+ await expectAcceptedWithoutParameters([
+ textBefore,
+ openCall,
+ sse({ type: 'response.function_call_arguments.done', item_id: 'item-1', arguments: '' }),
+ completed,
+ ])
+ })
+
+ it.each([
+ ['an empty string', ''],
+ ['a whitespace-only string', ' \n '],
+ ])(
+ 'refuses a call whose truncated deltas are closed by a done event with %s as arguments',
+ async (_label, rawArguments) => {
+ await expectRefused([
+ textBefore,
+ openCall,
+ truncatedDelta,
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ completed,
+ ])
+ }
+ )
+
+ // R9-9 (L-12): the positive twin of the case above. A blank close carries
+ // nothing new, so complete deltas survive it and the call runs with them.
+ const closeWith: Record<string, (rawArguments: string) => string> = {
+ 'response.output_item.done': rawArguments =>
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ 'response.function_call_arguments.done': rawArguments =>
+ sse({
+ type: 'response.function_call_arguments.done',
+ item_id: 'item-1',
+ arguments: rawArguments,
+ }),
+ }
+ it.each([
+ ['response.output_item.done', 'an empty string', ''],
+ ['response.output_item.done', 'a whitespace-only string', ' \n '],
+ ['response.function_call_arguments.done', 'an empty string', ''],
+ ['response.function_call_arguments.done', 'a whitespace-only string', ' \n '],
+ ])(
+ 'T-R9-9a keeps complete argument deltas when %s closes the call with %s',
+ async (event, _label, rawArguments) => {
+ const { settled, frames, fetchFn, finalize } = await runUpstream([
+ textBefore,
+ openCall,
+ truncatedDelta,
+ sse({ type: 'response.function_call_arguments.delta', item_id: 'item-1', delta: '"x"}' }),
+ closeWith[event]!(rawArguments),
+ completed,
+ ])
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(settled).toMatchObject({ rejected: false, result: { outcome: 'success' } })
+ expect(frames).toEqual([
+ { type: 'text', text: 'before' },
+ { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },
+ ])
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('success')
+ }
+ )
+
+ it('refuses a call opened with empty arguments and never closed before the stream ends', async () => {
+ await expectRefused([textBefore, openCall, completed])
+ })
+
+ // A canceled or failed stream also ends with its open call truncated. That
+ // truncation is a consequence of the stream's own outcome, so the outcome
+ // wins over the arguments refusal.
+ it('keeps a client cancel that lands mid-arguments as canceled', async () => {
+ const { settled, frames, fetchFn, finalize } = await runUpstream(
+ [openCall, truncatedDelta, textBefore],
+ { abortOnFrame: true }
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(frames).toEqual([{ type: 'text', text: 'before' }])
+ expect(settled).toMatchObject({ rejected: false, result: { outcome: 'canceled' } })
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('canceled')
+ })
+
+ it('reports an upstream failure that lands mid-arguments as provider_unavailable', async () => {
+ const { settled, frames, fetchFn, finalize } = await runUpstream([
+ textBefore,
+ openCall,
+ truncatedDelta,
+ sse({ type: 'response.failed', response: {} }),
+ ])
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(frames).toEqual([{ type: 'text', text: 'before' }])
+ expect(settled).toMatchObject({
+ rejected: true,
+ error: { name: 'CodexTransportError', code: 'provider_unavailable' },
+ })
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('error')
+ })
+ })
})
+
+ const TEXT_DELTA = 'data: {"type":"response.output_text.delta","delta":"x"}\n\n'
+ const COMPLETED = 'data: {"type":"response.completed","response":{"usage":{}}}\n\n'
+
+ /**
+ * An upstream body that ignores `init.signal`, like a socket that stays open
+ * while sending nothing. Only a transport that races each read against its own
+ * timers can end an attempt reading from it.
+ */
+ function upstreamBody(chunks: Array<{ afterMs: number; text: string } | 'stall'>): {
+ fetchFn: typeof fetch
+ cancel: ReturnType<typeof vi.fn>
+ } {
+ const encoder = new TextEncoder()
+ const cancel = vi.fn()
+ const fetchFn = vi.fn(async () => {
+ let index = 0
+ const body = new ReadableStream<Uint8Array>(
+ {
+ async pull(controller) {
+ const next = chunks[index]
+ index += 1
+ if (next === undefined) {
+ controller.close()
+ return
+ }
+ if (next === 'stall') {
+ await new Promise<never>(() => undefined)
+ return
+ }
+ await new Promise(resolve => setTimeout(resolve, next.afterMs))
+ controller.enqueue(encoder.encode(next.text))
+ },
+ cancel,
+ },
+ { highWaterMark: 0 }
+ )
+ return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream' } })
+ }) as unknown as typeof fetch
+ return { fetchFn, cancel }
+ }
+
+ function attemptInput(
+ overrides: Partial<StreamCodexCompletionInput>
+ ): StreamCodexCompletionInput & { finalize: ReturnType<typeof vi.fn> } {
+ const finalize = vi.fn(async (_input: Parameters<StreamCodexCompletionInput['finalize']>[0]) => ({
+ providerAttemptId: 'att-timeouts',
+ outcome: 'error' as const,
+ duplicate: false,
+ }))
+ return {
+ executionTicket: 'ticket-timeouts',
+ requestHash: REQUEST_HASH,
+ request: REQUEST,
+ ticket: {
+ jti: 'jti-timeouts',
+ hostRef: 'research-host',
+ model: REQUEST.model,
+ requestHash: REQUEST_HASH,
+ providerAttemptId: 'att-timeouts',
+ },
+ maxDeadlineMs: 300_000,
+ redeem: async () => redeemSuccess(),
+ finalize,
+ fetchFn: upstreamBody([]).fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ ...overrides,
+ } as StreamCodexCompletionInput & { finalize: ReturnType<typeof vi.fn> }
+ }
+
+ function finalizedOutcome(finalize: ReturnType<typeof vi.fn>): unknown {
+ expect(finalize).toHaveBeenCalledTimes(1)
+ return (finalize.mock.calls[0]?.[0] as { receipt: { outcome: string } }).receipt.outcome
+ }
+
+ describe('streamCodexCompletion upstream timeouts', () => {
+ it('ends a stalled upstream stream at the idle timeout with a typed provider_unavailable', async () => {
+ const upstream = upstreamBody([{ afterMs: 0, text: TEXT_DELTA }, 'stall'])
+ const input = attemptInput({ fetchFn: upstream.fetchFn, upstreamIdleTimeoutMs: 50 })
+ const frames: unknown[] = []
+ const error = await streamCodexCompletion({ ...input, onFrame: frame => void frames.push(frame) }).then(
+ () => undefined,
+ (err: unknown) => err
+ )
+ expect(frames).toEqual([{ type: 'text', text: 'x' }])
+ expect(error).toBeInstanceOf(CodexTransportError)
+ expect(error).toMatchObject({
+ code: 'provider_unavailable',
+ message: 'upstream stream idle timeout',
+ details: { idleTimeoutMs: 50 },
+ })
+ expect(finalizedOutcome(input.finalize)).toBe('error')
+ expect(upstream.cancel).toHaveBeenCalledTimes(1)
+ }, 2_000)
+
+ it('keeps the attempt alive while upstream chunks arrive inside the idle window', async () => {
+ const chunks = Array.from({ length: 6 }, () => ({ afterMs: 30, text: TEXT_DELTA }))
+ const upstream = upstreamBody([...chunks, { afterMs: 30, text: COMPLETED }])
+ const input = attemptInput({ fetchFn: upstream.fetchFn, upstreamIdleTimeoutMs: 50 })
+ const result = await streamCodexCompletion(input)
+ expect(result.outcome).toBe('success')
+ expect(finalizedOutcome(input.finalize)).toBe('success')
+ }, 2_000)
+
+ it('ends a still-active upstream stream at the total cap with stream_duration_exceeded', async () => {
+ const endless = Array.from({ length: 200 }, () => ({ afterMs: 10, text: TEXT_DELTA }))
+ const upstream = upstreamBody(endless)
+ const input = attemptInput({
+ fetchFn: upstream.fetchFn,
+ maxDeadlineMs: 150,
+ upstreamIdleTimeoutMs: 1_000,
+ })
+ const error = await streamCodexCompletion(input).then(
+ () => undefined,
+ (err: unknown) => err
+ )
+ expect(error).toBeInstanceOf(CodexTransportError)
+ expect(error).toMatchObject({
+ code: 'stream_duration_exceeded',
+ message: 'upstream stream exceeded maxStreamDurationMs',
+ details: { limitMs: 150 },
+ })
+ expect(finalizedOutcome(input.finalize)).toBe('error')
+ }, 2_000)
+
+ it('reports a client abort during a stalled stream as canceled, not as a timeout', async () => {
+ const upstream = upstreamBody([{ afterMs: 0, text: TEXT_DELTA }, 'stall'])
+ const abort = new AbortController()
+ const input = attemptInput({
+ fetchFn: upstream.fetchFn,
+ signal: abort.signal,
+ maxDeadlineMs: 1_000,
+ upstreamIdleTimeoutMs: 1_000,
+ })
+ setTimeout(() => abort.abort(), 50)
+ const result = await streamCodexCompletion(input)
+ expect(result.outcome).toBe('canceled')
+ expect(finalizedOutcome(input.finalize)).toBe('canceled')
+ expect(upstream.cancel).toHaveBeenCalledTimes(1)
+ }, 2_000)
+ })
+
+ describe('streamCodexCompletion deadline validation', () => {
+ // The redeem consumes the single-use ticket, so a deadline that cannot be
+ // served must be refused before it, while there is no receipt to finalize.
+ it('rejects an invalid deadline before redeeming the ticket', async () => {
+ const redeem = vi.fn(async () => redeemSuccess())
+ const fetchFn = vi.fn()
+ const input = attemptInput({
+ deadlineMs: 0,
+ redeem,
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ // Witness: the rejection names the deadline check, so the path ran.
+ await expect(streamCodexCompletion(input)).rejects.toMatchObject({
+ name: 'RequestLimitError',
+ message: 'deadline is invalid',
+ })
+ expect(redeem).not.toHaveBeenCalled()
+ expect(input.finalize).not.toHaveBeenCalled()
+ expect(fetchFn).not.toHaveBeenCalled()
+ })
+ })
diff --cc codex-llm-proxy/test/server.security.test.ts
index 6e54c4c58,4c3d2005b..4244d31b8
--- a/codex-llm-proxy/test/server.security.test.ts
+++ b/codex-llm-proxy/test/server.security.test.ts
@@@ -19,9 -17,8 +19,14 @@@ import
} from '../src/controlApiClient.js'
import { logger } from '../src/logger.js'
import { CODEX_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'
- import { DEFAULT_MAX_BODY_BYTES, ENVELOPE_ALLOWANCE_BYTES } from '../src/requestLimits.js'
-import { RequestLimitError, streamGate } from '../src/requestLimits.js'
++import {
++ DEFAULT_MAX_BODY_BYTES,
++ ENVELOPE_ALLOWANCE_BYTES,
++ RequestLimitError,
++ streamGate,
++} from '../src/requestLimits.js'
import { createProxyApps } from '../src/server.js'
+import { UPSTREAM_CONTEXT_OVERFLOW_FRAMES } from './fixtures/upstreamContextOverflow.js'
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
modulusLength: 2048,
@@@ -756,15 -841,19 +924,20 @@@ describe('codex-llm-proxy attempt telem
calls: number,
tamper?: (raw: Record<string, unknown>) => void,
deniedCode?: string,
- terminal?: string[],
- fetchFn?: typeof fetch
+ options: {
+ fetchFn?: typeof fetch
+ maxStreamDurationMs?: number
+ configOverrides?: Partial<CodexLlmProxyConfig>
+ controlApiClient?: ControlApiClient
++ terminal?: string[]
+ } = {}
) {
const info = vi.spyOn(logger, 'info')
const warn = vi.spyOn(logger, 'warn')
- const { client, receipts } = grantingClient()
- const apps = createProxyApps(config({ maxBodyBytes: 65_536 }), {
- controlApiClient: deniedCode ? denyingClient(deniedCode) : client,
- fetchFn: fetchFn ?? upstream(textDeltas, calls, terminal),
+ const { client, receipts } = grantingClient(options.maxStreamDurationMs)
+ const apps = createProxyApps(config({ maxBodyBytes: 65_536, ...options.configOverrides }), {
+ controlApiClient: options.controlApiClient ?? (deniedCode ? denyingClient(deniedCode) : client),
- fetchFn: options.fetchFn ?? upstream(textDeltas, calls),
++ fetchFn: options.fetchFn ?? upstream(textDeltas, calls, options.terminal),
lookup,
})
try {
@@@ -811,40 -900,6 +984,39 @@@
expect(failureCount(metricsText, 'tool_call_limit_exceeded')).toBe(1)
})
+ // Arguments that are not a JSON object are invalid model output. The refusal
+ // has to reach the Host as a 422 with its own code and metric label: the 503
+ // default would be classified as an overload and retried.
+ it('(a1) answers 422 invalid_tool_arguments when a call’s arguments are not a JSON object', async () => {
+ const { res, receipts, lines, metricsText } = await run(
+ 'att-bad-args-http',
+ 0,
+ 0,
+ undefined,
+ undefined,
- undefined,
- malformedArgumentsUpstream()
++ { fetchFn: malformedArgumentsUpstream() }
+ )
+ expect(res.status).toBe(422)
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'invalid_tool_arguments' })
+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ providerAttemptId: 'att-bad-args-http',
+ outcome: 'failed',
+ code: 'invalid_tool_arguments',
+ deliveredAs: 'http_status',
+ httpStatus: 422,
+ toolCalls: 0,
+ textChunks: 0,
+ })
+ expectNoForbiddenKeys(lines[0]!)
+ // The attempt line names the refusal, never the arguments that caused it.
+ expect(JSON.stringify(lines[0])).not.toContain('{\\"q\\":')
+ expect(failureCount(metricsText, 'invalid_tool_arguments')).toBe(1)
+ expect(failureCount(metricsText, 'other')).toBe(0)
+ })
+
it('(b) sends an SSE error frame when text was already streamed', async () => {
const { res, lines } = await run('att-limit-sse', 1, 257)
expect(res.status).toBe(200)
@@@ -863,56 -918,81 +1035,131 @@@
expectNoForbiddenKeys(lines[0]!)
})
+ it('(h) answers 504 stream_duration_exceeded when the total cap fires before any frame', async () => {
+ const { res, receipts, lines, metricsText } = await run(
+ 'att-total-http',
+ 0,
+ 0,
+ undefined,
+ undefined,
+ { fetchFn: stalledUpstream(0), maxStreamDurationMs: 100 }
+ )
+ expect(res.status).toBe(504)
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'stream_duration_exceeded' })
+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ providerAttemptId: 'att-total-http',
+ outcome: 'failed',
+ code: 'stream_duration_exceeded',
+ reason: 'upstream stream exceeded maxStreamDurationMs',
+ details: { limitMs: 100 },
+ deliveredAs: 'http_status',
+ httpStatus: 504,
+ })
+ expectNoForbiddenKeys(lines[0]!)
+ expect(failureCount(metricsText, 'stream_duration_exceeded')).toBe(1)
+ expect(failureCount(metricsText, 'other')).toBe(0)
+ expect(timeoutCount(metricsText, 'total')).toBe(1)
+ expect(timeoutCount(metricsText, 'idle')).toBe(0)
+ })
+
+ it('(i) sends a stream_duration_exceeded SSE frame when text was already streamed', async () => {
+ const { res, lines } = await run('att-total-sse', 0, 0, undefined, undefined, {
+ fetchFn: stalledUpstream(1),
+ maxStreamDurationMs: 100,
+ })
+ expect(res.status).toBe(200)
+ expect(res.text).toContain('data: {"type":"text","text":"t0"}')
+ expect(res.text).toContain('data: {"type":"error","code":"stream_duration_exceeded"}')
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ outcome: 'failed',
+ code: 'stream_duration_exceeded',
+ deliveredAs: 'sse_error',
+ textChunks: 1,
+ })
+ expect('httpStatus' in lines[0]!).toBe(false)
+ })
+
+ it('(j) answers 503 provider_unavailable and counts an idle timeout on a silent upstream', async () => {
+ const { res, receipts, lines, metricsText } = await run(
+ 'att-idle-http',
+ 0,
+ 0,
+ undefined,
+ undefined,
+ { fetchFn: stalledUpstream(0), configOverrides: { upstreamIdleTimeoutMs: 50 } }
+ )
+ expect(res.status).toBe(503)
+ expect(res.body).toEqual({ error: 'provider_unavailable' })
+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ providerAttemptId: 'att-idle-http',
+ outcome: 'failed',
+ code: 'provider_unavailable',
+ reason: 'upstream stream idle timeout',
+ details: { idleTimeoutMs: 50 },
+ deliveredAs: 'http_status',
+ httpStatus: 503,
+ })
+ expect(failureCount(metricsText, 'provider_unavailable')).toBe(1)
+ expect(timeoutCount(metricsText, 'idle')).toBe(1)
+ expect(timeoutCount(metricsText, 'total')).toBe(0)
+ })
+
+ // #731 — the recorded upstream context-window refusal reaches the Host as a
+ // 400 `context_length_exceeded` (the upstream's own status class), not as a
+ // retryable 503 `provider_unavailable`.
- it('(h) T-R7-2c answers 400 context_length_exceeded for the upstream context overflow', async () => {
++ it('(o) T-R7-2c answers 400 context_length_exceeded for the upstream context overflow', async () => {
+ const { res, receipts, lines, metricsText } = await run(
+ 'att-context-http',
+ 0,
+ 0,
+ undefined,
+ undefined,
- UPSTREAM_CONTEXT_OVERFLOW_FRAMES
++ { terminal: UPSTREAM_CONTEXT_OVERFLOW_FRAMES }
+ )
+ expect(res.status).toBe(400)
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'context_length_exceeded' })
+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ providerAttemptId: 'att-context-http',
+ outcome: 'failed',
+ code: 'context_length_exceeded',
+ deliveredAs: 'http_status',
+ httpStatus: 400,
+ })
+ expectNoForbiddenKeys(lines[0]!)
+ expect(failureCount(metricsText, 'context_length_exceeded')).toBe(1)
+ expect(failureCount(metricsText, 'provider_unavailable')).toBe(0)
+ })
+
- it('(i) T-R7-2c sends an SSE context_length_exceeded frame when text was already streamed', async () => {
++ it('(p) T-R7-2c sends an SSE context_length_exceeded frame when text was already streamed', async () => {
+ const { res, lines } = await run(
+ 'att-context-sse',
+ 1,
+ 0,
+ undefined,
+ undefined,
- UPSTREAM_CONTEXT_OVERFLOW_FRAMES
++ { terminal: UPSTREAM_CONTEXT_OVERFLOW_FRAMES }
+ )
+ expect(res.status).toBe(200)
+ expect(res.text).toContain('data: {"type":"text","text":"t0"}')
+ expect(res.text).toContain('data: {"type":"error","code":"context_length_exceeded"}')
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ outcome: 'failed',
+ code: 'context_length_exceeded',
+ deliveredAs: 'sse_error',
+ textChunks: 1,
+ })
+ })
+
it('(d) logs an invalid request without the caller-supplied parse message', async () => {
const secretField = 'sk-live-0123456789abcdef'
const { res, lines } = await run('att-invalid', 0, 0, raw => {
diff --cc control-api/package-lock.json
index 800bf8de6,211f524b5..5d5f667a3
--- 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.625",
- "version": "0.1.604",
++ "version": "0.1.626",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.625",
- "version": "0.1.604",
++ "version": "0.1.626",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --cc control-api/package.json
index e3a554297,64d00e152..2359d4712
--- a/control-api/package.json
+++ b/control-api/package.json
@@@ -1,6 -1,6 +1,6 @@@
{
"name": "clerum-control-api",
- "version": "0.1.625",
- "version": "0.1.604",
++ "version": "0.1.626",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --cc control-api/src/services/grokProviderAttemptRedemption.ts
index c4ae09ce3,364dfe432..430045d48
--- a/control-api/src/services/grokProviderAttemptRedemption.ts
+++ b/control-api/src/services/grokProviderAttemptRedemption.ts
@@@ -14,10 -13,7 +14,11 @@@ import
loadGrokSubscriptionSecrets,
} from './grokSubscriptionConnection.js'
import { GrokSubscriptionOAuthError, ensureFreshGrokAccessToken } from './grokSubscriptionOAuth.js'
+import {
+ type AllowedModelsConfigMapMaterializer,
+ publishAllowedModelsConfigMapAfterGrantChange,
+} from './llmAllowedModelsConfigMap.js'
+ import { GROK_MAX_STREAM_DURATION_MS } from './llmProviderAttemptEnvelope.js'
import { opaqueAttemptReceipt } from './llmProviderAttemptRedemption.js'
import {
loadLlmProviderAttempt,
diff --cc control-api/src/services/llmProviderAttemptRedemption.ts
index 5c8d451ad,6200ebc76..a3348fee7
--- a/control-api/src/services/llmProviderAttemptRedemption.ts
+++ b/control-api/src/services/llmProviderAttemptRedemption.ts
@@@ -13,10 -12,7 +13,11 @@@ import
CodexSubscriptionOAuthError,
ensureFreshCodexAccessToken,
} from './codexSubscriptionOAuth.js'
+import {
+ type AllowedModelsConfigMapMaterializer,
+ publishAllowedModelsConfigMapAfterGrantChange,
+} from './llmAllowedModelsConfigMap.js'
+ import { CODEX_MAX_STREAM_DURATION_MS } from './llmProviderAttemptEnvelope.js'
import {
loadLlmProviderAttempt,
lockLlmProviderAttemptTicket,
diff --cc deploy/base/control-plane/codex-llm-proxy.yaml
index 4ddebeb85,ae8f33b41..3cadaa971
--- a/deploy/base/control-plane/codex-llm-proxy.yaml
+++ b/deploy/base/control-plane/codex-llm-proxy.yaml
@@@ -18,13 -18,12 +18,13 @@@ data
CODEX_LLM_PROXY_RUNTIME_PORT: "8080"
CODEX_LLM_PROXY_ADMIN_PORT: "8081"
CODEX_LLM_PROXY_PROBE_PORT: "9090"
- CODEX_LLM_PROXY_MAX_BODY_BYTES: "1048576"
+ # CODEX_LLM_PROXY_MAX_BODY_BYTES is deliberately unset: the proxy derives it
+ # from LIMITS.maxRequestBodyBytes plus the envelope allowance (#731), and it
+ # governs ordinary and anonymous requests.
# 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-api
CODEX_LLM_PROXY_EXECUTION_ENABLED: "false"
CODEX_LLM_PROXY_CONTROL_API_URL: http://control-api-rpc-gateway.control-plane.svc.cluster.local:8090/api/v1
diff --cc deploy/base/control-plane/grok-llm-proxy.yaml
index 81f1b01b1,96aec866f..47fdb7dd7
--- a/deploy/base/control-plane/grok-llm-proxy.yaml
+++ b/deploy/base/control-plane/grok-llm-proxy.yaml
@@@ -18,8 -18,9 +18,8 @@@ data
GROK_LLM_PROXY_RUNTIME_PORT: "8080"
GROK_LLM_PROXY_ADMIN_PORT: "8081"
GROK_LLM_PROXY_PROBE_PORT: "9090"
- GROK_LLM_PROXY_MAX_STREAM_DURATION_MS: "300000"
- GROK_LLM_PROXY_MAX_DEADLINE_MS: "300000"
- GROK_LLM_PROXY_MAX_BODY_BYTES: "1048576"
+ GROK_LLM_PROXY_MAX_STREAM_DURATION_MS: "1800000"
+ GROK_LLM_PROXY_MAX_DEADLINE_MS: "1800000"
GROK_LLM_PROXY_JWT_ISSUER: control-api
GROK_LLM_PROXY_EXECUTION_ENABLED: "false"
GROK_LLM_PROXY_CONTROL_API_URL: http://control-api-rpc-gateway.control-plane.svc.cluster.local:8090/api/v1
diff --cc desktop-app/ui/src/components/agents/__tests__/ChatThread.renderSemantics.test.tsx
index e8048b225,e2255053e..a0f2cb4af
--- a/desktop-app/ui/src/components/agents/__tests__/ChatThread.renderSemantics.test.tsx
+++ b/desktop-app/ui/src/components/agents/__tests__/ChatThread.renderSemantics.test.tsx
@@@ -132,14 -132,12 +132,20 @@@ describe('ChatThread error code labels'
expect(text).not.toContain('Model Overloaded')
})
+ // The subscription proxies refuse a tool call whose arguments are not a JSON
+ // object with `invalid_tool_arguments`, which the Host maps to this code.
+ it('labels an invalid model response as "Invalid Model Response", not a connection error', () => {
+ const { label, text } = renderErrorLabel('LLM_INVALID_RESPONSE', 'codex-subscription')
+ expect(label).toBe('Invalid Model Response · CODEX-SUBSCRIPTION')
+ expect(text).not.toContain('Connection Error')
+ })
+
+ it('labels a stream duration cap as "Response Took Too Long", not "Model Overloaded"', () => {
+ const { label, text } = renderErrorLabel('LLM_STREAM_DURATION_EXCEEDED', 'grok-subscription')
+ expect(label).toBe('Response Took Too Long · GROK-SUBSCRIPTION')
+ expect(text).not.toContain('Model Overloaded')
+ })
+
it('labels a context length error as "Conversation Too Long"', () => {
expect(renderErrorLabel('LLM_CONTEXT_LENGTH_EXCEEDED').label).toBe('Conversation Too Long')
})
diff --cc desktop-app/ui/src/constants/agents.ts
index 12143d090,6281078c0..da7fa828a
--- a/desktop-app/ui/src/constants/agents.ts
+++ b/desktop-app/ui/src/constants/agents.ts
@@@ -15,8 -15,8 +15,9 @@@ export const AGENT_ERROR_CODE_LABELS: R
LLM_CONTENT_FILTERED: 'Content Filtered',
LLM_API_CALL_FAILED: 'Connection Error',
LLM_TOOL_CALL_LIMIT_EXCEEDED: 'Too Many Tool Calls',
+ LLM_STREAM_DURATION_EXCEEDED: 'Response Took Too Long',
LLM_CONTEXT_LENGTH_EXCEEDED: 'Conversation Too Long',
+ LLM_INVALID_RESPONSE: 'Invalid Model Response',
LLM_IMAGE_INPUT_UNSUPPORTED: 'Image Input Unsupported',
LLM_IMAGE_INPUT_UNKNOWN: 'Image Input Unverified',
LLM_INVALID_ATTACHMENT: 'Invalid Attachment',
diff --cc docs/architecture/codex-subscription-transport-contract.md
index 1380567bc,d2c829060..6a73ca554
--- a/docs/architecture/codex-subscription-transport-contract.md
+++ b/docs/architecture/codex-subscription-transport-contract.md
@@@ -156,18 -144,31 +156,31 @@@ OAuth scopes: `openid`, `profile`, `ema
All 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 | 8388608 |
- | 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 |
++| maxRequestBodyBytes | 8388608 |
+ | 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 request
contract. The entire serialized request, including all definitions, remains
@@@ -366,23 -372,28 +403,43 @@@ behavior changes
Stable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,
`budget_denied`, `connection_unavailable`, `provider_unavailable`,
`origin_denied`, `ticket_invalid`, `ticket_replayed`, `request_hash_mismatch`,
- `tool_call_limit_exceeded`, `context_length_exceeded`, `invalid_tool_arguments`,
- `payload_too_large`.
+ `invalid_request`, `tool_call_limit_exceeded`, `sse_buffer_exceeded`,
-`stream_duration_exceeded`. The freeze gate checks that every code the proxy
++`stream_duration_exceeded`, `context_length_exceeded`, `invalid_tool_arguments`,
++`payload_too_large`. The freeze gate checks that every code the proxy
+ constructs is in the fixture's `errorTaxonomy`.
+- `context_length_exceeded`: the upstream refused the request because it
+ exceeds the model's context window. The upstream sends an SSE `error` event
+ (`error.code`) and a `response.failed` event (`response.error.code`) with
+ this code; the proxy forwards it as HTTP 400, or as an SSE error frame when
+ text had already been streamed. The same `error.code` in the JSON body of a
+ non-success HTTP reply other than 401/403 is mapped the same way; the proxy
+ reads at most `UPSTREAM_ERROR_BODY_MAX_BYTES` (16 KiB) of that body and
+ otherwise keeps the status mapping (400 `invalid_request`, 401/403
+ `connection_unavailable`, any other `provider_unavailable`). It is the only
+ upstream code the proxy forwards: a streamed failure with any other upstream
+ code, or none, stays `provider_unavailable`. The Host maps it to
+ `LLM_CONTEXT_LENGTH_EXCEEDED`,
+ not retryable, like `request_limit_exceeded`.
+
+ - `invalid_request`: the request body failed the transport schema (HTTP 400
+ before redeem), or the upstream answered the completion with HTTP 400.
+ - `sse_buffer_exceeded`: the upstream sent more than 1 MiB without the blank
+ line that ends an SSE event.
+ - `stream_duration_exceeded`: the attempt reached `maxStreamDurationMs` (the
+ total cap, bounded again by the ticket's deadline). The proxy cancels the
+ upstream body and returns HTTP 504, or an SSE error frame when text had
+ already been streamed. The Host maps it to `LLM_STREAM_DURATION_EXCEEDED`.
+ It is not retryable and not failover-eligible: another attempt would spend
+ the same budget on the same turn.
+ - Idle timeout: when the upstream sends no byte for `upstreamIdleTimeoutMs`,
+ the proxy cancels the upstream body and fails the attempt with
+ `provider_unavailable` (HTTP 503, reason `upstream stream idle timeout`).
+ That code stays retryable and failover-eligible, because a silent upstream
+ is an outage of that provider, not a property of the turn.
+ `CODEX_LLM_PROXY_UPSTREAM_IDLE_TIMEOUT_MS` can lower the idle timeout; the
+ transport never raises it above the table value. Both cuts are counted in
+ `codex_proxy_upstream_timeouts_total{kind="idle"|"total"}`.
- `tool_call_limit_exceeded`: the upstream response carried more than
`maxToolCalls` tool calls. The proxy returns HTTP 422, or an SSE error frame
when text had already been streamed. The Host maps it to
diff --cc docs/architecture/grok-subscription-transport-contract.md
index 838723e21,e01f67ecb..66b44df60
--- a/docs/architecture/grok-subscription-transport-contract.md
+++ b/docs/architecture/grok-subscription-transport-contract.md
@@@ -126,12 -126,26 +126,31 @@@ Owned by `@clerum/grok-provider-attempt
message, not the conversation, and the 1:4 spread between the two numbers is a
design choice rather than an arithmetic requirement: a turn of N calls adds
N+1 messages, so a full 256-call turn occupies 257 of the 1024 message slots.
+`maxRequestBodyBytes` is 8388608 (8 MiB) for every request that carries no
+image, as for Codex (#731). It covers a 1M-token window serialized as escaped
+JSON. The proxy's body limit is that cap plus a 16 KiB envelope allowance,
+and the proxy admits bodies against an in-flight byte budget before parsing
+them.
+ | Limit | Value |
+ | --------------------- | ------- |
-| maxRequestBodyBytes | 1048576 |
++| maxRequestBodyBytes | 8388608 |
+ | maxMessages | 1024 |
+ | maxToolCalls | 256 |
+ | maxOutputTokens | 16384 |
+ | maxStreamDurationMs | 1800000 |
+ | maxDeadlineMs | 1800000 |
+ | maxConcurrentStreams | 8 |
+ | maxQueuedRequests | 16 |
+ | maxQueueWaitMs | 60000 |
+ | upstreamIdleTimeoutMs | 600000 |
+ | maxRetriesPerAttempt | 1 |
+
+ The limit values live only in the table above, which
+ `grok-llm-proxy/test/contractFreeze.test.ts` checks against the fixture; the
+ same suite pins the fixture to the contract `LIMITS` and the proxy
+ `STREAM_LIMITS`.
+
All three enforcement points read this module — the control-api authorizer,
`grok-llm-proxy` and the Host — so a deployment that mixes versions rejects
requests that fall between the old and the new bounds. Which code the caller
@@@ -250,10 -262,42 +278,43 @@@ Stable codes: `insufficient_scope`, `no
`budget_denied` (Host-side only), `connection_unavailable`,
`provider_unavailable`, `origin_denied`, `ticket_invalid`, `ticket_replayed`,
`request_hash_mismatch`, `client_upgrade_required`, `tool_call_limit_exceeded`,
- `tool_call_arguments_exceeded`, `invalid_tool_arguments`,
- `request_limit_exceeded` (Host-side only), `payload_too_large`,
- `context_length_exceeded`.
-
-`tool_call_arguments_exceeded`, `invalid_request`, `sse_buffer_exceeded`,
-`stream_duration_exceeded`, `request_limit_exceeded` (Host-side only). The
++`tool_call_arguments_exceeded`, `invalid_tool_arguments`, `invalid_request`,
++`sse_buffer_exceeded`, `stream_duration_exceeded`, `payload_too_large`,
++`context_length_exceeded`, `request_limit_exceeded` (Host-side only). The
+ freeze gate checks that every code the proxy constructs is in the fixture's
+ `errorTaxonomy`.
+
+ - `invalid_request`: the request body failed the transport schema (HTTP 400
+ before redeem), or the upstream answered the completion with HTTP 400.
+ - `sse_buffer_exceeded`: the upstream sent more than 1 MiB without the blank
+ line that ends an SSE event.
+ - `stream_duration_exceeded`: the attempt reached `maxStreamDurationMs` (the
+ total cap, bounded again by the ticket's deadline). The proxy cancels the
+ upstream body and returns HTTP 504, or an SSE error frame when text had
+ already been streamed. The Host maps it to `LLM_STREAM_DURATION_EXCEEDED`.
+ It is not retryable and not failover-eligible: another attempt would spend
+ the same budget on the same turn.
+ - Idle timeout: when the upstream sends no byte for `upstreamIdleTimeoutMs`
+ (the value read from the Grok Build client source), the proxy cancels the upstream
+ body and fails the attempt with `provider_unavailable` (HTTP 503, reason
+ `upstream stream idle timeout`). That code stays retryable and
+ failover-eligible, because a silent upstream is an outage of that provider,
+ not a property of the turn. `GROK_LLM_PROXY_UPSTREAM_IDLE_TIMEOUT_MS` can
+ lower the idle timeout; the transport never raises it above the contract
+ value. Both cuts are counted in
+ `grok_proxy_upstream_timeouts_total{kind="idle"|"total"}`.
+ - Keepalive: once the redeem succeeds, the proxy writes a `: keepalive` SSE
+ comment every `GROK_LLM_PROXY_HEARTBEAT_INTERVAL_MS` (default 15000, at most 60000) until
+ the response ends. A larger value stops the proxy at startup instead of
+ being lowered. The comments keep the Host's HTTP client, whose headers
+ and body timeouts are 300 s, from cutting an attempt while the upstream is
+ silent (reasoning, or tool calls buffered until the stream completes). SSE
+ readers, including `mcp-host`, ignore comment lines. The
+ `grok_proxy_attempt_finished` log line counts them in `heartbeats`. A
+ keepalive counts as a sent SSE byte, so every failure after the first one is
+ delivered as an SSE error frame instead of an HTTP status. A redeem denial
+ always keeps its HTTP status, because no keepalive is written before the
+ redeem succeeds.
- `tool_call_limit_exceeded`: the upstream response carried more than
`maxToolCalls` tool calls. The proxy returns HTTP 422 whose body is the code
alone — `{"error":"tool_call_limit_exceeded"}` — or an SSE error frame when
diff --cc grok-llm-proxy/src/config.ts
index 7b8760757,ed72472d1..8ca765fa6
--- a/grok-llm-proxy/src/config.ts
+++ b/grok-llm-proxy/src/config.ts
@@@ -1,4 -1,5 +1,5 @@@
- import { DEFAULT_MAX_BODY_BYTES } from './requestLimits.js'
-import { STREAM_LIMITS } from './requestLimits.js'
++import { DEFAULT_MAX_BODY_BYTES, STREAM_LIMITS } from './requestLimits.js'
+ import { DEFAULT_HEARTBEAT_INTERVAL_MS, MAX_HEARTBEAT_INTERVAL_MS } from './sseHeartbeat.js'
export type GrokLlmProxyConfig = {
runtimePort: number
diff --cc grok-llm-proxy/src/grokTransport.ts
index 92312b753,8c30d6336..47997499b
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@@ -269,19 -379,21 +379,22 @@@ async function dispatchUpstreamStream
message.role === 'assistant' ? (message.toolCalls ?? []).map(call => call.name) : []
),
])
- const response = await fetchFrozenOrigin({
- url,
- fetchFn: input.fetchFn,
- lookup: input.lookup,
- init: {
- method: 'POST',
- signal,
- headers,
- body: JSON.stringify(toUpstreamPayload(input.request, names)),
- },
- })
+ const response = await deadline.waitUpstream(
+ fetchFrozenOrigin({
+ url,
+ fetchFn: input.fetchFn,
+ lookup: input.lookup,
+ init: {
+ method: 'POST',
+ signal,
+ headers,
+ body: JSON.stringify(toUpstreamPayload(input.request, names)),
+ },
+ }),
+ signal
+ )
if (!response.ok || !response.body) {
+ const errorBody = await readUpstreamErrorBody(response)
logger.warn(
{
event: 'grok_upstream_http',
@@@ -320,61 -429,9 +433,61 @@@
}
throw new GrokTransportError('provider_unavailable', 'upstream completion failed')
}
- return consumeSse(response.body, input.onFrame, signal, names)
+ return consumeSse(response.body, input.onFrame, signal, names, deadline)
}
+// R10 (M1): a non-success completion body is read once, up to this bound, for
+// the log hint and to find a context-window refusal. The recorded refusal is
+// about 200 bytes; past the bound the read is cancelled, the prefix still feeds
+// the hint, and the status mapping stands.
+const UPSTREAM_ERROR_BODY_MAX_BYTES = 16 * 1024
+
+// The recorded refusal (HTTP 400 from /v1/responses, grok-4.6, 2026-09-23) has
+// the generic `code: 'invalid-argument'`; this bracketed marker inside its
+// `error` string is the only field that names the context window.
+const CONTEXT_OVERFLOW_MARKER = '[input_too_large]'
+
+async function readUpstreamErrorBody(
+ response: Response
+): Promise<{ text: string; complete: boolean }> {
+ if (!response.body) return { text: '', complete: true }
+ const reader = response.body.getReader()
+ const chunks: Uint8Array[] = []
+ let total = 0
+ let complete = true
+ try {
+ for (;;) {
+ const { done, value } = await reader.read()
+ if (done) break
+ total += value.byteLength
+ if (total > UPSTREAM_ERROR_BODY_MAX_BYTES) {
+ complete = false
+ await reader.cancel().catch(() => undefined)
+ break
+ }
+ chunks.push(value)
+ }
+ } catch {
+ complete = false
+ await reader.cancel().catch(() => undefined)
+ }
+ return { text: Buffer.concat(chunks).toString('utf8'), complete }
+}
+
+function isContextOverflowBody(text: string): boolean {
+ let parsed: unknown
+ try {
+ parsed = JSON.parse(text)
+ } catch {
+ return false
+ }
+ return (
+ isPlainObject(parsed) &&
+ typeof parsed.error === 'string' &&
+ parsed.error.includes(CONTEXT_OVERFLOW_MARKER)
+ )
+}
+
function toUpstreamPayload(
request: GrokCompletionRequestV1,
names: ToolNameMap
@@@ -519,17 -579,15 +635,24 @@@ async function consumeSse
await reader.cancel().catch(() => undefined)
throw error
} finally {
- reader.releaseLock()
+ try {
+ reader.releaseLock()
+ } catch {
+ // A read that lost the race to an abort may still hold the lock after cancel.
+ }
}
+ // A canceled or failed stream leaves its open call truncated; report the
- // stream's outcome before the flush can refuse those arguments.
- if (signal.aborted) return { outcome: 'canceled', usage }
++ // stream's outcome before the flush can refuse those arguments. A deadline
++ // abort carries its typed UpstreamTimeoutError as the signal reason, so
++ // throwing the reason surfaces stream_duration_exceeded or
++ // provider_unavailable to the route instead of a plain cancel.
++ signal.throwIfAborted()
+ if (failed) {
+ throw new GrokTransportError('provider_unavailable', 'upstream response failed')
+ }
for (const call of pending.values()) {
if (call.emitted) continue
- const args = parseToolArguments(call.arguments)
+ const args = parseToolArguments(call.arguments, { closed: false })
await acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })
call.emitted = true
}
diff --cc grok-llm-proxy/src/server.ts
index a9bdbfcfa,b03549e3c..baa451342
--- a/grok-llm-proxy/src/server.ts
+++ b/grok-llm-proxy/src/server.ts
@@@ -27,20 -22,9 +28,21 @@@ import
type OriginPolicyOptions,
defaultAddressLookup,
} from './originPolicy.js'
-import { RequestLimitError, streamGate } from './requestLimits.js'
+import {
+ BODY_READ_DEADLINE_MS,
+ BodyBudget,
+ IN_FLIGHT_BODY_BUDGET_BODIES,
+ RequestLimitError,
+ streamGate,
+} from './requestLimits.js'
+ import { startSseHeartbeat } from './sseHeartbeat.js'
+type GatedRequest = Request & {
+ /** Set by the platform gate before body admission (R9-M-B). */
+ grokPlatform?: PlatformJwtClaims
+}
+
+const COMPLETION_PATH = '/internal/runtime/v1/grok/completions'
const COMPLETION_KEYS = new Set(['executionTicket', 'requestHash', 'request', 'deadlineMs'])
const ADMIN_KEYS = new Set(['accessToken'])
@@@ -547,8 -414,10 +572,11 @@@ const ATTEMPT_ERROR_STATUS: Record<stri
ticket_replayed: 409,
tool_call_limit_exceeded: 422,
tool_call_arguments_exceeded: 422,
+ invalid_tool_arguments: 422,
client_upgrade_required: 426,
+ // The attempt ran for its whole stream budget. Retrying the same request
+ // would spend the same budget again, so it is a gateway timeout, not 503.
+ stream_duration_exceeded: 504,
connection_unavailable: 503,
provider_unavailable: 503,
sse_buffer_exceeded: 503,
diff --cc grok-llm-proxy/test/bodyAdmission.test.ts
index 86de347df,000000000..50897da2a
mode 100644,000000..100644
--- a/grok-llm-proxy/test/bodyAdmission.test.ts
+++ b/grok-llm-proxy/test/bodyAdmission.test.ts
@@@ -1,731 -1,0 +1,733 @@@
+/**
+ * #731 R3-2 - body admission before `express.json`.
+ *
+ * At an 8 MiB request cap, the stream gate alone would let 24 bodies in (8
+ * running plus 16 queued). Three bodies in flight already peaked at 230-292 MiB
+ * of RSS, so 24 would not fit the proxy's 768Mi limit by that ratio (not
+ * measured at 24). These tests drive the real runtime app over HTTP and use
+ * the control-api `redeem` call as the witness: it runs only after the whole
+ * body was read, JSON-parsed, contract-parsed and hash-checked, so the number
+ * of attempts held there is the number of bodies in memory.
+ */
+import { describe, expect, it, vi } from 'vitest'
+import jwt from 'jsonwebtoken'
+import { generateKeyPairSync, randomUUID } from 'node:crypto'
+import { request as httpRequest } from 'node:http'
+import type { AddressInfo } from 'node:net'
+import { gzipSync } from 'node:zlib'
+import {
+ LIMITS,
+ hashGrokCompletionRequestV1,
+ parseGrokCompletionRequestV1,
+} from '@clerum/grok-provider-attempt-contract'
+import type { GrokLlmProxyConfig } from '../src/config.js'
+import type {
+ ControlApiClient,
+ FinalizeAttemptSuccess,
+ RedeemAttemptSuccess,
+} from '../src/controlApiClient.js'
+import { logger } from '../src/logger.js'
+import { GROK_CATALOG_ORIGIN, GROK_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'
+import {
+ BODY_READ_DEADLINE_MS,
+ DEFAULT_MAX_BODY_BYTES,
+ // The admission budget holds this many bodies of the configured maximum size.
+ IN_FLIGHT_BODY_BUDGET_BODIES as BUDGET_BODIES,
+ STREAM_LIMITS,
+} from '../src/requestLimits.js'
+import { type ProxyRuntimeDeps, createProxyApps } from '../src/server.js'
+
+/** Captured before any test fakes the clock, so waits stay on real time. */
+const realSetTimeout = globalThis.setTimeout
+
+const { privateKey, publicKey } = generateKeyPairSync('rsa', {
+ modulusLength: 2048,
+ publicKeyEncoding: { type: 'spki', format: 'pem' },
+ privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
+})
+
+const HOST_REF = 'research-host'
+const COMPLETIONS_PATH = '/internal/runtime/v1/grok/completions'
+
+function sign(payload: Record<string, unknown>, audience: string): string {
+ return jwt.sign(payload, privateKey, {
+ algorithm: 'RS256',
+ issuer: 'control-api',
+ audience,
+ expiresIn: 60,
+ })
+}
+
+function config(maxBodyBytes: number): GrokLlmProxyConfig {
+ return {
+ runtimePort: 0,
+ adminPort: 0,
+ probePort: 0,
+ maxBodyBytes,
+ maxStreamDurationMs: 60_000,
+ maxDeadlineMs: 60_000,
++ upstreamIdleTimeoutMs: 600_000,
++ heartbeatIntervalMs: 15_000,
+ jwtIssuer: 'control-api',
+ jwtPublicKey: publicKey,
+ executionEnabled: true,
+ controlApiBaseUrl: 'http://control-api.invalid/api/v1',
+ controlApiServiceName: 'grok-llm-proxy',
+ controlApiServiceToken: 'unused',
+ }
+}
+
+const platformToken = sign(
+ {
+ sub: `default/${HOST_REF}`,
+ hostRefs: [HOST_REF],
+ workflowControlScopes: ['llm:grok:execute'],
+ scope: 'workflow:approval:request',
+ },
+ 'workflow-approvals'
+)
+
+/** A runtime envelope whose request carries one user message of `contentChars`. */
+function completionBody(contentChars: number, attempt: string): string {
+ const raw = {
+ schemaVersion: 'grok-completion-request.v1',
+ requestId: `req-${attempt}`,
+ idempotencyKey: `idem-${attempt}`,
+ provider: 'grok-subscription',
+ model: 'grok-4.6',
+ messages: [{ role: 'user', content: 'x'.repeat(contentChars) }],
+ }
+ const parsed = parseGrokCompletionRequestV1(raw)
+ if (!parsed.ok) throw new Error(parsed.message)
+ const requestHash = hashGrokCompletionRequestV1(parsed.value)
+ return JSON.stringify({
+ executionTicket: sign(
+ {
+ jti: randomUUID(),
+ typ: 'grok-execution-ticket',
+ hostRef: HOST_REF,
+ model: 'grok-4.6',
+ requestHash,
+ providerAttemptId: `att-${attempt}`,
+ },
+ 'grok-llm-proxy'
+ ),
+ requestHash,
+ request: raw,
+ })
+}
+
+const adminPermit = sign(
+ { sub: 'admin-1', typ: 'grok-admin-permit', operation: 'catalog_list' },
+ 'grok-llm-proxy-admin'
+)
+
+/**
+ * A proxy whose `redeem` holds every attempt until `releaseAll`, so an attempt
+ * that reached it keeps its parsed body in memory. `hold` holds again.
+ */
+async function heldProxy(maxBodyBytes: number, deps: Pick<ProxyRuntimeDeps, 'bodyReadDeadlineMs'> = {}) {
+ let redeemed = 0
+ let holding = true
+ const held: Array<() => void> = []
+ const client = {
+ async redeem(): Promise<RedeemAttemptSuccess> {
+ redeemed += 1
+ if (holding) await new Promise<void>(resolve => held.push(resolve))
+ return {
+ accessToken: 'test-access-admission',
+ transport: {
+ protocolVersion: 'grok-subscription-transport.v1',
+ completionsOrigin: GROK_COMPLETIONS_ORIGIN,
+ catalogOrigin: GROK_CATALOG_ORIGIN,
+ operation: 'completion_stream',
+ servedModel: 'grok-4.6',
+ maxStreamDurationMs: 60_000,
+ },
+ expiryClass: 'short_lived',
+ attemptReceipt: 'b'.repeat(64),
+ }
+ },
+ async finalize(input: {
+ receipt: { providerAttemptId: string; outcome: FinalizeAttemptSuccess['outcome'] }
+ }): Promise<FinalizeAttemptSuccess> {
+ return {
+ providerAttemptId: input.receipt.providerAttemptId,
+ outcome: input.receipt.outcome,
+ duplicate: false,
+ }
+ },
+ } as unknown as ControlApiClient
+ const fetchFn = (async () =>
+ new Response('data: {"type":"response.completed","response":{"usage":{}}}\n\n', {
+ status: 200,
+ headers: { 'content-type': 'text/event-stream' },
+ })) as typeof fetch
+
+ const servers = createProxyApps(config(maxBodyBytes), {
+ controlApiClient: client,
+ fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ ...deps,
+ })
+ await new Promise<void>(resolve => servers.runtime.listen(0, '127.0.0.1', () => resolve()))
+ await new Promise<void>(resolve => servers.admin.listen(0, '127.0.0.1', () => resolve()))
+ const { port } = servers.runtime.address() as AddressInfo
+ const { port: adminPort } = servers.admin.address() as AddressInfo
+ let arrived = 0
+ servers.runtime.on('request', () => {
+ arrived += 1
+ })
+ return {
+ port,
+ adminPort,
+ redeemed: () => redeemed,
+ /** Requests whose headers the runtime listener has received. */
+ arrived: () => arrived,
+ hold: () => {
+ holding = true
+ },
+ releaseAll: () => {
+ holding = false
+ for (const resolve of held.splice(0)) resolve()
+ },
+ close: () => servers.close(),
+ }
+}
+
+type Reply = { status: number; body: string }
+
+/** POST with a declared Content-Length, or with a chunked body of undeclared length. */
+function post(port: number, payload: string, options: { chunked?: boolean } = {}): Promise<Reply> {
+ return new Promise((resolve, reject) => {
+ const req = httpRequest(
+ {
+ host: '127.0.0.1',
+ port,
+ method: 'POST',
+ path: COMPLETIONS_PATH,
+ agent: false,
+ headers: {
+ 'content-type': 'application/json',
+ authorization: `Bearer ${platformToken}`,
+ ...(options.chunked
+ ? { 'transfer-encoding': 'chunked' }
+ : { 'content-length': Buffer.byteLength(payload) }),
+ },
+ },
+ res => {
+ const chunks: Buffer[] = []
+ res.on('data', chunk => chunks.push(chunk as Buffer))
+ res.on('end', () =>
+ resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString('utf8') })
+ )
+ res.on('error', reject)
+ }
+ )
+ req.on('error', reject)
+ req.end(payload)
+ })
+}
+
+/** Waits until `read()` is positive and stops changing for one poll interval. */
+async function settle(read: () => number): Promise<number> {
+ const deadline = Date.now() + 20_000
+ let last = -1
+ while (Date.now() < deadline) {
+ await new Promise(resolve => setTimeout(resolve, 300))
+ const now = read()
+ if (now > 0 && now === last) return now
+ last = now
+ }
+ throw new Error(`count did not settle within 20s (last ${last})`)
+}
+
+function sleep(ms: number): Promise<void> {
+ return new Promise(resolve => realSetTimeout(resolve, ms))
+}
+
+type TimedReply = Reply & { elapsedMs: number }
+
+/** The reply, or status 0 when none arrived within `ms` of real time. */
+function within(reply: Promise<TimedReply>, ms: number): Promise<TimedReply> {
+ return Promise.race([
+ reply,
+ sleep(ms).then(() => ({ status: 0, body: `no reply within ${ms} ms`, elapsedMs: ms })),
+ ])
+}
+
+/**
+ * A POST that declares `declaredBytes`, sends only `sent` and then stalls.
+ * `finish` sends the rest of the body; `destroy` closes the client socket so
+ * the listener can shut down.
+ */
+function stalledPost(
+ port: number,
+ options: { declaredBytes: number; token?: string; path?: string; sent?: string }
+): { reply: Promise<TimedReply>; finish: (rest: string) => void; destroy: () => void } {
+ const started = Date.now()
+ const req = httpRequest({
+ host: '127.0.0.1',
+ port,
+ method: 'POST',
+ path: options.path ?? COMPLETIONS_PATH,
+ agent: false,
+ headers: {
+ 'content-type': 'application/json',
+ 'content-length': options.declaredBytes,
+ ...(options.token ? { authorization: `Bearer ${options.token}` } : {}),
+ },
+ })
+ const reply = new Promise<TimedReply>((resolve, reject) => {
+ req.on('response', res => {
+ const chunks: Buffer[] = []
+ res.on('data', chunk => chunks.push(chunk as Buffer))
+ res.on('end', () =>
+ resolve({
+ status: res.statusCode ?? 0,
+ body: Buffer.concat(chunks).toString('utf8'),
+ elapsedMs: Date.now() - started,
+ })
+ )
+ res.on('error', reject)
+ })
+ req.on('error', reject)
+ })
+ req.write(options.sent ?? '{')
+ return { reply, finish: rest => req.end(rest), destroy: () => req.destroy() }
+}
+
+describe('grok-llm-proxy body admission (#731 R3-2)', () => {
+ it('T-R3-2a-grok holds at most three near-cap bodies at once and admits the rest as they finish', async () => {
+ const proxy = await heldProxy(DEFAULT_MAX_BODY_BYTES)
+ try {
+ const contentChars = Math.floor(LIMITS.maxRequestBodyBytes * 0.9)
+ const payloads = Array.from({ length: 6 }, (_, i) => completionBody(contentChars, `cap-${i}`))
+ const payloadBytes = Buffer.byteLength(payloads[0]!)
+ // Fixture check: three bodies fit the budget and a fourth does not.
+ expect(BUDGET_BODIES * payloadBytes).toBeLessThanOrEqual(
+ BUDGET_BODIES * DEFAULT_MAX_BODY_BYTES
+ )
+ expect((BUDGET_BODIES + 1) * payloadBytes).toBeGreaterThan(
+ BUDGET_BODIES * DEFAULT_MAX_BODY_BYTES
+ )
+
+ const replies = payloads.map(payload => post(proxy.port, payload))
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+
+ // Liveness: the three that waited are admitted once the first finish.
+ proxy.releaseAll()
+ const settled = await Promise.all(replies)
+ expect(settled.map(reply => reply.status)).toEqual([200, 200, 200, 200, 200, 200])
+ for (const reply of settled) expect(reply.body).toContain('"outcome":"success"')
+ expect(proxy.redeemed()).toBe(6)
+ } finally {
+ proxy.releaseAll()
+ await proxy.close()
+ }
+ }, 60_000)
+
+ it('T-R3-2b-grok refuses a body of undeclared length with 411 before reading it', async () => {
+ const proxy = await heldProxy(DEFAULT_MAX_BODY_BYTES)
+ proxy.releaseAll()
+ try {
+ const refused = await post(proxy.port, completionBody(64, 'chunked'), { chunked: true })
+ expect(refused.status).toBe(411)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'length_required' })
+ expect(proxy.redeemed()).toBe(0)
+
+ // Witness: the same request with a declared length is served.
+ const served = await post(proxy.port, completionBody(64, 'declared'))
+ expect(served.status).toBe(200)
+ expect(proxy.redeemed()).toBe(1)
+ } finally {
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-R3-2c-grok answers provider_unavailable once the admission queue is full', async () => {
+ // A small body limit keeps this cheap; the budget scales with it.
+ const maxBodyBytes = 16 * 1024
+ const proxy = await heldProxy(maxBodyBytes)
+ try {
+ const payload = (attempt: string) => completionBody(12_000, attempt)
+ const payloadBytes = Buffer.byteLength(payload('probe'))
+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)
+ expect((BUDGET_BODIES + 1) * payloadBytes).toBeGreaterThan(BUDGET_BODIES * maxBodyBytes)
+
+ const waiting = Array.from({ length: BUDGET_BODIES + STREAM_LIMITS.maxQueuedRequests }, (_, i) =>
+ post(proxy.port, payload(`queued-${i}`))
+ )
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+
+ const overflow = await post(proxy.port, payload('overflow'))
+ expect(overflow.status).toBe(503)
+ expect(JSON.parse(overflow.body)).toEqual({ error: 'provider_unavailable' })
+
+ // Liveness: every queued request is still served once the budget frees.
+ proxy.releaseAll()
+ const settled = await Promise.all(waiting)
+ expect(settled.every(reply => reply.status === 200)).toBe(true)
+ expect(proxy.redeemed()).toBe(BUDGET_BODIES + STREAM_LIMITS.maxQueuedRequests)
+ } finally {
+ proxy.releaseAll()
+ await proxy.close()
+ }
+ }, 60_000)
+
+ it('T-R9-2-grok drops a queued body whose client left, so the whole budget is free once the holders finish', async () => {
+ // A small body limit keeps this cheap; the budget scales with it. The read
+ // deadline is the production one (10 s), and every check below completes
+ // well inside it, so the deadline cannot reclaim a leaked reservation first.
+ const maxBodyBytes = 16 * 1024
+ const fillingBody = (attempt: string) => completionBody(12_000, attempt)
+ const proxy = await heldProxy(maxBodyBytes)
+ const holders = Array.from({ length: BUDGET_BODIES }, (_, i) =>
+ post(proxy.port, fillingBody(`holder-${i}`))
+ )
+ const refill: Array<Promise<Reply>> = []
+ const body = fillingBody('abandoned')
+ let abandoned: ReturnType<typeof stalledPost> | undefined
+ try {
+ const payloadBytes = Buffer.byteLength(body)
+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)
+ expect((BUDGET_BODIES + 1) * payloadBytes).toBeGreaterThan(BUDGET_BODIES * maxBodyBytes)
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+
+ // The holders fill the budget, so this body waits in the admission queue.
+ abandoned = stalledPost(proxy.port, {
+ declaredBytes: payloadBytes,
+ token: platformToken,
+ sent: body.slice(0, 1),
+ })
+ while (proxy.arrived() < BUDGET_BODIES + 1) await sleep(10)
+ await sleep(50)
+ abandoned.destroy()
+ await expect(abandoned.reply).rejects.toThrow('socket hang up')
+ await sleep(50)
+
+ proxy.releaseAll()
+ for (const reply of await Promise.all(holders)) expect(reply.status).toBe(200)
+
+ // Every reservation is back: bodies that fill the whole budget are all
+ // read and redeemed at once. A reservation granted to the departed
+ // waiter would leave the last of them queued until the read deadline.
+ proxy.hold()
+ for (let i = 0; i < BUDGET_BODIES; i += 1) refill.push(post(proxy.port, fillingBody(`refill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(2 * BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(refill)) {
+ expect(reply.status).toBe(200)
+ expect(reply.body).toContain('"outcome":"success"')
+ }
+ } finally {
+ abandoned?.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled([...holders, ...refill])
+ await proxy.close()
+ }
+ }, 30_000)
+})
+
+/**
+ * R9-M-B — a stalled body must not hold the budget. The platform JWT is
+ * checked before a reservation is taken, and a reserved body that is not read
+ * within the read deadline is answered 408 and releases its reservation.
+ */
+describe('grok-llm-proxy authentication before admission and body-read deadline (R9-M-B)', () => {
+ // A small body limit keeps these cheap; the budget scales with it.
+ const maxBodyBytes = 16 * 1024
+ const fillingBody = (attempt: string) => completionBody(12_000, attempt)
+
+ it('T-MB-0-grok fixes the production body-read deadline at 10 000 ms', () => {
+ expect(BODY_READ_DEADLINE_MS).toBe(10_000)
+ })
+
+ it('T-MB-1-grok answers anonymous stalled bodies 401 within 1 s without reserving the budget', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>
+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes })
+ )
+ const filling: Array<Promise<Reply>> = []
+ try {
+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {
+ expect(reply.status).toBe(401)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })
+ }
+ // Zero bytes reserved: authenticated bodies that fill the whole budget
+ // are all read and redeemed at once.
+ const payloadBytes = Buffer.byteLength(fillingBody('probe'))
+ expect(payloadBytes).toBeLessThanOrEqual(maxBodyBytes)
+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)
+ } finally {
+ for (const s of anonymous) s.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled(filling)
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-1-admin-grok answers anonymous stalled admin bodies 401 within 1 s without reserving the shared budget', async () => {
+ const deadlineMs = 1_000
+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })
+ const modelsPath = '/internal/admin/v1/grok/models'
+ const anonymous = Array.from({ length: BUDGET_BODIES }, () =>
+ stalledPost(proxy.adminPort, { declaredBytes: maxBodyBytes, path: modelsPath })
+ )
+ const permitted = stalledPost(proxy.adminPort, {
+ declaredBytes: maxBodyBytes,
+ path: modelsPath,
+ token: adminPermit,
+ })
+ const filling: Array<Promise<Reply>> = []
+ try {
+ for (const reply of await Promise.all(anonymous.map(s => within(s.reply, 1_000)))) {
+ expect(reply.status).toBe(401)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'Unauthorized' })
+ }
+ // Witness: a valid admin permit passes the gate and is then bounded by
+ // the read deadline, not refused by the gate.
+ const held = await within(permitted.reply, deadlineMs + 1_000)
+ expect(held.status).toBe(408)
+ expect(JSON.parse(held.body)).toEqual({ error: 'request_timeout' })
+ // The budget is shared with the runtime listener, and all of it is free.
+ for (let i = 0; i < BUDGET_BODIES; i += 1) filling.push(post(proxy.port, fillingBody(`fill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(filling)) expect(reply.status).toBe(200)
+ } finally {
+ for (const s of [...anonymous, permitted]) s.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled(filling)
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-2-grok answers an authenticated stalled body 408 request_timeout at the deadline and frees its reservation', async () => {
+ const deadlineMs = 1_000
+ const warn = vi.spyOn(logger, 'warn')
+ const error = vi.spyOn(logger, 'error')
+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })
+ proxy.releaseAll()
+ const stalled = Array.from({ length: BUDGET_BODIES }, () =>
+ stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })
+ )
+ const refill: Array<Promise<Reply>> = []
+ try {
+ while (proxy.arrived() < BUDGET_BODIES) await sleep(10)
+ await sleep(50)
+ // The stalled bodies hold the whole budget, so this one waits in the queue.
+ const queued = post(proxy.port, fillingBody('queued'))
+
+ for (const reply of await Promise.all(stalled.map(s => within(s.reply, deadlineMs + 1_000)))) {
+ expect(reply.status).toBe(408)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })
+ expect(reply.elapsedMs).toBeGreaterThanOrEqual(deadlineMs)
+ expect(reply.elapsedMs).toBeLessThan(deadlineMs + 1_000)
+ }
+ // Witness: the queued request is admitted once the reservations are freed.
+ const served = await queued
+ expect(served.status).toBe(200)
+ expect(served.body).toContain('"outcome":"success"')
+ expect(proxy.redeemed()).toBe(1)
+ expect(warn).toHaveBeenCalledWith(
+ { event: 'grok_proxy_denied', code: 'request_timeout' },
+ 'request denied'
+ )
+ expect(error).not.toHaveBeenCalledWith(
+ expect.objectContaining({ event: 'grok_proxy_error' }),
+ expect.anything()
+ )
+
+ // The budget is back to zero: bodies that fill all of it are admitted at once.
+ proxy.hold()
+ for (let i = 0; i < BUDGET_BODIES; i += 1) refill.push(post(proxy.port, fillingBody(`refill-${i}`)))
+ expect(await settle(proxy.redeemed)).toBe(1 + BUDGET_BODIES)
+ proxy.releaseAll()
+ for (const reply of await Promise.all(refill)) expect(reply.status).toBe(200)
+ } finally {
+ for (const s of stalled) s.destroy()
+ proxy.releaseAll()
+ await Promise.allSettled(refill)
+ await proxy.close()
+ warn.mockRestore()
+ error.mockRestore()
+ }
+ }, 30_000)
+
+ it('T-MB-2d-grok arms BODY_READ_DEADLINE_MS when no deadline is injected', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
+ const stalled = stalledPost(proxy.port, { declaredBytes: maxBodyBytes, token: platformToken })
+ try {
+ while (proxy.arrived() < 1) await sleep(10)
+ await sleep(50)
+ vi.advanceTimersByTime(BODY_READ_DEADLINE_MS - 1)
+ expect((await within(stalled.reply, 200)).status).toBe(0)
+ vi.advanceTimersByTime(1)
+ const reply = await within(stalled.reply, 2_000)
+ expect(reply.status).toBe(408)
+ expect(JSON.parse(reply.body)).toEqual({ error: 'request_timeout' })
+ } finally {
+ vi.useRealTimers()
+ stalled.destroy()
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-3-grok starts the deadline at grant, so a body queued past it is still served', async () => {
+ const deadlineMs = 500
+ const proxy = await heldProxy(maxBodyBytes, { bodyReadDeadlineMs: deadlineMs })
+ const holders = Array.from({ length: BUDGET_BODIES }, (_, i) =>
+ post(proxy.port, fillingBody(`holder-${i}`))
+ )
+ // The queued body sends one byte and holds the rest until half a deadline
+ // after the budget frees, so it can only be served if the clock started at
+ // grant. A fully buffered body would be parsed before any timer fired.
+ const body = fillingBody('queued')
+ let queued: ReturnType<typeof stalledPost> | undefined
+ try {
+ expect(await settle(proxy.redeemed)).toBe(BUDGET_BODIES)
+ queued = stalledPost(proxy.port, {
+ declaredBytes: Buffer.byteLength(body),
+ token: platformToken,
+ sent: body.slice(0, 1),
+ })
+ await sleep(3 * deadlineMs)
+ // Witness: the body really waited in the queue for longer than the deadline.
+ expect(proxy.redeemed()).toBe(BUDGET_BODIES)
+ proxy.releaseAll()
+ await sleep(deadlineMs / 2)
+ queued.finish(body.slice(1))
+ const served = await within(queued.reply, 5_000)
+ expect(served.status).toBe(200)
+ expect(served.body).toContain('"outcome":"success"')
+ expect(proxy.redeemed()).toBe(BUDGET_BODIES + 1)
+ for (const reply of await Promise.all(holders)) expect(reply.status).toBe(200)
+ } finally {
+ proxy.releaseAll()
+ queued?.destroy()
+ await Promise.allSettled(holders)
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-MB-4-grok answers an unauthenticated oversize body 401 without parsing it', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ proxy.releaseAll()
+ const anonymous = stalledPost(proxy.port, { declaredBytes: maxBodyBytes + 1 })
+ try {
+ const refused = await within(anonymous.reply, 1_000)
+ expect(refused.status).toBe(401)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'Unauthorized' })
+ // Witness: an oversize body with a platform JWT reaches the size check.
+ const oversizePayload = completionBody(20_000, 'oversize')
+ expect(Buffer.byteLength(oversizePayload)).toBeGreaterThan(maxBodyBytes)
+ const oversize = await post(proxy.port, oversizePayload)
+ expect(oversize.status).toBe(413)
+ expect(JSON.parse(oversize.body)).toEqual({ error: 'payload_too_large' })
+ expect(proxy.redeemed()).toBe(0)
+ } finally {
+ anonymous.destroy()
+ await proxy.close()
+ }
+ }, 30_000)
+})
+
+/**
+ * POST `body` with the given `Content-Encoding`. Keep-alive, so a reply sent
+ * before the body is read is not raced by the client's own write.
+ */
+function postEncoded(
+ port: number,
+ options: { path: string; token: string; body: Buffer; encoding: string }
+): Promise<Reply> {
+ return new Promise((resolve, reject) => {
+ const req = httpRequest(
+ {
+ host: '127.0.0.1',
+ port,
+ method: 'POST',
+ path: options.path,
+ agent: false,
+ headers: {
+ connection: 'keep-alive',
+ 'content-type': 'application/json',
+ 'content-encoding': options.encoding,
+ 'content-length': options.body.length,
+ authorization: `Bearer ${options.token}`,
+ },
+ },
+ res => {
+ const chunks: Buffer[] = []
+ res.on('data', chunk => chunks.push(chunk as Buffer))
+ res.on('end', () => {
+ req.destroy()
+ resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString('utf8') })
+ })
+ res.on('error', reject)
+ }
+ )
+ req.on('error', reject)
+ req.end(options.body)
+ })
+}
+
+/**
+ * R9-1 — the byte budget reserves the declared wire length, so a body the
+ * parser inflated could hold many times its reservation. Every parser refuses
+ * an encoded body before inflating it.
+ */
+describe('grok-llm-proxy encoded bodies (R9-1)', () => {
+ // A small body limit keeps these cheap; the budget scales with it.
+ const maxBodyBytes = 16 * 1024
+
+ it('T-R9-1a-grok refuses a gzip completion body that decodes past its reservation with 415', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ proxy.releaseAll()
+ try {
+ const plain = completionBody(12_000, 'gzip')
+ const gzipped = gzipSync(plain)
+ // Fixture check: the wire length reserved is a small fraction of what
+ // the parser would hold after inflating it, and inflated it fits the cap.
+ expect(10 * gzipped.length).toBeLessThan(Buffer.byteLength(plain))
+ expect(Buffer.byteLength(plain)).toBeLessThanOrEqual(maxBodyBytes)
+ const refused = await postEncoded(proxy.port, {
+ path: COMPLETIONS_PATH,
+ token: platformToken,
+ body: gzipped,
+ encoding: 'gzip',
+ })
+ expect(refused.status).toBe(415)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'unsupported_media_type' })
+ expect(proxy.redeemed()).toBe(0)
+ // Witness: the same request unencoded is read, redeemed and served.
+ const served = await post(proxy.port, completionBody(12_000, 'plain'))
+ expect(served.status).toBe(200)
+ expect(proxy.redeemed()).toBe(1)
+ } finally {
+ await proxy.close()
+ }
+ }, 30_000)
+
+ it('T-R9-1c-grok refuses a gzip admin body with 415', async () => {
+ const proxy = await heldProxy(maxBodyBytes)
+ try {
+ const gzipped = gzipSync(JSON.stringify({ accessToken: 'x'.repeat(8_000) }))
+ expect(10 * gzipped.length).toBeLessThan(8_000)
+ const refused = await postEncoded(proxy.adminPort, {
+ path: '/internal/admin/v1/grok/models',
+ token: adminPermit,
+ body: gzipped,
+ encoding: 'gzip',
+ })
+ expect(refused.status).toBe(415)
+ expect(JSON.parse(refused.body)).toEqual({ error: 'unsupported_media_type' })
+ } finally {
+ await proxy.close()
+ }
+ }, 30_000)
+})
diff --cc grok-llm-proxy/test/grokTransport.conformance.test.ts
index f0497a6e4,d3480f515..b9172f73b
--- a/grok-llm-proxy/test/grokTransport.conformance.test.ts
+++ b/grok-llm-proxy/test/grokTransport.conformance.test.ts
@@@ -575,65 -575,6 +584,66 @@@ describe('streamGrokCompletion', () =>
)
})
+ // R9-5 (L-8): the budget is taken from a byte cap, so it counts UTF-8 bytes.
+ // 'é' is one UTF-16 code unit and two UTF-8 bytes: these arguments are about
+ // half the budget in code units and two bytes over it in UTF-8.
+ it('T-R9-5 counts retained tool-call arguments in UTF-8 bytes', async () => {
+ const envelope = '{"q":""}'
+ const full = `{"q":"${'é'.repeat((MAX_TOOL_CALL_ARGUMENT_BYTES - envelope.length) / 2 + 1)}"}`
+ expect(full.length).toBeLessThan(MAX_TOOL_CALL_ARGUMENT_BYTES)
+ expect(Buffer.byteLength(full, 'utf8')).toBe(MAX_TOOL_CALL_ARGUMENT_BYTES + 2)
+ const deltas: string[] = []
+ for (let offset = 0; offset < full.length; offset += ARGUMENT_CHUNK_CHARS) {
+ deltas.push(full.slice(offset, offset + ARGUMENT_CHUNK_CHARS))
+ }
+ const emitted: StreamFrame[] = []
+ const finalize = vi.fn(async () => ({
+ providerAttemptId: 'att-args-utf8',
+ outcome: 'success' as const,
+ duplicate: false,
+ }))
+ const frames = [
+ `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'before' })}\n\n`,
+ `data: ${JSON.stringify({
+ type: 'response.output_item.added',
+ item: { type: 'function_call', id: 'call-args', name: 'lookup', arguments: '' },
+ })}\n\n`,
+ ...argumentFrames(deltas),
+ `data: ${JSON.stringify({ type: 'response.output_text.delta', delta: 'after' })}\n\n`,
+ `data: ${JSON.stringify({ type: 'response.completed', response: { usage: {} } })}\n\n`,
+ ]
+ const pending = streamGrokCompletion({
+ executionTicket: 'ticket-args-utf8',
+ requestHash: REQUEST_HASH,
+ request: REQUEST,
+ ticket: {
+ jti: 'jti-args-utf8',
+ hostRef: 'research-host',
+ model: REQUEST.model,
+ requestHash: REQUEST_HASH,
+ providerAttemptId: 'att-args-utf8',
+ },
++ maxDeadlineMs: 300_000,
+ redeem: vi.fn(async () => redeemSuccess()),
+ finalize,
+ fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => chunkedSseResponse(frames)),
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ onFrame: frame => {
+ emitted.push(frame)
+ },
+ })
+ await expect(pending).rejects.toMatchObject({
+ code: 'tool_call_arguments_exceeded',
+ details: { limit: MAX_TOOL_CALL_ARGUMENT_BYTES, observed: MAX_TOOL_CALL_ARGUMENT_BYTES + 2 },
+ })
+ expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
+ // Liveness witness: the leading text proves the stream was read.
+ expect(emitted.filter(frame => frame.type === 'text')).toEqual([{ type: 'text', text: 'before' }])
+ expect(finalize).toHaveBeenCalledWith(
+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
+ )
+ })
+
it('validates ticket bindings before redeem and maps stream frames including tool-call data', async () => {
const redeem = vi.fn(async () => redeemSuccess())
const finalize = vi.fn(async () => ({
@@@ -1387,115 -1418,6 +1487,116 @@@
expect(finalize.mock.calls[0]?.[0]?.receipt?.usage).toBeUndefined()
})
+ // R10 (M1): the Grok upstream refuses a prompt over the model's context window
+ // with an HTTP 400 before any stream starts. This body is the one recorded on
+ // 2026-09-23 against cli-chat-proxy.grok.com/v1/responses with grok-4.6: the
+ // `code` field is the generic `invalid-argument`, and the only specific marker
+ // is the bracketed `[input_too_large]` inside the `error` string.
+ describe('non-success upstream bodies (R10)', () => {
+ const recordedOverflowBody = JSON.stringify({
+ code: 'invalid-argument',
+ error:
+ "Failed to start sampling: [input_too_large] The prompt is too long for this model's context window (1107771 tokens > 500000 tokens)",
+ })
+
+ function streamWith(response: () => Response) {
+ const finalize = vi.fn(
+ async (_input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
+ providerAttemptId: 'att-1',
+ outcome: 'error' as const,
+ duplicate: false,
+ })
+ )
+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => response())
+ const pending = streamGrokCompletion({
+ 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',
+ },
++ maxDeadlineMs: 300_000,
+ redeem: async () => redeemSuccess(),
+ finalize,
+ fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ })
+ return { pending, finalize, fetchFn }
+ }
+
+ it('T-R10-1a maps the recorded HTTP 400 input_too_large body to context_length_exceeded', async () => {
+ const { pending, finalize, fetchFn } = streamWith(
+ () =>
+ new Response(recordedOverflowBody, {
+ status: 400,
+ headers: { 'content-type': 'application/json' },
+ })
+ )
+ await expect(pending).rejects.toMatchObject({ code: 'context_length_exceeded' })
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(finalize).toHaveBeenCalledWith(
+ expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
+ )
+ })
+
+ it('T-R10-1b keeps the status mapping for every other error body', async () => {
+ const other = JSON.stringify({ code: 'invalid-argument', error: 'unsupported parameter' })
+ await expect(
+ streamWith(() => new Response(other, { status: 400 })).pending
+ ).rejects.toMatchObject({ code: 'invalid_request' })
+ await expect(
+ streamWith(() => new Response('bad request', { status: 400 })).pending
+ ).rejects.toMatchObject({ code: 'invalid_request' })
+ await expect(
+ streamWith(() => new Response('{"error":', { status: 400 })).pending
+ ).rejects.toMatchObject({ code: 'invalid_request' })
+ // The marker counts only inside the `error` string, not anywhere in the body.
+ await expect(
+ streamWith(
+ () =>
+ new Response(JSON.stringify({ code: '[input_too_large]', error: 'other' }), {
+ status: 400,
+ })
+ ).pending
+ ).rejects.toMatchObject({ code: 'invalid_request' })
+ await expect(
+ streamWith(() => new Response(recordedOverflowBody, { status: 401 })).pending
+ ).rejects.toMatchObject({ code: 'connection_unavailable' })
+ await expect(
+ streamWith(() => new Response(other, { status: 503 })).pending
+ ).rejects.toMatchObject({ code: 'provider_unavailable' })
+ })
+
+ it('T-R10-1c reads a bounded prefix of an endless error body and keeps the status mapping', async () => {
+ const chunk = new TextEncoder().encode(`{"pad":"${'x'.repeat(1024)}`)
+ let pulled = 0
+ let canceled = false
+ // highWaterMark 0: nothing is pulled until a reader asks for it.
+ const endless = new ReadableStream<Uint8Array>(
+ {
+ pull(controller) {
+ pulled += chunk.byteLength
+ controller.enqueue(chunk)
+ },
+ cancel() {
+ canceled = true
+ },
+ },
+ { highWaterMark: 0 }
+ )
+ const { pending } = streamWith(() => new Response(endless, { status: 400 }))
+ await expect(pending).rejects.toMatchObject({ code: 'invalid_request' })
+ // Witness: the body was read, and the read stopped at a bound.
+ expect(pulled).toBeGreaterThan(0)
+ expect(canceled).toBe(true)
+ expect(pulled).toBeLessThan(1024 * 1024)
+ })
+ })
+
it('emits a tool call only after argument deltas complete', async () => {
const frames: unknown[] = []
const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) =>
@@@ -1534,263 -1457,167 +1636,427 @@@
{ type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },
])
})
+
+ describe('malformed tool-call arguments fail closed', () => {
+ const sse = (event: Record<string, unknown>) => `data: ${JSON.stringify(event)}\n\n`
+ const textBefore = sse({ type: 'response.output_text.delta', delta: 'before' })
+ const completed = sse({ type: 'response.completed', response: { usage: {} } })
+ const openCall = sse({
+ type: 'response.output_item.added',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: '',
+ },
+ })
+ const truncatedDelta = sse({
+ type: 'response.function_call_arguments.delta',
+ item_id: 'item-1',
+ delta: '{"q":',
+ })
+
+ async function runUpstream(events: string[], options: { abortOnFrame?: boolean } = {}) {
+ const frames: Array<{ type: string }> = []
+ const abort = new AbortController()
+ const fetchFn = vi.fn(async (_url: FetchInput, _init?: RequestInit) => sseResponse(events))
+ const finalize = vi.fn(
+ async (input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
+ providerAttemptId: 'att-1',
+ outcome: input.receipt.outcome,
+ duplicate: false,
+ })
+ )
+ const settled = await streamGrokCompletion({
+ 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',
+ },
+ signal: abort.signal,
++ maxDeadlineMs: 300_000,
+ redeem: async () => redeemSuccess(),
+ finalize,
+ fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ onFrame: frame => {
+ frames.push(frame)
+ if (options.abortOnFrame) abort.abort()
+ },
+ }).then(
+ result => ({ rejected: false as const, result }),
+ (error: unknown) => ({ rejected: true as const, error })
+ )
+ return { settled, frames, fetchFn, finalize }
+ }
+
+ async function expectRefused(events: string[]) {
+ const { settled, frames, fetchFn, finalize } = await runUpstream(events)
+ // Liveness: the upstream was called and its stream was consumed far enough
+ // to deliver the text that precedes the call. Without these, "no tool_call
+ // frame" below would also hold for a transport that never ran.
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(frames).toContainEqual({ type: 'text', text: 'before' })
+ // The defect: the malformed call became an executable call with `{}`.
+ expect(frames.filter(frame => frame.type === 'tool_call')).toEqual([])
+ expect(settled).toMatchObject({
+ rejected: true,
+ error: { name: 'GrokTransportError', code: 'invalid_tool_arguments' },
+ })
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('error')
+ }
+
+ it.each([
+ ['truncated JSON', '{"q":'],
+ ['a JSON value that is not an object', '[1,2]'],
+ ])(
+ 'refuses a function_call closed by response.output_item.done with %s as arguments',
+ async (_label, rawArguments) => {
+ await expectRefused([
+ textBefore,
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ completed,
+ ])
+ }
+ )
+
+ it('refuses a call whose argument deltas never complete before the stream ends', async () => {
+ await expectRefused([textBefore, openCall, truncatedDelta, completed])
+ })
+
+ // A closed call is the whole call, so empty `arguments` there mean "no
+ // parameters", not "truncated". An unclosed call gives no such guarantee.
+ async function expectAcceptedWithoutParameters(events: string[]) {
+ const { settled, frames, fetchFn, finalize } = await runUpstream(events)
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(settled).toMatchObject({ rejected: false, result: { outcome: 'success' } })
+ // Exactly one call, with no parameters: the positive frame is the witness.
+ expect(frames).toEqual([
+ { type: 'text', text: 'before' },
+ { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: {} },
+ ])
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('success')
+ }
+
+ it.each([
+ ['an empty string', ''],
+ ['a whitespace-only string', ' \n '],
+ ])(
+ 'accepts a function_call closed by response.output_item.done with %s as arguments as a call with no parameters',
+ async (_label, rawArguments) => {
+ await expectAcceptedWithoutParameters([
+ textBefore,
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ completed,
+ ])
+ }
+ )
+
+ it('accepts a call opened with empty arguments and closed by response.function_call_arguments.done with none', async () => {
+ await expectAcceptedWithoutParameters([
+ textBefore,
+ openCall,
+ sse({ type: 'response.function_call_arguments.done', item_id: 'item-1', arguments: '' }),
+ completed,
+ ])
+ })
+
+ it.each([
+ ['an empty string', ''],
+ ['a whitespace-only string', ' \n '],
+ ])(
+ 'refuses a call whose truncated deltas are closed by a done event with %s as arguments',
+ async (_label, rawArguments) => {
+ await expectRefused([
+ textBefore,
+ openCall,
+ truncatedDelta,
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ completed,
+ ])
+ }
+ )
+
+ // R9-9 (L-12): the positive twin of the case above. A blank close carries
+ // nothing new, so complete deltas survive it and the call runs with them.
+ const closeWith: Record<string, (rawArguments: string) => string> = {
+ 'response.output_item.done': rawArguments =>
+ sse({
+ type: 'response.output_item.done',
+ item: {
+ type: 'function_call',
+ id: 'item-1',
+ call_id: 'call-9',
+ name: 'lookup',
+ arguments: rawArguments,
+ },
+ }),
+ 'response.function_call_arguments.done': rawArguments =>
+ sse({
+ type: 'response.function_call_arguments.done',
+ item_id: 'item-1',
+ arguments: rawArguments,
+ }),
+ }
+ it.each([
+ ['response.output_item.done', 'an empty string', ''],
+ ['response.output_item.done', 'a whitespace-only string', ' \n '],
+ ['response.function_call_arguments.done', 'an empty string', ''],
+ ['response.function_call_arguments.done', 'a whitespace-only string', ' \n '],
+ ])(
+ 'T-R9-9a-grok keeps complete argument deltas when %s closes the call with %s',
+ async (event, _label, rawArguments) => {
+ const { settled, frames, fetchFn, finalize } = await runUpstream([
+ textBefore,
+ openCall,
+ truncatedDelta,
+ sse({ type: 'response.function_call_arguments.delta', item_id: 'item-1', delta: '"x"}' }),
+ closeWith[event]!(rawArguments),
+ completed,
+ ])
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(settled).toMatchObject({ rejected: false, result: { outcome: 'success' } })
+ expect(frames).toEqual([
+ { type: 'text', text: 'before' },
+ { type: 'tool_call', id: 'call-9', name: 'lookup', arguments: { q: 'x' } },
+ ])
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('success')
+ }
+ )
+
+ it('refuses a call opened with empty arguments and never closed before the stream ends', async () => {
+ await expectRefused([textBefore, openCall, completed])
+ })
+
+ // A canceled or failed stream also ends with its open call truncated. That
+ // truncation is a consequence of the stream's own outcome, so the outcome
+ // wins over the arguments refusal.
+ it('keeps a client cancel that lands mid-arguments as canceled', async () => {
+ const { settled, frames, fetchFn, finalize } = await runUpstream(
+ [openCall, truncatedDelta, textBefore],
+ { abortOnFrame: true }
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(frames).toEqual([{ type: 'text', text: 'before' }])
+ expect(settled).toMatchObject({ rejected: false, result: { outcome: 'canceled' } })
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('canceled')
+ })
+
+ it('reports an upstream failure that lands mid-arguments as provider_unavailable', async () => {
+ const { settled, frames, fetchFn, finalize } = await runUpstream([
+ textBefore,
+ openCall,
+ truncatedDelta,
+ sse({ type: 'response.failed', response: {} }),
+ ])
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(frames).toEqual([{ type: 'text', text: 'before' }])
+ expect(settled).toMatchObject({
+ rejected: true,
+ error: { name: 'GrokTransportError', code: 'provider_unavailable' },
+ })
+ expect(finalize).toHaveBeenCalledTimes(1)
+ expect(finalize.mock.calls[0]?.[0]?.receipt.outcome).toBe('error')
+ })
+ })
})
+
+ const TEXT_DELTA = 'data: {"type":"response.output_text.delta","delta":"x"}\n\n'
+ const COMPLETED = 'data: {"type":"response.completed","response":{"usage":{}}}\n\n'
+
+ /**
+ * An upstream body that ignores `init.signal`, like a socket that stays open
+ * while sending nothing. Only a transport that races each read against its own
+ * timers can end an attempt reading from it.
+ */
+ function upstreamBody(chunks: Array<{ afterMs: number; text: string } | 'stall'>): {
+ fetchFn: typeof fetch
+ cancel: ReturnType<typeof vi.fn>
+ } {
+ const encoder = new TextEncoder()
+ const cancel = vi.fn()
+ const fetchFn = vi.fn(async () => {
+ let index = 0
+ const body = new ReadableStream<Uint8Array>(
+ {
+ async pull(controller) {
+ const next = chunks[index]
+ index += 1
+ if (next === undefined) {
+ controller.close()
+ return
+ }
+ if (next === 'stall') {
+ await new Promise<never>(() => undefined)
+ return
+ }
+ await new Promise(resolve => setTimeout(resolve, next.afterMs))
+ controller.enqueue(encoder.encode(next.text))
+ },
+ cancel,
+ },
+ { highWaterMark: 0 }
+ )
+ return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream' } })
+ }) as unknown as typeof fetch
+ return { fetchFn, cancel }
+ }
+
+ function attemptInput(
+ overrides: Partial<StreamGrokCompletionInput>
+ ): StreamGrokCompletionInput & { finalize: ReturnType<typeof vi.fn> } {
+ const finalize = vi.fn(async (_input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
+ providerAttemptId: 'att-timeouts',
+ outcome: 'error' as const,
+ duplicate: false,
+ }))
+ return {
+ executionTicket: 'ticket-timeouts',
+ requestHash: REQUEST_HASH,
+ request: REQUEST,
+ ticket: {
+ jti: 'jti-timeouts',
+ hostRef: 'research-host',
+ model: REQUEST.model,
+ requestHash: REQUEST_HASH,
+ providerAttemptId: 'att-timeouts',
+ },
+ maxDeadlineMs: 300_000,
+ redeem: async () => redeemSuccess(),
+ finalize,
+ fetchFn: upstreamBody([]).fetchFn,
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ ...overrides,
+ } as StreamGrokCompletionInput & { finalize: ReturnType<typeof vi.fn> }
+ }
+
+ function finalizedOutcome(finalize: ReturnType<typeof vi.fn>): unknown {
+ expect(finalize).toHaveBeenCalledTimes(1)
+ return (finalize.mock.calls[0]?.[0] as { receipt: { outcome: string } }).receipt.outcome
+ }
+
+ describe('streamGrokCompletion upstream timeouts', () => {
+ it('ends a stalled upstream stream at the idle timeout with a typed provider_unavailable', async () => {
+ const upstream = upstreamBody([{ afterMs: 0, text: TEXT_DELTA }, 'stall'])
+ const input = attemptInput({ fetchFn: upstream.fetchFn, upstreamIdleTimeoutMs: 50 })
+ const frames: unknown[] = []
+ const error = await streamGrokCompletion({ ...input, onFrame: frame => void frames.push(frame) }).then(
+ () => undefined,
+ (err: unknown) => err
+ )
+ expect(frames).toEqual([{ type: 'text', text: 'x' }])
+ expect(error).toBeInstanceOf(GrokTransportError)
+ expect(error).toMatchObject({
+ code: 'provider_unavailable',
+ message: 'upstream stream idle timeout',
+ details: { idleTimeoutMs: 50 },
+ })
+ expect(finalizedOutcome(input.finalize)).toBe('error')
+ expect(upstream.cancel).toHaveBeenCalledTimes(1)
+ }, 2_000)
+
+ it('keeps the attempt alive while upstream chunks arrive inside the idle window', async () => {
+ const chunks = Array.from({ length: 6 }, () => ({ afterMs: 30, text: TEXT_DELTA }))
+ const upstream = upstreamBody([...chunks, { afterMs: 30, text: COMPLETED }])
+ const input = attemptInput({ fetchFn: upstream.fetchFn, upstreamIdleTimeoutMs: 50 })
+ const result = await streamGrokCompletion(input)
+ expect(result.outcome).toBe('success')
+ expect(finalizedOutcome(input.finalize)).toBe('success')
+ }, 2_000)
+
+ it('ends a still-active upstream stream at the total cap with stream_duration_exceeded', async () => {
+ const endless = Array.from({ length: 200 }, () => ({ afterMs: 10, text: TEXT_DELTA }))
+ const upstream = upstreamBody(endless)
+ const input = attemptInput({
+ fetchFn: upstream.fetchFn,
+ maxDeadlineMs: 150,
+ upstreamIdleTimeoutMs: 1_000,
+ })
+ const error = await streamGrokCompletion(input).then(
+ () => undefined,
+ (err: unknown) => err
+ )
+ expect(error).toBeInstanceOf(GrokTransportError)
+ expect(error).toMatchObject({
+ code: 'stream_duration_exceeded',
+ message: 'upstream stream exceeded maxStreamDurationMs',
+ details: { limitMs: 150 },
+ })
+ expect(finalizedOutcome(input.finalize)).toBe('error')
+ }, 2_000)
+
+ it('reports a client abort during a stalled stream as canceled, not as a timeout', async () => {
+ const upstream = upstreamBody([{ afterMs: 0, text: TEXT_DELTA }, 'stall'])
+ const abort = new AbortController()
+ const input = attemptInput({
+ fetchFn: upstream.fetchFn,
+ signal: abort.signal,
+ maxDeadlineMs: 1_000,
+ upstreamIdleTimeoutMs: 1_000,
+ })
+ setTimeout(() => abort.abort(), 50)
+ const result = await streamGrokCompletion(input)
+ expect(result.outcome).toBe('canceled')
+ expect(finalizedOutcome(input.finalize)).toBe('canceled')
+ expect(upstream.cancel).toHaveBeenCalledTimes(1)
+ }, 2_000)
+ })
+
+ describe('streamGrokCompletion deadline validation', () => {
+ // The redeem consumes the single-use ticket, so a deadline that cannot be
+ // served must be refused before it, while there is no receipt to finalize.
+ it('rejects an invalid deadline before redeeming the ticket', async () => {
+ const redeem = vi.fn(async () => redeemSuccess())
+ const fetchFn = vi.fn()
+ const input = attemptInput({
+ deadlineMs: 0,
+ redeem,
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ // Witness: the rejection names the deadline check, so the path ran.
+ await expect(streamGrokCompletion(input)).rejects.toMatchObject({
+ name: 'RequestLimitError',
+ message: 'deadline is invalid',
+ })
+ expect(redeem).not.toHaveBeenCalled()
+ expect(input.finalize).not.toHaveBeenCalled()
+ expect(fetchFn).not.toHaveBeenCalled()
+ })
+ })
diff --cc grok-llm-proxy/test/runtimePath.hermetic.e2e.test.ts
index 92162a9ba,8ea677f32..32ed06c18
--- a/grok-llm-proxy/test/runtimePath.hermetic.e2e.test.ts
+++ b/grok-llm-proxy/test/runtimePath.hermetic.e2e.test.ts
@@@ -76,9 -75,11 +76,11 @@@ function config(overrides: Partial<Grok
runtimePort: 0,
adminPort: 0,
probePort: 0,
- maxBodyBytes: 1_048_576,
+ maxBodyBytes: DEFAULT_MAX_BODY_BYTES,
maxStreamDurationMs: 30_000,
maxDeadlineMs: 30_000,
+ upstreamIdleTimeoutMs: 600_000,
+ heartbeatIntervalMs: 15_000,
jwtIssuer: 'control-api',
jwtPublicKey: publicKey,
executionEnabled: true,
diff --cc grok-llm-proxy/test/server.security.test.ts
index 6d03838b6,46a2a36d3..de67dd4d8
--- a/grok-llm-proxy/test/server.security.test.ts
+++ b/grok-llm-proxy/test/server.security.test.ts
@@@ -17,10 -15,10 +17,10 @@@ import
type FinalizeAttemptSuccess,
type RedeemAttemptSuccess,
} from '../src/controlApiClient.js'
-import { MAX_TOOL_CALL_ARGUMENT_CHARS } from '../src/grokTransport.js'
+import { MAX_TOOL_CALL_ARGUMENT_BYTES } from '../src/grokTransport.js'
import { REDACT_PATHS, logger } from '../src/logger.js'
import { GROK_CATALOG_ORIGIN, GROK_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'
- import { ENVELOPE_ALLOWANCE_BYTES } from '../src/requestLimits.js'
-import { RequestLimitError, streamGate } from '../src/requestLimits.js'
++import { ENVELOPE_ALLOWANCE_BYTES, RequestLimitError, streamGate } from '../src/requestLimits.js'
import { createProxyApps } from '../src/server.js'
const { privateKey, publicKey } = generateKeyPairSync('rsa', {
diff --cc grok-llm-proxy/test/sseBackpressure.test.ts
index 1e3fe96fa,656c33e85..88c6e11b3
--- a/grok-llm-proxy/test/sseBackpressure.test.ts
+++ b/grok-llm-proxy/test/sseBackpressure.test.ts
@@@ -90,9 -89,11 +90,11 @@@ function config(): GrokLlmProxyConfig
runtimePort: 0,
adminPort: 0,
probePort: 0,
- maxBodyBytes: 1_048_576,
+ maxBodyBytes: DEFAULT_MAX_BODY_BYTES,
maxStreamDurationMs: 60_000,
maxDeadlineMs: 60_000,
+ upstreamIdleTimeoutMs: 600_000,
+ heartbeatIntervalMs: 15_000,
jwtIssuer: 'control-api',
jwtPublicKey: publicKey,
executionEnabled: true,
diff --cc mcp-host/package-lock.json
index fdfe69f7a,7920f127f..912a54162
--- 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.430",
- "version": "0.1.370",
++ "version": "0.1.431",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.430",
- "version": "0.1.370",
++ "version": "0.1.431",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --cc mcp-host/package.json
index e811fda3d,fbb6fd5d6..9cd3124fb
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@@ -1,6 -1,6 +1,6 @@@
{
"name": "clerum-mcp-host",
- "version": "0.1.430",
- "version": "0.1.370",
++ "version": "0.1.431",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --cc mcp-host/src/agent/__tests__/taskExecutor.test.ts
index 0b9028a03,7bb7bccbe..dcca41f40
--- a/mcp-host/src/agent/__tests__/taskExecutor.test.ts
+++ b/mcp-host/src/agent/__tests__/taskExecutor.test.ts
@@@ -1379,14 -1376,9 +1379,15 @@@ describe('TaskExecutor error handling'
it.each([
[LlmErrorCode.ToolCallLimitExceeded, 'LLM_TOOL_CALL_LIMIT_EXCEEDED', false],
+ [LlmErrorCode.StreamDurationExceeded, 'LLM_STREAM_DURATION_EXCEEDED', false],
// Witness: the same path keeps an existing provider code unchanged.
[LlmErrorCode.ModelOverloaded, 'LLM_MODEL_OVERLOADED', true],
+ // The code a size refusal now carries. `codexSubscription.ts` raises
+ // `request_limit_exceeded` before authorization and `classifyError` maps it
+ // to this; J2 stops at that boundary, so this case is what pins the last
+ // hop into the task failure the Desktop renders as "Conversation Too Long".
+ // Not retryable: retrying an oversized request reproduces it (#731).
+ [LlmErrorCode.ContextLengthExceeded, 'LLM_CONTEXT_LENGTH_EXCEEDED', false],
] as const)(
'keeps %s from a loop error result as the task error code',
async (code, expected, retryable) => {
diff --cc mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
index bf35b72d0,a8e972809..e7ff65fd3
--- a/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/codexLlmProxyClient.test.ts
@@@ -181,141 -181,101 +181,234 @@@ describe('CodexLlmProxyClient', () =>
expect(classified.code).toBe(LlmErrorCode.ToolCallLimitExceeded)
expect(classified.retryable).toBe(false)
expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
+ // #731 — the proxy's body parser refuses an envelope over its limit with
+ // `reject(res, 413, 'payload_too_large')` (codex-llm-proxy/src/server.ts).
+ // That is a size refusal of this conversation, not a failed API call.
+ it('T-R2-6a classifies the proxy 413 payload_too_large as ContextLengthExceeded', async () => {
+ const fetchFn = vi.fn().mockResolvedValue({
+ ok: false,
+ status: 413,
+ json: async () => ({ error: 'payload_too_large' }),
+ })
+ const client = new CodexLlmProxyClient({
+ runtimeUrl: resolveCodexProxyRuntimeUrl(
+ 'http://codex-llm-proxy.control-plane.svc.cluster.local:8080'
+ ),
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ const err = await client
+ .stream({ executionTicket: 'ticket-123456', requestHash: 'a'.repeat(64), request: {} })
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toMatchObject({
+ code: 'payload_too_large',
+ message: 'Codex request is too large; use fewer or smaller images, or reduce context',
+ })
+
+ const classified = new CodexSubscriptionProvider('gpt-5.3-codex', {} as never).classifyError(
+ err
+ )
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ContextLengthExceeded,
+ retryable: false,
+ providerCode: 'payload_too_large',
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
+ // #731 — the upstream refuses a request over the model's context window with
+ // `context_length_exceeded`. The proxy forwards that code as a 400 before any
+ // frame, or as an SSE error frame after one. Retrying the same conversation
+ // cannot succeed, so it must not read as a transient outage.
+ it.each([
+ {
+ path: '400 JSON body before streaming',
+ response: {
+ ok: false,
+ status: 400,
+ json: async () => ({ error: 'context_length_exceeded' }),
+ },
+ message: 'proxy stream failed with 400 (context_length_exceeded)',
+ },
+ {
+ path: 'SSE error frame after a text frame',
+ response: {
+ ok: true,
+ body: sse([
+ { type: 'text', text: 'partial' },
+ { type: 'error', code: 'context_length_exceeded' },
+ ]),
+ },
+ message: 'proxy stream failed with context_length_exceeded',
+ },
+ ])(
+ 'T-R7-2d classifies the upstream context_length_exceeded from the $path as ContextLengthExceeded',
+ async ({ response, message }) => {
+ const fetchFn = vi.fn().mockResolvedValue(response)
+ const client = new CodexLlmProxyClient({
+ runtimeUrl: resolveCodexProxyRuntimeUrl(
+ 'http://codex-llm-proxy.control-plane.svc.cluster.local:8080'
+ ),
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ const err = await client
+ .stream({ executionTicket: 'ticket-123456', requestHash: 'a'.repeat(64), request: {} })
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(CodexProxyError)
+ expect(err).toMatchObject({ code: 'context_length_exceeded', message })
+
+ const classified = new CodexSubscriptionProvider('gpt-5.3-codex', {} as never).classifyError(
+ err
+ )
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ContextLengthExceeded,
+ retryable: false,
+ providerCode: 'context_length_exceeded',
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ }
+ )
+
+ // T-MB-5 — the proxy answers `408 { error: 'request_timeout' }` when the
+ // body upload overruns its read deadline. The body never reached the
+ // upstream, so this is not an outage: it must not enter failover cooldown.
+ it('T-MB-5a classifies the proxy 408 request_timeout as a non-retryable ApiCallFailed', async () => {
+ const fetchFn = vi.fn(async () => Response.json({ error: 'request_timeout' }, { status: 408 }))
+ const client = new CodexLlmProxyClient({
+ runtimeUrl: resolveCodexProxyRuntimeUrl(
+ 'http://codex-llm-proxy.control-plane.svc.cluster.local:8080'
+ ),
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ const err = await client
+ .stream({ executionTicket: 'ticket-123456', requestHash: 'a'.repeat(64), request: {} })
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(CodexProxyError)
+ expect(err).toMatchObject({
+ code: 'request_timeout',
+ message: 'proxy stream failed with 408 (request_timeout)',
+ })
+
+ const classified = new CodexSubscriptionProvider('gpt-5.3-codex', {} as never).classifyError(
+ err
+ )
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ApiCallFailed,
+ retryable: false,
+ providerCode: 'request_timeout',
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
})
+ // The proxy's total stream cap arrives as 504 before any frame, or as an SSE
+ // error frame after one. The attempt spent its whole budget, so the same
+ // request would spend it again elsewhere: no retry, no failover.
+ it.each([
+ {
+ path: '504 JSON body before streaming',
+ response: {
+ ok: false,
+ status: 504,
+ json: async () => ({ error: 'stream_duration_exceeded' }),
+ },
+ message: 'proxy stream failed with 504 (stream_duration_exceeded)',
+ },
+ {
+ path: 'SSE error frame after a text frame',
+ response: {
+ ok: true,
+ body: sse([
+ { type: 'text', text: 'partial' },
+ { type: 'error', code: 'stream_duration_exceeded' },
+ ]),
+ },
+ message: 'proxy stream failed with stream_duration_exceeded',
+ },
+ ])('surfaces stream_duration_exceeded from the $path', async ({ response, message }) => {
+ const fetchFn = vi.fn().mockResolvedValue(response)
+ const client = new CodexLlmProxyClient({
+ runtimeUrl: resolveCodexProxyRuntimeUrl(
+ 'http://codex-llm-proxy.control-plane.svc.cluster.local:8080'
+ ),
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ const err = await client
+ .stream({ executionTicket: 'ticket-123456', requestHash: 'a'.repeat(64), request: {} })
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(CodexProxyError)
+ expect(err).toMatchObject({ code: 'stream_duration_exceeded', message })
+
+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', {} as never)
+ const classified = provider.classifyError(err)
+ expect(classified.code).toBe(LlmErrorCode.StreamDurationExceeded)
+ expect(classified.retryable).toBe(false)
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ // Witness: the idle cut is an outage on the same classifier and does fail over.
+ const idle = provider.classifyError(
+ new CodexProxyError(
+ 'provider_unavailable',
+ 'proxy stream failed with 503 (provider_unavailable)'
+ )
+ )
+ expect(classifyFailoverClass(idle.code, idle.retryable)).toBe('provider_unavailable')
+ })
+
+ // The proxy writes `: keepalive` SSE comments while the upstream is silent,
+ // before and between data frames, and a comment can straddle two reads.
+ it('ignores proxy keepalive comments around and between data frames', async () => {
+ const encoder = new TextEncoder()
+ const chunks = [
+ ': keepalive\n\n: keep',
+ 'alive\n\n',
+ 'data: {"type":"text","text":"hel"}\n\n: keepalive\n\n',
+ 'data: {"type":"text","text":"lo"}\n\n',
+ ': keepalive\n\ndata: {"type":"done","outcome":"success"}\n\n',
+ ]
+ const fetchFn = vi.fn().mockResolvedValue({
+ ok: true,
+ body: new ReadableStream<Uint8Array>({
+ start(controller) {
+ for (const chunk of chunks) controller.enqueue(encoder.encode(chunk))
+ controller.close()
+ },
+ }),
+ })
+ const client = new CodexLlmProxyClient({
+ runtimeUrl: resolveCodexProxyRuntimeUrl('http://codex-llm-proxy:8080'),
+ readPlatformJwt: () => 'platform-jwt',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })
+ const result = await client.stream({
+ executionTicket: 'ticket-123456',
+ requestHash: 'a'.repeat(64),
+ request: { model: 'gpt-5.3-codex' },
+ })
+ expect(result.text).toBe('hello')
+ expect(result.toolCalls).toEqual([])
+ expect(result.outcome).toBe('success')
+ })
+
it('refuses a runtime URL that is not absolute', () => {
expect(
() =>
diff --cc mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
index b28f4fe1c,ade34df88..dd2ae83b9
--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
@@@ -187,151 -213,57 +213,202 @@@ describe('GrokLlmProxyClient', () =>
expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
})
+ // #731 — the proxy's body parser refuses an envelope over its limit with
+ // `reject(res, 413, 'payload_too_large')` (grok-llm-proxy/src/server.ts).
+ // That is a size refusal of this conversation, not a failed API call.
+ it('T-R2-6a-grok classifies the proxy 413 payload_too_large as ContextLengthExceeded', async () => {
+ const fetchFn = vi.fn().mockResolvedValue({
+ ok: false,
+ status: 413,
+ json: async () => ({ error: 'payload_too_large' }),
+ })
+ const err = await client(fetchFn)
+ .stream(STREAM_INPUT)
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect(err).toMatchObject({ code: 'payload_too_large' })
+
+ const classified = new GrokSubscriptionProvider('grok-4.6', {} as never).classifyError(err)
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ContextLengthExceeded,
+ retryable: false,
+ providerCode: 'payload_too_large',
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
+ // R10 (M1) — the Grok upstream refuses a prompt over the model's context
+ // window with an HTTP 400 whose `error` string carries `[input_too_large]`
+ // (recorded 2026-09-23). The proxy forwards it as a 400
+ // `context_length_exceeded`; the Host must read it as the conversation being
+ // too long, not as a failed API call.
+ it.each([
+ {
+ path: '400 JSON body before streaming',
+ response: {
+ ok: false,
+ status: 400,
+ json: async () => ({ error: 'context_length_exceeded' }),
+ },
+ message: 'proxy stream failed with 400 (context_length_exceeded)',
+ },
+ {
+ path: 'SSE error frame after a text frame',
+ response: {
+ ok: true,
+ body: sse([
+ { type: 'text', text: 'partial' },
+ { type: 'error', code: 'context_length_exceeded' },
+ ]),
+ },
+ message: 'proxy stream failed with context_length_exceeded',
+ },
+ ])(
+ 'T-R10-4 classifies the upstream context_length_exceeded from the $path as ContextLengthExceeded',
+ async ({ response, message }) => {
+ const fetchFn = vi.fn().mockResolvedValue(response)
+ const err = await client(fetchFn)
+ .stream(STREAM_INPUT)
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect(err).toMatchObject({ code: 'context_length_exceeded', message })
+
+ const classified = new GrokSubscriptionProvider('grok-4.6', {} as never).classifyError(err)
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ContextLengthExceeded,
+ retryable: false,
+ providerCode: 'context_length_exceeded',
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ }
+ )
+
+ // R9-7 (L-5) — a gateway in front of the proxy (nginx
+ // `client_max_body_size`) refuses an oversized body with an HTML 413 and no
+ // JSON code. It is the same size refusal, not a provider outage.
+ it('T-R9-7a reads an HTML 413 with no JSON code as payload_too_large', async () => {
+ const fetchFn = vi.fn(
+ async () =>
+ new Response('<html><body><h1>413 Request Entity Too Large</h1></body></html>', {
+ status: 413,
+ headers: { 'content-type': 'text/html' },
+ })
+ )
+ const err = await client(fetchFn)
+ .stream(STREAM_INPUT)
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect(err).toMatchObject({ code: 'payload_too_large' })
+
+ const classified = new GrokSubscriptionProvider('grok-4.6', {} as never).classifyError(err)
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ContextLengthExceeded,
+ retryable: false,
+ providerCode: 'payload_too_large',
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
+ it('T-R9-7b keeps the JSON code a 413 carries', async () => {
+ const fetchFn = vi.fn(async () => Response.json({ error: 'budget_denied' }, { status: 413 }))
+ await expect(client(fetchFn).stream(STREAM_INPUT)).rejects.toMatchObject({
+ name: 'GrokProxyError',
+ code: 'budget_denied',
+ message: 'proxy stream failed with 413 (budget_denied)',
+ })
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ })
+
+ // T-MB-5 — the proxy answers `408 { error: 'request_timeout' }` when the
+ // body upload overruns its read deadline. The body never reached the
+ // upstream, so this is not an outage: it must not enter failover cooldown.
+ it('T-MB-5b classifies the proxy 408 request_timeout as a non-retryable ApiCallFailed', async () => {
+ const fetchFn = vi.fn(async () => Response.json({ error: 'request_timeout' }, { status: 408 }))
+ const err = await client(fetchFn)
+ .stream(STREAM_INPUT)
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect(err).toMatchObject({
+ code: 'request_timeout',
+ message: 'proxy stream failed with 408 (request_timeout)',
+ })
+
+ const classified = new GrokSubscriptionProvider('grok-4.6', {} as never).classifyError(err)
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ApiCallFailed,
+ retryable: false,
+ providerCode: 'request_timeout',
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
+ // The proxy's total stream cap arrives as 504 before any frame, or as an SSE
+ // error frame after one. The attempt spent its whole budget, so the same
+ // request would spend it again elsewhere: no retry, no failover.
+ it.each([
+ {
+ path: '504 JSON body before streaming',
+ response: {
+ ok: false,
+ status: 504,
+ json: async () => ({ error: 'stream_duration_exceeded' }),
+ },
+ message: 'proxy stream failed with 504 (stream_duration_exceeded)',
+ },
+ {
+ path: 'SSE error frame after a text frame',
+ response: {
+ ok: true,
+ body: sse([
+ { type: 'text', text: 'partial' },
+ { type: 'error', code: 'stream_duration_exceeded' },
+ ]),
+ },
+ message: 'proxy stream failed with stream_duration_exceeded',
+ },
+ ])('surfaces stream_duration_exceeded from the $path', async ({ response, message }) => {
+ const fetchFn = vi.fn().mockResolvedValue(response)
+ const err = await client(fetchFn)
+ .stream(STREAM_INPUT)
+ .then(
+ () => undefined,
+ (e: unknown) => e
+ )
+ expect(fetchFn).toHaveBeenCalledTimes(1)
+ expect(err).toBeInstanceOf(GrokProxyError)
+ expect(err).toMatchObject({ code: 'stream_duration_exceeded', message })
+
+ const provider = new GrokSubscriptionProvider('grok-4.6', {} as never)
+ const classified = provider.classifyError(err)
+ expect(classified.code).toBe(LlmErrorCode.StreamDurationExceeded)
+ expect(classified.retryable).toBe(false)
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ // Witness: the idle cut is an outage on the same classifier and does fail over.
+ const idle = provider.classifyError(
+ new GrokProxyError(
+ 'provider_unavailable',
+ 'proxy stream failed with 503 (provider_unavailable)'
+ )
+ )
+ expect(classifyFailoverClass(idle.code, idle.retryable)).toBe('provider_unavailable')
+ })
+
it('fails closed when the proxy emits an SSE error frame after headers', async () => {
const fetchFn = vi.fn().mockResolvedValue({
ok: true,
diff --cc mcp-host/src/llm/codexSubscription.ts
index 06536535b,5d9305b19..1f98acf8b
--- a/mcp-host/src/llm/codexSubscription.ts
+++ b/mcp-host/src/llm/codexSubscription.ts
@@@ -375,39 -254,18 +375,50 @@@ export class CodexSubscriptionProvider
...(providerDispatched !== undefined ? { providerDispatched } : {}),
}
}
+ if (code === 'invalid_tool_arguments') {
+ // The proxy refused a tool call whose arguments are not a JSON object.
+ // Same reasoning as the limit above: the model output is invalid, and a
+ // retry or failover would re-run the turn on a rejected response.
+ return {
+ code: LlmErrorCode.InvalidResponse,
+ retryable: false,
+ message: err instanceof Error ? err.message : String(err),
+ providerCode: code,
+ ...(providerDispatched !== undefined ? { providerDispatched } : {}),
+ }
+ }
+ if (code === 'stream_duration_exceeded') {
+ // Not retryable: the proxy already spent the attempt's whole stream
+ // budget. A retry or a failover would spend it again on the same turn.
+ return {
+ code: LlmErrorCode.StreamDurationExceeded,
+ retryable: false,
+ message: err instanceof Error ? err.message : String(err),
+ providerCode: code,
+ ...(providerDispatched !== undefined ? { providerDispatched } : {}),
+ }
+ }
- if (code === 'request_limit_exceeded') {
+ if (code === CODEX_ATTACHMENT_TOO_LARGE) {
+ // An attached image broke a contract image budget. A shorter
+ // conversation cannot fix it and neither can another provider, so it is
+ // terminal; the message already names the limit for the user.
+ return {
+ code: LlmErrorCode.InvalidAttachment,
+ retryable: false,
+ message: err instanceof Error ? err.message : String(err),
+ providerCode: code,
+ ...(providerDispatched !== undefined ? { providerDispatched } : {}),
+ }
+ }
+ // `payload_too_large` is the proxy's 413 for an envelope over its body
+ // limit: the same size refusal of this conversation, one hop later.
+ // `context_length_exceeded` is the upstream's refusal of a request over the
+ // model's context window, forwarded by the proxy (#731).
+ if (
+ code === 'request_limit_exceeded' ||
+ code === 'payload_too_large' ||
+ code === 'context_length_exceeded'
+ ) {
return {
code: LlmErrorCode.ContextLengthExceeded,
retryable: false,
diff --cc mcp-host/src/llm/grokSubscription.ts
index 993146671,19309b1ba..781695e2c
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@@ -252,18 -197,17 +252,29 @@@ export class GrokSubscriptionProvider i
...(providerDispatched !== undefined ? { providerDispatched } : {}),
}
}
+ if (code === 'invalid_tool_arguments') {
+ // The proxy refused a tool call whose arguments are not a JSON object.
+ // Same reasoning as the limit above: the model output is invalid, and a
+ // retry or failover would re-run the turn on a rejected response.
+ return {
+ code: LlmErrorCode.InvalidResponse,
+ retryable: false,
+ message: err instanceof Error ? err.message : String(err),
+ providerCode: code,
+ ...(providerDispatched !== undefined ? { providerDispatched } : {}),
+ }
+ }
+ if (code === 'stream_duration_exceeded') {
+ // Not retryable: the proxy already spent the attempt's whole stream
+ // budget. A retry or a failover would spend it again on the same turn.
+ return {
+ code: LlmErrorCode.StreamDurationExceeded,
+ retryable: false,
+ message: err instanceof Error ? err.message : String(err),
+ providerCode: code,
+ ...(providerDispatched !== undefined ? { providerDispatched } : {}),
+ }
+ }
// Same family as `request_limit_exceeded`, from the response side: the
// transport refused a tool call whose arguments overran its size budget.
// Treating it as an outage would retry the identical oversized call and,
diff --cc packages/llm-provider-attempt-contract/index.test.cjs
index e232efccd,fba72280e..27c8a5e17
--- a/packages/llm-provider-attempt-contract/index.test.cjs
+++ b/packages/llm-provider-attempt-contract/index.test.cjs
@@@ -46,17 -46,36 +46,47 @@@ test('runtime exports stay aligned wit
assert.deepEqual(Object.keys(contract).sort(), declared)
})
-// The check above compares export names only. `LIMITS` is declared with literal
+// The proxy, control-api and mcp-host all import this allowance instead of
+// writing their own literal, so the declared literal type has to follow the
+// runtime value too.
+test('exports the 16 KiB envelope allowance with a matching declared literal', () => {
+ assert.equal(contract.ENVELOPE_ALLOWANCE_BYTES, 16 * 1024)
+ const declarations = fs.readFileSync(path.join(__dirname, 'index.d.ts'), 'utf8')
+ const declared = declarations.match(/export declare const ENVELOPE_ALLOWANCE_BYTES:\s*(\d+)\b/)
+ assert.ok(declared, 'index.d.ts must declare ENVELOPE_ALLOWANCE_BYTES as a numeric literal')
+ assert.equal(Number(declared[1]), contract.ENVELOPE_ALLOWANCE_BYTES)
+})
+
++// The export-name check above compares export names only. `LIMITS` is declared with literal
+ // types, so a bound raised in the runtime module and left behind in the
+ // declaration file compiles every TypeScript consumer against the old number
+ // while the runtime accepts the new one.
+ test('declared LIMITS literals match the runtime values', () => {
+ const declarations = fs.readFileSync(path.join(__dirname, 'index.d.ts'), 'utf8')
+ const block = declarations.match(/export declare const LIMITS: \{([\s\S]*?)\n\}/)
+ assert.ok(block, 'index.d.ts must declare a LIMITS object literal')
+ // Comments are stripped first, so a commented-out member cannot stand in for
+ // a real one. Every remaining line must be a member with a plain integer
+ // literal: a widened `number`, a decimal or a numeric separator fails here
+ // rather than being skipped by the scrape.
+ const members = block[1]
+ .replace(/\/\*[\s\S]*?\*\//g, '')
+ .replace(/\/\/.*$/gm, '')
+ .split('\n')
+ .map(line => line.trim())
+ .filter(line => line !== '')
+ const declared = Object.fromEntries(
+ members.map(line => {
+ const m = /^readonly ([A-Za-z0-9_]+): (\d+)$/.exec(line)
+ assert.ok(m, `LIMITS member must be "readonly <name>: <integer literal>": ${line}`)
+ return [m[1], Number(m[2])]
+ })
+ )
+ // fromEntries keeps the last of two members with the same name.
+ assert.equal(Object.keys(declared).length, members.length, 'LIMITS declares a member twice')
+ assert.deepEqual(declared, { ...contract.LIMITS })
+ })
+
test('parses the bounded V1 request and hashes with SHA-256', () => {
const parsed = contract.parseCodexCompletionRequestV1(BASE)
assert.equal(parsed.ok, true)
diff --cc tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
index f12aa32ed,3c534fd81..1e77a5798
--- a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
+++ b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
@@@ -65,8 -68,8 +68,10 @@@
"image_source_invalid",
"payload_too_large",
"tool_call_limit_exceeded",
+ "context_length_exceeded",
- "invalid_tool_arguments"
++ "invalid_tool_arguments",
+ "sse_buffer_exceeded",
+ "stream_duration_exceeded"
],
"termsAndTestAccount": {
"automationAuthorized": true,
diff --cc tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
index 47e64e8f4,0a8a9a563..17b3f0547
--- a/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
+++ b/tests/e2e/fixtures/grok-subscription/sanitized-upstream-contract.json
@@@ -79,8 -81,12 +81,14 @@@
"fingerprint_in_use",
"reauth_required",
"disabled",
+ "invalid_tool_arguments",
- "context_length_exceeded"
++ "context_length_exceeded",
+ "invalid_request",
+ "client_upgrade_required",
+ "tool_call_limit_exceeded",
+ "tool_call_arguments_exceeded",
+ "sse_buffer_exceeded",
+ "stream_duration_exceeded"
],
"termsAndTestAccount": {
"automationAuthorized": false,
diff --cc tests/e2e/fixtures/package-lock.json
index b0064d6de,78c5189ed..d5ec11397
--- 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.94",
- "version": "1.0.92",
++ "version": "1.0.95",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.94",
- "version": "1.0.92",
++ "version": "1.0.95",
"dependencies": {
"@kubernetes/client-node": "^1.0.0"
},
diff --cc tests/e2e/fixtures/package.json
index 18c8fded1,aa7639ef1..69c8d4a54
--- 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.94",
- "version": "1.0.92",
++ "version": "1.0.95",
"description": "E2E test fixtures and utilities for Clerum testing",
"type": "module",
"main": "index.ts",
diff --cc tests/e2e/integration/codex-subscription-contract-freeze.test.ts
index 760cf2e54,609709cf8..c73be1c9f
--- a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
+++ b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
@@@ -211,27 -253,55 +257,55 @@@ describe('codex-subscription contract f
}
expect(limits).not.toHaveProperty('maxTools')
+ // An unknown key is a typo or an unreviewed addition, not an extension.
+ expect(Object.keys(limits ?? {}).sort()).toEqual([...REQUIRED_LIMIT_KEYS].sort())
+
+ // The runtime package enforces these bounds, so the frozen description
+ // must agree with it, not only with itself. Without this, a bound changed
+ // in LIMITS and left behind here passes every gate (#738).
+ const runtimeLimits: Record<string, number> = { ...LIMITS }
+ const published = Object.keys(runtimeLimits).filter(key => key in (limits ?? {}))
+ expect(
+ [...published].sort(),
+ 'the LIMITS keys the fixture publishes changed; update PUBLISHED_RUNTIME_LIMIT_KEYS, REQUIRED_LIMIT_KEYS and the architecture doc table together'
+ ).toEqual([...PUBLISHED_RUNTIME_LIMIT_KEYS])
+ for (const key of RUNTIME_ONLY_LIMIT_KEYS) {
+ expect(
+ runtimeLimits,
+ `${key} is listed as runtime-only but LIMITS no longer has it; remove it from RUNTIME_ONLY_LIMIT_KEYS`
+ ).toHaveProperty(key)
+ expect(
+ limits,
+ `${key} is published in the fixture; remove it from RUNTIME_ONLY_LIMIT_KEYS`
+ ).not.toHaveProperty(key)
+ }
+ expect(
+ Object.keys(runtimeLimits).sort(),
+ 'every LIMITS key must be published in the fixture or listed in RUNTIME_ONLY_LIMIT_KEYS'
+ ).toEqual([...published, ...RUNTIME_ONLY_LIMIT_KEYS].sort())
+ for (const key of published) {
+ expect({ [key]: limits?.[key] }).toEqual({ [key]: runtimeLimits[key] })
+ }
+
expect(limits?.maxToolCalls).toBe(256)
expect(limits?.maxMessages).toBe(1024)
- expect(limits?.maxRequestBodyBytes).toBe(1048576)
+ expect(limits?.maxRequestBodyBytes).toBe(8388608)
expect(limits?.maxVisualRequestBodyBytes).toBe(25165824)
- // Every bound the fixture shares with the runtime contract must equal it, so
- // a change to `LIMITS` that forgets the fixture fails here for every field,
- // not only for the ones pinned above.
- const { LIMITS } = createRequire(import.meta.url)(
- join(repoRoot, 'packages/llm-provider-attempt-contract/index.cjs')
- ) as { LIMITS: Record<string, number> }
- const shared = Object.keys(LIMITS).filter(name => limits !== undefined && name in limits)
- expect(shared).toEqual([
- 'maxRequestBodyBytes',
- 'maxVisualRequestBodyBytes',
- 'maxMessages',
- 'maxToolCalls',
- 'maxOutputTokens',
- 'maxDeadlineMs',
- ])
- for (const name of shared) {
- expect({ [name]: limits?.[name] }).toEqual({ [name]: LIMITS[name] })
+
+ // 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.errorTaxonomy
@@@ -241,8 -311,11 +315,13 @@@
expect(String(code)).toMatch(/^[a-z][a-z0-9_]+$/)
}
expect(errors).toContain('tool_call_limit_exceeded')
+ expect(errors).toContain('context_length_exceeded')
+ expect(errors).toContain('invalid_tool_arguments')
+ expect(errors).toContain('stream_duration_exceeded')
+ expect(errors).toContain('sse_buffer_exceeded')
+ expect(new Set(errors as unknown[]).size, 'errorTaxonomy must not repeat a code').toBe(
+ (errors as unknown[]).length
+ )
const terms = contract.termsAndTestAccount as Record<string, unknown> | undefined
expect(terms && typeof terms === 'object').toBe(true)