Repository navigation
fix(opencode): cap images re-sent from history (backport #39929) - #62270
Open
adamdotdevin wants to merge 1 commit into
Open
adamdotdevin wants to merge 1 commit into
adamdotdevin wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Issue for this PR
Backport of #39929 (and the follow-up #56983) to the 1.x release line. No separate issue.
Type of change
What does this PR do?
1.x keeps every image in a session's history and re-sends all of them as base64 on every request. Auto-compaction only counts tokens, and images cost almost none, so on 1M-context models (e.g.
deepseek-v4.1-flash) request bodies reach 100–200 MB before compaction kicks in. On 2026-11-05, 1.x clients sent 23.4k of the 29.4k requests over 64 MB that reached the OpenCode gateway; 2.x sent 232. Those bodies run gateway isolates out of memory and get 413/400s from providers.Image cap (ported from 2.x
boundImages). At the end ofMessageV2.toModelMessagesEffect, once inline images in the converted history pass 25 MiB, the oldest are replaced with the same placeholder text 2.x uses until 15 MiB remain. It uses the stable-cut version from #56983: the cut only moves each time another 10 MiB of images arrives, so the replaced prefix and the provider prompt cache stay the same between cuts. It counts user image/file parts, tool-resultmedia/image-data/file-dataitems, and the synthetic user message that carries tool-result images for providers that can't take them in tool results (OpenAI-compatible and others). Remote URLs don't count.Differences from 2.x:
ModelMessage[], since 1.x has noLLMRequest. It's applied intoModelMessagesEffect, which feeds both the AI SDK and native runtimes, title generation and the compaction size estimate, rather than inProviderTransform.message(which receives the AI SDK's provider-level prompt shape on one path andModelMessageon the other).unsupportedPartscomposition. 1.x handles unsupported media separately inProviderTransform.message, which runs after this.Overflow classification.
Request body too largeandmultimodal data size is too largenow count as context overflow in@opencode-ai/llm'sisContextOverflow, so 1.x compacts and retries instead of failing the turn, the same way it already handles HTTP 413 andcontext_length_exceeded.How did you verify your code works?
boundImagestests from 2.x (below the trigger, oldest-first replacement with identity kept for untouched messages, stable cut across new images, images nested in tool results), plus one end-to-endtoModelMessagestest for the OpenAI-compatible synthetic user message path. Added both new error strings to the existing overflow tests inpackages/llmandmessage-v2.test.ts.bun typecheckinpackages/opencodeandpackages/llm.bun test test/session test/providerinpackages/opencode(1166 pass, 0 fail).bun testinpackages/llm(298 pass, 0 fail).@rekram1-node: this is your #39929 on the v1 branch. Could you take a look?
Screenshots / recordings
N/A
Checklist