Repository navigation
feat(coderd): add view_image tool behind fork-view-image - #284
ibetitsmike wants to merge 14 commits into
Conversation
Add a fork view_image chat tool that loads a PNG, JPEG, GIF or WebP file from the workspace into the model's context as a media tool result. The type comes from the content, the size limit from the route's per-image cap or the chat tool media limit, and a model without image input gets an error that names the model and the provider. The prompt keeps the newest 8 viewed images, up to 15 MiB. Closes #250
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fb3c1c2a59
ℹ️ 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".
Reject truncated or damaged images by structure, drop a stored image over the current model's view_image limit from the prompt, map tool results to the newest call with their ID, keep a dynamic view_image out of readonly and the Read alias, and pass the path as given.
@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". |
…ads-image-files-3a7d531b
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 692e193ce2
ℹ️ 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".
Count other tool media and attachments before viewed images in the 15 MiB budget, skip the bound when a caller-supplied view_image owns the name, drop semicolons from the messages and rename the docs section to View images.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b2c71f875d
ℹ️ 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".
Treat a caller-supplied view_image as the owner of the name only on turns that offer dynamic tools, and state the newest-image exception to the 15 MiB budget.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 934b236b91
ℹ️ 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 |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
…ads-image-files-3a7d531b
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 48f0b822d5
ℹ️ 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: 48f0b822d5
ℹ️ 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: c6bd1b7f68
ℹ️ 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: d80c0cdf08
ℹ️ 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: 0ebe0dfdf8
ℹ️ 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: 659094c6be
ℹ️ 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 |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
Closing in favor of a stack of three smaller PRs, per the lead's request to split this one:
Declined review findings worth doing later are in #291. The branch mike/issue-250-read-images stays as it is. |
Closes #250
What
A new fork chat tool,
view_image, behind a newfork-view-imageexperiment. 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_fileread_fileis upstream code (chattool). Teaching it to return images would mean changes inside upstream files. A fork tool lives infork_view_image.go. The upstream changes are the existingappendForkToolshook line inturn_environment.go:743, which now also passes the turn's model, prompt and pending tail (forkToolsWithModel), and one hook line ingeneration.go:1339for a compaction override model. The tool also gets its own experiment instead of riding onfork-chat-todos.Behavior
chatfiles.DetectMediaTypeon the bytes. A PNG namedshot.txtworks. A text file namedshot.pngfails with... is text/plain, not a PNG, JPEG, GIF or WebP image. Use read_file for text files.chatprovider.Model.AcceptsFilePartMediaType. A route that takes no image type fails withmodel "<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 withdoes not accept image/gif images.codersdk.AnthropicInlineImageCapBytes(5 MiB) times 3/4, so the base64 form fits Anthropic's per-image limit.codersdk.MaxChatFileSizeBytes(10 MiB), the limit chatloop already applies to every tool result media.imageCompletewalks the PNG chunks and their CRCs up toIEND, 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 fails with... is not a valid image/png image: ..., so a broken image never gets stored and replayed.imageDecodesthen 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 noIDAT. A total of 25,000,000 pixels (viewImageMaxPixels) bounds the memory of one decode to about 200 MB, andviewImageDecodeSlotruns one decode at a time.... is an animated GIF with N frames, and view_image accepts only still images, because OpenAI rejects them.gifFrameswalks the GIF blocks without decoding the frames.view_imagecalls per model response.buildTurnEnvironmentbuilds 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 withview_image runs at most 8 times per response. This bounds the reads and the stored results of one batch.read_file. Only an all-whitespace path fails as empty.png,jpegandgifDecodeConfig, and a small WebP header parser (VP8, VP8L, VP8X). No new dependency.chatprovider.go:155-163). Coder Agents cannot tell a text-only model behind them, so the image goes to the provider anyway. This is documented indocs/fork/pstack-adapter.md.Storage, later turns and compaction
is_media, the data and the MIME type, like other tool results, and is sent again on later turns.appendForkToolspasses the prompt throughlimitViewedImageson every step. The prompt keeps the newest 8view_imageimages, and always the newest one. Together with the prompt's other images it keeps at most 20, because Anthropic tightens its per-image limits above 20 images. The 10 MiB byte budget, sized so the base64 form fits Bedrock's 20 MB request limit, counts the prompt's other media first, such ascomputerscreenshots and attachments, so viewed images only fill what is left. That includes the attachments of the unanswered user tail, whichbuildTurnEnvironmentappends after the bound, soturn_environment.go:743passespendingPrompttoo. Older ones become the text note[image no longer in context: only the newest viewed images are kept. Call view_image again to see it.]followed by the original text. The stored conversation keeps every image. The bound also runs with the experiment off, so a chat that viewed images earlier stays bounded. With a compaction model override,generateCompactionapplies the bound again with the override model (one hook line ingeneration.go), because the override can have a lower image size limit.view_imagelimit becomes the note[image omitted: N bytes is larger than the view_image limit of M bytes for this model. ...]and does not count toward the 8. UpstreamreplaceUnsupportedMediacompares the decoded size with the base64 cap, so it would let such an image through.fork-view-imageis off and the turn offers the chat's dynamic tools with one namedview_image, the caller's tool owns the name, so the bound does not touch its results. Plan-mode turns, explore children and deployments that disable caller-supplied tools offer no dynamic tools, so the bound runs there.Access
explorechildren do not getview_image, because their upstream allow-lists (turn_environment.go:2050,:2122) do not name it.readonly: truekeeps it. TheReadalias maps toread_fileandview_image.view_imagegets neither:readonlydrops it, and atoolslist keeps it only when it namesview_imageitself.read_file. A caller-suppliedview_imageis not wrapped, so it stays a*dynamicTooland the custom agent policy above still drops it.Web UI
No
site/source change. The generic tool card already renders anis_mediaresult as the image (GenericToolRenderer,ToolResultMedia).site/src/api/typesGenerated.tsgains the experiment constant from the generator.Generated files
site/src/api/typesGenerated.tscomes from the generator.coderd/apidoc/docs.go,coderd/apidoc/swagger.jsonanddocs/reference/api/schemas.mdare hand edited to addfork-view-image, the same way feat: add the message_family_chat tool behind fork-family-messages #265 addedfork-family-messages. Running swag reorders the fork enum values (map order) and would churn every fork entry../.github/fork/check-apidoc.shpasses.make genstops at the helm golden target because helm is not installed in this workspace.Docs
docs/fork/pstack-adapter.md: a Tools table row, a new "View images" section with the limits, errors, gap, prompt bound and access, thereadonlyandReadnotes, the worktree path list, and source references.coderd/x/chatd/ARCHITECTURE.md:view_imagein 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.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 hasis_media, the data and the MIME type.TestViewImageTool: type by content, JPEG, GIF and WebP, a non-image file, the size limit, the pixel limit, invalid and truncated images (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 onlyIHDRandIEND, and a JPEG and a GIF with damaged data and a restored end marker), the total-pixel limit, an animated GIF, the per-response call limit, a path with outer spaces, a model without image input (Copilot route and an unknown provider), and a route that rejects one type.TestLimitViewedImages: the count bound, the byte bound, the newest kept, the input left unchanged, other media taking the byte budget first (a screenshot and an attachment), the pending tail's attachments taking it too, other images taking the image count (a PDF does not), a reused call ID, and the per-model size limit.TestViewImageCustomAgentPolicy:readonly, theReadalias and a named entry, each with the built-in and a dynamicview_image.TestViewImageToolGate: the tool only with the experiment on, the prompt bound in both cases, and no bound when a dynamicview_imageowns the name with the experiment off, except in plan mode.TestForkWorktreeBindingTools/ViewImageRelativeand/DynamicViewImageUnbound: the built-in tool resolves a relative path against the worktree, and a caller-suppliedview_imagecomes back unwrapped.limitViewedImagescall removed fromfork_tools.go,TestViewImageReachesModeland bothTestViewImageToolGatesubtests fail. With the other-media count or the dynamic-tool check removed,TestLimitViewedImages/OtherMediaTakesBudgetandTestViewImageToolGate/OffWithDynamicToolfail. With the plan-mode check removed,TestViewImageToolGate/OffWithDynamicToolInPlanModefails.Commands run, all under
flock ~/.fork-go.lock:go test ./coderd/x/chatd/ -run 'TestViewImageReachesModel|TestViewImageTool|TestLimitViewedImages|TestViewImageToolGate' -count=1 -v: passgo test ./coderd/x/chatd/ -run 'AgentToolPolicy|CustomAgent|WorktreeBinding|ForkTools|ChatTodoTools|MediaSanitize|ReplaceUnsupportedMedia|TestViewImage|TestLimitViewed' -count=1: passgo test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|AgentToolPolicy|CustomAgent|ForkTools' -count=1: pass, andTestViewImageReachesModel -v: passgo test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|AgentToolPolicy|CustomAgent|ForkTools|MediaSanitize|ReplaceUnsupportedMedia|DynamicTool' -count=1: passgo test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|ForkTools' -count=1: passgo test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|AgentToolPolicy|CustomAgent|ForkTools|MediaSanitize|ReplaceUnsupportedMedia|DynamicTool' -count=1: pass. With the chunk walk removed,TestViewImageTool/InvalidImagesfails.go test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|AgentToolPolicy|CustomAgent|ForkTools|DynamicTool|MediaSanitize|ReplaceUnsupportedMedia' -count=1: pass. With the pending tail left out of the count,TestLimitViewedImages/PendingMediaTakesBudgetfails.go test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|ForkTools|DynamicTool|CompactionModelOverride|Compaction' -count=1: pass, andTestActiveServer_CompactionModelOverrideandTestViewImageToolGatewith-v: every subtest passes.go test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|ForkTools' -count=1: pass. WithimageDecodesdisabled,TestViewImageTool/InvalidImagesfails on the new PNG, JPEG and GIF cases.go test ./coderd/x/chatd/ -run 'TestForkWorktreeBinding|TestViewImage|ForkTools|AgentToolPolicy|CustomAgent' -count=1: pass. With the dynamic-tool check removed from the worktree binding,TestForkWorktreeBindingTools/DynamicViewImageUnboundfails.go test ./coderd/x/chatd/ -run 'TestViewImage|TestLimitViewedImages|ForkTools|TestForkWorktreeBinding|AgentToolPolicy|CustomAgent|MediaSanitize|ReplaceUnsupportedMedia|DynamicTool' -count=1: pass. With the call counter, the frame check and the image count disabled,TestViewImageTool/CallsPerResponse,TestViewImageTool/AnimatedGIFandTestLimitViewedImages/OtherImagesTakeCountfail.go test ./codersdk -run 'TestExperimentDisplayNames|TestForkExperiments' -count=1: passgolangci-lint runon./coderd/x/chatd/and./codersdk/: no finding in the changed files. The new-code config with--new-from-merge-base=origin/main: 0 issues.make lint/emdash lint/check-experiment-keys lint/architecture,paralleltestctx,intxcheckon the changed packages, markdownlint ondocs/fork/pstack-adapter.md: passNot verified
generation.go:1339, runs inTestActiveServer_CompactionModelOverride, but no test has a viewed image over the override's limit.TestViewImageToolGatecoverslimitViewedImagesForModelitself.make lintand fullmake gendid not run here; fork-ci covers them.fork-view-imagefor the tool to appear.