Skip to content

feat: add chat workspace file upload endpoint - #27079

Merged
ibetitsmike merged 4 commits into
mainfrom
mike/chat-workspace-upload/backend
Sep 26, 2026
Merged

ibetitsmike merged 4 commits into
mainfrom
mike/chat-workspace-upload/backend

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Jul 7, 2026 •

Copy link
Copy Markdown
Collaborator

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 the workspace-file-reference message 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 deterministic agentselect.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, and coderd_chat_workspace_upload_total{status} counts outcomes. The route shares the files rate limit and documents its 429.
  • Input validation: client-supplied workspace-file-reference parts must point inside /.coder/chats/<chat-id>/files/ with a matching basename, non-negative size, and a workspace_file_workspace_id equal to the chat's currently bound workspace (the upload response echoes it as workspace_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, ChatMessageWorkspaceFileReference builder, and Client.UploadChatWorkspaceFile (RFC 5987 filename in Content-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.
  • Rebased onto main 2026-09-21: the chat API moved from /api/experimental to /api/v2, so the route is registered in coderd/chat_routes.go and the SDK method lives on codersdk.Client.

Follow-ups

  • The files rate limit counts requests, not bytes or concurrency; whether this route should use a per-user concurrency cap instead, and whether the new-chat page should keep partial successes on a 429, is still open (CODAGT-1171).

Part of CODAGT-211. Stack: agent API -> coderd endpoint (this PR) -> UI.

This PR was authored by Mux (AI) working on Mike's behalf.

@github-actions

github-actions Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Docs preview

Check 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.

ibetitsmike commented Jul 7, 2026 •

Copy link
Copy Markdown
Collaborator Author

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.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@ibetitsmike ibetitsmike changed the title refactor(coderd/x/chatd): promote agentselect out of internal feat: add chat workspace file upload endpoint Jul 7, 2026
@linear-code

linear-code Bot commented Jul 7, 2026

Copy link
Copy Markdown

CODAGT-211

@ibetitsmike
ibetitsmike marked this pull request as ready for review July 7, 2026 22:25
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/coderd.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from 2b0b052 to 144539d Compare July 7, 2026 22:57
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from 144539d to d959604 Compare July 7, 2026 23:02
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/exp_chats.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from d959604 to eeecc00 Compare July 7, 2026 23:18
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: eeecc0061b

ℹ️ 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".

@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from eeecc00 to 7c9b457 Compare July 8, 2026 17:06
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/exp_chats.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from 7c9b457 to d2b7673 Compare July 8, 2026 17:29
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/x/chatd/chatprompt/chatprompt.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from d2b7673 to 9ba7486 Compare July 8, 2026 17:50
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/exp_chats.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/exp_chats.go Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/exp_chats.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from 25b3f7b to 950f7c8 Compare September 21, 2026 21:03
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/chat_routes.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from 950f7c8 to e2b7717 Compare September 21, 2026 21:47
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread coderd/exp_chats.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from e2b7717 to ca3ec60 Compare September 21, 2026 22:17
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: ca3ec60133

ℹ️ 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".

@github-actions github-actions Bot removed the stale This issue is like stale bread. label Sep 22, 2026
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/chat_routes.go Resolved
Comment thread coderd/exp_chats.go Resolved
Comment thread coderd/exp_chats_workspace_file_test.go Resolved

@jscottmiller jscottmiller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from ca3ec60 to 5e17e95 Compare September 23, 2026 19:01

@jscottmiller jscottmiller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Base automatically changed from mike/chat-workspace-upload/agent to main September 25, 2026 07:05
@ibetitsmike
ibetitsmike requested a review from a team as a code owner September 25, 2026 07:05
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.
ibetitsmike added a commit that referenced this pull request Sep 25, 2026
## 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.
@ibetitsmike
ibetitsmike force-pushed the mike/chat-workspace-upload/backend branch from e6249db to dda3e32 Compare September 25, 2026 07:06
@ibetitsmike
ibetitsmike merged commit 5dde49d into main Sep 26, 2026
32 of 33 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-workspace-upload/backend branch September 26, 2026 19:41
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants