Class: Brute::Compactor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(provider, **opts) ⇒ Compactor

Returns a new instance of Compactor.



20
21
22
23
# File 'lib/brute/compactor.rb', line 20

def initialize(provider, **opts)
  @provider = provider
  @config = DEFAULTS.merge(opts)
end

Instance Attribute Details

#configObject (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(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

#should_compact?(messages, usage: nil) ⇒ Boolean

Check whether compaction should run based on current context state.

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/brute/compactor.rb', line 26

def should_compact?(messages, usage: nil)
  return true if messages.size > @config[:message_threshold]
  return true if usage && (usage.total_tokens || 0) > @config[:token_threshold]
  false
end