Skip to content

feat(opencode): add threshold-based compaction via maxTurns config#21567

Open
YGoetschel wants to merge 1 commit intoanomalyco:devfrom
YGoetschel:feat/threshold-based-compaction
Open

feat(opencode): add threshold-based compaction via maxTurns config#21567
YGoetschel wants to merge 1 commit intoanomalyco:devfrom
YGoetschel:feat/threshold-based-compaction

Conversation

@YGoetschel
Copy link
Copy Markdown

Issue for this PR

Closes #

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds a compaction.maxTurns config 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. maxTurns fills that gap by letting users set a fixed compaction cadence.

Implementation details:

  • overflow.ts: new isThresholdReached() pure function, symmetric with the existing isOverflow() — reads cfg.compaction.maxTurns and returns true when turnsSinceCompaction >= maxTurns
  • compaction.ts: exposes isThresholdReached on the SessionCompaction service interface and as a standalone async export
  • config.ts: adds maxTurns (optional positive integer) to the compaction config schema
  • prompt.ts: introduces a dedicated turnsSinceCompaction counter 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 by lastFinished.summary !== true to avoid triggering immediately after a compaction message.

How did you verify your code works?

  • Added 4 unit tests in test/session/compaction.test.ts covering: disabled (no config), exact boundary (turnsSinceCompaction === maxTurns), below threshold, and edge case (maxTurns: 1)
  • bun test test/session/compaction.test.ts — 42/42 pass
  • bun turbo typecheck — 0 errors across all packages
  • Manually traced the loop execution to verify the counter semantics (increments only on LLM turns, not on compaction-processing iterations)

Screenshots / recordings

N/A — no UI changes.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

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
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found several potentially related PRs:

Most Related:

  1. feat(session): add custom compaction thresholds along with prevention of continuous compaction #10123 - feat(session): add custom compaction thresholds along with prevention of continuous compaction

    • Directly related as it also deals with compaction thresholds for sessions
  2. fix: trigger auto-compaction at the configured threshold #18951 - fix: trigger auto-compaction at the configured threshold

    • Similar theme of triggering compaction based on configured thresholds
  3. feat(compaction): harden compaction system with breaker, retry, caps, and budget #20516 - feat(compaction): harden compaction system with breaker, retry, caps, and budget

    • Related to the broader compaction system improvements
  4. fix(opencode): scale prune thresholds to model context size #21209 - fix(opencode): scale prune thresholds to model context size

    • Related to threshold-based context management

Why they might be related: These PRs address compaction thresholds, context management, and automated compaction triggers—similar to your maxTurns threshold feature. PR #10123 in particular seems to address "custom compaction thresholds" which overlaps conceptually with your new maxTurns config option.

I'd recommend reviewing #10123 and #18951 most closely to ensure there's no functional overlap.

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.

1 participant