feat: Collapse Compaction maintains agent intelligence beyond compaction by preserving recent interactions and curating knowledge collapsing the older stale messages
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:
-
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
-
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
Dependencies
This PR includes the fork fix from #6445 (broken forked sessions with compactions due to missing parent-child message references). If #6445 is merged first, this PR will be rebased to remove the duplicate commit.
Changes
-
packages/opencode/src/session/compaction.ts- Main collapse logic withprocessCollapse(), token estimation, markdown conversion, and previous summary merging -
packages/opencode/src/session/index.ts- Fork fix: preserve parentID references when forking sessions -
packages/opencode/src/id/id.ts- AddcreateLike()for inserting messages at past timestamps,detectFormat()for backward compatibility -
packages/opencode/src/session/message-v2.ts- UpdatefilterCompacted()for breakpoint detection -
packages/opencode/src/config/config.ts- New config options (method, trigger, extractRatio, recentRatio, summaryMaxTokens, previousSummaries) -
packages/opencode/src/cli/cmd/tui/routes/session/- TUI toggle and sidebar display -
packages/opencode/src/server/server.ts- Unified flow for both modes -
packages/opencode/src/cli/cmd/tui/ui/link.tsx- Fix underline prop error from #6317
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").
Testing
- Set
compaction.methodto"collapse"in config or toggle via TUI - Have a long conversation that triggers context overflow
- Verify collapse message appears at breakpoint position
- Verify recent messages remain untouched
- Verify no infinite compaction loops
- Fork a compacted session and verify it works correctly
- Trigger multiple compactions and verify previous summaries are merged into new summary