Skip to content

Conversation

@ryanwyler
Copy link

@ryanwyler ryanwyler commented Dec 29, 2025

Summary

Adds a new "collapse" compaction mode that preserves recent context while summarizing older messages.

The problem: Standard compaction compresses the entire conversation into a tiny summary, causing massive context loss (100% -> 3%). The AI forgets everything.

The solution: Collapse only compresses the OLD, stale parts while keeping current 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 100% context down to 15-25%, leaving 75% remaining context window while preserving all critical knowledge

Changes

  • packages/opencode/src/session/compaction.ts - Main collapse logic with processCollapse(), token estimation, and markdown conversion
  • packages/opencode/src/id/id.ts - Add createLike() for inserting messages at past timestamps, detectFormat() for backward compatibility
  • packages/opencode/src/session/message-v2.ts - Update filterCompacted() for breakpoint detection
  • packages/opencode/src/config/config.ts - New config options (method, trigger, extractRatio, recentRatio)
  • 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 fix(tui): make auth URLs clickable regardless of line wrapping #6317

Configuration

{
  "compaction": {
    "method": "collapse",
    "trigger": 0.85,
    "extractRatio": 0.65,
    "recentRatio": 0.15
  }
}

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

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

@ryanwyler ryanwyler force-pushed the feature/collapse-compaction branch 2 times, most recently from 4aa734a to 395b76b Compare December 29, 2025 17:50
@ariane-emory
Copy link
Contributor

Not really in favour of changing the defaults, new behaviour ought be opt-in IMO.

@ryanwyler ryanwyler force-pushed the feature/collapse-compaction branch from 395b76b to 41c4df7 Compare December 29, 2025 18:18
Adds a new 'collapse' compaction mode that preserves recent context while
summarizing older messages. Unlike standard compaction which summarizes the
entire conversation, collapse extracts the oldest 65% of tokens, summarizes
them at a breakpoint, and leaves the newest 35% untouched.

Changes:
- Add processCollapse() for breakpoint-based summarization
- Add Identifier.createLike() for inserting messages at past timestamps
- Add detectFormat() for backward compatibility with existing sessions
- Add configurable ratios: trigger (0.85), extractRatio (0.65), recentRatio (0.15)
- Add TUI toggle for switching between standard/collapse modes
- Fix isOverflow() to include cache.write tokens
- Fix link.tsx underline prop error from sst#6317

Default remains 'standard' - users can opt-in to collapse via config or TUI toggle.
@ryanwyler ryanwyler force-pushed the feature/collapse-compaction branch from 41c4df7 to e51bdba Compare December 29, 2025 18:47
@ryanwyler
Copy link
Author

Not really in favour of changing the defaults, new behaviour ought be opt-in IMO.

Fixed it. Thank you for the feedback.

@ryanwyler ryanwyler changed the title feat: add collapse compaction mode feat: collapse compaction keeps the recent context perfect collapsing only the stale context keeping agent intelligent after compaction Dec 29, 2025
@ryanwyler ryanwyler changed the title feat: collapse compaction keeps the recent context perfect collapsing only the stale context keeping agent intelligent after compaction feat: Collapse Compaction maintains agent intelligence after compaction by preserving recent interactions and curating knowledge collapsing stale context Dec 29, 2025
@ryanwyler ryanwyler changed the title feat: Collapse Compaction maintains agent intelligence after compaction by preserving recent interactions and curating knowledge collapsing stale context feat: Collapse Compaction maintains agent intelligence beyond compaction by preserving recent interactions and curating knowledge collapsing the older stale messages Dec 29, 2025
@ryanwyler
Copy link
Author

Before Compaction: 170,424 Tokens
After Compaction: 67,289 Tokens

60% Reduction in token consumption and 100% MEMORY RETENTION

before-collapse-compact

After collapse compact, AI Agent able to produce pages and pages of details about the project on many topics without the use of any tools and no reading of documents.

after-collapse-screen1 after-collapse-screen2 after-collapse-screen3 after-collapse-screen4 after-collapse-screen5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants