Repository navigation
fix(core): fit compaction output limit to the request actually sent - #55613
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
No blocking problems. The output limit is only ever lowered, so normal compactions and limits set by hooks are untouched. Retries shrunk after a rejection get more room again, still no more than the original limit. I checked it by running the new test against the base: the summary request is sent with maxTokens: 20000 there and 1024 with this change. Compaction-related tests and typecheck pass in packages/core.
Public API is unchanged; the change is internal to compaction.
One concern that doesn't block the merge, noted inline: fitOutput repeats the logic of outputLimit in model-request.ts but leaves out its estimate margin.
Related: #54797
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
No issue filed; reported in Slack with a repro script. Related: #54797 (same provider error shape on normal turns).
Type of change
What does this PR do?
Compaction picked the summary's output limit in
prepare()assuming the request would be shrunk tobudget:max_tokens = window − budget.deliver()then sends a bigger request when it can't shrink the conversation (e.g. a single exchange larger than the window), but kept that same output limit. Input + requested output then exceeded the window even by our own estimate, and providers that enforceinput + max_tokens <= windowreject it withcontext_length_exceeded. Shrink retries only reduce input, so compaction failed with "cannot be reduced further".Wire capture from the repro (
opencode/cat-jam-free, configured window 262,144, real 1,048,576):The fix lowers the output limit right before each summary request is sent, to the room that request leaves in the window, never below 1,024 and never above the existing limit (including a limit set by a plugin hook). Normal compactions are unchanged; only requests sent above the target get a smaller limit. This matches what pi does at its transport layer.
It does not fix token estimates that run low (#54797), or a single message too large for the real window.
How did you verify your code works?
maxTokens: 1024(20,000 before the fix).bun run checkpasses;session-compaction,session-compact,session-runner,plugin/session-compact,session-model-request-hookstests pass.max_tokens=1024, is accepted, and both cases replyOK(2 clean runs).Screenshots / recordings
N/A
Checklist
Added by the opencode bot (AI-generated).
Related: #54797