Add context compaction event system and tiered compaction strategy#22
Merged
Add context compaction event system and tiered compaction strategy#22
Conversation
Introduces a structured context management system inspired by Claude Code's auto-compress and OpenAI Codex's context condensing patterns: - Add ContextCompaction event type with CompactionTier (Trim/Summarize/Reset) - Add CompactionSummary scratchpad entry for LLM-generated summaries - Implement evaluate_and_compact() with configurable usage thresholds - Add emergency_reset() for >95% context usage scenarios - Wire compaction events into CLI printer with visual output - Handle Summary entries in scratchpad formatter and native history - Include distrijs patch for @distri/core types and @distri/react hooks - Add design document covering architecture and integration points https://claude.ai/code/session_01BG3BRtrAJf7C7uE11X8TXK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a comprehensive context management system with tiered compaction strategies to handle token budget constraints in long-running agent conversations. It adds event emission, configuration thresholds, and the foundational infrastructure for both mechanical and semantic (LLM-powered) context compression.
Key Changes
Core Infrastructure
New
CompactionTierenum (distri-types/events.rs): Defines three compaction levels:Trim: Mechanical truncation of old entries and payload compactionSummarize: LLM-powered semantic compression (recommended at >80% usage)Reset: Emergency mode preserving only essentials (>95% usage)New
ContextCompactionevent (distri-types/events.rs): Emitted when compaction occurs, includes:New
CompactionSummaryscratchpad entry type (distri-types/execution.rs): Stores LLM-generated summaries of compacted history with metadata (entries summarized, timestamp range, tokens saved)Context Size Manager Enhancements
New configuration thresholds (
context_size_manager.rs):trim_threshold(default 0.6): Trigger mechanical compactionsummarize_threshold(default 0.8): Trigger semantic compactionreset_threshold(default 0.95): Trigger emergency resetpost_compaction_target(default 0.4): Target usage after compactionCompactionResultstruct: Encapsulates compaction evaluation results with tier, token counts, affected entries, and usage ratioevaluate_and_compact()method: Evaluates current context usage and applies appropriate tier without modifying entries (caller responsible for LLM summarization)emergency_reset()method: Preserves only user task + last 2 entries for extreme casesExecutor Context Integration
evaluate_compaction()method (context.rs): Evaluates context and emitsContextCompactionevent; should be called before each LLM call in agent loopstore_compaction_summary()method (context.rs): Stores LLM-generated summaries in scratchpad after Tier 2 compactionCLI Support
distri-server-cli/printer.rs): Displays compaction events with tier label, token savings, entry count, and usage percentage; includes summary preview for Tier 2Scratchpad Formatting
formatter.rs): Compaction summaries formatted as assistant messages in contextscratchpad.rs): Summaries included in scratchpad context alongside tasks and plan stepsClient-Side Types (distrijs)
distrijs-context-management.patch):CompactionTier,ContextCompactionEvent,ContextHealthtypesuseContextHealth()hook for tracking context usage and compaction eventsContextIndicatorReact component for visual context health display with color-coded progress barImplementation Notes
evaluate_and_compact()method returns a result indicating summarization is needed, but the actual LLM call is left to the caller (agent loop) to implementevaluate_and_compact()Testing Considerations
https://claude.ai/code/session_01BG3BRtrAJf7C7uE11X8TXK