Skip to content

fix(session): prune tool outputs before serializing compaction request - #2070

Open
YOMXXX wants to merge 2 commits into
XiaomiMiMo:mainfrom
YOMXXX:fix/compaction-prune-before-serialize
Open

fix(session): prune tool outputs before serializing compaction request#2070
YOMXXX wants to merge 2 commits into
XiaomiMiMo:mainfrom
YOMXXX:fix/compaction-prune-before-serialize

Conversation

@YOMXXX

@YOMXXX YOMXXX commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1661. Compaction fails with "context exceeds model limit" even when the thread's live token count is well under the model window (e.g. 100k used vs 1M window) — large tool outputs serialized into the compaction request inflate it past the limit.

Root cause: packages/opencode/src/session/compaction.ts has a DB prune() for tool outputs that is defined but never called. compaction.process builds its modelMessages from selected.head via toModelMessagesEffect WITHOUT pruning old tool outputs, so a session with big tool bodies (file reads, diffs) overflows the compaction model.

Fix: strip old tool outputs on the in-memory copy before serialization:

  • New pruneToolOutputs(messages) mirrors the DB prune walk (protect last 2 user turns + summary boundary + PRUNE_PROTECTED_TOOLS, accumulate past PRUNE_PROTECT, strip output and providerOutput).
  • Called in compaction.process after structuredClone(selected.head) and before toModelMessagesEffect — never touches the DB or the visible transcript.

Test Plan

  • 4 focused tests: strips old outputs past PRUNE_PROTECT; keeps protected skill outputs; small sessions untouched; summary boundary stops the walk.
  • bun typecheck clean; session suite 913 pass no regression.

Notes

  • The in-memory strip is the right scope for Compaction fails - context exceeds model limit (it shouldn't) #1661 (it bounds the compaction request). The DB prune() remains unwired (separate storage-savings enhancement).
  • Known limitation (documented in review): tool input and non-provider-executed structured output are not bounded — the fix targets the common large-output case (read/bash/edit/write).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compaction fails - context exceeds model limit (it shouldn't)

1 participant