Skip to content

fix(core): bound compaction request size - #36589

Closed
kitlangton wants to merge 2 commits into
v2from
bounded-compaction-prod
Closed

kitlangton wants to merge 2 commits into
v2from
bounded-compaction-prod

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

What

Large sessions no longer become permanently wedged when their model context fits but the serialized inference request exceeds the service's 10 MiB body limit.

Before, auto-compaction only considered model token pressure. A session at roughly 240k of a 1M-token context could therefore skip compaction and receive HTTP 413 forever. Compaction itself could also exceed an upstream body limit because it summarized the entire old history in one request.

After this change, OpenCode compacts before the request reaches 8 MiB and rolls large histories through bounded summary requests while preserving a final recent tail.

How

  • packages/core/src/session/runner/llm.ts builds the semantic request before the compaction decision and measures its system, messages, and tools payload.
  • packages/core/src/session/compaction.ts treats 8 MiB as independent compaction pressure and summarizes old history in 32k-token chunks, carrying each structured summary into the next chunk.
  • Intermediate summaries remain internal; only the final summary delta and durable compaction result are published.
  • Compaction output is capped at 4,096 tokens.

Scope

This does not raise the inference service's 10 MiB request-body limit. It keeps OpenCode requests safely below that independent transport boundary.

Testing

  • bun run test test/session-compaction.test.ts in packages/core: 6 passed
  • bun typecheck in packages/core
  • Commit hook: all 31 workspace typechecks passed
  • Production recovery: the previously wedged Slack-backed session completed bounded compaction, persisted session.compaction.ended, and then completed the requested response in the same thread

Flow

flowchart TD
  A[Build semantic LLM request] --> B{Token pressure or body >= 8 MiB?}
  B -- no --> C[Send normal model request]
  B -- yes --> D[Keep recent tail]
  D --> E[Split older history into bounded chunks]
  E --> F[Summarize chunk]
  F --> G{More chunks?}
  G -- yes --> H[Carry summary into next chunk]
  H --> F
  G -- no --> I[Persist final summary + recent tail]
  I --> A
Loading

@skyzhao1223

Copy link
Copy Markdown
Contributor

Hi! Thanks for this work — bounding the serialized request below the 10 MiB body limit and chunked summarization looks like a solid fix for the HTTP 413 wedge.

I'm looking into a related but (I believe) distinct failure mode: Session too large to compact - context exceeds model limit even after stripping media. In that case the model context itself is already over the limit, so there isn't enough room to even send a summarization request — the session just errors out and is unrecoverable.

My idea is a model-free pre-prune before compaction (drop oldest messages / drop tool outputs / strip media locally, purely by size), so there's always some headroom to run a bounded summarization afterwards. Does your approach leave room for that, or would it conflict with the compaction flow you've built here? Happy to coordinate and avoid overlap.

@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.

2 participants