Files
coder/coderd/x
Mathias Fredriksson 94f40c3a73 fix(coderd/x/chatd): describe rejected tool input in error results (#32925)
When a malformed tool call reaches tool-call admission in `chatd`,
`chatd` does not execute it, persistence discards invalid arguments on
save, and `chatprompt.normalizeToolCallInput` replays calls without a
valid JSON object as `{}`. Previously `malformedToolResult` only told
the model that the input was "not valid JSON", so models saw `{}` next
to that error and started sending `{}` on subsequent calls. (Calls cut
off by the output token limit already receive their own error result in
the chat loop and are unchanged.)

Update `malformedToolResult` to inspect the raw bytes in memory before
persistence discards them. The error result now distinguishes empty or
whitespace-only input, truncated input (reporting how many bytes arrived
before the value ended), and syntax errors (reporting the received byte
length, the `encoding/json` error, and the byte offset). When the input
consists of multiple complete JSON values back to back, it also reports
how many concatenated values arrived. It notes that if the call's input
appears as `{}` in conversation history, that is a placeholder rather
than what was sent.

Tested with `gofmt -l coderd/x/chatd/` and `go test ./coderd/x/chatd/
-run
'TestMalformedToolResultDescribesInput|TestMalformedToolInputReplayDescribesReceivedInput'
-count=1`. Unit cases cover concatenated objects, whitespace-separated
objects, concatenated non-objects, trailing garbage, truncated object,
truncated literal, truncated string escape, empty input, whitespace-only
input, and non-JSON text; an end-to-end replay test asserts that the
second model request carries `{}` alongside the diagnostic error result
(byte length, concatenated value count, parse error, byte offset, and
placeholder note).

Refs CODAGT-1658.

<details>
<summary>Implementation plan and decision log</summary>

This change is separate from the `coder/fantasy` index-less tool-call
merge fix and is still needed alongside it, because truncated or
malformed input from any provider takes the same admission rejection
path.

- Keep the `{}` replay placeholder in
`chatprompt.normalizeToolCallInput`, which every provider accepts.
- Rewrite `malformedToolResult` so it describes the bytes received
before persistence discards invalid arguments:
  - empty or whitespace-only input;
  - truncated input and how many bytes arrived;
- the byte length, `encoding/json` parse error, and byte offset for
other syntax errors;
- the count of concatenated JSON values when the input contains multiple
complete values back to back;
- a conditional note that `{}` in conversation history is a placeholder,
not what the model sent.
- This needs no schema change, only affects newly written results, and
no existing test asserts the old text.
- Rejected alternatives:
- **Replay the raw text:** Google's OpenAI-compatible endpoint returned
HTTP 400 for a merged call in history, and
`chatprompt.normalizeToolCallInput` falls back to `{}` for all
providers.
- **Wrap the raw text in an object:** it invents an argument shape the
model would copy, and it resends up to about 10 KB per failure on every
request.
- **Drop the call and its result from history:** the model loses its
only feedback.
- Tests:
  - A unit table in `toolinput_internal_test.go`.
- An end-to-end chattest run where one call's input is
`{"path":"a"}{"path":"b"}`, asserting the second request carries `{}`
and the new explanation.
- The UI's "Failed to spawn Sub-agent" row with no prompt follows from
the missing arguments (`Tool.tsx`, `subagentDescriptor.ts`) and needs no
change.

</details>

> 🤖 Created with the help of Coder Agents.
2026-12-03 17:28:21 +02:00
..