Skip to content

Feature Request: Add "/compact" Command for Context Compression #13

@will-lamerton

Description

@will-lamerton

Description

Add a new /compact command that compresses the current message history into a smaller, more efficient representation while preserving essential information and context continuity.

Use Case

When working on long conversations or complex projects, message history can consume significant context space, leading to:

  • Context limit pressure: Approaching model token limits
  • Performance degradation: Slower responses due to large context
  • Loss of recent context: Important information gets truncated
  • Inefficient token usage: Redundant or verbose information taking up space

The /compact command addresses these issues by:

  • Preserving key information: Maintaining critical decisions, code changes, and context
  • Reducing redundancy: Eliminating repetitive exchanges and verbose explanations
  • Maintaining continuity: Keeping enough context for coherent conversation flow
  • Optimizing token usage: Maximizing available space for new interactions

This is especially valuable for users working on large codebases, long debugging sessions, or extended feature development.

Proposed Solution

Implement a /compact command that processes message history and creates a condensed version:

1. Message Analysis

Analyze conversation history to identify:

  • Key decisions and outcomes: Important conclusions reached
  • Code changes: Files modified, functions added/changed
  • Active context: Current working files, ongoing tasks
  • User preferences: Established patterns and requirements
  • Error resolutions: Successfully resolved issues

2. Compression Strategy

Apply intelligent compression techniques:

  • Summarize repetitive exchanges: Convert back-and-forth debugging into concise summaries
  • Consolidate file operations: Group multiple edits to the same file
  • Extract key insights: Preserve important discoveries and decisions
  • Maintain recent context: Keep last few messages at full detail
  • Preserve tool results: Retain essential command outputs and file contents

3. Output Format

🗜️ Context Compacted: 2,845 tokens → 1,230 tokens (57% reduction)

Preserved:
• 3 key decisions and outcomes
• 8 file modifications across 5 files
• Current task: implementing user authentication
• Last 2 full message exchanges
• 4 important tool results

Previous conversation summary available with /compact --show

4. Compression Modes

  • Default: Balanced compression maintaining good context
  • Aggressive (--aggressive): Maximum compression for tight limits
  • Conservative (--conservative): Minimal compression, safer approach
  • Preview (--preview): Show what would be compressed without applying

Technical Implementation

Command Structure

  • Location: src/core/commands/compact.ts
  • Integration: Add to existing command registry
  • Usage: /compact [--aggressive|--conservative|--preview|--show]

Message Processing Pipeline

  1. Message Classification: Categorize messages by importance and type
  2. Content Analysis: Identify key information using pattern matching
  3. Redundancy Detection: Find repetitive or verbose content
  4. Summarization Logic: Create concise summaries of lengthy exchanges
  5. Context Preservation: Ensure critical context remains intact

Compression Techniques

  • Template-based summaries: Use structured formats for common patterns
  • Code diff compression: Represent file changes more efficiently
  • Tool result filtering: Keep only essential command outputs
  • Conversation threading: Group related message chains
  • Smart truncation: Remove verbose explanations while keeping conclusions

Safety Measures

  • Backup original: Store original context in case of reversion needed
  • Validation checks: Ensure compressed context maintains coherence
  • User confirmation: Optional preview before applying compression
  • Undo capability: Allow restoration of original context

Integration Points

  • Message history access: Interface with ChatSession message storage
  • Token counting: Use existing tokenization for before/after metrics
  • Context management: Update session context efficiently
  • Tool integration: Handle tool call summaries appropriately

Acceptance Criteria

  • /compact command appears in help menu
  • Compresses message history while preserving key information
  • Shows before/after token count and reduction percentage
  • Provides different compression modes (default, aggressive, conservative)
  • Includes preview mode to show changes before applying
  • Maintains conversation continuity and context flow
  • Preserves recent messages at full detail
  • Handles tool results and code changes appropriately
  • Includes safety measures (backup, validation, confirmation)
  • Shows summary of what was preserved/compressed
  • Optional display of compressed content with --show

Additional Context

This feature would be particularly valuable for:

  • Long development sessions: Extended coding conversations
  • Complex debugging: Multi-step problem-solving processes
  • Code review workflows: Lengthy back-and-forth discussions
  • Feature development: Extended implementation conversations
  • Local model users: Working with smaller context windows

The compression should be intelligent enough to maintain the essential context needed for the AI to continue providing relevant assistance while significantly reducing token consumption.

Technical Considerations

  • Compression quality: Balance between size reduction and information preservation
  • Context coherence: Ensure compressed context doesn't break conversation flow
  • Performance: Compression process should be fast and efficient
  • Reversibility: Consider ability to expand or restore original context
  • Model compatibility: Work across different AI providers and models

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Documentation update

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions