Skip to content

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

TypeScript

fix: refuse malformed tool-call arguments in the Codex and Grok proxies

Alfredo Lopez5fc3f1df5fc3
diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.ts
index 82dee9e4b..907d30615 100644
--- a/codex-llm-proxy/src/codexTransport.ts
+++ b/codex-llm-proxy/src/codexTransport.ts
@@ -414,12 +414,8 @@ async function consumeSse(
} finally {
reader.releaseLock()
}
- for (const call of pending.values()) {
- if (call.emitted) continue
- const args = parseToolArguments(call.arguments)
- await acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })
- call.emitted = true
- }
+ // 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 }
if (failed) {
if (contextLengthExceeded) {
@@ -427,6 +423,12 @@ async function consumeSse(
}
throw new CodexTransportError('provider_unavailable', 'upstream response failed')
}
+ for (const call of pending.values()) {
+ if (call.emitted) continue
+ const args = parseToolArguments(call.arguments)
+ await acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })
+ call.emitted = true
+ }
if (completed) {
for (const frame of toolFrames) await deliverFrame(onFrame, frame, signal)
return { outcome: 'success', usage }
@@ -469,11 +471,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 tool-call arguments, and that refusal has to reach
+ // `consumeSse` instead of being read as an empty frame.
return {}
}
+ return mapUpstreamEvent(event, pending)
}
type PendingToolCall = {
@@ -598,15 +606,27 @@ function isCompleteJson(raw: string): boolean {
}
}
-function parseToolArguments(raw: unknown): Record<string, unknown> {
- if (isPlainObject(raw)) return raw
- if (typeof raw !== 'string' || raw.length === 0) return {}
+// A call runs with exactly the arguments the model produced. Arguments that are
+// not a JSON object (truncated, a non-object value, or empty) refuse the whole
+// response instead of executing the tool with `{}`; a call without parameters
+// arrives as the string "{}".
+function parseToolArguments(raw: string): Record<string, unknown> {
+ let parsed: unknown
try {
- const parsed = JSON.parse(raw)
- return isPlainObject(parsed) ? parsed : {}
+ parsed = JSON.parse(raw)
} catch {
- return {}
+ throw new CodexTransportError(
+ 'invalid_tool_arguments',
+ 'upstream tool call arguments are not valid JSON'
+ )
+ }
+ if (!isPlainObject(parsed)) {
+ throw new CodexTransportError(
+ 'invalid_tool_arguments',
+ 'upstream tool call arguments are not a JSON object'
+ )
}
+ return parsed
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
diff --git a/codex-llm-proxy/src/server.ts b/codex-llm-proxy/src/server.ts
index 281e61b4f..68ac5c96c 100644
--- a/codex-llm-proxy/src/server.ts
+++ b/codex-llm-proxy/src/server.ts
@@ -447,6 +447,7 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
disabled: 404,
ticket_replayed: 409,
tool_call_limit_exceeded: 422,
+ invalid_tool_arguments: 422,
connection_unavailable: 503,
provider_unavailable: 503,
sse_buffer_exceeded: 503,
diff --git a/codex-llm-proxy/test/codexTransport.conformance.test.ts b/codex-llm-proxy/test/codexTransport.conformance.test.ts
index 271d348c4..2381fc238 100644
--- a/codex-llm-proxy/test/codexTransport.conformance.test.ts
+++ b/codex-llm-proxy/test/codexTransport.conformance.test.ts
@@ -1306,14 +1306,30 @@ describe('streamCodexCompletion', () => {
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[]) {
+ 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]) => ({
+ async (input: Parameters<StreamCodexCompletionInput['finalize']>[0]) => ({
providerAttemptId: 'att-1',
- outcome: 'error' as const,
+ outcome: input.receipt.outcome,
duplicate: false,
})
)
@@ -1328,12 +1344,14 @@ describe('streamCodexCompletion', () => {
requestHash: REQUEST_HASH,
providerAttemptId: 'att-1',
},
+ signal: abort.signal,
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 }),
@@ -1384,15 +1402,39 @@ describe('streamCodexCompletion', () => {
)
it('refuses a call whose argument deltas never complete before the stream ends', async () => {
- await expectRefused([
+ await expectRefused([textBefore, openCall, truncatedDelta, 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,
- sse({
- type: 'response.output_item.added',
- item: { type: 'function_call', id: 'item-1', call_id: 'call-9', name: 'lookup', arguments: '' },
- }),
- sse({ type: 'response.function_call_arguments.delta', item_id: 'item-1', delta: '{"q":' }),
- completed,
+ 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')
})
})
})
diff --git a/codex-llm-proxy/test/server.security.test.ts b/codex-llm-proxy/test/server.security.test.ts
index 869b6b3a7..767693531 100644
--- a/codex-llm-proxy/test/server.security.test.ts
+++ b/codex-llm-proxy/test/server.security.test.ts
@@ -573,6 +573,22 @@ describe('codex-llm-proxy attempt telemetry', () => {
})) as typeof fetch
}
+ // One call whose `arguments` are truncated JSON, closed by a completed response.
+ function malformedArgumentsUpstream(): typeof fetch {
+ const frames = [
+ `data: ${JSON.stringify({
+ type: 'response.output_item.done',
+ item: { type: 'function_call', id: 'call-bad', name: 'lookup', arguments: '{"q":' },
+ })}\n\n`,
+ 'data: {"type":"response.completed"}\n\n',
+ ]
+ return (async () =>
+ new Response(frames.join(''), {
+ status: 200,
+ headers: { 'content-type': 'text/event-stream' },
+ })) as typeof fetch
+ }
+
function denyingClient(code: string): ControlApiClient {
return {
async redeem() {
@@ -597,14 +613,15 @@ describe('codex-llm-proxy attempt telemetry', () => {
calls: number,
tamper?: (raw: Record<string, unknown>) => void,
deniedCode?: string,
- terminal?: string[]
+ terminal?: string[],
+ fetchFn?: typeof fetch
) {
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: upstream(textDeltas, calls, terminal),
+ fetchFn: fetchFn ?? upstream(textDeltas, calls, terminal),
lookup,
})
try {
@@ -651,6 +668,40 @@ describe('codex-llm-proxy attempt telemetry', () => {
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()
+ )
+ 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)
diff --git a/desktop-app/ui/src/components/agents/__tests__/ChatThread.renderSemantics.test.tsx b/desktop-app/ui/src/components/agents/__tests__/ChatThread.renderSemantics.test.tsx
index b97c4f21b..e8048b225 100644
--- 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,6 +132,14 @@ 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 context length error as "Conversation Too Long"', () => {
expect(renderErrorLabel('LLM_CONTEXT_LENGTH_EXCEEDED').label).toBe('Conversation Too Long')
})
diff --git a/desktop-app/ui/src/constants/agents.ts b/desktop-app/ui/src/constants/agents.ts
index b5a58ade5..12143d090 100644
--- a/desktop-app/ui/src/constants/agents.ts
+++ b/desktop-app/ui/src/constants/agents.ts
@@ -16,6 +16,7 @@ export const AGENT_ERROR_CODE_LABELS: Record<string, string> = {
LLM_API_CALL_FAILED: 'Connection Error',
LLM_TOOL_CALL_LIMIT_EXCEEDED: 'Too Many Tool Calls',
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 --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.md
index a9dc4b8f9..d985f54ca 100644
--- a/docs/architecture/codex-subscription-transport-contract.md
+++ b/docs/architecture/codex-subscription-transport-contract.md
@@ -258,7 +258,7 @@ 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`.
+`tool_call_limit_exceeded`, `context_length_exceeded`, `invalid_tool_arguments`.
- `context_length_exceeded`: the upstream refused the request because it
exceeds the model's context window. The upstream sends an SSE `error` event
@@ -275,6 +275,17 @@ Stable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,
`LLM_TOOL_CALL_LIMIT_EXCEEDED`. It is not retryable and not
failover-eligible (failover class `null`), so the task fails with that code
instead of `LLM_MODEL_OVERLOADED`.
+- `invalid_tool_arguments`: a tool call's `arguments` are not a JSON object —
+ truncated JSON, a non-object value, or an empty string (a call without
+ parameters arrives as `"{}"`). The transport refuses the whole response
+ instead of running the tool with `{}`, both when the call is closed by
+ `response.output_item.done` / `response.function_call_arguments.done` and
+ when a pending call is flushed at `response.completed`. A stream that was
+ canceled or that the upstream failed keeps its own outcome (`canceled`,
+ `context_length_exceeded` or `provider_unavailable`), because its open call
+ is truncated as a consequence. Delivered like `tool_call_limit_exceeded` —
+ 422 carrying the code, or an SSE error frame once text is on the wire. The
+ Host maps it to `LLM_INVALID_RESPONSE`, not retryable, failover class `null`.
- `request_limit_exceeded` (Host-side only): the turn carries too much. The
Host raises it before authorization — so no provider attempt is spent — and
maps it to `LLM_CONTEXT_LENGTH_EXCEEDED`, not retryable, which the UI shows
diff --git a/docs/architecture/grok-subscription-transport-contract.md b/docs/architecture/grok-subscription-transport-contract.md
index b26378f06..18f52b6df 100644
--- a/docs/architecture/grok-subscription-transport-contract.md
+++ b/docs/architecture/grok-subscription-transport-contract.md
@@ -250,7 +250,8 @@ Stable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,
`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`, `request_limit_exceeded` (Host-side only).
+`tool_call_arguments_exceeded`, `invalid_tool_arguments`,
+`request_limit_exceeded` (Host-side only).
- `tool_call_limit_exceeded`: the upstream response carried more than
`maxToolCalls` tool calls. The proxy returns HTTP 422 whose body is the code
@@ -279,6 +280,17 @@ Stable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,
several smaller calls. The bound and that wording are interim: the #731
request-size work classifies this refusal as context length (above) and
leaves the bound's value and the wording as they are.
+- `invalid_tool_arguments`: a tool call's `arguments` are not a JSON object —
+ truncated JSON, a non-object value, or an empty string (a call without
+ parameters arrives as `"{}"`). The transport refuses the whole response
+ instead of running the tool with `{}`, both when the call is closed by
+ `response.output_item.done` / `response.function_call_arguments.done` and
+ when a pending call is flushed at `response.completed`. A stream that was
+ canceled or that the upstream failed keeps its own outcome (`canceled`,
+ `provider_unavailable`), because its open call is truncated as a
+ consequence. Delivered like `tool_call_limit_exceeded` — 422 carrying the
+ code, or an SSE error frame once text is on the wire. The Host maps it to
+ `LLM_INVALID_RESPONSE`, not retryable, failover class `null`.
- `request_limit_exceeded` (Host-side only): the turn carries too much. The
Host raises it before authorization — so no provider attempt is spent — and
maps it to `LLM_CONTEXT_LENGTH_EXCEEDED`, not retryable, which the UI shows
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index 8969bd458..609f45994 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -465,16 +465,18 @@ async function consumeSse(
} finally {
reader.releaseLock()
}
+ // 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 }
+ 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)
await acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })
call.emitted = true
}
- if (signal.aborted) return { outcome: 'canceled', usage }
- if (failed) {
- throw new GrokTransportError('provider_unavailable', 'upstream response failed')
- }
if (completed) {
for (const frame of toolFrames) await deliverFrame(onFrame, frame, signal)
return { outcome: 'success', usage }
@@ -658,15 +660,27 @@ function isCompleteJson(raw: string): boolean {
}
}
-function parseToolArguments(raw: unknown): Record<string, unknown> {
- if (isPlainObject(raw)) return raw
- if (typeof raw !== 'string' || raw.length === 0) return {}
+// A call runs with exactly the arguments the model produced. Arguments that are
+// not a JSON object (truncated, a non-object value, or empty) refuse the whole
+// response instead of executing the tool with `{}`; a call without parameters
+// arrives as the string "{}".
+function parseToolArguments(raw: string): Record<string, unknown> {
+ let parsed: unknown
try {
- const parsed = JSON.parse(raw)
- return isPlainObject(parsed) ? parsed : {}
+ parsed = JSON.parse(raw)
} catch {
- return {}
+ throw new GrokTransportError(
+ 'invalid_tool_arguments',
+ 'upstream tool call arguments are not valid JSON'
+ )
+ }
+ if (!isPlainObject(parsed)) {
+ throw new GrokTransportError(
+ 'invalid_tool_arguments',
+ 'upstream tool call arguments are not a JSON object'
+ )
}
+ return parsed
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
diff --git a/grok-llm-proxy/src/server.ts b/grok-llm-proxy/src/server.ts
index f0b837777..dc4d4b3dc 100644
--- a/grok-llm-proxy/src/server.ts
+++ b/grok-llm-proxy/src/server.ts
@@ -455,6 +455,7 @@ const ATTEMPT_ERROR_STATUS: Record<string, number> = {
ticket_replayed: 409,
tool_call_limit_exceeded: 422,
tool_call_arguments_exceeded: 422,
+ invalid_tool_arguments: 422,
client_upgrade_required: 426,
connection_unavailable: 503,
provider_unavailable: 503,
diff --git a/grok-llm-proxy/test/grokTransport.conformance.test.ts b/grok-llm-proxy/test/grokTransport.conformance.test.ts
index ba309146f..f11e57118 100644
--- a/grok-llm-proxy/test/grokTransport.conformance.test.ts
+++ b/grok-llm-proxy/test/grokTransport.conformance.test.ts
@@ -1371,14 +1371,30 @@ describe('streamGrokCompletion', () => {
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[]) {
+ 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]) => ({
+ async (input: Parameters<StreamGrokCompletionInput['finalize']>[0]) => ({
providerAttemptId: 'att-1',
- outcome: 'error' as const,
+ outcome: input.receipt.outcome,
duplicate: false,
})
)
@@ -1393,12 +1409,14 @@ describe('streamGrokCompletion', () => {
requestHash: REQUEST_HASH,
providerAttemptId: 'att-1',
},
+ signal: abort.signal,
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 }),
@@ -1449,15 +1467,39 @@ describe('streamGrokCompletion', () => {
)
it('refuses a call whose argument deltas never complete before the stream ends', async () => {
- await expectRefused([
+ await expectRefused([textBefore, openCall, truncatedDelta, 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,
- sse({
- type: 'response.output_item.added',
- item: { type: 'function_call', id: 'item-1', call_id: 'call-9', name: 'lookup', arguments: '' },
- }),
- sse({ type: 'response.function_call_arguments.delta', item_id: 'item-1', delta: '{"q":' }),
- completed,
+ 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')
})
})
})
diff --git a/grok-llm-proxy/test/server.security.test.ts b/grok-llm-proxy/test/server.security.test.ts
index 415f27ee0..a401c594a 100644
--- a/grok-llm-proxy/test/server.security.test.ts
+++ b/grok-llm-proxy/test/server.security.test.ts
@@ -635,6 +635,22 @@ describe('grok-llm-proxy attempt telemetry', () => {
}) as typeof fetch
}
+ // One call whose `arguments` are truncated JSON, closed by a completed response.
+ function malformedArgumentsUpstream(): typeof fetch {
+ const frames = [
+ `data: ${JSON.stringify({
+ type: 'response.output_item.done',
+ item: { type: 'function_call', id: 'call-bad', name: 'lookup', arguments: '{"q":' },
+ })}\n\n`,
+ 'data: {"type":"response.completed"}\n\n',
+ ]
+ return (async () =>
+ new Response(frames.join(''), {
+ status: 200,
+ headers: { 'content-type': 'text/event-stream' },
+ })) as typeof fetch
+ }
+
function denyingClient(code: string): ControlApiClient {
return {
async redeem() {
@@ -772,6 +788,35 @@ describe('grok-llm-proxy attempt telemetry', () => {
expect(failureCount(metricsText, 'tool_call_arguments_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('(a1b) answers 422 invalid_tool_arguments when a call’s arguments are not a JSON object', async () => {
+ const { res, receipts, lines, metricsText } = await run({
+ providerAttemptId: 'att-bad-args-http',
+ 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('(a2) sends an SSE error frame when text was already streamed', async () => {
const { res, lines } = await run({
providerAttemptId: 'att-limit-sse',
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index 0c86f97a4..d3e602f93 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.404",
+ "version": "0.1.405",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.404",
+ "version": "0.1.405",
"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 a7d89d34e..d48bbe38c 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.404",
+ "version": "0.1.405",
"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__/codexSubscription.test.ts b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
index d0e96528f..f80c6acd3 100644
--- a/mcp-host/src/llm/__tests__/codexSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/codexSubscription.test.ts
@@ -774,6 +774,27 @@ describe('CodexSubscriptionProvider', () => {
expect(unavailable.retryable).toBe(true)
})
+ // The proxy refused a tool call whose arguments are not a JSON object. The
+ // model output is invalid, so retrying or failing over would re-run the turn
+ // on a response the contract already rejected.
+ it('classifies invalid tool-call arguments as an invalid response, not an overload', () => {
+ const provider = new CodexSubscriptionProvider('gpt-5.3-codex', deps() as never)
+ const classified = provider.classifyError(
+ new CodexProxyError(
+ 'invalid_tool_arguments',
+ 'proxy stream failed with invalid_tool_arguments'
+ )
+ )
+ expect(classified).toEqual({
+ code: LlmErrorCode.InvalidResponse,
+ retryable: false,
+ message: 'proxy stream failed with invalid_tool_arguments',
+ providerCode: 'invalid_tool_arguments',
+ providerDispatched: true,
+ })
+ expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
+ })
+
it('records whether a classified Codex failure had already left the process', () => {
const provider = new CodexSubscriptionProvider('gpt-5.3-codex', deps() as never)
diff --git a/mcp-host/src/llm/__tests__/grokSubscription.test.ts b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
index 2335131d9..c9208ea10 100644
--- a/mcp-host/src/llm/__tests__/grokSubscription.test.ts
+++ b/mcp-host/src/llm/__tests__/grokSubscription.test.ts
@@ -613,6 +613,27 @@ describe('GrokSubscriptionProvider', () => {
expect(classifyFailoverClass(classified.code, classified.retryable)).toBeNull()
})
+ // The proxy refused a tool call whose arguments are not a JSON object. The
+ // model output is invalid, so retrying or failing over would re-run the turn
+ // on a response the contract already rejected.
+ it('classifies invalid tool-call arguments as an invalid response, not an overload', () => {
+ const provider = new GrokSubscriptionProvider('grok-4.6', deps() as never)
+ const classified = provider.classifyError(
+ new GrokProxyError(
+ 'invalid_tool_arguments',
+ 'proxy stream failed with invalid_tool_arguments'
+ )
+ )
+ expect(classified).toEqual({
+ code: LlmErrorCode.InvalidResponse,
+ retryable: false,
+ message: 'proxy stream failed with invalid_tool_arguments',
+ providerCode: 'invalid_tool_arguments',
+ providerDispatched: true,
+ })
+ 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/codexSubscription.ts b/mcp-host/src/llm/codexSubscription.ts
index a7d9959bb..513a5fff9 100644
--- a/mcp-host/src/llm/codexSubscription.ts
+++ b/mcp-host/src/llm/codexSubscription.ts
@@ -232,6 +232,18 @@ export class CodexSubscriptionProvider implements SingleTurnProvider {
...(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 } : {}),
+ }
+ }
// `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
diff --git a/mcp-host/src/llm/grokSubscription.ts b/mcp-host/src/llm/grokSubscription.ts
index 3b6d85e5e..5af59b690 100644
--- a/mcp-host/src/llm/grokSubscription.ts
+++ b/mcp-host/src/llm/grokSubscription.ts
@@ -248,6 +248,18 @@ export class GrokSubscriptionProvider implements SingleTurnProvider {
...(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 } : {}),
+ }
+ }
// 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 --git a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
index 537a5a1bc..f4b8ced51 100644
--- a/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
+++ b/tests/e2e/fixtures/codex-subscription/sanitized-upstream-contract.json
@@ -62,7 +62,8 @@
"ticket_replayed",
"request_hash_mismatch",
"tool_call_limit_exceeded",
- "context_length_exceeded"
+ "context_length_exceeded",
+ "invalid_tool_arguments"
],
"termsAndTestAccount": {
"automationAuthorized": true,
diff --git a/tests/e2e/fixtures/package-lock.json b/tests/e2e/fixtures/package-lock.json
index 3a9bac504..87f7ab185 100644
--- a/tests/e2e/fixtures/package-lock.json
+++ b/tests/e2e/fixtures/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.90",
+ "version": "1.0.91",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.90",
+ "version": "1.0.91",
"dependencies": {
"@kubernetes/client-node": "^1.0.0"
},
diff --git a/tests/e2e/fixtures/package.json b/tests/e2e/fixtures/package.json
index 91a0be7f9..db0b424af 100644
--- a/tests/e2e/fixtures/package.json
+++ b/tests/e2e/fixtures/package.json
@@ -1,6 +1,6 @@
{
"name": "@clerum/e2e-test-fixtures",
- "version": "1.0.90",
+ "version": "1.0.91",
"description": "E2E test fixtures and utilities for Clerum testing",
"type": "module",
"main": "index.ts",
diff --git a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
index 0cd706816..da779300e 100644
--- a/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
+++ b/tests/e2e/integration/codex-subscription-contract-freeze.test.ts
@@ -166,6 +166,7 @@ describe('codex-subscription contract freeze', () => {
}
expect(errors).toContain('tool_call_limit_exceeded')
expect(errors).toContain('context_length_exceeded')
+ expect(errors).toContain('invalid_tool_arguments')
const terms = contract.termsAndTestAccount as Record<string, unknown> | undefined
expect(terms && typeof terms === 'object').toBe(true)
diff --git a/tests/e2e/package-lock.json b/tests/e2e/package-lock.json
index 2c10f9a69..e8e24e0d9 100644
--- a/tests/e2e/package-lock.json
+++ b/tests/e2e/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.87",
+ "version": "0.1.88",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-e2e-tests",
- "version": "0.1.87",
+ "version": "0.1.88",
"devDependencies": {
"@types/jsonwebtoken": "^9.0.10",
"@types/node": "^22.0.0",
diff --git a/tests/e2e/package.json b/tests/e2e/package.json
index da4a3e8a1..3ae01f2c5 100644
--- a/tests/e2e/package.json
+++ b/tests/e2e/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-e2e-tests",
- "version": "0.1.87",
+ "version": "0.1.88",
"private": true,
"type": "module",
"scripts": {