Follow-up to https://github.com/coder/coder/pull/25601, where I missed
the other way agents avoid `*_internal_test.go`: an `export_test.go`
file that re-exports unexported symbols, just so a `package foo_test`
test can reach them. There were 13 of these on `main`, most recently
from #32063.
This PR removes 8 of them. Tests of package internals are now
`*_internal_test.go` files, with no changes beyond the package name and
the symbols they call. `messagepartbuffer` and `agentcontext` only
needed one small accessor each, so `Buffer.EpisodeCount` and
`Manager.Started` are just exported for real, and their tests stay where
they were.
To stop new ones appearing, the `testpackage` linter no longer skips
`export_test.go` (the 5 that are left are excluded by path), and
`AGENTS.md` and `.claude/docs/TESTING.md` now state the rule.
`export_test.go` is an agent idiom, and imo we should try and get rid of
it. So, once this PR is approved, we'll fix up all the remaining ones in
a follow-up PR, somehow, and undo the lint exclusion when we do.
Zed connects over plain SSH and sends no app name, so its sessions show
up as SSH. When no app name is set, the agent now labels a session `zed`
if its command starts Zed's remote server proxy (`zed-remote-server-…
proxy --identifier …`). `zed` was already in the list of apps Coder
knows, so it now shows up in session counts, connection logs and metrics
with no server change.
Closes ENG-359
Linear:
[CODAGT-1101](https://linear.app/codercom/issue/CODAGT-1101/agent-plugins-read-only-support-behind-the-agent-plugins-experiment)
**tl;dr:** Adds the `agent-plugins` experiment and the Agent API v2.17
messages that carry Agent Plugins data from the agent to coderd. First
of eight stacked PRs adding read-only [Agent
Plugins](https://agent-plugins.org/) v1.0.0 support.
A plugin is a directory with a `plugin.json` manifest, skills under
`skills/`, and an optional `mcp.json`. Across the stack, the agent
discovers plugins in `plugins/` and `.agents/plugins/` under its scan
roots (for example `~/.coder/plugins/<name>`), chatd lists their skills
with plugin attribution, the agent runs the MCP servers of plugins in
the workspace working directory, and the Agents page shows plugin
attribution in the chat context. Hooks, commands, agents, install flows,
and org-managed plugins are out of scope. Background:
[research/agent-plugins-standard.md](https://github.com/coder/scott-misc/blob/main/research/agent-plugins-standard.md)
(internal).
This PR adds `codersdk.ExperimentAgentPlugins` and a `PluginBody` oneof
variant on `ContextResource` (tag 18) that carries the validated
manifest fields and nests the plugin's skills and MCP servers.
`Manifest` gains `agent_plugins_enabled` (field 23), which coderd sets
when the experiment is enabled; the agent discovers plugins only then.
coderd stores each nested resource as its own row with the plugin's name
in a new `plugin_name` column (migration 000642), which the hydrate, MCP
sync, and re-pin queries copy. With the experiment off,
`PushContextState` drops plugin resources and everything nested in them.
Otherwise it rejects pushes with plugin names that fail
`workspacesdk.ValidatePluginName` (the spec's name grammar, shared by
agent and coderd), OK plugins without a name or with a duplicate name, a
non-OK plugin that nests resources, nested entries of the wrong kind,
plugin skill sources outside `<plugin source>/skills/`, and plugin MCP
servers with an empty `server_name` or a source other than `<plugin
name>/<server_name>`. Nested resources count toward the 1000-resource
cap.
Agents now dial v2.17, so a new agent needs a coderd that supports it,
as with every Agent API minor. Turning the experiment off does not
remove plugin data already pinned to existing chats.
<!-- pr-map:start -->
<details><summary>PR map</summary>
`main..4b0cfc9657`
| Category | Files | + | - |
|---|---:|---:|---:|
| prod (backend) | 15 | 342 | 78 |
| test (backend) | 9 | 534 | 3 |
| migrations | 2 | 14 | 0 |
| generated (backend) | 7 | 1216 | 1027 |
| generated (frontend) | 1 | 2 | 0 |
| **Total** | 34 | 2108 | 1108 |
1. `coderd/agentapi/context.go` (+211/-51, API): drops plugin resources
when the experiment is off; otherwise validates plugin names and
nesting, and stores each nested skill and MCP server as its own row with
the plugin's name.
2. `agent/proto/agent.proto` (+39/-9, API): `PluginBody` with nested
`skills` and `mcp_servers`, and `Manifest.agent_plugins_enabled`.
3. `codersdk/workspacesdk/frontmatter.go` (+31/-0): `ValidatePluginName`
and `MaxPluginNameLength` (manifest name grammar).
4. `coderd/database/queries/chats.sql`, `workspaceagentcontext.sql`,
migration 000642 (+28/-7, DB): the `plugin_name` column on both context
resource tables, copied by the hydrate, MCP sync, and re-pin queries.
5. `codersdk/agentsdk/agentsdk.go`, `agent/agent.go` (+18/-4, API):
`Manifest.AgentPluginsEnabled` and `ConnectRPC217WithRole`; the agent
dials v2.17.
6. `coderd/agentapi/api.go`, `manifest.go` (+4): the manifest flag
follows the experiment.
7. `tailnet/proto/version.go` (+8/-1, API): Agent API v2.17.
8. `codersdk/deployment.go` (+4, API): the `agent-plugins` experiment.
</details>
<!-- pr-map:end -->
---
This PR was generated by Coder Agents on behalf of @jscottmiller.
Co-authored-by: Garrett Delfosse <delfossegarrett@gmail.com>
Part of [CODAGT-1430](https://linear.app/codercom/issue/CODAGT-1430).
Stacked on #31384.
Workspace MCP tool results go through the agent's `convertResult`, which
turns an embedded resource into a `[embedded resource: ...]` string and
drops structured content, so a PDF from a workspace MCP server never
reaches the model. Remote MCP tools already go through `mcpclient`'s
converter, which keeps both.
When a call-tool request sets `include_result`, the agent now returns
the MCP `CallToolResult` as-is in `Result` and leaves `Content` empty.
chattool always sets it and converts the result with
`mcpclient.ConvertCallResult`, so workspace and remote MCP tools share
one converter. Older agents ignore the flag and send the legacy
`Content`, which we adapt to MCP content and pass to the same converter.
The workspace MCP docs get a short Tool results section too.
**Stack:**
- #31232
- #31233
- #31234
- #31235
- #31236
- #31237
`agent/unit.Graph` used gonum's `simple`, `topo`, and `encoding/dot`
packages, which pull in `gonum/mat` and friends (~2 MB) for a graph with
a handful of nodes.
Replace it with forward/reverse adjacency maps, a DFS reachability check
for cycle detection, and a small DOT writer. The existing golden files
are unchanged.
<details>
<summary>Context: binary size investigation</summary>
Measured with full `make build/coder_linux_amd64` builds (zstd `-22
--ultra`) of `main` vs. the whole stack:
| Artifact | Before | After |
|---|---|---|
| `coder` (fat, linux/amd64) | 469 MB | 339 MB |
| `coder-slim` (linux/amd64) | 62 MB | 52 MB |
| Embedded `site/out` | 169 MB | 72 MB |
| Embedded `coder.tar.zst` | 83 MB | 60 MB |
Stack, each PR removes one source of size:
1. Hidden frontend source maps (~97 MB of embedded assets)
2. Slim tarball ordering so zstd `--long` dedupes across OSes (~14 MB on
the embedded archive)
3. `aisdk-go` only supplied two struct types but pulled in `openai-go`
v1 and `genai`
4. `gonum` used only for a tiny DAG in `agent/unit`
5. `gofakeit` root package links every faker; only two word lists are
used
6. Hugo `pageparser` used only to read YAML front matter in `templates
push`
Sizes were attributed with `go-size-analyzer` and an import-graph
dominator pass. Not in this stack: compiling out trivy's module download
resolvers in `coder/trivy` (~29 MB, drops go-getter, GCS, and S3 SDKs),
swagger-ui's bundled source maps (~7 MB), and OPA's embedded historical
capability JSON (~4 MB).
</details>
---
_Generated by Coder Agents on behalf of @kylecarbs._
The agent now sets CODER_WORKSPACE_TEMPLATE_ID and
CODER_WORKSPACE_TEMPLATE_NAME for every command it runs, from new
template_id and template_name manifest fields. Templates run different
scripts in different orders, so a user's own script needs to know which
template it runs under to coordinate with that template's scripts.
The Agent API goes to v2.16 because release/2.39 shipped v2.15.
CODER_WORKSPACE_BUILD_ID loses its nil checks: an agent only connects
to servers that support its API version, and those always send the
build ID. The environment variables docs now list the variables the
agent sets.
The recovery write-deadline test can finish its message assertions while
the fake harness is still sending session updates, letting cleanup close
the session underneath notification handlers. Wait for the prompt turn
to reach idle after checking the HTTP response, using the existing
`awaitTestSession` helper.
Refs
[CODAGT-1500](https://linear.app/codercom/issue/CODAGT-1500/flake-testapisessionrecoverywritedeadlinemessage).
<details>
<summary>Implementation decision</summary>
Use test-side synchronization rather than allowing the teardown ERROR or
changing production shutdown. The fake harness sends its updates before
its prompt response, and the pinned SDK waits for those notification
callbacks before returning the prompt result. Requiring idle prevents a
failed turn from satisfying the wait. HTTP write-deadline assertions
remain before the wait; production logging and lossless disconnect
draining are unchanged.
</details>
Generated by Coder Agents on behalf of @hugodutka.
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.
Route ACP SDK diagnostics through the workspace agent's Coder logger
instead of the process-wide standard slog logger, which Trivy replaces
with an unsafe buffering handler. Install the logger before starting the
harness so even immediate output and disconnects use the configured
logger.
Bump the ACP SDK to commit `0da7315c23b5`, which merged the synchronized
`SetLogger` fix into SDK main in
https://github.com/coder/acp-go-sdk/pull/59. Preserve diagnostic levels
and structured fields, identify the harness, and omit raw protocol
frames that can contain conversation content or workspace secrets. The
previous Trivy dependency update is removed;
https://github.com/coder/trivy/pull/87 is closed as superseded.
Fixes
https://linear.app/codercom/issue/ENG-3489/flake-testagent-acpreadiness-data-race-in-trivy-slog-deferredhandler
Generated by Coder Agents on behalf of @hugodutka.
> Xum opened this PR for Mike.
`read_skill_file` checked the requested path lexically, then read
`<skill dir>/<path>` through the agent's general file reader, which
follows symlinks. A supporting file that is a symlink to a file outside
the skill directory returned that file's contents to the model.
The agent's `read-file` endpoint now accepts an optional `root`
parameter. With it, the agent opens `root` with Go's `os.OpenRoot`,
refuses it if `root` itself is a symlink, and opens the file with
`Root.Open`, which follows symlinks only while they stay beneath `root`
and refuses absolute symlinks. `os.Root` opens one path component at a
time relative to the directory it already holds and resolves each
symlink itself, so a file swapped for a symlink during the walk cannot
slip through. `codersdk` adds `AgentConn.ReadFileInRoot`, and
`read_skill_file` reads supporting files through it. Reads without
`root` are unchanged.
- Relative symlinks that never leave the skill directory still work.
Absolute symlinks, and links that climb out of the directory, are
refused. Refusal messages never name the link target.
- The agent refuses a `root` that is itself a symlink, because
`os.OpenRoot` follows it: a skill directory replaced with a symlink to
`/etc` would otherwise become `/etc`. It compares the opened directory
with an `Lstat` of `root`, so a swap between the two calls is also
refused. Symlinks in the parent directories of `root` still work.
- Agents older than this change reject the `root` parameter, so
`read_skill_file` fails closed in a workspace until it restarts with the
new agent.
- Not in this PR: the agent's `resolve-path` endpoint joins a relative
link target to the parent path before resolving the parent's own
symlinks, so it can return the wrong file. Plan path checks still use
it; skill reads no longer do.
Fixes CODAGT-1496
## Testing
- New tests: `TestReadFileInRoot` (handler, real symlinks),
`TestReadFileInRootSwap` (swaps `root` for a symlink while reads run),
`TestReadFileInRootRegularDirectory` (no symlinks, so it also runs on
Windows), `TestAgentConnReadFileInRoot` (SDK over tailnet against the
real handler), `TestReadSkillFileTool/AgentRefusalIsToolError`. The
refusal tests fail when the protection is removed (plain open instead of
`os.Root`, no check that `root` is not a symlink, SDK joining root and
path, or the tool dropping the agent error). Without the `root` check,
`TestReadFileInRootSwap` returned outside bytes in 45 of 500 reads.
- Remote dogfood UAT passed on 132c7bed66 with a real model: allowed
reads (including an in-skill directory symlink followed by `..`),
refused escapes with no outside bytes or target paths in errors, a
30-call symlink swap race with no outside bytes, and an old agent
failing closed. The merge base leaked `/etc/passwd` in the same setup.
Later commits change tests and docs, rewrite agent errors for the model,
and add the `root` symlink check. Unit tests cover those changes; the
UAT did not.
- Cleanup passes stayed inside the implementation diff: polish touched
only `skill_test.go`, and the test audit added only `agentconn_test.go`.
No new dependencies.
---------
Co-authored-by: Ethan Dickson <ethan@coder.com>
Fixes the `TestServer_Metrics/Forwarding` flake reported in
[PLAT-609](https://linear.app/codercom/issue/PLAT-609/flake-testserver-metricsforwarding)
and, in addition, other defects similar to it.
## Testing defects
### The Original
`TestServer_Metrics/Forwarding` waited for `batches_dropped_total` and
then asserted on `logs_dropped_total`. The forwarder updates those two
counters separately, on adjacent lines, so a read between them sees the
first updated and the second not. CI caught exactly that window:
expected 2, actual 0 at `proxy_test.go:780`. Each counter is atomic on
its own, so the race detector reports nothing.
### The Additional
The fallacy of the original is a _de facto_ assumption that sequential
updates are atomic as a group. The defect arose from not taking into
account that they are atomic individually. The test asserted before the
work was completed. A repo-wide sweep for related metrics written
together found similar mistakes in other tests:
- Live races: all three phases of `TestServer_Metrics/Forwarding` and
`TestServer_Metrics/BufferFull` in `agent/boundarylogproxy`, and
`TestMetadataBatcher_DropsWhenFull`, which waited for the update channel
to empty although the final flush runs after that.
- Correct only because of the current implementation's ordering:
`TestMetrics_TokenUseCount` in `aibridge`, four tests in
`coderd/agentapi/metadatabatcher`, and `TestAgentStatsSessionCounts` in
`coderd/prometheusmetrics`. These pass today but would start failing if
the code under test reordered its work.
## Fixes
All tests the sweep found were fixed. Each test now waits for every
value it asserts before asserting it. Where a value is already non-zero
from earlier work, or legitimately zero, the wait is for the final value
rather than for a change, and several tests use
`require.EventuallyWithT` so the wait and the assertion are one step.
Two Forwarding phases gain assertions on values they already waited for:
`batches_dropped_total == 1` in phase 1, and the reporter's recorded
request count in phase 2.
Two phases of `TestAgentStatsSessionCounts` assert that the counts did
not change. No wait on an unchanged value can show that the collection
cycle ran, so they keep waiting on the execution histogram, and a
comment at that wait names the dependency.
Each fix was checked by temporarily blocking or reordering the code
under test between the relevant writes: the old test failed on every run
and the new one passed.
## Documentation
As a guard against recurrence of defects of this kind, we add the
following documentation targeted mostly at agents. The material in
`TESTING.md` provides guidance about how to write such a test correctly
in the first place. That in `AGENT_FAILURES.md` describes how to
recognize the symptoms of this class of test failure, should they arise.
- `.claude/docs/TESTING.md`: new section "Wait for Full Completion
before Asserting", covering waits on one value with assertions on
another, Prometheus metrics in particular, and assertions that nothing
changed.
- `.claude/docs/AGENT_FAILURES.md`: a catalog entry for the symptom.
- `coderd/coderdtest/promhelp`: doc comments noting that each helper
call is a separate gather.
Generated with Claude Code under the direction of @eric-hughes-coder.
ACP harness resources bypass context drift detection, but only MCP
resources were live-synced into bound chats. Later ACP discoveries,
capability updates, probe errors, and removals could therefore leave
chat context stale without offering a refresh. Extend the existing
live-sync query to include ACP harnesses and publish context updates for
affected chats while keeping instructions and skills pinned.
Depends on #31350. This PR contains the ongoing synchronization change;
#31350 fixes initial probe ordering.
ACP discovery ran asynchronously after workspace readiness, so the
initial context snapshot could omit harnesses installed by startup
scripts. Wait for the initial discovery and probe pass after startup
scripts and before readiness and the first context push, preserving the
existing 30-second discovery timeout and periodic polling.
The initial snapshot includes successful harness capabilities and failed
probe errors. This addresses the startup race seen with the [dogfood ACP
template](https://github.com/coder/dogfood/commit/dfcbf3f8a336b18d16fd31bb7d6375a6bc548c5e).
The ACP fake harness rejects valid working directories on macOS when
`/var/...` and `/private/var/...` refer to the same directory. Compare
filesystem identity with `os.Stat` and `os.SameFile` instead of
comparing path strings. Production code is unchanged.
Adds a discovery regression that probes through a symlinked working
directory. It reproduces the original harness error on Linux before the
fix and succeeds afterward.
Fixes
https://linear.app/codercom/issue/CODAGT-1450/flake-testdiscovery-multiple-variants
<details>
<summary>Implementation rationale and validation evidence</summary>
Filesystem identity accepts directory aliases while preserving rejection
of a different working directory or unexpected MCP servers. The
regression exercises the real ACP subprocess through `Manager.Reload`;
it skips Windows because creating symlinks requires developer mode or
administrator privileges.
The regression failed before the fix with `unexpected directory or MCP
configuration`. The complete ACP package passed 20 runs, plus 10 runs
with the race detector. Targeted standard and new-code golangci-lint
checks and `paralleltestctx` passed. Local validation ran on Linux. CI
passed, including Go tests on Linux, macOS, and Windows and the
race-enabled Go tests.
</details>
Generated by Coder Agents on behalf of @hugodutka.
Expose agent-local ACP session creation, messaging, interruption,
transcript reads, and event streams through the agent HTTP and WebSocket
API and workspace SDK. Support cursor-based reconnection and extend
response write deadlines to allow harness initialization and session
recovery to finish.
Depends on #30857.
Generated with Codex, carefully reviewed by Hugo.
Restore missing or failed agent-local ACP sessions using load or resume,
preferring load to replay the latest turn's history. Share concurrent
recovery attempts and preserve failed transcripts without automatically
retrying messages.
Depends on #30903.
Generated with Codex, carefully reviewed by Hugo.
Add atomic transcript snapshots and live event subscriptions for
agent-local ACP sessions, with cursor-based replay across turns. Close
subscriptions on cancellation, shutdown, or buffer overflow so slow
consumers do not block session updates.
Depends on #30841.
Generated with Codex, carefully reviewed by Hugo.
Cover agent-local ACP session initialization, configuration, environment
propagation, permission callbacks, and transcript ordering with a fake
harness. Verify busy-session rejection, interruption, write deadlines,
and preservation of partial output after failures.
Depends on #30835.
Generated with Codex, carefully reviewed by Hugo.
Create and run agent-local ACP sessions with configurable harnesses,
prompt submission, interruption, and ordered transcripts. Track session
status and failures, scope permission grants to running turns, and stop
session processes when the manager shuts down.
Tests are in the next PR in the stack.
Depends on #31128.
Generated with Codex, carefully reviewed by Hugo.
Publish discovered ACP harnesses, their capabilities, and configuration
options in agent context snapshots. Refresh snapshots when the harness
catalog changes, keep launch configuration local, and apply snapshot
limits without treating harness updates as changes to pinned context.
Depends on #31057.
Generated with Codex, carefully reviewed by Hugo.
Probes configured ACP harnesses to discover session load and resume
capabilities and available configuration options. Caches successful
probes until the configuration, workspace directory, or environment
changes, and exposes probe errors through the agent API and workspace
SDK.
Depends on #30557.
Generated with Codex, carefully reviewed by Hugo.
Discovers ACP harnesses configured in `~/.coder/acp/*.json`, validates
their configuration, and refreshes the cached catalog every 30 seconds.
Exposes harness names and configuration errors through the agent API and
workspace SDK.
Generated with Codex. Carefully reviewed by Hugo.
Stacked on #30484.
The agent only reconnects workspace MCP servers when `.mcp.json`
changes, so a server that is down when the agent first connects (for
example, a dev server like Storybook started after the workspace is up)
stays unavailable until the config file is touched.
`agentcontext.Manager.Resync` now calls a new
`agentmcp.Manager.Reconnect` before resolving. `Reconnect` re-runs the
last MCP reload even when the config is unchanged, so servers that
failed to connect are retried; connected servers keep their session
because their config is unchanged. This makes `coder exp chat context
refresh` (no argument) and the agent's `POST /api/v0/context/resync`
retry failed servers. Resync is only called on explicit user request
today, so no background traffic is added.
Not covered here: a server that restarts after it connected keeps its
stale session, since its config is unchanged.
<details>
<summary>Plan and decisions</summary>
**Problem**
1. **Never connected.** A server that is down at the first reload is
listed as `failed to connect` until the config file changes, because
`startReloadIfNeeded` short-circuits when `SnapshotChanged` is false.
2. **Connected, then dead.** A server that restarts after connecting
keeps its stale `ClientSession`; `classifyServers` keeps it because its
config is unchanged. Out of scope for this PR.
**Current wiring**
- Reload triggers: once after startup scripts (`agent/agent.go`) and the
`.mcp.json` fsnotify watcher.
- After a reload changes the catalog, the agent pushes context and
coderd live-syncs MCP rows into every bound chat, so new tools reach
chats on their next turn without a chat refresh.
- `coder exp chat context refresh` (no argument) calls the agent socket
`ResyncContext`, which runs `agentcontext.Manager.Resync`. Before this
PR, Resync re-read the MCP catalog but did not reconnect.
- The UI "Refresh context" button calls `chatd.RefreshChatContext`,
which is DB-only (re-pin) and never reaches the agent.
**Options considered**
| Trigger | Latency | Cost | Decision |
| --- | --- | --- | --- |
| A. Periodic agent ticker while a server is unhealthy | Up to one
interval | Periodic dials to dead servers | Rejected |
| B. On-demand via agent Resync (CLI, agent HTTP route) | Immediate |
None unless invoked | This PR |
| C. On-demand via UI "Refresh context", running the same agent resync
as the CLI | Immediate | None unless clicked | Follow-up PR |
| D. Per-turn nudge from chatd | Next turn | One agent request per turn,
fleet-wide | Rejected |
**Implementation choices**
- `Reconnect` clears the stored config file snapshot and calls `Reload`,
rather than adding a force flag to the reload path. `SnapshotChanged`
then reports a change, the singleflight reload runs, and
`installServers` writes a fresh snapshot. False-positive reloads were
already documented as safe.
- No backoff or retry state.
**Follow-up (C)**
Make the existing "Refresh context" button run the same agent resync as
the CLI (coderd calls the agent's `/api/v0/context/resync`, then
re-pins), and render it when MCP server issues exist.
</details>
---
Generated by Coder Agents on behalf of @jeremyruppel.
#31011 fixed a flake where a closure in `TestProjectSkillQueries` used
the parent test's `testutil.Context` from parallel subtests. Once that
deadline passed, the closure's `require.NoError` called `FailNow` on the
parent `t`, which panics and takes down the whole `coderd/database`
package. `paralleltestctx` only checked direct references to the
context, so it missed this. coder/paralleltestctx#6 now follows calls to
local closures too.
This pins `paralleltestctx` to the current `main` commit, as there's no
tag yet. That commit also brings in the outer variable reassignment
check from coder/paralleltestctx#4 and the `x/tools` bump from
coder/paralleltestctx#5. The closure check flagged the `loadPrompt`
helper in `TestMediaToolResultRoundTrip`, so it now makes its own
context from the subtest's `t`. The `TestGraph` and `TestRuntimeAudit`
fixes for the outer variable check are the same as in #30288, so that PR
only needs to repin.
Workspace stops and deletes closed active SSH connections with generic
shutdown or process-exit reasons. This obscured the workspace action
that ended a connection and made related logs harder to correlate.
The agent’s SSH tracking and logging now identify connections closed by
the SSH server as server_shutdown, while preserving earlier client or
network disconnects. The agent includes the client session ID when
available for ordinary sessions and connections used only for port
forwarding.
Coderd now enriches shutdown reasons when it processes connection
reports. It translates server_shutdown into workspace_stopped when the
latest build is a running or successful stop or delete requested before
the connection closed. Coderd persists the enriched reason and logs the
build reason, transition, and build ID alongside the client session ID.
Unavailable or unrelated build state retains the generic shutdown
reason.
The CLI now logs shutdown context once per stop or delete build through
its existing workspace watcher. This includes the client session ID when
available and preserves the existing graceful SSH shutdown behavior.
Regarding the PTY connections, when the agent shut down, the
reconnecting PTY server closed each PTY
and its attached connections, and the connection handler then returned
without an error. The server therefore logged and reported those
connections as graceful closes. Its fallback for a forced shutdown
reported a free-form reason that coderd did not recognize, so coderd
never refined either case into a workspace shutdown.
We change the reconnecting PTY server so that it reports any connection
that ends after the agent begins shutting down as a server shutdown
initiated by the agent, and its log keeps the client session ID. The
forced shutdown fallback now reports the same standard reason, which
lets coderd translate it into a workspace stop or delete in the
connection log, as it already does for SSH. A client that closes its
connection before the shutdown still keeps its graceful reason.
Regression tests cover shutdown attribution, fallback behavior, CLI
logging, and PostgreSQL persistence with and without a client session
ID. The test logging helper also synchronizes with cleanup to prevent
logging after a test completes
- resolves
https://linear.app/codercom/issue/CLIENT-665/close-workspace-tunnel-with-a-reason
<!--
If you have used AI to produce some or all of this PR, please ensure you
have read our [AI Contribution
guidelines](https://github.com/coder/coder/blob/main/contributing/AI_CONTRIBUTING.md)
before submitting.
-->
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
feat(agent): report skipped and canceled script outcomes
Send startup-script outcomes to coderd and the Build Timeline, completing
the agent-side support for #30875.
- Report one zero-duration timing for scripts skipped or canceled before
starting. Interrupted startup scripts report `canceled` instead of
`exit_failure`, including on templates without ordering.
- Add cancellation logs for ordered scripts waiting on dependencies or
running. Completed outcomes, timeouts, exit codes, and stop/cron
reporting remain unchanged.
- Give reports an independent timeout and make `Runner.Close` wait for
in-flight executions so shutdown does not discard cancellation timings.
Synchronize cron startup with `Close` to prevent shutdown hangs.
- Dial Agent API v2.15; agents now require coderd supporting that version.
Tests cover skips, cancellation, duplicate prevention, and shutdown races.
Race-detector tests and all 14 manual scenarios passed.
Refs https://linear.app/codercom/issue/PLAT-550/phase-3-expose-skipped-and-canceled-script-outcomes
feat: expose skipped and canceled script outcomes
Add `skipped` and `canceled` statuses across the Agent API, database,
SDK, and Build Timeline for startup scripts.
- Store and return both outcomes, preserving zero-duration timings,
and reject unknown statuses. Rollbacks rewrite the new outcomes to
`exit_failure` with exit code 255.
- Add typed legends, outcome-specific tooltips, and keyboard-accessible
bars. Fix timeline crashes for `timed_out` and `pipes_left_open`.
- Bump the Agent API to v2.15 while agents continue using v2.14.
Add API, migration, and Storybook coverage.
First of two PRs for PLAT-550. Agents do not emit the new statuses yet;
a follow-up after #30765 enables reporting. Shutdown scripts remain
outside the timeline, and agent health and badge rules are unchanged.
Refs: https://linear.app/codercom/issue/PLAT-550/phase-3-expose-skipped-and-canceled-script-outcomes
`coder secret list` now shows the current workspace build's secrets next to your user secrets, and which user secrets they replace. Part of [RELAY-7](https://linear.app/codercom/issue/RELAY-7/agent-relay-deliver-provider-credentials-via-coder-secrets-instead-of).
**Stack** (design decisions in #30248)
1. #30248 Database schema, queries, and RBAC
2. #30249 Encryption at rest
3. #30250 Set secrets on workspace builds
4. #30251 Deliver secrets to the agent
5. #30252 Docs
6. **#30388 List a build's secrets (this PR)**
7. #30459 Audit a build's secrets
- New `GET /workspacebuilds/{workspacebuild}/secrets` lists a build's secrets (metadata only, never values) and which user secret each one replaces.
- Inside a workspace, `coder secret list` includes the current build's secrets. Outside a workspace, use `--workspace` or `--build-id`.
- The agent now exposes `CODER_WORKSPACE_BUILD_ID`, so the CLI knows which build it is running in.
- The agent manifest and the new endpoint use one shared resolver, so they always agree on which secret wins.
- The user secret API is unchanged.
<details>
<summary>Implementation details</summary>
- The manifest carries `workspace_build_id`; the agent sets it as `CODER_WORKSPACE_BUILD_ID`.
- `coderd/workspacesecrets.Resolve(user, build, policy)` decides which secret is delivered on each env var and file. The manifest no longer relies on entry order.
- `codersdk.WorkspaceSecret` embeds `UserSecret` and adds `workspace_build_id`, `ephemeral`, `cleared_at`, `env_replaces`, and `file_replaces`. Secrets whose values a later build cleared are still listed, with `cleared_at` set.
- CLI columns: `source` and `replaced by` are shown by default only when a build is included; `ephemeral` is opt-in. The CLI makes two requests: user secrets, then build secrets.
- Agents that predate the build ID fall back to `CODER_WORKSPACE_ID` and its latest build.
</details>
<details>
<summary>Decision log</summary>
(existing decision log, unchanged)
</details>
---
*Created by Coder Agents on behalf of @Emyrk.*
feat(agent): enforce lifecycle script ordering
Honor `coder_script_order` dependencies for startup and shutdown scripts
using the existing outcome-aware unit manager. Cron, legacy scripts,
and scripts without dependencies retain their existing behavior.
- Wait for all prerequisites before running. `success` requires a
successful prerequisite; `completion` also accepts failure, timeout,
or skip. Skip dependents as soon as a required success becomes
impossible, preserving the original failure as the lifecycle error.
- Reject invalid dependency graphs before any scripts start and write
the reason to every selected script's log.
- Log unmet dependencies every 30 seconds and explain skips. Populate
display names in the manifest so skip messages use readable names,
falling back to Terraform addresses.
- Exclude dependency waits from script timeouts, record an outcome on
every exit, and use fresh state per execution. Cancellation stops
waiters without starting dependents or emitting skip messages.
Add regression coverage for ordering, skip propagation, timeouts,
cancellation, invalid graphs, and unchanged unordered execution.
Refs https://linear.app/codercom/issue/PLAT-549/phase-3-enforce-lifecycle-script-ordering
test: add memory guard and heap profiles for CI timeouts
Add a memory guard to `cli`, `agent`, and `enterprise/cli` to diagnose
recurring macOS `test-go-pg` timeouts. Recent logs show runaway memory
usage and heavy swapping, but the allocation source remains unknown.
- Check Go runtime memory once per second, with per-process CI limits
of 5 GiB on macOS and 6 GiB on Linux and Windows. Disabled when
`CODER_TEST_MEMORY_LIMIT_MIB` is unset.
- On exceeding the limit, save a heap profile, attempt a second profile
after forced GC with a five-second wait limit, then panic with memory
usage, profile paths, and all goroutine stacks.
- Upload heap profiles from unsuccessful CI jobs on every OS, retaining
them for seven days.
- Test guard lifecycle, environment parsing, panic diagnostics, profile
validity, and post-GC allocation attribution. Validate with race
tests, vet, lint, and a manual macOS test.
Refs https://linear.app/codercom/issue/PLAT-251/flake-test-go-pg-macos-job-timeoutcancelled
Refs https://github.com/coder/internal/issues/1365
feat: include script dependencies in agent manifests
Send persisted script Terraform addresses and dependency lists on every
manifest request, preserving them across agent restarts, reconnects, and
shutdown without rebuilding the workspace.
- Add `resource_address` and `dependencies` to the agent proto, with
`SUCCESS` and `COMPLETION` requirements.
- Reuse shared database-to-SDK validation and add bidirectional SDK/proto
conversion. Reject empty prerequisite addresses and invalid requirements
rather than accepting partial lists.
- Bump the agent API to v2.13 and update the agent's RPC connection.
Older agents ignore the new fields; older servers return empty fields.
- Test manifest delivery, subagent isolation, round trips, invalid
dependencies, and unchanged encoding when the new fields are unset.
Script execution is unchanged; ordering enforcement follows in PLAT-549.
Production dependency lists remain empty until #30556 merges.
Stacked on #30625. Refs PLAT-548.
A git watch `refresh` only rescanned the repos already being watched. A
path recorded before its repo existed, for example before `git init`, or
a repo that was deleted and recreated, stayed hidden until the client
reconnected, so a page reload showed repos the refresh button didn't.
Refresh now resolves the chat's paths again, as a new connection does.
The first scan of a connection is also sent even when no repos are
watched, so a client can replace state left over from an earlier
connection; later scans with no repos still send nothing.
### Before
https://github.com/user-attachments/assets/33bf9046-0448-4537-9a97-c97f51e1cc39
### After
https://github.com/user-attachments/assets/82b15d03-6460-4fe0-b5e7-b4bafbbd72d1
<details>
<summary>Decision log</summary>
The git panel's refresh button should show everything a page reload
would. Investigation found five ways it didn't, each fixed in its own PR
on the current `changes` protocol:
1. `execute` without a workdir didn't record the directory the process
ran in (`agent/agentproc`).
2. Refresh only rescanned repos already being watched, so a repo created
or recreated after its path was recorded stayed hidden
(`agent/agentgit`). A connection with no repos also got no first
message.
3. After a git watch reconnect, the new full state was merged into the
previous connection's map, so deleted repos stayed visible
(`useGitWatcher`).
4. A chat watch reconnect didn't refetch the open chat, so missed
`diff_status_change` events were lost (`AgentsPageLayout`).
5. Refresh didn't refetch the chat, whose diff statuses back the PR
tabs, and was disabled until git context existed (`GitPanel`,
`AgentChatPageView`).
Considered and deferred: a snapshot protocol with diffs fetched by hash,
which would keep large multi-repo diffs under the 4 MiB proxy read
limit, and making scans faster at scale. Neither is needed to fix
refresh, and together they made a single ~5900-line change that couldn't
be reviewed. The refresh spinner still runs for a fixed time, since
tying it to the response needs a request ID in the protocol.
</details>
---
_Generated by Coder Agents on behalf of @DanielleMaywood._
A process started through the agent's process API without a `workdir`
runs in the agent's default directory, but only an explicit `workdir`
was recorded in the chat's path store. Repositories touched that way
were never watched by the git panel.
The path store now records `proc.workDir`, the directory the process
actually ran in, whether requested or defaulted.
### Before
https://github.com/user-attachments/assets/b98c290a-0c77-4ce4-bb00-eb4088d2067e
### After
https://github.com/user-attachments/assets/0383aa1a-eced-4338-97af-3c54501cdedb
<details>
<summary>Decision log</summary>
The git panel's refresh button should show everything a page reload
would. Investigation found five ways it didn't, each fixed in its own PR
on the current `changes` protocol:
1. `execute` without a workdir didn't record the directory the process
ran in (`agent/agentproc`).
2. Refresh only rescanned repos already being watched, so a repo created
or recreated after its path was recorded stayed hidden
(`agent/agentgit`). A connection with no repos also got no first
message.
3. After a git watch reconnect, the new full state was merged into the
previous connection's map, so deleted repos stayed visible
(`useGitWatcher`).
4. A chat watch reconnect didn't refetch the open chat, so missed
`diff_status_change` events were lost (`AgentsPageLayout`).
5. Refresh didn't refetch the chat, whose diff statuses back the PR
tabs, and was disabled until git context existed (`GitPanel`,
`AgentChatPageView`).
Considered and deferred: a snapshot protocol with diffs fetched by hash,
which would keep large multi-repo diffs under the 4 MiB proxy read
limit, and making scans faster at scale. Neither is needed to fix
refresh, and together they made a single ~5900-line change that couldn't
be reviewed. The refresh spinner still runs for a fixed time, since
tying it to the response needs a request ID in the protocol.
</details>
---
_Generated by Coder Agents on behalf of @DanielleMaywood._
Agents now report how a connection was made and which app made it, so
the connection log records the method and the client app separately.
- Bump the Agent API to v2.13. `Connection` gains `connection_method`
and `app_name`, and `type` is deprecated.
- SSH and reconnecting PTY handlers set the method themselves, so a
client-supplied app name can't change it.
- SSH sessions report the app from `CODER_SSH_SESSION_TYPE`. Sessions
without one count as SSH but report no app.
- Disconnect reports repeat the connect report's method, app, and
client session ID.
- Coderd logs the reported method and normalized app name. For agents
older than v2.13, it maps `type` as before.
- Coderd rejects unknown methods instead of logging them as SSH.
Closes CLIENT-1037
Stack context: two-layer stack tightening Go lint gates. This bottom
layer fixes the lint exclusion; the layer above adds new rules.
Why: a `.git` path exclusion in the golangci config was hiding Go files,
masking 70 findings. This fixes the exclusion, fixes the findings
(`agent/agentgit` now runs git through `agentexec.Execer`), and adds a
`make lint/go` check that fails when an exclusion hides Go files. `make
lint/go` now waits for a concurrent golangci-lint run instead of
failing. The exhaustruct exclusion rule for `scripts/*` had the same
problem: as a regex it also matched `agent/agentscripts` and
`coderd/agentapi/scripts.go`, so it is now `^scripts/`.
Validation: full-tree lint reports 0 issues; the new check fails on the
old entry and passes on the new one.
> Xum acted for Mike to open and update this PR.
tl;dr is this should fix the storybook mcp server in Coder Agents. the
storybook mcp never responds to the SSE GET, so it never connects, and
reload fails. this disables that SSE GET and should let storybook
reconnect on config reload
---
HTTP workspace MCP servers whose `/mcp` endpoint never answers the
standalone SSE GET (for example, the Storybook dev server) hang the
agent's MCP reload indefinitely. The go-sdk opens that GET inside
`Connect` on a context the agent's 30s connect timeout does not bound,
and because reloads are singleflighted, every later `.mcp.json` change
waits behind the hung one. The server never shows up as connected or
failed, and no MCP config change is picked up until the agent restarts.
Set `DisableStandaloneSSE: true` on the streamable HTTP transport. The
agent's client registers no handlers for server-initiated messages, so
the stream is unused; tool call responses and in-request notifications
still arrive on the POST.
<details>
<summary>Investigation and decision log</summary>
**Symptom:** a `storybook` HTTP server in `.mcp.json` never produced
`storybook__*` tools in chats, while stdio servers worked.
**Findings:**
- Agent log showed the startup connect failing with `connection refused`
(Storybook was not up yet), and a later reload failing with `standalone
SSE request failed (session ID: ...)`, meaning `initialize` had
succeeded and the GET stream was the failure point.
- After touching `.mcp.json` with Storybook running, no reload result
was ever logged (success or warning).
- A standalone probe using go-sdk v1.8.0 and the same transport config
hung in `Connect` past its 30s timeout. Goroutine dump: `Client.Connect
-> sessionUpdated -> connectStandaloneSSE -> connectSSE`, which uses the
connection's lifetime context, not the connect context.
- `curl` against Storybook: `POST initialize` returns a session ID; `GET
/mcp` with that session sends no response headers at all. The spec
requires either `text/event-stream` or 405, so Storybook is
non-compliant, but the agent should not wedge on it.
- With `DisableStandaloneSSE: true`, the same probe connected in ~7ms
and listed all 8 Storybook tools.
**Why disable instead of alternatives:**
- `mcp.NewClient(..., nil)` in `manager.go` registers no
`tools/list_changed`, sampling, elicitation, roots, or logging handlers,
so nothing on the standalone stream is consumed today.
- The spec makes the standalone GET optional for clients; responses to
client requests are never sent on it.
- Bounding `Connect` with our own timeout would require abandoning a
stuck goroutine and session per bad server, since the hang is inside the
SDK call on a context we do not control.
- Tradeoff: live `tools/list_changed` refresh would need the stream
back, opened after `Connect` and off the reload path.
**Out of scope, noted for follow-up:**
- A server that is down when the agent first connects is not retried
until `.mcp.json` changes.
- A server that restarts invalidates the session ID, so the next POST
fails and nothing reconnects the session. Disabling the standalone
stream does not change this.
</details>
---
Generated by Coder Agents on behalf of @jeremyruppel.
Move the upgrade listener close to where it is actually used rather than
smushing it with the reconnecting pty listener close.
Also add a comment explaining why we close it even though the ssh server
is supposed to do that.
Skip `TestAgent_ToolCall` and `TestEditMessageCancelsToolCallsOnAgent`
until the agent netstack fix in PLAT-717 lands.
The agent's gVisor netstack accepts connections through `tcp.Forwarder`
and has no listening endpoint, so it drops a SYN that reuses a source
port it still holds in TIME_WAIT (google/gvisor#15013). Agent API
requests open a new connection each and the agent closes first, so a
client that reuses a port within 60s hangs for about 63s. Both tests
make enough requests to one agent to hit it (CODAGT-1336, CODAGT-1331).
The same stall was reproduced against a production agent on dogfood, so
this is a product bug, tracked in PLAT-717.
feat(agent): add outcome-aware unit dependencies
Extend the unit manager for declarative script ordering without changing
existing status dependencies or `coder exp sync` behavior.
- Add a separate conditional dependency graph with `success` and
`completion` requirements, outcome tracking, and runnable, skipped,
or waiting decisions.
- Failed, timed-out, or skipped prerequisites skip `success` dependents
but satisfy `completion`. Canceled or unregistered prerequisites keep
dependents waiting.
- Add context-cancelable waiting with no built-in timeout and sorted
unmet prerequisites for logging. Callers remain responsible for
recording skips.
- Enforce immutable final outcomes, reject cycles and conflicting edge
requirements, and accept identical duplicate edges.
- Add race-tested coverage for outcome semantics, validation, graph
isolation, cancellation, and concurrent waiters.
No callers yet; script-runner integration follows in PLAT-549.
Refs PLAT-546.
We have flakes where the agent's close hangs, theorized to
be because the new ssh upgrade listener is never closed.
It does seem strange though, the listener should be closed by the ssh
server since it closes all the listeners passed into it...so this may
not do anything, but it seems wise to be explicit anyway.
Add a new /tcp endpoint on the agent that is used to pass along
the client session ID (with the existing baggage mechanisms). The agent
then can upgrade that into the desired connection type, based on the
port. For now that is only the standard SSH port and reconnecting pty.
The `ssh`, `ssh --stdio`, `ssh --stdio` over Coder Connect, `exp rpty`,
and web terminal paths are all updated to use this new endpoint, and
fall back to the old method when the endpoint is not available.
That ID is added to the log context, and is also used to populate the
session ID column in the connection logs table.
There are some logs related to a connection that will not be tagged with
the ID (like the "accepted connection" line) because they happen before
the http handler gets to run.
Instead of passing around a function and wrapping it for each listener
to set `Type`, we pass around a connection reporter which is patterned
after the stats reporter. Each listener sets their type directly on
their report rather than having the wrapped function do it like before.
The other big difference is that it uses a buffered channel instead
of a slice.
chatd resends unresolved tool calls on every retry and replica
takeover, and each resend of execute started a new process: one
command in CODAGT-757 started 36 times. An interrupt could neither
stop the process nor report what happened.
The agent now runs a request that carries Coder-Tool-Call-Id once
per chat and tool call ID and responds to repeats with the saved
response. StartProcess, EditFiles, and WriteFile send the header.
POST /api/v0/tool-calls/{id}/cancel refuses later requests for the
call, waits for a running one, stops its process, and returns the
saved response.
A process started with a tool call ID uses it as its process ID, and
its timeout_ms counts from the first start, so a retry does not
restart the timeout. Processes are keyed by chat ID: a chat sees only
its own processes, and a request without a chat ID sees only
processes started without one. Exited processes are kept for an hour
instead of 5 minutes so that retries can still read their output.
Requests without the header behave as before. Old agents ignore the
header and respond to a cancel with 404.
Refs CODAGT-757
## What
Runs the workspace agent logger at `Info` with a slog flight recorder
(`v3.2.0`) instead of unconditionally at `Debug`. Entries below the
current log level are kept in a rolling in-memory ring and flushed to
the sinks when the agent loses its connection to coderd (and before any
`Error`+ log), so the detail leading up to a failure is available
without writing debug logs during normal operation.
Implements CLIENT-672 (connection-log RFC req 13) for the workspace
agent. Depends on the `cdr.dev/slog/v3` `v3.2.0` bump (already on
`main`).
## How
- `cli/agent.go`:
- The main agent logger is
`AppendSinks(...).Leveled(level).FlightRecorder(flightRecorderSize)`,
where `level` is `Info` by default and `Debug` under the global `-v` /
`CODER_VERBOSE`. The recorder is always attached; at `Debug` it is
inert, which keeps the setup correct if the configured level changes in
the future. The reaper logger is unchanged.
- New `--agent-flight-recorder-size` /
`CODER_AGENT_FLIGHT_RECORDER_SIZE` (default `1000`, `0` disables the
flight recorder), so the ring size is configurable per RFC req 13. The
agent flag is distinct from CLI-4's global `--flight-recorder-size`
(#29752) so the two don't collide when `coder agent` inherits the root
global.
- `workspaceAgent` is a `RootCmd` method so it reuses the root command's
global verbose flag instead of defining an agent-specific one.
- `agent/agent.go` (`runLoop`): on a lost coderd connection (the
`io.EOF` disconnect and the generic run error) the agent calls
`logger.Flush(ctx)` to emit the recorded history and then logs at
`Warn`. These are `Warn` rather than `Error` because reconnectable
disconnects are expected and `slogtest` fails tests on any `Error` log;
the manual flush is what surfaces the recorded detail. Graceful shutdown
paths (`ctx` canceled, agent closed) are not flushed.
- Tests: `agent.TestAgent_FlushFlightRecorderOnDisconnect` covers the
flush-on-disconnect path; `cli.TestWorkspaceAgent/DisabledServers` runs
with verbose since its debug messages are otherwise recorded. Golden
`--help` output regenerated.
## Default log verbosity change
This changes the default agent-log verbosity. Today `coder-agent.log`
receives `Debug` unconditionally; with this change, normal operation
logs `Info`, and recorded below-level entries are emitted only when the
agent loses its coderd connection (via a manual flush) or before any
`Error`+ log, or always under `-v` / `CODER_VERBOSE`. This is the
intended behavior of the feature: a quiet steady state with full detail
retained for diagnosing failures. Operators who want the previous
always-debug output can run with `-v` / `CODER_VERBOSE`.
## Alignment with #29752 (CLI)
This mirrors the CLI-4 (CLI log buffer) design so the two
implementations agree:
- **Level model:** Info by default, debug available on demand via the
global `-v` / `CODER_VERBOSE`.
- **Flight recorder size:** default `1000`, `0` disables. CLI-4 exposes
a global `--flight-recorder-size`; the agent uses a distinct
`--agent-flight-recorder-size` / `CODER_AGENT_FLIGHT_RECORDER_SIZE` (the
`CODER_AGENT_*` env convention) so the inherited global and the agent
flag coexist without collision.
- **Flush trigger:** the recorded history is emitted before any `Error`+
log via slog's auto-flush. The agent additionally flushes manually on a
lost coderd connection, which it logs at `Warn` (reconnectable, and
`Error` would fail `slogtest`-based tests); the CLI's failures are
fatal, so it relies on the auto-flush-on-error.
<details>
<summary>Rollout context</summary>
Cross-repo sequence: coder/slog flight recorder (released as v3.2.0) ->
go.mod bump in coder/coder (#29600, merged) -> this PR (CLIENT-672,
agent) and CLI-4 (CLI), which are independent consumers and can merge in
any order.
</details>
---
<sub>Opened by Coder Agents on behalf of @aqandrew.</sub>
Add telemetry to diagnose intermittent macOS `test-go-pg` stalls and preserve timeout diagnostics.
- Log runner details at startup; sample load, swap, and `vm_stat` every second and `top` every 10 seconds to identify runner-wide pauses.
- Add stall detection to `cli` and `agent` `TestMain`, logging heap, GC, and goroutine counts when ticks arrive more than 5 seconds late.
- Run artifact uploads with `always() && !success()` so canceled test steps do not skip diagnostics.
- Add a `TEST_TIMEOUT` override: use 20m on macOS to leave room for goroutine dumps within the 30m job, retaining the 25m default for the nightly gauntlet.
Validated with race tests, binary compilation, `actionlint`, local macOS checks, and Makefile timeout checks.
Adds opportunistic JWT parsing for values returned from the
`--header-command`. If a JWT is parsed we use the value of its `exp`
field to determine when we should re-run the command to refresh the
values.
The client session ID is not populated yet; this just adds the ability
for agents to send a client session ID with the rest of the connection
data, once it gets piped through.
## What
Adds a dedicated workspace-agent endpoint, `POST
/api/v0/upload-chat-file`, that streams a request body into the chat's
upload directory `~/.coder/chats/<chat-id>/files/<name>`, plus the SDK
plumbing to call it:
- `coderd/x/chatfiles`: upload-name sanitization
(`SanitizeWorkspaceUploadName`), collision suffixes (compound extensions
such as `.tar.gz` stay intact: `archive_2.tar.gz`), and the shared
`~/.coder/chats/<id>/files` path layout.
- `agent/agentfiles`: the HTTP handler. On Linux/macOS the directory
chain is opened with `openat(O_NOFOLLOW)`, so symlinked path components
are rejected. The body streams into a hidden `.<name>.upload-<random>`
temp file and is published under its final name only when complete, via
`linkat` over the `_2`, `_3`, ... collision sequence, so existing files
are never overwritten and a half-written file is never visible under its
final name. Filesystems without hard links fall back to an exclusive
placeholder plus `renameat`. Other platforms use an afero-based fallback
with the same semantics. Stale temp files older than an hour are swept
before each upload, and failures are logged with a structured warning.
- `codersdk/workspacesdk`: `AgentConn.UploadChatFile` + regenerated
`agentconnmock`.
## Why
Coder Agents chat can attach images/text/PDF to prompts, but arbitrary
files (zips, datasets, binaries) have nowhere to land where the agent's
tools can act on them. This is the agent-side foundation; the coderd
route that fronts it is stacked on top.
Design notes:
- There is deliberately no size cap; the resource profile matches
existing SSH/WebSocket proxying, and client cancellation aborts the
stream (a failed write removes the temp file, so no partial files
remain). Because the agent HTTP server uses fixed 20 s read/write
timeouts, the handler pushes both deadlines forward on every received
chunk, so a slow upload only fails after a 20 s stall. If the connection
does not support deadline changes, the agent logs one warning per
request.
- This endpoint is not gated by `CODER_AGENT_BLOCK_FILE_TRANSFER`. That
flag targets SSH-transport file tools (scp/rsync/sftp). Chat uploads are
an agent API capability like `write-file` and `edit-files`, which
already bypass the flag; the interaction is documented in the FAQ.
## How tested
- `go test ./agent/agentfiles ./coderd/x/chatfiles
./codersdk/workspacesdk` (unit + umask/symlink/collision integration
tests).
- UAT on a dogfood-style deployment (2026-09-21): uploads longer than 20
s went from 502 to 201 once the workspace agent was restarted on this
build; a coderd restart alone keeps the old agent running, so the
per-chunk deadline extension needs the agent rolled (older agents get
the 409 "Restart the workspace" from #27079).
Part of CODAGT-211. Stack: agent API (this PR) -> coderd endpoint -> UI.
> This PR was authored by Mux (AI) working on Mike's behalf.