Expand description
Context compaction — summarise old messages to reclaim token budget. Session compaction — summarize old messages to reclaim context.
When the conversation grows long, compaction replaces older messages with a concise summary, freeing context window space for new work.
§How it works
- Trigger: user types
/compact, or auto-compact fires at ~80% context usage - Summarization: a cheap model (Standard tier) generates a summary of old messages
- Replacement: old messages are archived in the DB, replaced by the summary
- Result: context usage drops, conversation continues with full history awareness
§Auto-compaction
When context usage exceeds the threshold (configurable, default ~80%), compaction runs automatically before the next inference call. The user sees a brief “⚡ Compacting…” indicator.
§What’s preserved
- Summary of all prior conversation and decisions
- Progress tracking entries (survive compaction via DB metadata)
- Memory facts (injected from
MEMORY.md, not from conversation) - File ownership state (tracked in SQLite, not in messages)
Pure logic, zero UI dependencies. Returns structured results for the caller (TUI or headless) to render however it likes.
Compaction uses a cheap model (Standard tier) when available, falling back to the main model. Summarization is a simple task that doesn’t need frontier-class reasoning.
Structs§
- Compact
Result - Result of a successful compaction.
Enums§
- Compact
Skip - Why compaction was skipped (not an error, just a precondition).
Constants§
- COMPACT_
PRESERVE_ COUNT - Minimum number of recent messages to keep verbatim during compaction.
Functions§
- compact_
session - Attempt to compact a session.
- compact_
session_ with_ provider - Core compaction logic — accepts
&dyn LlmProviderdirectly. - is_
compact_ circuit_ broken - Check if the circuit breaker is tripped.
- record_
compact_ failure - Record a compaction failure. Returns true if the circuit breaker just tripped.
- reset_
compact_ failures - Reset the failure counter (call after successful compaction or new session).
- strip_
analysis_ block - Strip the
<analysis>scratchpad block from the summary.