Skip to content

fix(coderd/x/chatd): reject damaged and animated images in view_image, and document it - #310

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

ibetitsmike merged 4 commits into
mainfrom
mike/issue-250-read-images-hardening

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #250

Last PR of a three-PR stack that replaces #284. #308 and #309 are merged, and this PR is now based on main. Head 27d7546 merges main 667f057 with a merge commit. In the conflicted files I took main's version, which holds the squashed #308 and #309, and reapplied this PR's own diff on top.

Two changes from the merge:

  • coderd/x/chatd/ARCHITECTURE.md keeps main's "Chat notes (fork-token-budget)" bullet and adds "Viewing images" after it.
  • docs/fork/pstack-adapter.md points to fork_tools.go:122-124 for the view_image append. The token budget work moved it from lines 119-121.

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

What

View_image checks more than the header, limits its calls per response, and the docs describe the feature.

  • Structure check: imageComplete walks the PNG chunks and their CRCs up to IEND, requires a JPEG end marker after the last scan, a GIF trailer, and WebP chunks that fit the RIFF size and the file, with a VP8, VP8L or ANMF image chunk. A VP8 chunk must hold the first partition its frame tag names. A truncated or damaged file, such as a screenshot still being written, fails with ... is not a valid image/png image: ..., so a broken image never gets stored and replayed.
  • Decode check: imageDecodes fully decodes a PNG, a JPEG or the first GIF frame with the stdlib decoders, so damaged image data behind an intact structure fails too, for example a PNG with no IDAT. A total of 25,000,000 pixels (viewImageMaxPixels) bounds the memory of one decode to about 200 MB, and viewImageDecodeSlot runs one decode at a time.
  • Animated GIFs fail with ... is an animated GIF with N frames, and view_image accepts only still images, because OpenAI rejects them. gifFrames walks the GIF blocks without decoding the frames.
  • At most 8 view_image calls per model response. buildTurnEnvironment builds the tools for each step, so a counter in the tool value counts one response's calls, which chatloop runs concurrently. The ninth and later fail with view_image runs at most 8 times per response.

Docs

  • docs/fork/pstack-adapter.md: a Tools table row, a "View images" section with the limits, errors, the openai-compat, OpenRouter and Vercel gap, the prompt bound and access, the readonly and Read notes, the worktree path list, and source references. The line references match this head.
  • coderd/x/chatd/ARCHITECTURE.md: view_image in the list of fork tools and a "Viewing images" bullet. The repository rule says agents leave TODOs there for the human author. The lead asked this workstream to write the update itself and leave no TODOs.

Size

373 added and 3 removed lines, of which 105 are tests and 53 are docs. No generated files.

Tests

  • TestViewImageTool/InvalidImages: cut PNG, bad PNG CRC, cut JPEG, GIF and WebP, a WebP chunk past the file end, a VP8 chunk shorter than its first partition, a WebP with no image chunk, a PNG with only IHDR and IEND, and a JPEG and a GIF with damaged data and a restored end marker.
  • TestViewImageTool/DimensionLimit: the total-pixel limit.
  • TestViewImageTool/AnimatedGIF and /CallsPerResponse.
  • Mutation checks on this tree: with imageDecodes, the frame check or the call counter disabled, TestViewImageTool/InvalidImages, /AnimatedGIF and /CallsPerResponse fail. With the chunk walk removed, /InvalidImages fails.

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

  • On this head: go test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|ForkTools|TestForkWorktreeBinding|AgentToolPolicy|CustomAgent|MediaSanitize|ReplaceUnsupportedMedia|DynamicTool|CompactionModelOverride|ChatNotes|TokenBudget' -count=1 -v: pass
  • On this head: go test ./codersdk -run 'TestExperimentDisplayNames|TestForkExperiments': pass
  • On this head: go vet ./coderd/x/chatd/ and golangci-lint run --fix=false --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/: 0 issues
  • make lint/emdash and markdownlint on docs/fork/pstack-adapter.md: pass

The PR is based on main, so fork-ci runs on this head.

Not verified

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.
limitViewedImages keeps the newest view_image results as images, at most 8
of them, 20 images in the prompt and 10 MiB of media, and turns older ones into
a text note. The stored conversation keeps every image. A compaction override
model gets the same bound with its own size limit.

Part of #250.
…, and document it

view_image checks the structure of the whole file, decodes PNG, JPEG and GIF
pixels one at a time under a 25 megapixel cap, rejects animated GIFs and runs
at most 8 times per response. docs/fork/pstack-adapter.md and the chatd
architecture notes describe the tool, its limits and the experiment.

Closes #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-14T07:10:50.040164Z 27d7546 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 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: 823c46480a

ℹ️ 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/fork_view_image.go Resolved
@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. Chef's kiss.

Reviewed commit: 823c46480a

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

Base automatically changed from mike/issue-250-read-images-bound to main November 14, 2026 07:04
# Conflicts:
#	coderd/apidoc/docs.go
#	coderd/apidoc/swagger.json
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_tools.go
#	coderd/x/chatd/fork_view_image.go
#	coderd/x/chatd/fork_view_image_internal_test.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. Bravo.

Reviewed commit: 27d7546b6f

ℹ️ 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 3a79dd3 into main Nov 14, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-250-read-images-hardening branch November 14, 2026 07:18
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 3a79dd3 (squash) at 07:18Z. Before the merge, the server was confirmed on the previous deploy, 667f057.

Gates on head 27d7546:

  1. The head matched the SHA I verified, and main was 667f057 at merge time.
  2. All check runs passed. fork-ci run 43569568834 passed on this exact head.
  3. Codex receipt: issuecomment-6873607761, "Didn't find any major issues", reviewed commit 27d7546.
  4. 0 unresolved threads.
  5. The branch was 0 commits behind main. The diff against main was 4 files, +373 -3.
  6. My tests on the exact head, run under flock: go test -count=1 -v -run '(TestViewImage|TestLimitViewedImages|ForkTools|ChatNotes|TokenBudget|MediaSanitize|ReplaceUnsupportedMedia|CompactionModelOverride)' ./coderd/x/chatd/ passed 9 top-level tests, including TestViewImageTool with the new AnimatedGIF and CallsPerResponse cases.

Not exercised:

  • a real provider run and a browser check;
  • an end-to-end run with an image over a compaction override's limit;
  • the decode-slot context cancel path, which has no test.

This closes #250. fork-view-image is off by default and must be enabled for the tool to appear.

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