class Compactor
Context compaction service. When the conversation grows past configurable thresholds, older messages are summarized into a condensed form and the original messages are dropped, keeping the context window manageable.
Definitions
def should_compact?(messages, usage: nil)
Check whether compaction should run based on current context state.
Implementation
def should_compact?(messages, usage: nil)
return true if messages.size > @config[:message_threshold]
return true if usage && (usage[:total] || 0) > @config[:token_threshold]
false
end
def compact(messages)
Compact the message history by summarizing older messages.
Returns [summary_message, kept_messages] — the caller rebuilds the context from these.
Implementation
def compact(messages)
total = messages.size
keep_count = [@config[:retention_window], total].min
return nil if total <= keep_count
old_messages = messages[0...(total - keep_count)]
recent_messages = messages[(total - keep_count)..]
summary_text = summarize(old_messages)
[summary_text, recent_messages]
end