feat(opencode): add threshold-based compaction via maxTurns config#21567
feat(opencode): add threshold-based compaction via maxTurns config#21567YGoetschel wants to merge 1 commit intoanomalyco:devfrom
Conversation
Introduces `compaction.maxTurns` config option that triggers proactive compaction every N agent turns, regardless of context size. - Add `isThresholdReached()` pure function to `overflow.ts` - Expose `isThresholdReached` on `SessionCompaction` service and interface - Track `turnsSinceCompaction` counter in the session run loop, resetting on every compaction trigger and incrementing only after real LLM turns - Add 4 unit tests covering disabled, boundary, below-threshold, and edge-case (maxTurns=1) scenarios
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found several potentially related PRs: Most Related:
Why they might be related: These PRs address compaction thresholds, context management, and automated compaction triggers—similar to your I'd recommend reviewing #10123 and #18951 most closely to ensure there's no functional overlap. |
Issue for this PR
Closes #
Type of change
What does this PR do?
Adds a
compaction.maxTurnsconfig option that triggers proactive context compaction every N agent turns, regardless of how full the context window is.The existing compaction strategy is purely reactive — it fires only when token usage approaches the model's context limit. This works for overflow prevention but leaves sessions that accumulate irrelevant context over many turns untouched until they hit the limit.
maxTurnsfills that gap by letting users set a fixed compaction cadence.Implementation details:
overflow.ts: newisThresholdReached()pure function, symmetric with the existingisOverflow()— readscfg.compaction.maxTurnsand returns true whenturnsSinceCompaction >= maxTurnscompaction.ts: exposesisThresholdReachedon theSessionCompactionservice interface and as a standalone async exportconfig.ts: addsmaxTurns(optional positive integer) to the compaction config schemaprompt.ts: introduces a dedicatedturnsSinceCompactioncounter in the run loop — increments only after real LLM turns complete, resets whenever any compaction is triggered. The threshold check sits after the overflow check and is guarded bylastFinished.summary !== trueto avoid triggering immediately after a compaction message.How did you verify your code works?
test/session/compaction.test.tscovering: disabled (no config), exact boundary (turnsSinceCompaction === maxTurns), below threshold, and edge case (maxTurns: 1)bun test test/session/compaction.test.ts— 42/42 passbun turbo typecheck— 0 errors across all packagesScreenshots / recordings
N/A — no UI changes.
Checklist