All three layers (Truncation, Pruning, Compaction) have been fully mapped and documented.
Comprehensive architecture document (18KB)
Contains:
- Complete overview of all three compression layers
- Detailed code analysis for each layer
- File locations and class descriptions
- Method signatures and parameters
- Data classes and result structures
- Cross-layer integration patterns
- Message metadata tracking system
- Token estimation formulas
- Configuration patterns
- Logging points mapped by file and line
- Differences between expand vs core_v2 implementations
- Test file locations
Best for: Understanding the complete architecture
Quick reference guide (11KB)
Contains:
- One-page summary per layer
- Configuration parameters cheat sheet
- Logging quick map
- Message type classification
- File storage strategies
- Integration points
- Token estimation quick formula
- Typical flow example
- Debugging tips
- Common issues & solutions
- Key takeaways
Best for: Quick reference during implementation
File organization and API reference (14KB)
Contains:
- Complete file organization by layer
- Key classes & methods for each layer
- Data flow diagram
- Detailed logging locations with line numbers
- Configuration hierarchy
- Testing information
- Four integration paths (ReActMaster, Core v2, Unified, Hierarchical)
- Navigation commands
- Summary statistics
Best for: Implementation and code navigation
- Read COMPRESSION_LAYERS_MAPPING.md sections:
- "Overview"
- "Layer 1/2/3: Implementation Files"
- "Cross-Layer Integration"
- Use COMPRESSION_LAYERS_QUICK_REFERENCE.md section "Layer Locations"
- Or use COMPRESSION_LAYERS_FILE_INVENTORY.md sections:
- "File Organization by Layer"
- "Key Classes & Methods"
- Reference COMPRESSION_LAYERS_FILE_INVENTORY.md:
- "Integration Paths" (4 different approaches)
- "Logging Locations" (exact file:line pairs)
- Use COMPRESSION_LAYERS_QUICK_REFERENCE.md:
- "Debugging Tips"
- "Common Issues"
expand:
packages/derisk-core/src/derisk/agent/expand/react_master_agent/truncation.py
core_v2:
packages/derisk-core/src/derisk/agent/core_v2/builtin_agents/react_components/output_truncator.py
expand:
packages/derisk-core/src/derisk/agent/expand/react_master_agent/prune.py
core_v2:
packages/derisk-core/src/derisk/agent/core_v2/builtin_agents/react_components/history_pruner.py
expand:
packages/derisk-core/src/derisk/agent/expand/react_master_agent/session_compaction.py
core_v2:
packages/derisk-core/src/derisk/agent/core_v2/builtin_agents/react_components/context_compactor.py
shared:
packages/derisk-core/src/derisk/agent/shared/hierarchical_context/hierarchical_compactor.py
unified:
packages/derisk-core/src/derisk/agent/core/memory/compaction_pipeline.py
┌─────────────────────────────────────────────────────────┐
│ Layer 1: Truncation (Immediate) │
│ - Truncates single large tool output │
│ - Saves full content to AgentFileSystem │
│ - Default: 50 lines / 5KB (expand) or 2000/50KB (v2) │
│ - Triggers: When single output exceeds limit │
└─────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Layer 2: Pruning (Periodic) │
│ - Marks old tool outputs with placeholder │
│ - Preserves context metadata │
│ - Default: 4000 tokens threshold │
│ - Triggers: Every 5 rounds or when tokens accumulate │
└─────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ Layer 3: Compaction (On Demand) │
│ - Summarizes old messages using LLM │
│ - Archives compressed chapters │
│ - Default: 80% of 128K token window = 102.4K │
│ - Triggers: When context exceeds threshold │
└─────────────────────────────────────────────────────────┘
Truncation adds:
- file_key (AFS identifier for full content)
- suggestion (hint for agent how to access full content)
Pruning adds:
- context["compacted"] = True
- context["compacted_at"] = timestamp
- context["original_summary"] = brief excerpt
Compaction adds:
- context["is_compaction_summary"] = True
- context["compacted_roles"] = [list of compressed roles]
- context["compaction_timestamp"] = timestamp
Tokens ≈ len(text_in_characters) / 4
Triggers:
- Prune: cumulative tokens > 4000
- Compact: total tokens > 102400 (80% of 128K)
| Metric | Count |
|---|---|
| Total compression-related files | 20+ |
| Lines of code | 3000+ |
| Distinct log points | 20+ |
| Configuration parameters | 30+ |
| Message metadata flags | 10+ |
| Supported integrations | 4+ |
grep -r "class Truncator" packages/derisk-core/src/
grep -r "truncate" packages/derisk-core/src/derisk/agent/expand/react_master_agent/grep -r "class.*Pruner" packages/derisk-core/src/
grep -r "compacted" packages/derisk-core/src/derisk/agent/expand/react_master_agent/grep -r "class.*Compaction" packages/derisk-core/src/
grep -r "is_overflow" packages/derisk-core/src/grep -r "logger.info" packages/derisk-core/src/derisk/agent/ | grep -E "(Truncat|Prun|Compact)"
grep -r "\[AFS\]" packages/derisk-core/src/
grep -r "\[Truncator\]" packages/derisk-core/src/
grep -r "\[Pruner\]" packages/derisk-core/src/
grep -r "\[Compactor\]" packages/derisk-core/src/- COMPRESSION_LAYERS_MAPPING.md - Architecture overview
- COMPRESSION_LAYERS_QUICK_REFERENCE.md - Layer summaries
- COMPRESSION_LAYERS_FILE_INVENTORY.md - File locations
- COMPRESSION_LAYERS_QUICK_REFERENCE.md - Config parameters
- Source code files for exact implementation
- ReActMasterAgent - All-in-one solution
- Core v2 Components - Pick and choose
- Unified Pipeline - v1 + v2 compatibility
- Hierarchical Compaction - Advanced chapter-based
- COMPRESSION_LAYERS_QUICK_REFERENCE.md - Debugging tips
- Logging statements in source code
- Test files for reference implementations
- ✅ Main implementation (expand/truncation.py)
- ✅ Simplified version (core_v2/output_truncator.py)
- ✅ AgentFileSystem integration
- ✅ Legacy fallback mode
- ✅ Async/sync versions
- ✅ Logging points
- ✅ Configuration options
- ✅ Main implementation (expand/prune.py)
- ✅ Simplified version (core_v2/history_pruner.py)
- ✅ Message classification
- ✅ Token-based strategy
- ✅ Metadata preservation
- ✅ Logging points
- ✅ Configuration options
- ✅ Session compaction (expand/session_compaction.py)
- ✅ Context compaction (core_v2/context_compactor.py)
- ✅ Hierarchical compaction (shared/hierarchical_compactor.py)
- ✅ Unified pipeline (core/memory/compaction_pipeline.py)
- ✅ LLM-based summarization
- ✅ Logging points
- ✅ Configuration options
- ✅ Archive system
- ✅ Message adapters (v1/v2 compatibility)
- ✅ History archival system
- ✅ Token estimation
- ✅ Content protection mechanisms
- ✅ Recovery tools
- ✅ Test file locations
- ✅ Integration paths
- ✅ Configuration hierarchy
- ✅ Data flow diagrams
The documentation is complete. Ready for:
- Logging Instrumentation - Add detailed logging to each layer
- Monitoring Dashboard - Track compression metrics
- Performance Benchmarking - Measure token savings
- Integration Testing - Validate long conversation flows
- Recovery Tools - Add debugging/recovery utilities
- Documentation Generation - Auto-generate from docstrings
- Architecture Overview → QUICK_REFERENCE.md "Three-Layer Architecture"
- File Locations → FILE_INVENTORY.md "File Organization by Layer"
- Configuration → QUICK_REFERENCE.md "Configuration Parameters"
- Logging → FILE_INVENTORY.md "Logging Locations"
- Layer Details → MAPPING.md "Layer 1/2/3: Core Implementation Files"
- Integration → FILE_INVENTORY.md "Integration Paths"
- Configuration → MAPPING.md "Configuration Patterns"
- Complete Code → Source files in packages/derisk-core/src/
- Testing → packages/derisk-core/tests/
- Architecture → MAPPING.md "Cross-Layer Integration"
Last updated: 2025-03-04
Documents cover:
- All production code in packages/derisk-core/src/
- All test files in packages/derisk-core/tests/
- All documentation in docs/
If you find outdated information:
- Update the source code
- Update relevant documentation file
- Cross-reference between documents