Skip to content

Conversation

@ulivz
Copy link
Member

@ulivz ulivz commented Sep 13, 2025

Summary

Implement comprehensive context compression system for @tarko/agent to prevent "Prompt too long" errors in multi-turn conversations.

Features

  • 4 Built-in Strategies: sliding_window, structured_summary, tool_response_compression, smart_truncation
  • Safety Guards: Multi-layer protection with emergency compression fallback
  • Token Management: Accurate token counting with model-specific limits
  • Custom Strategy Support: Extensible registry for High-level Agents
  • Backward Compatible: Maintains existing toMessageHistory API while adding async compression

Strategy Details

  • Sliding Window (Gemini CLI): 70% threshold, 30% preservation
  • Structured Summary (Claude Code): 8-section summarization, 92% threshold
  • Tool Response Compression (Manus): External storage pattern for large outputs
  • Smart Truncation: Intelligent message selection by importance

Configuration

const agent = new Agent({
  context: {
    compression: {
      enabled: true,
      strategy: 'sliding_window',
      compressionThreshold: 0.7,
    },
  },
});

Backward Compatibility

  • No Breaking Changes: Existing toMessageHistory() method remains synchronous
  • Async Support: New toMessageHistoryAsync() method handles compression in agent runner
  • All Tests Pass: 16/16 existing tests pass without modification

Checklist

  • Added or updated necessary tests (Optional).
  • Updated documentation to align with changes (Optional).
  • Verified no breaking changes, or prepared solutions for any occurring breaking changes (Optional).
  • My change does not involve the above items.

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
@netlify
Copy link

netlify bot commented Sep 13, 2025

Deploy Preview for agent-tars-docs ready!

Name Link
🔨 Latest commit 4238bba
🔍 Latest deploy log https://app.netlify.com/projects/agent-tars-docs/deploys/68c5b62a43b1930008cc203a
😎 Deploy Preview https://deploy-preview-1496--agent-tars-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@ulivz ulivz changed the title feat(tarko-agent): implement context compression strategies feat(tarko-agent): context compression strategies Sep 13, 2025
Maintain sync toMessageHistory for existing tests while adding async version
for context compression support in agent runner
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