Skip to content

fix(opencode): bound compaction request payload#29860

Closed
timrichardson wants to merge 3 commits into
anomalyco:devfrom
timrichardson:fix/compact-large-session-budget
Closed

fix(opencode): bound compaction request payload#29860
timrichardson wants to merge 3 commits into
anomalyco:devfrom
timrichardson:fix/compact-large-session-budget

Conversation

@timrichardson

@timrichardson timrichardson commented May 29, 2026

Copy link
Copy Markdown

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

@timrichardson timrichardson force-pushed the fix/compact-large-session-budget branch from 8a5f9a4 to 0180516 Compare May 31, 2026 02:09
@timrichardson timrichardson force-pushed the fix/compact-large-session-budget branch from 0180516 to 4484742 Compare May 31, 2026 02:12
@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: /compact can exceed model context before summarization on large sessions

1 participant