Skip to content

fix: budget tool images against the selected model context window - #4032

Merged
kev1n77 merged 4 commits into
openBitFun:mainfrom
uiharuayako:codex/fix-tool-image-context-budget
Nov 25, 2026
Merged

kev1n77 merged 4 commits into
openBitFun:mainfrom
uiharuayako:codex/fix-tool-image-context-budget

Conversation

@uiharuayako

@uiharuayako uiharuayako commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4031, including the follow-up about HTTP 413 after several screenshot reads.

Changes

Each model request sends the new uploads and tool results that still need inspection. Once the assistant has responded, subsequent requests carry the history text, tool-call IDs and available image references. Reading another screenshot sends that screenshot. Explicitly reopening a reference supplies the pixels again. A parallel tool batch that has yet to reach the model keeps all of its images eligible for the configured count and token budgets.

The runtime saves content-addressed copies of new image observations, including tool frames without a source file. When an older session has only a source path, the model can request the current file again. If the older runtime discarded the pixels and recorded no path, recovering that image requires another attachment.

Image estimates use the encoded dimensions and the selected model’s vision rules. The send path reserves room for output, keeps new images at full resolution when they fit, and can make a proportional preview. If the server rejects the request body with HTTP 413, the client surfaces crop or batch guidance without retrying the identical request. Explicit model token-limit errors retain context-overflow handling.

Reproduction and verification

The same local HTTP gateway capped the entire JSON body at 128 KiB in both runs. The model had a 128k-token window. A and B were distinct 192 × 128 noise PNGs.

Request Previous PR Updated PR
Read A 99,132 bytes, A, HTTP 200 99,132 bytes, A, HTTP 200
Ask a text-only follow-up 99,229 bytes, A, HTTP 200 520 bytes, no pixels, HTTP 200
Read B in the same conversation 198,296 bytes, A and B, HTTP 413 99,587 bytes, B, HTTP 200

A separate loopback regression explicitly reopens A and verifies the four request bodies contain A, no pixels, B and A in order. Provider-conversion tests cover OpenAI Chat, Responses, Anthropic and Gemini tool-result identity. The error regressions exercise HTML gateway errors, structured model errors and localized user guidance.

The runtime regression also saves and reloads a sanitized session snapshot. A pending tool result recovers its original image bytes. A later request carries the tool ID and its image reference. Other tests overwrite the original file, reopen a pathless frame through a scoped runtime URI, and verify a compression summary cannot resend an image already inspected in its retained tail.

The focused runs passed: 120 core tests, 121 adapter tests with vision input enabled, 16 in the minimal adapter build, 10 error classification tests, 156 configuration tests and 5 Web UI tests. Web UI type checking, localization, Rust dependency boundaries and Git object-size checks also passed.

Local verification commands
cargo test --locked -p openbitfun-ai-adapters --features vision-input --lib -- client::image_budget vision_tokens providers::openai::message_converter providers::gemini::message_converter providers::anthropic::message_converter client::tests::aggregated client::responses_websocket client::sse::tests oversized_http_body
cargo test --locked -p openbitfun-ai-adapters --no-default-features --lib -- client::image_budget oversized_http_body
cargo test --locked -p openbitfun-core --no-default-features --features agent-runtime,git,tools-image-analysis --lib -- compression context_budget restored_tool_results image_projection configured_image_limit image_input_requirement prompt_suggestion multi_channel_pool_preserves_history_protocol_credentials_and_actual_usage builtin_miniapp_image image_inputs_keep smart_routing_keeps_visual computer_use_pixels_and_geometry input_image_count image_analysis::attachments image_analysis::image_processing view_image_tool::tests analyze_image_tool::tests image_snapshot image_persistence_tests provider_rejections_stop_after_one
cargo test --locked -p openbitfun-core-types --lib errors::tests
cargo test --locked -p openbitfun-config-contracts --lib
pnpm --dir src/web-ui run test:run src/shared/ai-errors/aiErrorPresenter.test.ts
pnpm run type-check:web
pnpm run i18n:audit
pnpm run check:core-boundaries
git diff --check

AI-assisted. Verified locally against loopback providers and a simulated remote-workspace filesystem. Live provider gateways, SSH hosts, remote control, peer devices and detached dispatch were not exercised.

@kev1n77
kev1n77 merged commit d0e7aea into openBitFun:main Nov 25, 2026
13 checks passed
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.

[Bug]: 工具返回的图片固定按 850 token 估算,多轮读图可能超出上下文窗口

2 participants