Repository navigation
feat: add chat workspace file upload endpoint - #27079
Conversation
Docs previewCheck off each page once it's been reviewed. If a page changes in a later push, its checkbox clears automatically so it gets a fresh look. Pages not yet wired into the docs navigation aren't listed here. |
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2b0b0522f3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
2b0b052 to
144539d
Compare
|
@codex review |
144539d to
d959604
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d959604f47
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
d959604 to
eeecc00
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
eeecc00 to
7c9b457
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7c9b4576a8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
7c9b457 to
d2b7673
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d2b7673604
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
d2b7673 to
9ba7486
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9ba74861ba
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9ba74861ba
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 25b3f7b0da
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
25b3f7b to
950f7c8
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 950f7c8d1e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
950f7c8 to
e2b7717
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e2b77171c2
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
e2b7717 to
ca3ec60
Compare
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
jscottmiller
left a comment
There was a problem hiding this comment.
Approved. See the summary comment on #27078 and the non-blocking inline follow-ups across the stack.
Approval posted by Coder Agents on Scott's behalf.
ca3ec60 to
5e17e95
Compare
jscottmiller
left a comment
There was a problem hiding this comment.
Re-approved after the fix commits. Verified the addressed threads against the code (agent atomic publish, workspace status, normalized references, logging/metric, first-message fallback title, reasoning-effort override, Vitest coverage, archive-409 navigation) and ran the affected Go and Vitest suites; the remaining notes are acceptable as is. The three open follow-up threads are resolved as agreed follow-ups.
Approval posted by Coder Agents on Scott's behalf.
Adds POST /api/v2/chats/{chat}/workspace-files, which
streams a request body through the workspace agent's upload-chat-file
API into ~/.coder/chats/<chat-id>/files/. Introduces the
workspace-file-reference chat message/input part so sent messages can
reference uploaded files; prompt rendering is text-only metadata, the
bytes never reach the LLM.
- middleware: chat ActionUpdate RBAC, owner-only, reject archived
- handler: workspace exec authz (409 statuses), bound-agent
preference with deterministic agentselect fallback (no binding
persisted), 30s dial timeout, chat headers mirrored onto the agent
conn, agent codersdk.Error status preserved (502 otherwise)
- createChatInputFromParts: validate workspace-file-reference parts
against the chat-scoped upload path shape
- chatprompt: render workspace file parts as [workspace file: ...]
- codersdk: part consts/fields, builder, response type, client method
- Report deleted, stopped, starting, and pending workspaces with their
own 409 messages instead of "Chat workspace has no agents."
- Require workspace-file-reference names to already be sanitized,
persist the cleaned path (4096-byte cap, control characters
rejected), and store the base media type. Edits share the same path.
- Log one structured warning per failed upload, count uploads in
coderd_chat_workspace_upload_total{status}, and stop returning agent
transport errors (which embed the tailnet URL) in 502 details.
- Inject quartz.Clock into workspaceUsageReader and track bytes read.
- Document the 429 files rate limit on the upload route.
- Tests: run TestPostChatWorkspaceFile in parallel via agent EnvInfo,
cover workspace states, the explicit SSH check (ACL collaborator and
app-connect-only token), concurrent same-name uploads, RFC 5987
filenames, the rate limit, bound-agent selection, and the usage bump
cadence.
TestWorkspaceSharingDisabled/NoAccessWhenDisabled enables DisableWorkspaceSharing, which sets the process-global rbac.WorkspaceACLDisabled, and never reset it. Parallel tests in the package run after the sequential ones, so every later ACL-based test saw workspaces with their ACLs stripped. This made TestPostChatWorkspaceFile/SharedWorkspaceRequiresSSH fail deterministically in CI.
…are names in titles TitleText prefixed workspace file references with an internal [workspace file] marker. When the title model failed, that marker leaked into the visible fallback chat title. Render the trimmed file name instead and skip references without one.
## 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.
e6249db to
dda3e32
Compare

What
Adds
POST /api/v2/chats/{chat}/workspace-files, which streams the request body to the chat's bound workspace agent and returns the final path/name/size, plus theworkspace-file-referencemessage part type that carries the uploaded file's metadata through chat messages:coderd/exp_chats.go: owner-only middleware (chat RBAC update + reject archived), workspace exec authorization (403/409 like the existing chat exec paths), agent selection (prefer the chat's bound agent, else deterministicagentselect.FindChatAgent, without persisting a binding), 30s dial timeout, chat/ancestor tracking headers on the agent connection, and agent error passthrough (agent HTTP status preserved, 502 with a generic detail for transport failures; the real error is logged). Deleted and non-running workspaces get a specific 409 ("Workspace is stopped. Start the workspace before uploading files.") before agent lookup. Each failed upload logs one structured warning, andcoderd_chat_workspace_upload_total{status}counts outcomes. The route shares the files rate limit and documents its 429.workspace-file-referenceparts must point inside/.coder/chats/<chat-id>/files/with a matching basename, non-negative size, and aworkspace_file_workspace_idequal to the chat's currently bound workspace (the upload response echoes it asworkspace_id). References uploaded before a workspace rebind are rejected instead of silently sending the agent an unreadable path. The file name must already be in sanitized form (SanitizeWorkspaceUploadName(name) == name), and the cleaned path and base media type are what gets persisted; message edits share the same validation. Parts are rejected on chat creation (no chat ID exists yet).coderd/x/chatd/chatprompt: references render into the prompt as text only,[workspace file: <name> (<size>) at <path>]; file bytes never reach the LLM. Title derivation (TitleText) uses the bare file name, so fallback titles carry no internal marker.codersdk: part-type constants/fields,ChatMessageWorkspaceFileReferencebuilder, andClient.UploadChatWorkspaceFile(RFC 5987 filename inContent-Disposition).No DB migration: parts persist as JSON in
chat_messages.content.Why
Bridges chat and the workspace filesystem for CODAGT-211: users drop a file in chat, the bytes land where the agent's tools can operate on them, and the model learns the path from the reference part.
How tested
go test ./coderd -run 'WorkspaceFile|ChatInput|UploadChat'including an integration test that runs a real in-memory agent, uploads through the coderd route, verifies bytes on disk, and exercises collision suffixes.go test ./codersdk ./coderd/x/chatd/...,make gen(swagger/types/docs),make lint./api/experimentalto/api/v2, so the route is registered incoderd/chat_routes.goand the SDK method lives oncodersdk.Client.Follow-ups
Part of CODAGT-211. Stack: agent API -> coderd endpoint (this PR) -> UI.