Class: Brute::Middleware::CompactionCheck

Inherits:
Base
  • Object
show all
Defined in:
lib/brute/middleware/compaction_check.rb

Overview

Checks context size after each LLM call and triggers compaction when thresholds are exceeded.

Runs POST-call: inspects message count and token usage from the response. If compaction is needed, summarizes older messages and rebuilds the context with the summary + recent messages.

Instance Method Summary collapse

Constructor Details

#initialize(app, compactor:, system_prompt:, tools:, stream: nil) ⇒ CompactionCheck

Returns a new instance of CompactionCheck.



18
19
20
21
22
23
24
# File 'lib/brute/middleware/compaction_check.rb', line 18

def initialize(app, compactor:, system_prompt:, tools:, stream: nil)
  super(app)
  @compactor = compactor
  @system_prompt = system_prompt
  @tools = tools
  @stream = stream
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/brute/middleware/compaction_check.rb', line 26

def call(env)
  response = @app.call(env)

  ctx = env[:context]
  messages = ctx.messages.to_a.compact
  usage = ctx.usage rescue nil

  if @compactor.should_compact?(messages, usage: usage)
    result = @compactor.compact(messages)
    if result
      summary_text, _recent = result
      rebuild_context!(env, summary_text)
      env[:metadata][:compaction] = {
        messages_before: messages.size,
        timestamp: Time.now.iso8601,
      }
    end
  end

  response
end