feat(tarko-agent): context compression strategies#1496
Open
Conversation
Add comprehensive context compression system to prevent context window overflow: - Four built-in strategies: sliding_window, structured_summary, tool_response_compression, smart_truncation - Inspired by Manus (external storage), Claude Code (8-section summary), Gemini CLI (sliding window) - Configurable compression thresholds and strategies - Automatic compression triggers and manual compression support - Integration with Agent message history and event stream - Comprehensive test coverage and documentation - Backward compatible with existing Agent configurations Strategies: - sliding_window: Keeps recent messages (Gemini CLI approach, 70% threshold) - structured_summary: Creates structured summaries (Claude Code approach, 92% threshold) - tool_response_compression: Compresses large tool responses (Manus approach) - smart_truncation: Intelligent message selection based on importance scoring Features: - Token counting and context window management - Compression statistics and monitoring - Strategy registry for custom implementations - Event stream integration for compression notifications - Graceful fallbacks and error handling
Add comprehensive safety mechanisms to prevent context overflow: - ContextSafetyGuards class with multi-layer protection - Automatic preprocessing to truncate oversized messages - Emergency compression as last resort fallback - Validation of compressed results with safety margins - Graceful degradation when normal strategies fail Safety layers: 1. Preprocessing: Truncate messages > 30% of context window 2. Strategy compression: Apply selected compression strategy 3. Validation: Ensure results don't exceed limits (10% margin) 4. Emergency compression: Aggressive fallback (50% target) 5. Graceful fallback: Minimal viable context if all else fails Features: - Individual message size validation and truncation - Multimodal content handling in safety checks - Emergency compression preserving system + last user message - Comprehensive test coverage (51 tests passing) - Detailed logging with [EMERGENCY] markers This provides near-guarantee against context overflow while preserving maximum possible context through intelligent fallback mechanisms.
Add comprehensive context compression system with multiple strategies: - sliding_window: Gemini CLI approach (70% threshold, 30% preservation) - structured_summary: Claude Code approach (8-section summarization) - tool_response_compression: Manus approach (external storage pattern) - smart_truncation: Intelligent message selection Includes safety guards, token counting, and automatic compression
✅ Deploy Preview for agent-tars-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Maintain sync toMessageHistory for existing tests while adding async version for context compression support in agent runner
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
Implement comprehensive context compression system for
@tarko/agentto prevent "Prompt too long" errors in multi-turn conversations.Features
sliding_window,structured_summary,tool_response_compression,smart_truncationtoMessageHistoryAPI while adding async compressionStrategy Details
Configuration
Backward Compatibility
toMessageHistory()method remains synchronoustoMessageHistoryAsync()method handles compression in agent runnerChecklist