Skip to content

feat: configurable auto-compact threshold with Ctrl+L keybinding#1723

Closed
aboimpinto wants to merge 2 commits into
Hmbown:mainfrom
aboimpinto:feat/auto-compact-threshold
Closed

feat: configurable auto-compact threshold with Ctrl+L keybinding#1723
aboimpinto wants to merge 2 commits into
Hmbown:mainfrom
aboimpinto:feat/auto-compact-threshold

Conversation

@aboimpinto

Copy link
Copy Markdown
Contributor

Problem

At ~99.6% context saturation, the TUI becomes completely unresponsive — a chicken-and-egg deadlock where the model is too slow to suggest /compact and no automatic compaction fires. Both guardrails (capacity controller and auto-compact) are disabled by default since v0.8.11.

Closes #1722

Solution

Adds a configurable auto-compact threshold (default 70%) and a Ctrl+L keybinding for manual compaction, closing the gap between model guidance (suggest /compact at 60%) and engine action (which previously only fired at 95%).

Changes

File +/- Description
crates/tui/src/settings.rs +25 New auto_compact_threshold_percent field, default 70.0, set handler with validation (10-100), help text
crates/tui/src/tui/app.rs +3 auto_compact_threshold_pct field in App struct, wired from settings
crates/tui/src/tui/ui.rs +49 New 60% early-warning constant, maybe_warn_context_pressure() early-warning tier, should_auto_compact_before_send() uses configurable threshold + 500K floor, Ctrl+L keybinding
crates/tui/src/prompts.rs +1/-1 System prompt informs model about auto-compact threshold and Ctrl+L

Configuration

# ~/.deepseek/config.toml
auto_compact = true                     # master switch (still defaults to false)
auto_compact_threshold_percent = 70     # fires at 70% (NEW)

Behavior

  • 60% -> Status bar: "Context building - Auto-compaction will fire before next send." (non-intrusive)
  • 70% -> Engine compacts before next send (if >500K tokens)
  • Ctrl+L -> Manual compaction at any time, even mid-turn

Backward Compatibility

  • auto_compact still defaults to false — no change for existing users
  • 500K hard floor unchanged — protects small sessions from cache destruction
  • Existing /compact command unchanged

Adds configurable auto-compact threshold (default 70%) and Ctrl+L keybinding for manual compaction.

- New auto_compact_threshold_percent setting (default 70.0, range 10-100)
- should_auto_compact_before_send() now uses configurable threshold + 500K hard floor
- Early context-pressure warning at 60% (CONTEXT_SUGGEST_COMPACT_THRESHOLD_PERCENT)
- Ctrl+L keybinding triggers Op::CompactContext directly, even mid-turn
- Updated system prompt to inform model about auto-compact and Ctrl+L

Backward compatible: auto_compact still defaults to false.
Closes Hmbown#1722

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a configurable threshold for automatic context compaction, defaulting to 70%, and adds a Ctrl+L shortcut for manual compaction. It also implements an early warning status message when context usage exceeds 60% and updates the system prompt to inform the AI of these mechanisms. Feedback was provided regarding the early warning hint, which may be misleading when auto-compaction is blocked by the 500K token floor or when the user-configured threshold has not yet been reached; a more precise logic for these status messages was suggested.

Comment thread crates/tui/src/tui/ui.rs Outdated
The hint now checks both the 500K floor and configured threshold before claiming auto-compaction will fire. Warning threshold uses min(60%, user_threshold) so custom thresholds still see early warnings.
@aboimpinto

Copy link
Copy Markdown
Contributor Author

Fixed. The hint now checks both gates (500K floor and configured threshold) before claiming auto-compaction will fire. Four distinct hints cover every combination. Warning threshold also uses min(60%, user_threshold).

@Hmbown Hmbown added this to the v0.8.40 milestone May 17, 2026
@Hmbown

Hmbown commented May 17, 2026

Copy link
Copy Markdown
Owner

This is attached to v0.8.40 because context-pressure hangs matter, but please keep the bar high here. The safe path is a minimal, well-tested change: no default auto-compaction behavior changes without clear cache/cost analysis, and Ctrl+L / threshold behavior needs targeted tests plus a manual long-context smoke note. We should prefer the smallest fix that breaks the 99% context deadlock.

aboimpinto added a commit to aboimpinto/CodeWhale that referenced this pull request May 17, 2026
…#1723)

- 6 tests for should_auto_compact_before_send: 500K floor, threshold boundaries, master switch
- 5 tests for maybe_warn_context_pressure: all 4 hint messages, disabled hint
@aboimpinto

Copy link
Copy Markdown
Contributor Author

Maintainer hardening requests — addressed

A1: Cache/cost analysis ✅

auto-compact-cache-cost-analysis.md

Three independent gates prevent surprise compaction:

  • auto_compact defaults to false — zero impact unless user opts in
  • 500K token floor (MINIMUM_AUTO_COMPACTION_TOKENS) unchanged since v0.8.11
  • Configurable threshold (70% default) only fires above both floor and threshold

No prefix-cache regression: the 500K floor is the same constant from the compaction.rs code.

A2: Targeted tests ✅ (11 tests)

Pushed to feat/combined-local-build (commit e0b1222a):

should_auto_compact_before_send() — 6 tests:

  • 500K floor blocks compaction even at high percentage
  • Fires when above floor and threshold
  • Blocked when above floor but below threshold
  • Threshold 10% fires early, 100% never fires (boundary extremes)
  • Master switch auto_compact = false always blocks

maybe_warn_context_pressure() — 5 tests:

  • Without auto_compact: "Consider enabling auto_compact or use /compact."
  • Below floor + below threshold: "below 500K floor and X% threshold"
  • Below floor + above threshold: "below 500K token floor"
  • Above floor + below threshold: "will fire at X%"
  • Above floor + above threshold: "would fire now"

A3: Manual smoke test ✅

auto-compact-smoke-test.md

6 test scenarios: 60% warning, threshold firing, 500K floor blocking, Ctrl+L (idle, mid-turn, with text), threshold boundaries, disabled auto-compact. Step-by-step with expected outputs.

Bonus: Multi-tier trailing compaction proposal

trailing-auto-compact-proposal.md

For v0.8.41 discussion: instead of one heavy compaction at 70%, compact in small increments at 50% / 60% / 70%. User feedback: "I prefer small compacts that don't affect memory much and don't take much time, rather than wait several minutes for a heavy compact."

@Hmbown

Hmbown commented May 23, 2026

Copy link
Copy Markdown
Owner

This PR was opened before the v0.8.41 rebrand and is now stale. Feel free to rebase onto current main and reopen. 鲸鱼兄弟们等你 🐋

@Hmbown Hmbown closed this May 23, 2026
Hmbown added a commit that referenced this pull request Jun 1, 2026
Refs #1722

Preserves auto_compact as opt-in, adds the saved threshold setting, keeps the 500K hard floor, and wires Ctrl+L as a manual compaction shortcut for context-pressure recovery.

Harvested from PR #1723 by @aboimpinto

Co-authored-by: Paulo Aboim Pinto <aboimpinto@gmail.com>
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.

feat: configurable auto-compact threshold with Ctrl+L keybinding

2 participants