Skip to main content

Module compact

Module compact 

Source
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

  1. Trigger: user types /compact, or auto-compact fires at ~80% context usage
  2. Summarization: a cheap model (Standard tier) generates a summary of old messages
  3. Replacement: old messages are archived in the DB, replaced by the summary
  4. 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§

CompactResult
Result of a successful compaction.

Enums§

CompactSkip
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 LlmProvider directly.
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.