Skip to content

feat(coderd/x/chatd): bound the viewed images a prompt carries - #309

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

ibetitsmike merged 3 commits into
mainfrom
mike/issue-250-read-images-bound

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. Middle of a three-PR stack that replaces #284. #308 is merged, and this PR is now based on main. The top PR, #310, adds the structure and decode checks, the per-response call limit and the docs.

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

What

The prompt bound for viewed images, so base64 does not grow without bound over a long chat.

  • appendForkTools passes the prompt through limitViewedImages on every step. The prompt keeps the newest 8 view_image images, 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 as computer screenshots and attachments, so viewed images only fill what is left. That includes the attachments of the unanswered user tail, which buildTurnEnvironment appends after the bound, so turn_environment.go:743 passes pendingPrompt too.
  • 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.
  • The bound uses the turn's model. After a switch to a route with a lower limit, such as from OpenAI to Anthropic, an earlier image over the new view_image limit becomes the note [image omitted: N bytes is larger than the view_image limit of M bytes for this model. ...].
  • A result belongs to the newest earlier call with its ID, so a reused call ID does not make another tool's result count as a viewed image.
  • While fork-view-image is off and the turn offers the chat's dynamic tools with one named view_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.
  • The bound runs before the prompt is captured for compaction. A compaction override model gets the same bound with its own size limit, through one hook line in generation.go:1339.
  • The tool description now tells the model that only the newest viewed images stay in context.

Size

362 added and 13 removed lines, of which 191 are tests. No generated files.

Tests

  • 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.
  • TestViewImageToolGate: the prompt bound with the experiment on and off, no bound when a dynamic view_image owns the name with the experiment off, except in plan mode, and the same for limitViewedImagesForModel.
  • Mutation checks run on feat(coderd): add view_image tool behind fork-view-image #284 with the same code: with the limitViewedImages call removed, both TestViewImageToolGate bound subtests fail. With the other-media count, the dynamic-tool check, the plan-mode check, the pending tail or the image count removed, TestLimitViewedImages/OtherMediaTakesBudget, TestViewImageToolGate/OffWithDynamicTool, /OffWithDynamicToolInPlanMode, TestLimitViewedImages/PendingMediaTakesBudget and /OtherImagesTakeCount fail.

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

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

After the merge of main (e44cdc1) the same tests pass on the new head, and fork-ci runs on it.

Not verified

  • No end-to-end test with a viewed image over a compaction override's limit. TestViewImageToolGate covers limitViewedImagesForModel itself.
  • No run against a real provider.
  • The newest viewed image always stays, so a prompt that already holds 20 or more other images still goes over 20 (chatd: view_image follow-ups declined from the #250 review #291).

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.
@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:42:49.228323Z 90bf3f9 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: ce6b152759

ℹ️ 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_tools.go Resolved
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. You're on a roll.

Reviewed commit: ce6b152759

ℹ️ 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-core to main November 14, 2026 06:38
# Conflicts:
#	coderd/apidoc/docs.go
#	coderd/apidoc/swagger.json
#	coderd/x/chatd/fork_tools.go
#	coderd/x/chatd/fork_view_image.go
#	coderd/x/chatd/fork_view_image_internal_test.go
#	coderd/x/chatd/turn_environment.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. Nice work!

Reviewed commit: 90bf3f9618

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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

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

Gates on head 90bf3f9:

  1. The head matched the SHA I verified, and main was e44cdc1 at merge time.
  2. All check runs passed. fork-ci run 43567445258 passed on this exact head.
  3. Codex receipt: issuecomment-6873289023, "Didn't find any major issues", reviewed commit 90bf3f9.
  4. 0 unresolved threads.
  5. The branch was 0 commits behind main. The diff against main was 5 files, +362 -13.
  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 TestLimitViewedImages and TestViewImageReachesModel.

Not exercised: a real provider run, and an end-to-end run with viewed images over a compaction override's limit. fork-view-image stays off until #310 lands.

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.

1 participant