Repository navigation
fix: budget tool images against the selected model context window - #4032
Open
uiharuayako wants to merge 1 commit into
Open
uiharuayako wants to merge 1 commit into
uiharuayako wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4031.
Summary
Tool-result images previously added 850 estimated tokens each, regardless of their pixel dimensions. Repeated screenshot reads could therefore pass the local context check and exceed the model's input budget.
Read the dimensions from the encoded attachment, including when replaying existing sessions. At the shared send boundary, budget the actual request's messages, tools and images against the selected model's context window, with room reserved for output. Known vision processors use model-specific rules. Custom model IDs use the existing tile estimate.
When space is tight, replace the oldest images with text in the outgoing copy. Keep the latest screenshot at full resolution when that is sufficient. Otherwise prepare a proportional preview or return crop/batch guidance before transport. Tool-call IDs and stored attachments are preserved.
Verification
All 268 local tests below passed (86 adapter, 28 core and 154 configuration tests).
cargo test --locked -p openbitfun-ai-adapters --features vision-input --lib -- client::image_budget vision_tokens providers::openai::message_converter providers::gemini::message_converter client::tests::aggregated client::responses_websocketcargo test --locked -p openbitfun-core --no-default-features --features agent-runtime,git --lib -- context_budget restored_tool_results image_projection configured_image_limit image_input_requirement auto_compression_pressure prompt_suggestion elision multi_channel_pool_preserves_history_protocol_credentials_and_actual_usagecargo test --locked -p openbitfun-config-contracts --libcargo check --locked -p openbitfun-ai-adapters --no-default-features --libpnpm run check:core-boundaries,pnpm run i18n:auditandgit diff --checkThe PNG regression measures increments of 250 tokens at 512×512 and 3,250 at 2048×2048 with the model-neutral estimate. A separate regression uses a screenshot with small lettering to check all three budget outcomes and the original attachment bytes. Local HTTP tests inspect outgoing image dimensions, model selection and preflight rejection.
AI-assisted, tested locally with synthetic requests and loopback providers. Live provider usage and remote-device scenarios were not exercised.