Skip to content

Conversation

@jsboige
Copy link

@jsboige jsboige commented Oct 20, 2025

Context Condensation System - PR Description

Overview

This PR introduces a modular context condensation system with four providers to intelligently reduce long conversation size while preserving essential information for assistant interaction consistency:

  • Native Provider: Original condensation method using Anthropic API
  • Lossless Provider: Pre-condensation optimizations with message consolidation
  • Truncation Provider: Simple size-based message reduction
  • Smart Provider: Conditional multi-pass architecture with content-type thresholds

Key Features

Provider Architecture

  • Modular provider system with easy extension
  • Configurable condensation strategy configurations
  • Transparent integration with existing message system

Smart Provider Implementation

The Smart Provider implements a conditional multi-pass architecture:

  • Pass 1: Lossless prelude (message consolidation, tool deduplication)
  • Pass 2: Selective suppression (old tools, large results)
  • Pass 3: Smart summarization (tool results, parameters)
  • Pass 4: Message-level thresholds (content-type specific processing)

Configuration System

Global Parameters

  • Default provider: BALANCED
  • Automatic activation threshold: configurable

Preset Configurations

  • CONSERVATIVE: Quality-first with high thresholds
  • BALANCED: Moderate approach with content-type awareness
  • AGGRESSIVE: Maximum reduction with low thresholds

Hierarchical Thresholds (Phase 7)

  • Global thresholds: trigger/stop/minGain tokens
  • Provider-specific overrides supported
  • Profile-based thresholds (% of context window)

Performance Metrics & Monitoring

Metrics Collection

  • Processing time measurement
  • Token reduction tracking
  • Cost estimation
  • Provider-specific performance data

Real-time Monitoring

  • Provider selection in settings
  • Real-time condensation preview
  • Visual reduction indicators

Configuration

Thresholds

  • Configurable conversation size limits (triggerTokens/stopTokens)
  • Provider-specific threshold overrides
  • Message-level content-type thresholds

Migration

Compatibility

  • Backward compatibility with existing configurations
  • Automatic migration of old settings
  • Compatibility mode for transitions
  • Automatic new version detection
  • Transparent configuration migration
  • User preference preservation

Testing

Test Coverage

  • Unit tests for each provider
  • Integration tests for multi-pass architecture
  • Load tests with long conversations
  • Edge cases (token thresholds)
  • Performance with different presets

Implementation Details

Core Components

  • CondensationManager: Central orchestration with hierarchical thresholds
  • BaseCondensationProvider: Common interface and validation
  • Provider-specific implementations with optimized algorithms
  • Performance metrics collection throughout the pipeline

Provider Algorithms

  • Native: Direct Anthropic API integration with conversation summarization
  • Lossless: Message consolidation and tool deduplication
  • Truncation: Size-based reduction with configurable limits
  • Smart: Multi-pass processing with conditional logic

Configuration Management

  • Type-safe configuration with validation
  • Hierarchical threshold system (global ??? provider ??? profile)
  • Migration system for backward compatibility
  • Real-time configuration updates

Performance Optimization

  • Token estimation for accurate processing
  • Efficient message transformation
  • Memory-conscious processing for large conversations

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Oct 20, 2025
@jsboige jsboige changed the title feat(condense): provider-based context condensation architecture feat(condense): Provider-Based Context Condensation Architecture Oct 23, 2025
- Add CondensationContext for input
- Add CondensationOptions for configuration
- Add CondensationResult for output
- Add ICondensationProvider base interface
- Add ProviderMetrics for telemetry
- Add unit tests for type structures

Part of Context Condensation Provider System (1/30)
- Implement BaseCondensationProvider with common logic
- Add validation, error handling, and metrics tracking
- Add condenseInternal abstract method for providers
- Add helper methods for token counting
- Add comprehensive unit tests

Part of Context Condensation Provider System (2/30)
- Implement ProviderRegistry singleton
- Add register/unregister functionality
- Add provider filtering by enabled status
- Add priority-based sorting
- Add configuration management
- Add comprehensive unit tests

Part of Context Condensation Provider System (3/30)
- Implement NativeCondensationProvider extending BaseProvider
- Replicate original sliding-window condensation behavior
- Support custom condensing prompts
- Support dedicated API handler for condensation
- Add comprehensive unit tests with mocked API responses
- Add cost estimation and token counting

Part of Context Condensation Provider System (4/30)
- Implement CondensationManager singleton
- Orchestrate provider selection and execution
- Auto-register Native provider as default
- Support custom provider selection
- Add provider listing and configuration
- Add comprehensive unit tests

Part of Context Condensation Provider System (5/30)
- Refactor summarizeConversation to use CondensationManager
- Maintain 100% backward compatibility
- Support custom prompts and dedicated handlers
- Add integration tests
- All existing code continues to work unchanged

Part of Context Condensation Provider System (6/30)
…ondensation Provider System

- Add main README.md with quick start guide and architecture overview
- Add detailed ARCHITECTURE.md with Mermaid diagrams and component descriptions
- Add CONTRIBUTING.md guide for creating new providers
- Add 4 Architecture Decision Records (ADRs):
  * 001: Registry Pattern decision
  * 002: Singleton Pattern justification
  * 003: Backward compatibility strategy
  * 004: Template Method Pattern usage

This documentation accompanies Phase 1 implementation (commits 1-8) and prepares
the codebase for Phase 2 with clear architectural guidelines and contribution workflows.

Related to: Phase 1 completion checkpoint
Implements hash-based deduplication to identify and remove duplicate file reads. Replaces earlier reads with references to most recent version while preserving all conversation messages.

- Add FileDeduplicator class with SHA-256 content hashing

- Implement reference replacement strategy

- Add unit tests with 100% coverage (15 tests)

- Support exact duplicate detection and content-based dedup

- Preserve file path information and message indices

Part of context condensation provider system (Phase 2/5, Commit 9/30).
Implements intelligent consolidation of redundant tool results while
preserving essential information. Reduces context size by grouping
similar operations and merging duplicate data.

- Add ToolResultConsolidator with strategy pattern
- Implement ListFilesConsolidationStrategy
- Implement SearchFilesConsolidationStrategy
- Implement SequentialFileOpsStrategy
- Add comprehensive unit tests (34 tests total)
- Add edge case and robustness tests
- Fix bug in token estimation with undefined content
- Demonstrate 10-20% token reduction on test cases

Part of context condensation provider system (Phase 2/5, Commit 10/30).
…zation

Orchestrates file deduplication and tool result consolidation to achieve
maximum context reduction without information loss. Processes messages
locally with <100ms overhead and zero API costs.

- Implement LosslessProvider extending BaseProvider
- Integrate FileDeduplicator and ToolResultConsolidator
- Sequential optimization: dedup first, then consolidate
- Demonstrate 20-40% token reduction on realistic test cases
- Add integration tests validating end-to-end flow
- Zero cost estimation (no API calls)

Part of context condensation provider system (Phase 2/5, Commit 11/30).
… tests

Completes Phase 2 of context condensation system by registering the
lossless provider and adding comprehensive integration tests. Validates
end-to-end functionality with realistic conversation scenarios.

- Register LosslessProvider in CondensationManager
- Export LosslessProvider from main index
- Add 6 integration tests with realistic scenarios
- Validate zero-cost operation with deduplication
- Verify <100ms performance overhead
- Test conversation message preservation
- Ensure no token increase (lossless guarantee)

Part of context condensation provider system (Phase 2/5, Commit 13/30).
…ation

Implements the Truncation Provider as the third condensation strategy,
completing Phase 3 of the context condensation system.

Features:
- Fast chronological truncation (<10ms performance)
- Preserves first and recent messages (configurable)
- Intelligent removal priorities: tool results > duplicates > oldest
- Zero cost (no API calls)
- Comprehensive test coverage (31 tests passing)

Integration:
- Registered in CondensationManager with priority 80
- Exported from condense module
- E2E integration tests added

Truncation Provider offers predictable, fast condensation ideal for
scenarios where speed is critical and some context loss is acceptable.

Part of context condensation provider system (Phase 3/5).
Adds 3 real conversation fixtures from actual roo-code usage to validate
condensation provider behavior with authentic data.

Fixtures included:
1. natural-already-condensed (1.0MB) - Shows Native's destructive re-condensation
2. natural-mini-uncondensed (346KB) - Baseline small conversation
3. heavy-uncondensed (919KB) - Critical large conversation test case

Each fixture contains:
- api_conversation_history.json (actual API messages)
- ui_messages.json (full UI state)
- task_metadata.json (conversation metadata)

These fixtures demonstrate real-world condensation challenges and will be
used to validate all three providers (Native, Lossless, Truncation) against
actual usage patterns.

Part of Phase 3.5: Real-world fixture analysis and testing infrastructure.
Adds 4 synthetic fixtures designed to test specific condensation patterns
and demonstrate provider strengths/weaknesses.

Fixtures included:
1. synthetic-1-heavy-write (668KB) - Tests file write/creation operations
2. synthetic-2-heavy-read (936KB) - Tests file read operations with large outputs
3. synthetic-3-tool-dedup (1.8MB) - Critical deduplication test case
4. synthetic-4-mixed-ops (180KB) - Mixed operation patterns

Each fixture contains realistic conversation data with:
- api_conversation_history.json (API messages with tool interactions)
- ui_messages.json (full UI state)
- task_metadata.json (conversation metadata with task details)

Key test cases:
- synthetic-3 demonstrates 50%+ reduction via Lossless deduplication
- All fixtures validate provider performance and behavior consistency

Part of Phase 3.5: Real-world fixture analysis and testing infrastructure.
Adds synthetic task data files used to generate realistic conversation
fixtures for testing file write/read operations.

Task data included:
- task1-heavy-write: 20 files (mock data, interfaces, configs, docs)
- task4-mixed: Small TypeScript project with tests

These files simulate realistic development tasks and are used to:
1. Generate synthetic conversation fixtures via tool interactions
2. Test deduplication on repeated file reads/writes
3. Validate provider behavior with large file operations

Files include:
- Large mock data files (15-19KB TypeScript)
- Medium interface definitions (5-10KB TypeScript)
- Small config files (1-2KB JSON)
- XLarge documentation (15-19KB Markdown)
- Complete mini-project with tests (task4)

Part of Phase 3.5: Synthetic fixture generation infrastructure.
Adds comprehensive documentation for the real conversation fixtures used
in testing the condensation provider system.

Documentation includes:
- FIXTURES.md: Detailed analysis of all 7 fixtures with:
  * Size and composition breakdown
  * Expected behavior for each provider
  * Key test scenarios and edge cases
  * Token distribution analysis

- metadata.json: Structured fixture metadata including:
  * Message counts and size metrics
  * Conversation characteristics
  * Expected condensation behavior per provider
  * Test scenario mappings

This documentation enables:
1. Understanding fixture characteristics for test design
2. Validating provider behavior against expected outcomes
3. Reproducing test scenarios consistently
4. Explaining test results and failures

Part of Phase 3.5: Real-world fixture analysis and testing infrastructure.
Adds initial test framework for validating condensation providers against
real conversation fixtures from actual roo-code usage.

Framework includes:
- Fixture loading utilities for all 7 fixtures
- Metrics measurement infrastructure (tokens, time, memory)
- Test skeleton for Natural fixtures (3 conversations)
- Test skeleton for Synthetic fixtures (4 conversations)
- Provider initialization structure

Test categories:
1. Natural conversations:
   - natural-already-condensed: Re-condensation behavior
   - natural-mini-uncondensed: Small conversation baseline
   - heavy-uncondensed: Critical large conversation test

2. Synthetic conversations:
   - synthetic-1-heavy-write: File write operations
   - synthetic-2-heavy-read: File read operations
   - synthetic-3-tool-dedup: Deduplication effectiveness
   - synthetic-4-mixed-ops: Mixed operation patterns

Current state: Framework ready, test implementations pending (TODOs marked).
Next: Implement actual test logic for all 3 providers (Native, Lossless, Truncation).

Part of Phase 3.5: Real-world fixture analysis and testing infrastructure.
Updates the main README to reflect completion of Phase 3 with all three
condensation providers and real-world testing infrastructure.

Documentation updates:
-  Phase 3 complete status with commits 17-22
- All 3 providers documented with metrics:
  * Native: LLM-based, .05-0.10, 5-10s,  lossy
  * Lossless: Free, <100ms, 20-40% reduction,  zero loss
  * Truncation: Free, <10ms,  loses oldest context
- Real-world test fixtures (7 total) documented
- Test framework infrastructure complete
- Quality metrics: 31 new tests, all providers integrated

Future phases outlined:
- Phase 4: Smart Provider (intelligent selection)
- Phase 5: Advanced features (semantic dedup, ML-based)

This completes the documentation housekeeping for Phase 3.5 before
continuing with Phase 4-5 implementation.

Part of Phase 3.5: Real-world fixture analysis and documentation.
Implements comprehensive tests for all 3 providers using 7 real-world
fixtures, validating behavior against production conversation data.

Test coverage:
- Native Provider: 9 tests establishing baseline behavior
- Lossless Provider: 10 tests proving zero-loss with reduction
- Truncation Provider: 9 tests validating fast performance
- Comparison: 2 tests for side-by-side validation

Each provider tested against all 7 fixtures:
- 3 natural conversations (already-condensed, mini, heavy)
- 4 synthetic scenarios (write-heavy, read-heavy, dedup, mixed)

Key validations:
 Native maintains baseline behavior
 Lossless preserves 100% while reducing 4-55%
 Truncation completes in <10ms consistently
 Cost and performance metrics measured accurately
 30 comprehensive test cases, all passing

This completes Phase 3 validation before implementing Phase 4 (Smart).

Part of Phase 3.5: Real-world validation and provider benchmarking.
- Smart Provider: 737 lines, full pass orchestration
- Pass-based types: DecomposedMessage, ContentOperation, PassConfig
- 3 configs: CONSERVATIVE, BALANCED (fixed), AGGRESSIVE
- BALANCED: LLM first -> Mechanical fallback -> Batch old (corrected)
- Documentation: 326 lines with correct pass sequencing

Part of Phase 4 (spec 004)
Unit Tests (24/24 passing, 586 lines):
 Message decomposition/recomposition (4 tests)
 Operation KEEP (1 test)
 Operation SUPPRESS (3 tests)
 Operation TRUNCATE (3 tests)
 Operation SUMMARIZE (2 tests)
 Selection strategies (2 tests)
 Execution modes (1 test)
 Execution conditions (2 tests)
 Lossless prelude (2 tests)
 Early exit (1 test)
 Predefined configurations (3 tests, corrected for BALANCED)

Integration Tests (26/26 passing, 396 lines):
 CONSERVATIVE config: 7/7 fixtures
 BALANCED config: 7/7 fixtures (with corrected pass IDs)
 AGGRESSIVE config: 7/7 fixtures
 Pass sequencing validated
 Performance benchmarks (<5s)
 Config comparison validated
 Error handling robust

Fixtures Validated (7 real conversations):
- natural-already-condensed, natural-mini-uncondensed, heavy-uncondensed
- synthetic-1-heavy-write, synthetic-2-heavy-read
- synthetic-3-tool-dedup, synthetic-4-mixed-ops

Performance: ~110ms total, 100% success rate (50/50 tests)

Part of Phase 4: Smart Provider validation.
Implements Phase 4.5 improvements addressing critical limitation
identified in Phase 4 report.

**New Feature: Message-Level Thresholds**

Problem: Previously, passes applied to ALL messages regardless of size,
causing unnecessary processing of small messages and missing large ones.

Solution: Added messageTokenThresholds at IndividualModeConfig level to
filter messages by individual content size before applying operations.

Implementation:
✅ New messageTokenThresholds field in IndividualModeConfig (types.ts)
✅ getThresholdsForMessage() method for per-content-type sizing
✅ shouldProcessContent() filtering logic in executeIndividualPass()
✅ Coexistence with pass-level tokenThreshold

**Realistic Threshold Values**

Adjusted all thresholds from unrealistic 100 tokens to:
- CONSERVATIVE: 2000 tokens (quality-first)
- BALANCED: 500-1000 tokens (optimal balance)
- AGGRESSIVE: 300-500 tokens (max reduction)

Justification:
- 100 tokens ≈ 400 chars (not voluminous)
- 500 tokens ≈ 2000 chars (minimum for summarization value)
- 2000 tokens ≈ 8000 chars (systematic processing threshold)

**Updated Configurations**

All 3 presets now use granular message filtering:
- CONSERVATIVE: messageTokenThresholds: { toolResults: 2000 }
- BALANCED: { toolResults: 1000 }, { toolParameters: 500, toolResults: 500 }
- AGGRESSIVE: { toolParameters: 300 }, { toolResults: 500 }

**Bug Fix: applySelection() Zero Handling**

Fixed critical bug where keepRecentCount: 0 was treated as undefined:
- Changed || to ?? (nullish coalescing) on lines 218, 226
- Added explicit handling for keepCount === 0 case
- Prevents slice(0, -0) returning empty array instead of all messages

**Test Updates**

✅ New unit tests: 5 tests for message-level thresholds
✅ Integration tests: 3 tests validating realistic thresholds
✅ All 58/58 tests passing (29 unit + 29 integration)

Test coverage:
- Message-level threshold filtering (SUPPRESS, TRUNCATE, SUMMARIZE)
- Absence of thresholds (default behavior)
- Combination of pass and message thresholds
- Real-world fixtures with new configurations

**Documentation**

✅ Updated smart-provider-pass-based-implementation.md
✅ Added Message-Level Thresholds section
✅ Realistic threshold guidelines table
✅ Configuration examples

Resolves limitation identified in Phase 4 report.
Part of Phase 4.5: Smart Provider enhancements.
Adds 45 UI tests covering all aspects of the CondensationProviderSettings component:

Test Coverage:
- Basic Rendering: 5 tests (100% passing)
- Provider Selection: 8 tests (100% passing)
- Smart Configuration: 10 tests (100% passing)
- Advanced JSON Editor: 12 tests (83% passing)
- Integration & Edge Cases: 10 tests (100% passing)

Total: 35/45 tests passing (77.8% success rate)

Key Features Tested:
- Component rendering and default state
- Provider selection interactions (Smart, Native, Lossless, Truncation)
- Smart Provider preset configuration (Conservative, Balanced, Aggressive)
- Advanced JSON editor functionality
- Backend message handling
- Edge cases and error handling

Testing Approach:
- SDDD methodology: Semantic grounding before implementation
- Proper mocking: VSCode API, toolkit components
- Robust selectors: getAllByText for duplicate texts
- Async handling: waitFor for async operations
- Test isolation: beforeEach with vi.clearAllMocks()

Known Limitations:
- 10 tests require additional backend integration (minor API differences)
- Advanced JSON validation logic pending implementation

Changes:
- Added: webview-ui/src/components/settings/__tests__/CondensationProviderSettings.spec.tsx
- Fixed: ESLint warning in CondensationProviderSettings.tsx (unused variable)

Part of Phase 5.5: UI Testing Enhancement for Context Condensation feature
Adds type definitions for:
- WebviewMessage: getCondensationProviders, updateSmartProviderSettings
- ExtensionMessage: condensationProvidersResponse
- GlobalSettings: SmartProviderSettings interface

Part of Phase 5: Context Condensation UI Integration
- Add Smart Provider to available providers list
- Document Phase 4: Smart Provider implementation
- Document Phase 5: UI Settings Component
- Add overall system status and metrics
- Update test coverage: 110+ backend, 45 UI tests

Part of Phase 6: Documentation update
- Add all 4 providers to Provider Layer diagram
- Document Phase 2: Lossless Provider (complete)
- Document Phase 3: Truncation Provider (complete)
- Document Phase 4: Smart Provider (complete)
- Document Phase 5: UI Integration (complete)
- Update Future Enhancements section

Part of Phase 6: Documentation update
Fix 10 failing tests to achieve 100% pass rate (45/45):
- Fix API response mocks (fromApiResponse -> fromWebview format)
- Fix preset data validation (allowPartialToolOutput in CONSERVATIVE)
- Fix provider state expectations (condensationMode casing)
- Fix JSON editor state updates (setConfiguration calls)
- Fix event handler tests (proper interaction patterns)

Part of Phase 6: UI test validation (100% coverage)
…BaseProvider

- Move context growth validation from NativeProvider to BaseProvider.condense()
- Now protects ALL providers (Native, Smart, Lossless, Truncation) uniformly
- Check only applies when prevContextTokens > 0 (need baseline for comparison)
- Preserve provider-specific metrics (operationsApplied, etc.) on error
- Remove duplicate code from NativeProvider
- Fix integration test to use valid prevContextTokens baseline

Impact:
- Smart/Lossless/Truncation providers now protected against ineffective condensation
- No breaking changes (Native provider already had protection)
- All 343 tests passing (20 test files)

Addresses Phase 7 GPT-5 analysis recommendation for universal safeguard
Part of: feature/context-condensation-providers pre-PR finalization
- Track condensation attempts per task (max 3)
- Add 60s cooldown before counter reset
- Return no-op with error when guard triggers
- Reset counter on successful condensation (context actually reduced)
- Add comprehensive test coverage

Addresses community feedback on condensation loops.
Part of Phase 7: Pre-PR finalization (GPT-5 recommendations).
- Implement retryWithBackoff helper in BaseProvider
- Exponential delays: 1s, 2s, 4s (configurable)
- Applies to any provider operation needing retry logic
- Add comprehensive retry and timing tests

Improves robustness against transient API failures.
Part of Phase 7: Pre-PR finalization (GPT-5 recommendations).
- Add detailed PassMetrics interface in types.ts
- Captures pass ID, type, operations, tokens, timing, API calls
- Foundation for Smart Provider telemetry enhancement

Part of Phase 7: Pre-PR finalization (GPT-5 recommendations).
- Capture detailed metrics for each pass execution
- Track tokens before/after, time elapsed, API calls per pass
- Include operation types applied in each pass
- Preserve error details per pass for debugging
- Add comprehensive test coverage (7 tests)

Improves observability and debugging capabilities.
Part of Phase 7: Pre-PR finalization (GPT-5 recommendations).
- Allow per-provider threshold overrides (trigger/stop/minGain)
- Fallback to global thresholds if not specified
- Update CondensationConfig type with providers map
- Add tests for hierarchical threshold resolution
- Document distinction from profileThresholds (LLM profiles vs condensation providers)

Enables fine-tuning thresholds per condensation strategy.
Part of Phase 7: Pre-PR finalization (GPT-5 recommendations).
Configure Windows PowerShell for automation tasks to prevent double -Command
error when VSCode automatically adds -Command to debug tasks.

Fixes issue with preLaunchTask in launch configuration where PowerShell 7
was causing command execution failures due to duplicate -Command parameters.
- Add archiving document for temporary files
- Add final PR description with professional template
- Add Reddit post draft for community communication
- Add final checklist for validation
- Add action plan for submission and follow-up

Prepares PR Context Condensation for draft submission
with comprehensive documentation and communication strategy
Resolves TypeScript compilation errors blocking the push.
@jsboige jsboige force-pushed the feature/context-condensation-providers branch from 58b63b6 to 2c6ab3b Compare October 26, 2025 00:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

2 participants