Issue for this PR
Closes #29857
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
This branch changes /compact so very large sessions are more likely to compact successfully instead of overflowing while trying to summarize.
The compaction path now builds a bounded model payload. It computes a compaction budget as floor(usable(...) * 0.85), where usable(...) is opencode's safe history budget for the selected model:
- If the model has
limit.input, usable(...) is limit.input - reserved.
- Otherwise it is
limit.context - maxOutputTokens.
reserved comes from config.compaction.reserved, or defaults to min(20_000, maxOutputTokens).
- Models with
limit.context === 0 keep the existing overflow-disabled behavior.
For extra safety, compaction first performs a local preflight using the legacy payload shape: media stripped, reasoning preserved, and tool output capped at 2_000 chars. If that estimate fits the compaction budget, /compact sends the legacy payload unchanged.
If the legacy payload would exceed the budget, /compact falls back to a reduced payload. In that fallback it strips reasoning parts, including large provider metadata like encrypted reasoning blobs, and adaptively truncates tool outputs by binary-searching from 0 to 2_000 chars and keeping the largest preview that still fits the budget.
This also fixes toolOutputMaxChars: 0: on dev, 0 was treated as "no limit"; on this branch, only undefined means "no limit", so 0 can fully remove tool output content while keeping the truncation marker.
Net effect: before, /compact could fail because the summarization request itself was too large. Now it preserves the old payload when safe, and only sends a reduced, budget-aware history when needed, so sessions with huge tool outputs or reasoning metadata can be summarized rather than immediately overflowing.
How did you verify your code works?
bun test test/session/compaction.test.ts (55 pass, 1 skip)
bun test test/session/message-v2.test.ts test/session/revert-compact.test.ts (45 pass)
The compaction tests cover:
- overflow threshold behavior, including
limit.input, output headroom, cache reads, disabled auto-compaction, and unknown context limits
- compaction marker creation and summary process behavior
- tool-output pruning, including protected
skill tool output
- auto-continue, overflow replay, repeated compactions, summary anchoring, abort handling, and no tool calls during summary generation
- the new bounded payload path preserving the legacy payload when it fits
- the new reduced payload path stripping reasoning metadata, truncating tool output, and staying within the calculated compaction budget
- irreducibly oversized payloads failing cleanly by marking the summary message as errored
- plugin-injected compaction context being included in budget sizing
- message conversion behavior for compacted and truncated tool output
- revert plus compact workflows
I also verified the anonymized fixture from #29857 locally:
- legacy payload shape:
788327 estimated tokens, overflows
- fixed payload shape:
308291 estimated tokens, compacts successfully
Note: local bun typecheck / pre-push typecheck is currently blocked by existing event-stream typing errors outside this compaction change (stream.transport.ts, TUI event/sync, desktop server-sdk.tsx). The test commit was pushed with hook bypass after explicit maintainer authorization.
Screenshots / recordings
N/A
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR