mirror of
https://github.com/coder/coder.git
synced 2026-11-22 03:18:31 +08:00
feea5ab959f59cd2135443ed2a0c19bf7643db8f
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
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. |
||
|
|
98332c739b |
feat: align MCP workspace tools with Coder Agents and add execute tools (#30275)
The control-plane MCP server's workspace tools now share their
arguments, behavior, and result formats with the Coder Agents tools. The
MCP server also exposes the Coder Agents execute and process tools.
The shared logic lives in a new `codersdk/toolsdk/workspacetools`
package that both `coderd/x/chatd/chattool` and `codersdk/toolsdk` call.
The Coder Agents tool schemas, descriptions, and JSON responses are
unchanged and the existing chattool tests pass unmodified. The one
exception is the missing-shell guidance, which now reads "Workspace
commands run with `sh -c`" instead of naming Coder Agents. Golden files
in `coderd/mcp/testdata/workspace_tools` hold the shared contract for
each tool, and a test checks both the Coder Agents and MCP tools against
them, so contract changes show up in review. The test also fails if MCP
text names a tool that only exists in Coder Agents.
The MCP versions differ from Coder Agents in four ways:
- Each tool takes a `workspace` argument.
- Invalid arguments and agent API errors come back as MCP tool errors.
- A single call waits at most 5 minutes, matching the agent's maximum
blocking wait. Longer timeouts are rejected with a pointer to
`run_in_background`.
- There is no `model_intent` argument.
**Breaking changes to `/api/experimental/mcp/http` and `coder exp
mcp`:**
| Before | After |
|---|---|
| `coder_workspace_read_file`: byte offset/limit, base64 `content` +
`mimeType`, binary-safe | Line-based `offset`/`limit`, text only,
returns `{content, file_size, lines_read, total_lines}` |
| `coder_workspace_write_file`: base64 `content`, binary-safe |
Plain-text `content`, returns `{ok: true}` |
| `coder_workspace_edit_file` + `coder_workspace_edit_files` |
`coder_workspace_edit_files` only, returns `{files: [{path, diff}], ok}`
|
| `coder_workspace_bash` over SSH: `timeout_ms` (default 60s, 5m cap),
kills the command on timeout (exit 124), trims output |
`coder_workspace_execute`: `timeout` (default 10s, 5m cap), `workdir`,
`run_in_background`. On timeout the command keeps running and the result
has `background_process_id`. Output is untrimmed and capped at 32KB. |
| None | `coder_workspace_process_output`,
`coder_workspace_process_list`, `coder_workspace_process_signal` |
`--allowed-tools` lists and templates that name the removed tools will
need updating. A code search found `coder_workspace_bash` in a couple of
demo templates (`coder/demo-env-templates`, partner demos).
<details>
<summary>Decision log</summary>
- **Shared core vs. duplication.** The execute lifecycle has the most
logic that could drift: waiting in the foreground, handing off to the
background on timeout, recovering a snapshot after transport errors, and
truncating output. It's extracted rather than copied, along with
edit-file validation, read-file handling, and result types. Each surface
keeps its own protocol handling: fantasy and MCP response envelopes,
plan-turn restrictions, `model_intent`, and workspace resolution and
authorization. Struct-tag descriptions must be literals, so the
golden-file test is what keeps them in sync with the shared constants.
Shared result structs keep their fields in alphabetical order so the
JSON matches what Coder Agents produced from maps before.
- **Replace instead of add alongside.** Keeping `coder_workspace_bash`
next to `coder_workspace_execute` would give models two overlapping ways
to run commands. The read and write contracts change anyway, so keeping
the old names wouldn't preserve compatibility. The endpoint and CLI
command are both experimental.
- **5-minute cap on MCP calls.** Each stateless MCP request has to
return before the client gives up. Otherwise the `background_process_id`
needed to follow up is lost and the command keeps running with no ID the
model knows about. Coder Agents has no cap, so its behavior is
unchanged.
- **No SSH fallback.** `execute` doesn't fall back to SSH on older
agents, because SSH can't support the process management contract. The
agent process API and line-based reads shipped in February 2026.
- **Process scoping.** The agent scopes processes by chat ID, which
coderd sets only for Coder Agents connections. MCP calls carry no chat
ID, so they can't see or signal Coder Agents chat processes. All MCP
callers share one process namespace per workspace, which means anyone
with SSH access can list and signal processes another MCP user started.
That matches what SSH access already allows. The existing `ActionSSH`
check in `coderd/mcp_http.go` still runs before every agent connection.
Nothing reaps MCP processes; they run until they exit, are signaled, or
the agent stops.
- **Errors.** As in Coder Agents, command failures and timeouts come
back as result JSON (`success: false`, `background_process_id`). Agent
API errors have transport metadata removed, the same way Coder Agents
formats them.
</details>
---
🤖 Generated by Coder Agents on behalf of @kylecarbs.
|