Skip to content

Conversation

@code-yeongyu
Copy link
Owner

Summary

Fixes #272 - Dynamic context pruning for compaction recovery

  • Previously, DCP ran before any compaction strategy (as a first attempt)
  • Now DCP runs after summarize fails, giving it a chance to prune context when compaction is actually failing
  • After successful DCP pruning, the compaction retries (summarize) with the reduced context

Changes

  • Added DcpState interface to track DCP attempts per session
  • Moved DCP execution from running first → to running after summarize fails
  • Added retry logic: after DCP prunes items, reset retry state and retry compaction
  • Added state management to prevent infinite DCP loops (only runs once per compaction cycle)

New Flow

  1. Aggressive truncation (if enabled)
  2. Single tool output truncation
  3. Summarize (with retries)
  4. NEW: If summarize fails → Run DCP
  5. If DCP pruned something → Retry compaction
  6. Revert (fallback, only if DCP didn't help)
  7. Final failure

🤖 GENERATED WITH ASSISTANCE OF OhMyOpenCode

@greptile-apps
Copy link

greptile-apps bot commented Dec 27, 2025

Greptile Summary

This PR successfully refactors the Dynamic Context Pruning (DCP) execution to run after summarization fails instead of before, addressing issue #272. The change improves the compaction recovery strategy by giving summarization a chance to work first before attempting more aggressive pruning.

Key Changes

  • Execution Order: DCP now runs as a fallback after summarize attempts are exhausted (moved from step 1 to step 4 in the compaction flow)
  • State Management: Added DcpState interface with attempted flag to prevent infinite loops - DCP runs only once per compaction cycle
  • Retry Logic: After successful DCP pruning, the retry counter is reset to 0 and compaction is retried from the beginning
  • Proper Cleanup: DCP state is cleared in clearSessionState() along with other session state

Flow Analysis

The new flow is:

  1. Aggressive truncation (experimental)
  2. Single tool output truncation
  3. Summarize with retries (max 2 attempts)
  4. If summarize fails → Run DCP (new position)
  5. If DCP pruned items → Reset retry state and retry compaction
  6. Revert strategy (fallback)
  7. Final failure

Notes

  • The dcpState.attempted flag correctly prevents infinite loops by ensuring DCP runs only once per compaction cycle
  • State is properly cleared after successful revert (line 680) and at final failure (line 708)
  • Missing test coverage for the new DCP retry flow in executor.test.ts

Confidence Score: 4/5

  • This PR is safe to merge with minor considerations around test coverage
  • The implementation correctly moves DCP execution from before compaction to after summarize fails, with proper state management to prevent infinite loops. The dcpState.attempted flag ensures DCP runs only once per compaction cycle, and state is properly cleared during cleanup. However, test coverage for the new DCP flow is missing.
  • Pay attention to src/hooks/anthropic-auto-compact/executor.test.ts - consider adding tests for the DCP retry flow

Important Files Changed

Filename Overview
src/hooks/anthropic-auto-compact/executor.ts Added DCP execution after summarize failures with proper state management and retry logic
src/hooks/anthropic-auto-compact/types.ts Added DcpState interface to track DCP attempts and items pruned per session
src/hooks/anthropic-auto-compact/pruning-executor.ts Orchestrates DCP strategies (deduplication, supersede, purge errors) and applies pruning to session storage
src/hooks/anthropic-auto-compact/executor.test.ts Added dcpStateBySession to test state initialization, but no tests for DCP flow itself

Sequence Diagram

sequenceDiagram
    participant User
    participant Hook as Auto-Compact Hook
    participant Executor as executeCompact
    participant DCP as Dynamic Context Pruning
    participant Storage as Session Storage

    User->>Hook: Token limit error
    Hook->>Executor: executeCompact()
    
    Note over Executor: 1. Aggressive Truncation (if enabled)
    
    Note over Executor: 2. Single Tool Output Truncation
    
    Executor->>Executor: Try Summarize (max 2 attempts)
    
    alt Summarize Succeeds
        Executor->>User: Continue with compaction
    else Summarize Fails
        Note over Executor: 3. Check DCP State
        
        alt DCP not attempted yet
            Executor->>DCP: executeDynamicContextPruning()
            DCP->>Storage: Run deduplication strategy
            DCP->>Storage: Run supersede writes strategy
            DCP->>Storage: Run purge errors strategy
            Storage->>DCP: Return pruned items & tokens saved
            
            alt Items were pruned
                DCP->>Executor: Success (items > 0)
                Executor->>Executor: Reset retry state (attempt = 0)
                Executor->>Executor: Recursive call executeCompact()
                Note over Executor: Retries from step 1 with reduced context
            else No items pruned
                DCP->>Executor: No items pruned
                Executor->>Executor: Continue to Revert
            end
        else DCP already attempted
            Note over Executor: Skip DCP (already ran once)
        end
        
        Note over Executor: 4. Revert Strategy
        Executor->>Storage: Revert last message pair
        Executor->>User: Retry with reverted state
    end
Loading

@code-yeongyu code-yeongyu force-pushed the fix-compaction-retry-with-dcp branch from 5c60138 to bcafefb Compare December 27, 2025 15:31
…etry after pruning

Make DCP behavior opt-in via new 'dcp_on_compaction_failure' experimental flag (disabled by default).

When enabled, Dynamic Context Pruning only executes after summarization fails, then retries compaction. By default, DCP runs before truncation as before.

Changes:
- Add 'dcp_on_compaction_failure' boolean flag to experimental config (default: false)
- Update executor.ts to check flag before running DCP behavior
- Add corresponding documentation to all 4 README files (EN, KO, JA, ZH-CN)
- Update JSON schema

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
@code-yeongyu code-yeongyu force-pushed the fix-compaction-retry-with-dcp branch from bcafefb to 70d4d09 Compare December 27, 2025 15:35
@code-yeongyu code-yeongyu changed the base branch from master to dev December 27, 2025 15:43
@code-yeongyu code-yeongyu merged commit c4c0d82 into dev Dec 27, 2025
6 checks passed
@code-yeongyu code-yeongyu deleted the fix-compaction-retry-with-dcp branch December 27, 2025 15:43
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.

Dynamic context pruning for compaction recovery

2 participants