Class: Brute::Compactor
- Inherits:
-
Object
- Object
- Brute::Compactor
- Defined in:
- lib/brute/compactor.rb
Overview
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.
Modeled after forgecode’s Compactor which uses an eviction window and retention window strategy.
Constant Summary collapse
- DEFAULTS =
{ token_threshold: 100_000, # Compact when estimated tokens exceed this message_threshold: 200, # Compact when message count exceeds this retention_window: 6, # Minimum recent messages to always keep summary_model: nil, # Model for summarization (uses agent's model if nil) }.freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#compact(messages) ⇒ Object
Compact the message history by summarizing older messages.
-
#initialize(provider, **opts) ⇒ Compactor
constructor
A new instance of Compactor.
-
#should_compact?(messages, usage: nil) ⇒ Boolean
Check whether compaction should run based on current context state.
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
18 19 20 |
# File 'lib/brute/compactor.rb', line 18 def config @config end |
Instance Method Details
#compact(messages) ⇒ Object
Compact the message history by summarizing older messages.
Returns [summary_message, kept_messages] — the caller rebuilds the context from these.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/brute/compactor.rb', line 36 def compact() total = .size keep_count = [@config[:retention_window], total].min return nil if total <= keep_count = [0...(total - keep_count)] = [(total - keep_count)..] summary_text = summarize() [summary_text, ] end |
#should_compact?(messages, usage: nil) ⇒ Boolean
Check whether compaction should run based on current context state.
26 27 28 29 30 |
# File 'lib/brute/compactor.rb', line 26 def should_compact?(, usage: nil) return true if .size > @config[:message_threshold] return true if usage && (usage.total_tokens || 0) > @config[:token_threshold] false end |