Skip to content

feat(coderd/x/chatd): add the view_image tool behind fork-view-image - #308

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/issue-250-read-images-core
Nov 14, 2026
Merged

ibetitsmike merged 2 commits into
mainfrom
mike/issue-250-read-images-core

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #250. Bottom of a three-PR stack that replaces #284:

  1. This PR: the experiment, the view_image tool core and the model check.
  2. feat(coderd/x/chatd): bound the viewed images a prompt carries #309 (mike/issue-250-read-images-bound): the bound on viewed images in the prompt.
  3. fix(coderd/x/chatd): reject damaged and animated images in view_image, and document it #310 (mike/issue-250-read-images-hardening): the structure and decode checks, the per-response call limit and the docs. It closes chatd: let the model read image files from the workspace #250.

The tool sits behind fork-view-image, which is off by default. A gap in this PR, such as no prompt bound and no decode check, is closed by the PRs above it. Enable the experiment only after the whole stack is in.

Declined review findings worth doing later are collected in #291.

What

A new fork chat tool, view_image, behind a new fork-view-image experiment. It loads a PNG, JPEG, GIF or WebP file from the workspace into the model's context as an image, so a chat can review its own screenshots.

Why a separate tool and not read_file

read_file is upstream code (chattool). Teaching it to return images would mean changes inside upstream files. A fork tool lives in fork_view_image.go. The only upstream change is the existing appendForkTools hook line in turn_environment.go:743, which now also passes the turn's model (forkToolsWithModel). The tool gets its own experiment.

Behavior

  • Type by content: chatfiles.DetectMediaType on the bytes. A PNG named shot.txt works. A text file named shot.png fails with ... is text/plain, not a PNG, JPEG, GIF or WebP image. Use read_file for text files.
  • Model check: the route signal chatprovider.Model.AcceptsFilePartMediaType. A route that takes no image type fails with model "<model>" on provider "<provider>" does not accept image input. Switch to a model with image input to view images. A route that rejects one type, such as Google and GIF, fails with does not accept image/gif images.
  • Size limit:
    • On a route with an inline image cap (Anthropic, and Bedrock through the Anthropic API) the limit is 3,932,160 bytes. That is codersdk.AnthropicInlineImageCapBytes (5 MiB) times 3/4, so the base64 form fits Anthropic's per-image limit.
    • On other routes it is codersdk.MaxChatFileSizeBytes (10 MiB), the limit chatloop already applies to every tool result media.
    • Over the limit the error names the number and the reason. The tool does not resize.
  • Pixel limit: 8000 pixels per side, Anthropic's documented limit. Dimensions come from the stdlib png, jpeg and gif DecodeConfig, and a small WebP header parser (VP8, VP8L, VP8X). No new dependency.
  • The path is used as given, like read_file. Only an all-whitespace path fails as empty.
  • Known gap: the OpenAI-compatible, OpenRouter and Vercel routes report that they take any image (chatprovider.go:155-163). Coder Agents cannot tell a text-only model behind them, so the image goes to the provider anyway. The top PR documents this in docs/fork/pstack-adapter.md.

Storage

The result is a normal media tool result. It is stored with is_media, the data and the MIME type, like other tool results, and is sent again on later turns. The next PR bounds how many stay in the prompt.

Access

  • Plan-mode turns and explore children do not get view_image, because their upstream allow-lists do not name it.
  • A custom agent with readonly: true keeps it. The Read alias maps to read_file and view_image.
  • A caller-supplied dynamic tool named view_image gets neither: readonly drops it, and a tools list keeps it only when it names view_image itself.
  • In an isolated worktree child, a relative path resolves against the worktree, like read_file. A caller-supplied view_image is not wrapped, so it stays a *dynamicTool and the policy above still drops it.

Web UI

No site/ source change. The generic tool card already renders an is_media result as the image (GenericToolRenderer, ToolResultMedia). site/src/api/typesGenerated.ts gains the experiment constant from the generator.

Generated files

  • site/src/api/typesGenerated.ts comes from the generator.
  • coderd/apidoc/docs.go, coderd/apidoc/swagger.json and docs/reference/api/schemas.md are hand edited to add fork-view-image, the same way feat: add the message_family_chat tool behind fork-family-messages #265 added fork-family-messages. Running swag reorders the fork enum values (map order) and would churn every fork entry.
  • Full make gen stops at the helm golden target because helm is not installed in this workspace.

Size

681 added lines, of which 14 are generated and 432 are tests.

Tests

  • TestViewImageReachesModel (end to end, chattest.NewAnthropic): the tool is offered, a PNG with a known payload reaches the second model request as an image block with the exact base64, the next turn's request still carries it, and the stored tool message has is_media, the data and the MIME type.
  • TestViewImageTool: type by content, JPEG, GIF and WebP, a non-image file, the size limit, the side limit, empty, invalid and missing files, an empty path, a path with outer spaces, a model without image input (Copilot route and an unknown provider), and a route that rejects one type.
  • TestViewImageCustomAgentPolicy: readonly, the Read alias and a named entry, each with the built-in and a dynamic view_image.
  • TestViewImageToolGate: the tool only with the experiment on.
  • TestForkWorktreeBindingTools/ViewImageRelative and /DynamicViewImageUnbound.
  • The tests fail without the change: with the tool append removed from fork_tools.go, TestViewImageReachesModel, TestViewImageToolGate/On and /OnWithDynamicTool fail.

Commands run on this head, all under flock ~/.fork-go.lock:

  • go test ./coderd/x/chatd/ -run 'TestViewImage|ForkTools|TestForkWorktreeBinding|AgentToolPolicy|CustomAgent|MediaSanitize|ReplaceUnsupportedMedia|DynamicTool|CompactionModelOverride' -count=1 -v: pass
  • go test ./codersdk -run 'TestExperimentDisplayNames|TestForkExperiments' -count=1: pass
  • golangci-lint run --fix=false --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/ ./codersdk/: 0 issues. The default config reports no new finding in the changed files.

Not verified

  • No run against a real provider. The end-to-end test uses the Anthropic mock.
  • Not exercised in a browser. The UI claim rests on the existing generic renderer code.
  • Full make lint and full make gen did not run here; fork-ci covers them.
  • Mike needs to enable fork-view-image for the tool to appear, after the whole stack is in.

The tool reads a PNG, JPEG, GIF or WebP file from the workspace and returns it
to the model as an image tool result. It detects the type by content, checks
the per-provider size limit and the 8000 pixel side limit, and fails with the
model and provider named when the route accepts no image input.

Part of #250.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T06:26:35.267512Z c61d4fc Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: c2706428e1

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

# Conflicts:
#	coderd/apidoc/docs.go
#	coderd/apidoc/swagger.json
#	coderd/x/chatd/fork_tools.go
#	codersdk/fork_experiments.go
#	docs/reference/api/schemas.md
#	site/src/api/typesGenerated.ts
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c61d4fc8cd

ℹ️ 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 merged commit e44cdc1 into main Nov 14, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-250-read-images-core branch November 14, 2026 06:38
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as e44cdc1 (squash) at 06:38Z.

Gates on head c61d4fc:

  1. The head matched the SHA I verified, and main was c7efc9c at merge time.
  2. All check runs passed. fork-ci run 43565994128 passed on this exact head.
  3. Codex receipt: issuecomment-6873099831, "Didn't find any major issues", reviewed commit c61d4fc.
  4. 0 unresolved threads.
  5. The branch was 0 commits behind main.
  6. My tests on the exact head, run under flock:
    • go test -count=1 -v -run '(TestViewImage|ForkTools|ChatNotes|TokenBudget|MediaSanitize|ReplaceUnsupportedMedia)' ./coderd/x/chatd/: 7 top-level tests passed, including TestViewImageReachesModel and TestChatNotesTool.
    • go test -run '(TestExperimentDisplayNames|TestForkExperiments)' ./codersdk: passed.
    • .github/fork/check-apidoc.sh: passed.

Not exercised: a real provider run and a browser check. fork-view-image stays off until #310 lands. #309 is next in the stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chatd: let the model read image files from the workspace

1 participant