Skip to content

CLI-56: Implement Automatic Context Compaction and Retry - #620

Merged
dwash96 merged 16 commits into
cecli-dev:v1.0.0from
szmania:cli-56-add-automatic-context-compaction
Jul 26, 2026
Merged

CLI-56: Implement Automatic Context Compaction and Retry#620
dwash96 merged 16 commits into
cecli-dev:v1.0.0from
szmania:cli-56-add-automatic-context-compaction

Conversation

@szmania

@szmania szmania commented Jul 25, 2026

Copy link
Copy Markdown

This pull request implements automatic context compaction and retry for tool calls when a context window exceeded error occurs.

Key Changes:

  • Automatic Compaction: When a ContextWindowExceededError is encountered, the system will automatically attempt to compact the chat history and file diffs.
  • Intelligent Worthiness Check: Compaction will only trigger if the compactable parts of the context (chat history, file diffs) constitute a significant portion of the total context, or if the potential savings from compaction are enough to bring the request within the token limit. This prevents unnecessary compaction when most tokens are from added files.
  • Consolidated Messaging: The "Skipping compaction" message will now appear only once per prompt, improving user experience.
  • Retry Mechanism: After compaction, the tool call is automatically retried.
  • Configurable Retries: The max-compaction-retries option (default 3, range 1-10) limits the number of compaction attempts.
  • Agent Mode Safety: In agent mode, the retry cap is more conservative (2 retries).
  • Immutable Settings: Compaction settings are frozen at startup to prevent runtime tampering.
  • Structured Logging: Each compaction attempt is logged with relevant details for observability.
  • Error Scrubbing: Sensitive information is removed from user-facing error messages.

Conditions for Auto-Compaction Triggering:
Automatic context compaction is triggered under the following conditions:

  1. Reactive Trigger (Primary): When a ContextWindowExceededError is returned by the LLM API during a tool call, and enable-context-compaction is set to true.
  2. Proactive Trigger (Pre-check): Before sending a request, if the estimated token count exceeds the model's limit AND enable-context-compaction is true, the system will attempt compaction first before prompting the user.
  3. Worthiness Check: Before compacting, the system evaluates whether compaction would be effective:
    • Percentage Check: Chat history + file diffs must constitute at least 20% of the total context.
    • Absolute Savings Check: The potential savings from compaction (90% of compactable tokens) must be enough to bring the request within the token limit.
    • If neither condition is met, compaction is skipped with a message suggesting /drop to remove files.
  4. Token Floor Guard: Compaction will not re-compact if the context is already below 25% of the maximum tokens, preventing infinite compaction loops on near-empty context.
  5. Retry Limit: Compaction will not retry more than max-compaction-retries times (default 3, max 10). In agent mode, the cap is 2.

Commits included:

  • 82ddc83 cli-56: added automatic context compaction
  • dff1146 refactor: Consolidate context compaction checks
  • a0e4f3e refactor: Add absolute savings check for context compaction
  • 518f6f6 feat: add automatic context compaction fallback in BaseCoder
  • 4518495 cli-56: add automatic context compaction
  • 686f4ad fix: Enable context compaction by default in tests
  • ed8ee0e fix: Add dataclass decorator to FrozenCompactionSettings
  • 63f9b9d fix: Implement context compaction safety guards
  • bd234bc fix: Fix UT-CTX-009 mock path and APIError status code
  • d4eb2a2 fix: Implement Phase 3 Safety Guards for CLI-56

Note: This PR is from my fork (szmania/cecli) to the upstream cecli-dev/cecli repository. The branch cli-56-add-automatic-context-compaction has been pushed to origin.

@dwash96
dwash96 changed the base branch from main to v1.0.0 July 26, 2026 03:14
@dwash96
dwash96 merged commit fd7c479 into cecli-dev:v1.0.0 Jul 26, 2026
2 of 12 checks passed
@dwash96 dwash96 mentioned this pull request Jul 26, 2026
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.

2 participants