Monitors token usage and dispatches the appropriate compaction level. Includes a circuit breaker that silences compaction after 3 consecutive failures to prevent infinite compaction loops.
Methods
CompactionController$maybe_compact()
Check token usage and compact if needed.
Usage
CompactionController$maybe_compact(
chat,
model_limit = 200000L,
compact_model = .HAIKU_MODEL
)Arguments
chatAn
ellmer::Chatobject.model_limitInteger. Model context window token limit.
compact_modelCharacter. Model for compaction tasks (haiku).
CompactionController$compact_now()
Run the two-level compaction now (snip -> session-memory ->
full 9-section), guarded by the circuit breaker. Unlike
maybe_compact() this skips the token-threshold check, so callers that
have already decided to compact (e.g. mid-loop) can reuse the exact
same Claude Code-aligned flow.
Arguments
chatAn
ellmer::Chatobject.compact_modelCharacter. Model for compaction tasks (haiku).
CompactionController$handle_ptl_error()
Handle a prompt-too-long (PTL) error by dropping turns.
Arguments
chatAn
ellmer::Chatobject.errorAn error condition or message string (parsed for a real context limit when present).