Files
coder/coderd/mcp/testdata
Ethan ea79a9885a fix: return a partial page from read_file instead of an error (#31470)
Closes CODAGT-435

When a `read_file` response would go over 32 KB, the agent returned an
error and no content, telling the model to "Read less at a time using
offset and limit parameters". That's most source files over about 32 KB,
so the model would lose a turn on each one just to pick a smaller range.
#22400 made these hard errors on purpose, so the model wouldn't answer
from partial data, but most of the prior art I looked at (pi-mono,
Gemini CLI, opencode, Cline) just returns what fits and says where to
continue. I think that handles the partial data concern better, as the
model is told exactly which lines it has.

So, the agent now ends the page at whichever comes first of `limit`,
2000 lines, 32 KB, or the end of the file, and returns it as a success.
`workspacetools.ReadFile` then adds a `note` to any page that stops
before the end of the file, so the chat tool and the MCP tool both get
it. Reading `agent/agentfiles/files.go` with no `offset` or `limit` used
to error, and now gives 956 lines with:

> Showing lines 1-956 of 1576. To read more, call this tool again with
offset=957.

The issue suggested returning a chunk plan (e.g.
`[[0,200],[200,600],...]`) instead. The model needs one call per page
either way, and a chunk list would depend on the current limits and line
lengths, whereas the next offset is just `offset + lines_read`.

A `limit` over 2000 is now clamped rather than erroring, and
`total_lines` no longer counts a trailing newline as an extra line,
since the note's maths depends on it. I also dropped the "use offset and
limit to read a portion" hint from the 1 MB error, as that check runs
before `offset` and `limit` are applied. Workspaces running an older
agent will keep returning the error until their agent is updated.

I tested this on a dev deployment through Coder Agents, asking Claude
Haiku 4.5 and GPT-4.1 mini to read a 108 KB and a 118 KB file (3000 and
3200 lines) using only `read_file`, then report the last line. Both
followed the note through to the end of the file (Haiku in 4 calls,
GPT-4.1 mini in 5) and got the last line right, whereas on `main` the
first call just errors. `coder_workspace_read_file` over MCP gives the
same note. The only odd behaviour I saw was Haiku once skipping straight
to the end of a file when the prompt only asked about the last line,
which I think is on the model rather than the tool.
2026-10-30 12:23:17 +11:00
..