Skip to content

Conversation

@ryanwyler
Copy link

Summary

Adds a new "collapse" compaction mode that preserves recent context while summarizing older messages, with intelligent merging of historical summaries across multiple compaction cycles.

The problem: Standard compaction compresses the entire conversation into a tiny summary, causing massive context loss (100% -> 3%). The AI forgets everything. Additionally, when multiple compactions occur, each new summary only knows about messages since the last compaction - historical context from earlier summaries is permanently lost.

The solution: Collapse compaction addresses both problems:

  1. Selective compression - Only compresses OLD, stale messages while keeping recent work pristine:

    • Extracts oldest 65% of tokens and summarizes them at a breakpoint
    • Uses newest 15% as reference context for the summary LLM
    • Leaves 35% of recent conversation completely untouched
    • Result: Compresses to 15-25% usage, leaving 75% context window available
  2. Historical summary merging - When generating a new summary, includes up to N previous compaction summaries in the prompt, instructing the LLM to merge and consolidate all historical context. This creates a "rolling summary" that carries forward critical knowledge across unlimited compaction cycles:

    • Working code blocks preserved verbatim
    • User directives and preferences maintained
    • Design decisions and lessons learned accumulated
    • No information loss regardless of session length

Changes (v1.1 - Modular Rebase)

This version refactors the collapse logic into a self-contained module for easier future rebasing:

File Lines Purpose
compaction-collapse.ts +530 New - All collapse logic isolated here
compaction.ts +20 Minimal router to collapse module
config.ts +36 Config schema options
id/id.ts +63 insert() function for breakpoint IDs
message-v2.ts +25 Logging in filterCompacted
index.tsx +13 TUI toggle command
sidebar.tsx +6 Show compaction method in sidebar
types.gen.ts +24 SDK types (auto-generated)

Key architectural decision: Bulk of feature (~530 lines) is in compaction-collapse.ts with minimal touchpoints in existing files. This makes future rebases much easier - just bolt the module back on.

Configuration

{
  "compaction": {
    "method": "collapse",
    "trigger": 0.85,
    "extractRatio": 0.65,
    "recentRatio": 0.15,
    "summaryMaxTokens": 10000,
    "previousSummaries": 3
  }
}
Option Default Description
method "standard" Compaction method: "standard" or "collapse"
trigger 0.85 Trigger compaction at this fraction of context
extractRatio 0.65 Fraction of oldest tokens to extract and summarize
recentRatio 0.15 Fraction of newest tokens to use as reference
summaryMaxTokens 10000 Target token count for summary output
previousSummaries 3 Number of previous summaries to include for merging

Default method remains "standard" - users can opt-in to collapse via config or TUI toggle (Ctrl+P -> "Use collapse compaction").

Related

Testing

  1. Set compaction.method to "collapse" in config or toggle via TUI
  2. Have a long conversation that triggers context overflow
  3. Verify collapse message appears at breakpoint position
  4. Verify recent messages remain untouched
  5. Verify no infinite compaction loops
  6. Fork a compacted session and verify it works correctly
  7. Trigger multiple compactions and verify previous summaries are merged into new summary

@ryanwyler ryanwyler force-pushed the feature/collapse-compaction-1.1 branch from 081bb72 to 209abc8 Compare January 12, 2026 01:27
@gignit gignit deleted a comment from github-actions bot Jan 12, 2026
@ryanwyler ryanwyler force-pushed the feature/collapse-compaction-1.1 branch 2 times, most recently from 481303a to 9a3d5bb Compare January 12, 2026 01:38
@ryanwyler ryanwyler force-pushed the feature/collapse-compaction-1.1 branch from 9a3d5bb to b3e33bb Compare January 17, 2026 20:08
@ryanwyler ryanwyler force-pushed the feature/collapse-compaction-1.1 branch from 5ddbfd5 to 18bef8c Compare January 17, 2026 21:05
Adds a new 'collapse' compaction method that preserves recent context while
summarizing older messages, with intelligent merging of historical summaries.

- Selective compression: only compresses oldest 65% of tokens
- Historical summary merging: includes previous summaries for context continuity
- Breakpoint insertion: places summary at correct position in timeline
- TUI toggle: switch between standard/collapse via command palette

New file compaction-collapse.ts contains all collapse logic for easy rebasing.

Config options:
- compaction.method: 'standard' | 'collapse'
- compaction.trigger: overflow threshold (default 0.85)
- compaction.extractRatio: fraction to extract (default 0.65)
- compaction.recentRatio: recent context reference (default 0.15)
- compaction.summaryMaxTokens: target summary size (default 10000)
- compaction.previousSummaries: history to merge (default 3)
Adds insertTriggers config option to allow compaction methods to control
whether they create trigger messages. Standard compaction needs triggers
(default: true), but collapse compaction creates its own breakpoint
messages and should skip triggers (default: false).

This fixes the infinite loop bug where collapse compaction would:
1. Insert breakpoint at timestamp T
2. Create trigger at same timestamp T
3. filterCompacted() would stop at breakpoint
4. Exclude messages after breakpoint with updated token counts
5. Loop would see old token count and trigger again

With this fix, collapse compaction directly calls process() without
creating triggers, preventing timestamp collisions and ensuring the
loop sees the correct post-compaction token counts.

Resolves: Infinite compaction loop in ses_43534dcd4ffedAJydNtU3YLQ9S
@ryanwyler ryanwyler force-pushed the feature/collapse-compaction-1.1 branch 2 times, most recently from 6a3d42a to 7a421ce Compare January 17, 2026 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.