Skip to content

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

TypeScript

fix(grok): bound the tool-call arguments one response may retain

Alfredo Lopez12ccf495ece3
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index b394f3c01..3e6983ca4 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -48,6 +48,23 @@ export type TransportTicket = {
*/
export const GROK_UPSTREAM_TEMPERATURE_PROBE_CONFIRMED: boolean = false
+/**
+ * Ceiling on the combined `arguments` text retained for every pending tool call
+ * of one response, in UTF-16 code units.
+ *
+ * `LIMITS.maxToolCalls` bounds how many calls a response may carry, never how
+ * large each one is, and the SSE buffer guard cannot cover this: it bounds the
+ * unparsed tail between two `\n\n` boundaries and is reset on every iteration,
+ * so a long run of `response.function_call_arguments.delta` events grows the
+ * proxy's heap and the response body without any ceiling.
+ *
+ * Interim value, matched to `LIMITS.maxRequestBodyBytes` so a response cannot
+ * be larger than a request the Host is allowed to send back. Issue #731 owns
+ * the end-to-end size budget and will replace this constant with the value the
+ * contract derives; keep the two in sync until then.
+ */
+export const MAX_TOOL_CALL_ARGUMENT_CHARS = 1_048_576
+
export class GrokTransportError extends Error {
constructor(
readonly code: string,
@@ -380,6 +397,7 @@ async function consumeSse(
const reader = body.getReader()
const decoder = new TextDecoder()
const pending = new Map<string, PendingToolCall>()
+ const argumentBudget: ToolArgumentBudget = { chars: 0 }
// Text stays streaming. Calls become executable only once the entire
// response succeeds and its independent call budget has been validated.
const toolFrames: Array<Extract<StreamFrame, { type: 'tool_call' }>> = []
@@ -425,7 +443,7 @@ async function consumeSse(
const parts = buffer.split('\n\n')
buffer = parts.pop() ?? ''
for (const part of parts) {
- const mapped = ingestSseBlock(part, pending)
+ const mapped = ingestSseBlock(part, pending, argumentBudget)
await acceptFrame(mapped.frame)
if (mapped.usage) usage = mapped.usage
if (mapped.completed) completed = true
@@ -435,7 +453,7 @@ async function consumeSse(
}
buffer += decoder.decode().replace(/\r\n/g, '\n').replace(/\r/g, '\n')
if (buffer.trim()) {
- const mapped = ingestSseBlock(buffer, pending)
+ const mapped = ingestSseBlock(buffer, pending, argumentBudget)
await acceptFrame(mapped.frame)
if (mapped.usage) usage = mapped.usage
if (mapped.completed) completed = true
@@ -490,7 +508,8 @@ async function deliverFrame(
function ingestSseBlock(
part: string,
- pending: Map<string, PendingToolCall>
+ pending: Map<string, PendingToolCall>,
+ budget: ToolArgumentBudget
): ReturnType<typeof mapUpstreamEvent> {
const dataLine = part
.split('\n')
@@ -499,11 +518,17 @@ function ingestSseBlock(
if (!dataLine) return {}
const payload = dataLine.slice(5).trim()
if (!payload || payload === '[DONE]') return {}
+ let event: unknown
try {
- return mapUpstreamEvent(JSON.parse(payload), pending)
+ event = JSON.parse(payload)
} catch {
+ // An unparseable frame is upstream noise and the stream continues. Only
+ // `JSON.parse` may be swallowed here: mapping the event can refuse the
+ // response over its argument budget, and that refusal has to reach
+ // `consumeSse` instead of being read as an empty frame.
return {}
}
+ return mapUpstreamEvent(event, pending, budget)
}
type PendingToolCall = {
@@ -513,9 +538,13 @@ type PendingToolCall = {
emitted: boolean
}
+/** Running total of the `arguments` text retained across `pending`. */
+type ToolArgumentBudget = { chars: number }
+
function mapUpstreamEvent(
event: unknown,
- pending: Map<string, PendingToolCall>
+ pending: Map<string, PendingToolCall>,
+ budget: ToolArgumentBudget
): {
frame?: StreamFrame
usage?: SafeUsage
@@ -533,17 +562,21 @@ function mapUpstreamEvent(
isPlainObject(row.item) &&
row.item.type === 'function_call'
) {
- const call = upsertPendingTool(pending, row.item)
+ const call = upsertPendingTool(pending, row.item, budget)
if (isCompleteJson(call.arguments)) return { frame: emitToolCall(call) }
return {}
}
if (type === 'response.function_call_arguments.delta') {
- upsertPendingTool(pending, {
- id: row.item_id,
- item_id: row.item_id,
- arguments: typeof row.delta === 'string' ? row.delta : '',
- append: true,
- })
+ upsertPendingTool(
+ pending,
+ {
+ id: row.item_id,
+ item_id: row.item_id,
+ arguments: typeof row.delta === 'string' ? row.delta : '',
+ append: true,
+ },
+ budget
+ )
return {}
}
if (
@@ -553,7 +586,7 @@ function mapUpstreamEvent(
row.item.type === 'function_call')
) {
const source = isPlainObject(row.item) ? row.item : row
- const call = upsertPendingTool(pending, source)
+ const call = upsertPendingTool(pending, source, budget)
if (call && !call.emitted) return { frame: emitToolCall(call) }
return {}
}
@@ -569,7 +602,8 @@ function mapUpstreamEvent(
function upsertPendingTool(
pending: Map<string, PendingToolCall>,
- source: Record<string, unknown> & { append?: boolean }
+ source: Record<string, unknown> & { append?: boolean },
+ budget: ToolArgumentBudget
): PendingToolCall {
const key = String(source.item_id || source.id || source.call_id || 'tool')
const current =
@@ -583,6 +617,7 @@ function upsertPendingTool(
if (typeof source.call_id === 'string' && source.call_id.trim()) current.id = source.call_id
if (typeof source.name === 'string' && source.name.trim()) current.name = source.name
const rawArgs = source.arguments
+ const retainedBefore = current.arguments.length
if (typeof rawArgs === 'string') {
current.arguments = source.append
? `${current.arguments}${rawArgs}`
@@ -590,6 +625,14 @@ function upsertPendingTool(
} else if (isPlainObject(rawArgs) && !source.append) {
current.arguments = JSON.stringify(rawArgs)
}
+ budget.chars += current.arguments.length - retainedBefore
+ if (budget.chars > MAX_TOOL_CALL_ARGUMENT_CHARS) {
+ throw new GrokTransportError(
+ 'tool_call_arguments_exceeded',
+ `tool call arguments exceed ${MAX_TOOL_CALL_ARGUMENT_CHARS} characters`,
+ { limit: MAX_TOOL_CALL_ARGUMENT_CHARS, observed: budget.chars }
+ )
+ }
pending.set(key, current)
return current
}
diff --git a/grok-llm-proxy/src/server.ts b/grok-llm-proxy/src/server.ts
index 4723715f2..c3f0d53df 100644
--- a/grok-llm-proxy/src/server.ts
+++ b/grok-llm-proxy/src/server.ts
@@ -388,6 +388,7 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
disabled: 404,
ticket_replayed: 409,
tool_call_limit_exceeded: 422,
+ tool_call_arguments_exceeded: 422,
client_upgrade_required: 426,
connection_unavailable: 503,
provider_unavailable: 503,
@@ -396,6 +397,13 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
conflict: 503,
}
+// A control-api code is an unbounded string, so both readers of the table must
+// ignore inherited names: `ATTEMPT_ERROR_STATUS['constructor']` is a function,
+// not undefined, and `??` would pass it straight to `res.status()`.
+function attemptErrorStatus(code: string): number {
+ return Object.hasOwn(ATTEMPT_ERROR_STATUS, code) ? ATTEMPT_ERROR_STATUS[code]! : 503
+}
+
function failureLabel(code: string): string {
return Object.hasOwn(ATTEMPT_ERROR_STATUS, code) ? code : 'other'
}
@@ -404,7 +412,7 @@ function mapError(err: unknown): { status: number; code: string } {
if (err instanceof OriginDeniedError) return { status: 403, code: 'origin_denied' }
if (err instanceof RequestLimitError) return { status: 503, code: 'provider_unavailable' }
if (err instanceof GrokTransportError || err instanceof ControlApiClientError) {
- return { status: ATTEMPT_ERROR_STATUS[err.code] ?? 503, code: err.code }
+ return { status: attemptErrorStatus(err.code), code: err.code }
}
return { status: 503, code: 'provider_unavailable' }
}
diff --git a/grok-llm-proxy/test/grokTransport.conformance.test.ts b/grok-llm-proxy/test/grokTransport.conformance.test.ts
index 745f0cd98..5b9213762 100644
--- a/grok-llm-proxy/test/grokTransport.conformance.test.ts
+++ b/grok-llm-proxy/test/grokTransport.conformance.test.ts
@@ -7,6 +7,8 @@ import type { RedeemAttemptSuccess } from '../src/controlApiClient.js'
import {
GROK_UPSTREAM_TEMPERATURE_PROBE_CONFIRMED,
GrokTransportError,
+ MAX_TOOL_CALL_ARGUMENT_CHARS,
+ type StreamFrame,
type StreamGrokCompletionInput,
streamGrokCompletion,
} from '../src/grokTransport.js'
@@ -67,6 +69,55 @@ function sseResponse(
})
}
+/**
+ * One SSE frame per `read()`, unlike `sseResponse`, which hands the whole body
+ * over as a single chunk. The argument-budget cases need this: the transport's
+ * `maxSseBufferBytes` guard bounds the unparsed tail between two `\n\n`
+ * boundaries, so a body joined into one >1 MiB chunk trips that guard instead
+ * of the budget under test.
+ */
+function chunkedSseResponse(frames: string[]): Response {
+ const encoder = new TextEncoder()
+ let index = 0
+ const body = new ReadableStream<Uint8Array>({
+ pull(controller) {
+ const next = frames[index]
+ index += 1
+ if (next === undefined) controller.close()
+ else controller.enqueue(encoder.encode(next))
+ },
+ })
+ return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream' } })
+}
+
+const ARGUMENT_CHUNK_CHARS = 65_536
+
+/**
+ * `{"q":"aaa…"}` split into `ARGUMENT_CHUNK_CHARS`-sized deltas, sized so the
+ * retained total lands exactly on `MAX_TOOL_CALL_ARGUMENT_CHARS`. Valid JSON,
+ * so the at-limit case can assert the parsed arguments the sink receives.
+ */
+function argumentDeltas(): string[] {
+ const envelopeChars = '{"q":""}'.length
+ const full = `{"q":"${'a'.repeat(MAX_TOOL_CALL_ARGUMENT_CHARS - envelopeChars)}"}`
+ const chunks: string[] = []
+ for (let offset = 0; offset < full.length; offset += ARGUMENT_CHUNK_CHARS) {
+ chunks.push(full.slice(offset, offset + ARGUMENT_CHUNK_CHARS))
+ }
+ return chunks
+}
+
+function argumentFrames(deltas: string[]): string[] {
+ return deltas.map(
+ delta =>
+ `data: ${JSON.stringify({
+ type: 'response.function_call_arguments.delta',
+ item_id: 'call-args',
+ delta,
+ })}\n\n`
+ )
+}
+
describe('streamGrokCompletion', () => {
it('does not read further upstream bytes while the frame consumer is back-pressured', async () => {
const encoder = new TextEncoder()
@@ -402,12 +453,119 @@ describe('streamGrokCompletion', () => {
expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
// Liveness witness and mutation detector in one: the leading text proves
// the stream was read, and the absence of the trailing text proves the
- // throw happened at the 65th pending call rather than after the stream.
+ // throw happened on the first call past `maxToolCalls` rather than after
+ // the stream drained.
expect(emitted.filter(frame => frame.type === 'text')).toEqual([{ type: 'text', text: 'before' }])
expect(finalize).toHaveBeenCalledWith(
expect.objectContaining({ receipt: expect.objectContaining({ outcome: 'error' }) })
)
})
+
+ // `maxToolCalls` bounds how many calls a response may carry, not how large
+ // each one is. A single call whose `arguments` grow without a ceiling costs
+ // the proxy heap and the Host body budget just the same, and the SSE buffer
+ // guard cannot see it: it is reset on every `\n\n` boundary.
+ it('accepts tool-call arguments that land exactly on the retained budget', async () => {
+ const deltas = argumentDeltas()
+ expect(deltas.join('')).toHaveLength(MAX_TOOL_CALL_ARGUMENT_CHARS)
+ const emitted: StreamFrame[] = []
+ 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.completed', response: { usage: {} } })}\n\n`,
+ ]
+ const result = await streamGrokCompletion({
+ executionTicket: 'ticket-args-ok',
+ requestHash: REQUEST_HASH,
+ request: REQUEST,
+ ticket: {
+ jti: 'jti-args-ok',
+ hostRef: 'research-host',
+ model: REQUEST.model,
+ requestHash: REQUEST_HASH,
+ providerAttemptId: 'att-args-ok',
+ },
+ redeem: vi.fn(async () => redeemSuccess()),
+ finalize: vi.fn(async () => ({
+ providerAttemptId: 'att-args-ok',
+ outcome: 'success' as const,
+ duplicate: false,
+ })),
+ fetchFn: vi.fn(async (_url: FetchInput, _init?: RequestInit) => chunkedSseResponse(frames)),
+ lookup: async () => [{ address: '1.2.3.4', family: 4 }],
+ onFrame: frame => {
+ emitted.push(frame)
+ },
+ })
+ expect(result.outcome).toBe('success')
+ const toolCalls = emitted.flatMap(frame => (frame.type === 'tool_call' ? [frame] : []))
+ expect(toolCalls).toHaveLength(1)
+ expect(toolCalls[0]?.arguments).toEqual({
+ q: 'a'.repeat(MAX_TOOL_CALL_ARGUMENT_CHARS - '{"q":""}'.length),
+ })
+ })
+
+ it('refuses a stream whose retained tool-call arguments exceed the budget', async () => {
+ const deltas = [...argumentDeltas(), 'a'.repeat(ARGUMENT_CHUNK_CHARS)]
+ const emitted: StreamFrame[] = []
+ const finalize = vi.fn(async () => ({
+ providerAttemptId: 'att-args-over',
+ 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-over',
+ requestHash: REQUEST_HASH,
+ request: REQUEST,
+ ticket: {
+ jti: 'jti-args-over',
+ hostRef: 'research-host',
+ model: REQUEST.model,
+ requestHash: REQUEST_HASH,
+ providerAttemptId: 'att-args-over',
+ },
+ 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_CHARS,
+ observed: MAX_TOOL_CALL_ARGUMENT_CHARS + ARGUMENT_CHUNK_CHARS,
+ },
+ })
+ expect(emitted.filter(frame => frame.type === 'tool_call')).toHaveLength(0)
+ // Liveness witness and mutation detector in one: the leading text proves
+ // the stream was read, and the absence of the trailing text proves the
+ // refusal fired on the delta that crossed the budget rather than after the
+ // stream drained.
+ 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 () => ({
diff --git a/grok-llm-proxy/test/server.security.test.ts b/grok-llm-proxy/test/server.security.test.ts
index f9c0ec632..41adcd868 100644
--- a/grok-llm-proxy/test/server.security.test.ts
+++ b/grok-llm-proxy/test/server.security.test.ts
@@ -15,7 +15,8 @@ import {
type FinalizeAttemptSuccess,
type RedeemAttemptSuccess,
} from '../src/controlApiClient.js'
-import { logger } from '../src/logger.js'
+import { MAX_TOOL_CALL_ARGUMENT_CHARS } from '../src/grokTransport.js'
+import { REDACT_PATHS, logger } from '../src/logger.js'
import { GROK_CATALOG_ORIGIN, GROK_COMPLETIONS_ORIGIN } from '../src/originPolicy.js'
import { createProxyApps } from '../src/server.js'
@@ -456,8 +457,26 @@ describe('grok-llm-proxy startup config', () => {
describe('grok-llm-proxy attempt telemetry', () => {
const lookup = async () => [{ address: '1.2.3.4', family: 4 }]
// Keys that carry caller content. The attempt line is identifiers and counts;
- // none of these may ever reach it.
- const FORBIDDEN_LOG_KEYS = ['body', 'request', 'executionTicket', 'arguments']
+ // none of these may ever reach it. The four names this suite has always
+ // checked are joined by every leaf the service itself redacts, so a value the
+ // logger would scrub cannot be declared safe here merely because this list
+ // forgot it — `accessToken` and `attemptReceipt` both come back from the
+ // redeem fixture below.
+ const FORBIDDEN_LOG_KEYS = [
+ ...new Set([
+ 'body',
+ 'request',
+ 'executionTicket',
+ 'arguments',
+ ...REDACT_PATHS.map(
+ redacted =>
+ redacted
+ .replace(/\['([^']+)'\]/g, '.$1')
+ .split('.')
+ .pop() as string
+ ).filter(key => key !== '*'),
+ ]),
+ ]
function completionBody(
providerAttemptId: string,
@@ -552,6 +571,48 @@ describe('grok-llm-proxy attempt telemetry', () => {
})) as typeof fetch
}
+ // One tool call whose `arguments` deltas add up past the retained budget,
+ // delivered one frame per read so the 1 MiB SSE buffer guard — which only
+ // bounds the unparsed tail between two `\n\n` boundaries — never fires and
+ // the argument budget is the guard under test.
+ const ARGUMENT_OVERRUN_CHUNK = 65_536
+
+ function oversizedArgumentsUpstream(): typeof fetch {
+ const chunks = MAX_TOOL_CALL_ARGUMENT_CHARS / ARGUMENT_OVERRUN_CHUNK + 1
+ const frames = [
+ `data: ${JSON.stringify({
+ type: 'response.output_item.added',
+ item: { type: 'function_call', id: 'call-args', name: 'lookup', arguments: '' },
+ })}\n\n`,
+ ...Array.from(
+ { length: chunks },
+ () =>
+ `data: ${JSON.stringify({
+ type: 'response.function_call_arguments.delta',
+ item_id: 'call-args',
+ delta: 'a'.repeat(ARGUMENT_OVERRUN_CHUNK),
+ })}\n\n`
+ ),
+ 'data: {"type":"response.completed"}\n\n',
+ ]
+ const encoder = new TextEncoder()
+ return (async () => {
+ let index = 0
+ const body = new ReadableStream<Uint8Array>({
+ pull(controller) {
+ const next = frames[index]
+ index += 1
+ if (next === undefined) controller.close()
+ else controller.enqueue(encoder.encode(next))
+ },
+ })
+ return new Response(body, {
+ status: 200,
+ headers: { 'content-type': 'text/event-stream' },
+ })
+ }) as typeof fetch
+ }
+
function denyingClient(code: string): ControlApiClient {
return {
async redeem() {
@@ -602,8 +663,32 @@ describe('grok-llm-proxy attempt telemetry', () => {
}
}
+ /** Returns how many keys it inspected, so an empty payload cannot pass. */
+ function walkForbiddenKeys(value: unknown, trail: string): number {
+ if (Array.isArray(value)) {
+ return value.reduce<number>(
+ (total, item, index) => total + walkForbiddenKeys(item, `${trail}[${index}]`),
+ 0
+ )
+ }
+ if (value === null || typeof value !== 'object') return 0
+ let visited = 0
+ for (const [key, nested] of Object.entries(value)) {
+ visited += 1
+ if (FORBIDDEN_LOG_KEYS.includes(key)) {
+ throw new Error(`log line carries the forbidden key ${trail}.${key}`)
+ }
+ visited += walkForbiddenKeys(nested, `${trail}.${key}`)
+ }
+ return visited
+ }
+
function expectNoForbiddenKeys(line: Record<string, unknown>): void {
- for (const key of FORBIDDEN_LOG_KEYS) expect(key in line).toBe(false)
+ // A flat check would clear `details` and `usage` without ever opening them.
+ const visited = walkForbiddenKeys(line, 'line')
+ // Liveness witness: a walk over an empty or non-object payload inspects
+ // nothing and would otherwise report the line as clean.
+ expect(visited).toBeGreaterThan(0)
}
it('(a) answers 422 and logs one attempt line when 257 calls arrive before any text', async () => {
@@ -633,6 +718,38 @@ describe('grok-llm-proxy attempt telemetry', () => {
expect(failureCount(metricsText, 'tool_call_limit_exceeded')).toBe(1)
})
+ // A call count within `maxToolCalls` says nothing about the size of each
+ // call. This refusal has to reach the Host as a 422 with its own code: a 503
+ // would be classified as an overload and retried with the same oversized
+ // response.
+ it('(a1) answers 422 when one call’s arguments exceed the retained budget', async () => {
+ const { res, receipts, lines, metricsText } = await run({
+ providerAttemptId: 'att-args-http',
+ fetchFn: oversizedArgumentsUpstream(),
+ })
+ expect(res.status).toBe(422)
+ expect(res.headers['content-type']).toMatch(/^application\/json/)
+ expect(res.body).toEqual({ error: 'tool_call_arguments_exceeded' })
+ expect(receipts).toEqual([expect.objectContaining({ outcome: 'error' })])
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({
+ providerAttemptId: 'att-args-http',
+ outcome: 'failed',
+ code: 'tool_call_arguments_exceeded',
+ details: {
+ limit: MAX_TOOL_CALL_ARGUMENT_CHARS,
+ observed: MAX_TOOL_CALL_ARGUMENT_CHARS + ARGUMENT_OVERRUN_CHUNK,
+ },
+ deliveredAs: 'http_status',
+ httpStatus: 422,
+ toolCalls: 0,
+ textChunks: 0,
+ })
+ // The refusal text names the bound, never the arguments that tripped it.
+ expectNoForbiddenKeys(lines[0]!)
+ expect(failureCount(metricsText, 'tool_call_arguments_exceeded')).toBe(1)
+ })
+
it('(a2) sends an SSE error frame when text was already streamed', async () => {
const { res, lines } = await run({
providerAttemptId: 'att-limit-sse',
@@ -738,4 +855,27 @@ describe('grok-llm-proxy attempt telemetry', () => {
expect(failureCount(metricsText, 'other')).toBe(1)
expect(metricsText).not.toContain(rawCode)
})
+
+ // `ATTEMPT_ERROR_STATUS` is an object literal, so an inherited name resolves
+ // to a function or an object instead of undefined. A plain index read would
+ // hand that value to `res.status()`, which throws ERR_HTTP_INVALID_STATUS_CODE
+ // inside the request IIFE's catch — an unhandled rejection that takes every
+ // other in-flight stream down with the process.
+ it.each(['constructor', '__proto__', 'toString', 'hasOwnProperty'])(
+ '(f) answers 503 for the inherited control-api code %j instead of crashing',
+ async code => {
+ const { res, lines, metricsText } = await run({
+ providerAttemptId: `att-proto-${code}`,
+ deniedCode: code,
+ })
+ expect(res.status).toBe(503)
+ expect(res.body).toEqual({ error: code })
+ // Witness: the attempt was reached and logged, so the status above is the
+ // mapped refusal and not a connection that never produced a response.
+ expect(lines).toHaveLength(1)
+ expect(lines[0]).toMatchObject({ outcome: 'failed', code, httpStatus: 503 })
+ expect(failureCount(metricsText, 'other')).toBe(1)
+ expect(failureCount(metricsText, code)).toBe(0)
+ }
+ )
})
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index d9d139b9d..a366bc26f 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.366",
+ "version": "0.1.367",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.366",
+ "version": "0.1.367",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index 7b940acdd..957c51bd2 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.366",
+ "version": "0.1.367",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
index 6b54f8b88..fa1269c01 100644
--- a/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
+++ b/mcp-host/src/llm/__tests__/grokLlmProxyClient.test.ts
@@ -260,4 +260,16 @@ describe('GrokLlmProxyClient', () => {
)
expect(grokProxyErrorMessage('ticket_expired')).toBe('proxy stream failed with ticket_expired')
})
+
+ // This one is the agent's to resolve, not an operator's: the model asked for
+ // a tool call whose arguments the transport will not carry. The proxy body
+ // carries only the code, so this function is the last place a sentence can be
+ // built, and a bare status code would leave the agent repeating the same
+ // oversized call.
+ it('tells the caller to send a more bounded request on tool_call_arguments_exceeded', () => {
+ const message = grokProxyErrorMessage('tool_call_arguments_exceeded', 422)
+ expect(message).toMatch(/tool call[\s\S]*arguments[\s\S]*exceed/i)
+ expect(message).toMatch(/smaller|bounded|split/i)
+ expect(message).not.toMatch(/proxy stream failed/i)
+ })
})
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index 25d48a377..dfa3b95d9 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -320,8 +320,14 @@ describe('GrokSubscriptionProvider', () => {
// Liveness witness for the negative assertion below: the identical call
// one message shorter does reach authorize, so "authorize was not called"
- // reports the guard rather than a provider that never ran.
- await provider.completeSingleTurn(Array.from({ length: 1024 }, () => message))
+ // reports the guard rather than a provider that never ran. The returned
+ // completion is part of the witness — a call that reached authorize and
+ // then failed downstream would satisfy the call count alone — and it is
+ // also the only case that falsifies the bound this PR replaced, where 1024
+ // messages were refused.
+ await expect(
+ provider.completeSingleTurn(Array.from({ length: 1024 }, () => message))
+ ).resolves.toMatchObject({ content: 'hello from grok proxy', finish_reason: 'stop' })
expect(wired.authorizer.authorize).toHaveBeenCalledTimes(1)
const refused = provider.completeSingleTurn(Array.from({ length: 1025 }, () => message))
@@ -373,6 +379,24 @@ describe('GrokSubscriptionProvider', () => {
expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
})
+ // A response whose tool-call arguments overrun the transport budget is a size
+ // refusal, the same family as a request that overruns `maxMessages`. Reading
+ // it as an overload would retry the identical oversized call and, being
+ // failover-eligible, would spend a second provider on it.
+ it('classifies oversized tool-call arguments as a size refusal, not an overload', () => {
+ const provider = new GrokSubscriptionProvider('grok-4.6', deps() as never)
+ const classified = provider.classifyError(
+ new GrokProxyError('tool_call_arguments_exceeded', 'tool call arguments exceed 1048576')
+ )
+ expect(classified).toMatchObject({
+ code: LlmErrorCode.ContextLengthExceeded,
+ retryable: false,
+ providerCode: 'tool_call_arguments_exceeded',
+ })
+ expect(classified.code).not.toBe(LlmErrorCode.ModelOverloaded)
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
it('classifies authorize denials as non-retryable and never dispatched', () => {
const provider = new GrokSubscriptionProvider('grok-4.6', deps() as never)
const scope = provider.classifyError(new CodexAuthorizeError('insufficient_scope', 'scope'))
diff --git a/mcp-host/src/llm/grokLlmProxyClient.ts b/mcp-host/src/llm/grokLlmProxyClient.ts
index 9010735c1..d2e988cd0 100644
--- a/mcp-host/src/llm/grokLlmProxyClient.ts
+++ b/mcp-host/src/llm/grokLlmProxyClient.ts
@@ -8,6 +8,14 @@ export function grokProxyErrorMessage(code: string, status?: number): string {
if (code === 'client_upgrade_required') {
return 'Grok subscription inference is unavailable: xAI now requires a newer Grok client version than this deployment sends. An operator can set GROK_LLM_PROXY_CLIENT_VERSION to a current Grok Build release, or contact support — retrying will not help.'
}
+ if (code === 'tool_call_arguments_exceeded') {
+ // The remedy belongs to whoever composes the next turn, not to an
+ // operator: the transport refused a tool call whose arguments exceeded its
+ // per-response size budget, so the response was rejected rather than
+ // truncated. Interim text — issue #731 owns the end-to-end size budget and
+ // will restate this guidance against the contract's own value.
+ return 'Grok returned a tool call whose arguments exceed the transport size budget, so the response was refused instead of truncated. Retry with a more bounded request: ask for fewer items per call, request narrower fields, or split the work into several smaller tool calls.'
+ }
return status === undefined
? `proxy stream failed with ${code}`
: `proxy stream failed with ${status} (${code})`
diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index f28aa0cff..034a0178c 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -197,7 +197,11 @@ export class GrokSubscriptionProvider implements SingleTurnProvider {
...(providerDispatched !== undefined ? { providerDispatched } : {}),
}
}
- if (code === 'request_limit_exceeded') {
+ // 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,
+ // being failover-eligible, spend a second provider on it.
+ if (code === 'request_limit_exceeded' || code === 'tool_call_arguments_exceeded') {
return {
code: LlmErrorCode.ContextLengthExceeded,
retryable: false,