xonecas.io
64875
Mc

mini-coder / commit 0e65188

fix: handle malformed tool calls gracefully to avoid 400 Bad Request

xonecas2026-03-11 0e65188e3f5ef09604623e35d479e35cf1f3dbed parent a129afe

2 files changed

TODO.md+0−72
@@ -1,77 +1,5 @@
11 # TODO
22
3-- Investigate why orc subagent never calls subagents.
4-
5-## Investigate error:
6-
7-```
8-◆ Now add `renderSubagentStart` and `renderSubagentResult` exports, and remove the subagent no-ops from `renderToolResultInline` and `renderToolResult`. Let me find these:
9- ← read src/cli/tool-render.ts:90+15
10- · src/cli/tool-render.ts lines 90–104 of 571 (truncated)
11-◆ Now find the subagent no-op in `renderToolResultInline`:
12- ← read
13- ✖ Invalid input for tool read: JSON parsing failed: Text: {"path": "src/cli/tool-render.ts", "line"": 210, "count": 20}.
14-⠹ thinking2522 | const responseBody = await response.text();
15-2523 | const responseHeaders = extractResponseHeaders(response);
16-2524 | if (responseBody.trim() === "") {
17-2525 | return {
18-2526 | responseHeaders,
19-2527 | value: new APICallError4({
20- ^
21-AI_APICallError: Bad Request
22- cause: undefined,
23- url: "https://opencode.ai/zen/v1/messages",
24- requestBodyValues: {
25- model: "claude-sonnet-4-6",
26- max_tokens: 128000,
27- temperature: undefined,
28- top_k: undefined,
29- top_p: undefined,
30- stop_sequences: undefined,
31- thinking: [Object ...],
32- output_config: [Object ...],
33- system: [
34- [Object ...]
35- ],
36- messages: [
37- [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Objec …
38- [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...],
39- ... 35 more items
40- ],
41- tools: [
42- [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...], [Object ...]
43- ],
44- tool_choice: [Object ...],
45- stream: true,
46-},
47- statusCode: 400,
48- responseHeaders: {
49- "cf-ray": "9da4778aca7960ae-ORD",
50- connection: "keep-alive",
51- "content-type": "application/json",
52- date: "Tue, 10 Mar 2026 18:43:47 GMT",
53- server: "cloudflare",
54- "transfer-encoding": "chunked",
55-},
56- responseBody: "",
57- isRetryable: false,
58- data: undefined,
59- vercel.ai.error: true,
60- vercel.ai.error.AI_APICallError: true,
61-
62- at /home/xonecas/src/mini-coder/node_modules/@ai-sdk/provider-utils/dist/index.mjs:2527:18
63- at /home/xonecas/src/mini-coder/node_modules/@ai-sdk/provider-utils/dist/index.mjs:2388:34
64- at processTicksAndRejections (native:7:39)
65-
66-✖ API error 400
67- https://opencode.ai/zen/v1/messages
68-✖ API error 400
69- https://opencode.ai/zen/v1/messages
70-error: script "dev" exited with code 1
71-```
72-
73----
74-
753 ## LSP Diagnostics (not very important, with tool-hooks and strong linting, this is not as necessary, but we should implement asap)
764
775 We should have a closed loop feedback for LSP diagnostics on edits/reads.
src/llm-api/turn.ts+7−0
@@ -173,6 +173,13 @@ export async function* runTurn(options: {
173173 ? { providerOptions: mergedProviderOptions }
174174 : {}),
175175 ...(signal ? { abortSignal: signal } : {}),
176+ experimental_repairToolCall: async ({ toolCall }) => {
177+ // To avoid 400 Bad Request from the API when it receives malformed JSON
178+ // in the assistant's tool_calls history, we repair it to a minimal valid JSON.
179+ // This allows the framework to parse it, fail schema validation gracefully,
180+ // and send a proper validation error back to the model without crashing the step loop.
181+ return { ...toolCall, args: "{}" };
182+ },
176183 };
177184
178185 const result = streamText(streamOpts) as StreamTextResultFull;