Class: Brute::Turn::CompactionPipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/brute/turn/compaction_pipeline.rb

Overview

A compactor built out of middleware.

Like ToolPipeline it composes a Pipeline rather than inheriting: the definition block is instance_eval'd into the internal Pipeline, so use / run inside it are the builder's methods.

The stack order is the policy. A layer that got the conversation under target does not call the next one, so the strategies that cost nothing sit at the top and the terminal app -- the summariser, the only thing here that spends money -- is reached only when they could not get there. That is run meaning what it means everywhere else in Brute: the model call, at the bottom, with the layers deciding what reaches it.

Brute::Turn::CompactionPipeline.new do
use Brute::Compaction::Middleware::ToolResults,   keep_steps: 1
use Brute::Compaction::Middleware::SlidingWindow, keep_steps: 2

run Brute::Compaction::Summarize.new(
  Brute::Completion::OpenRouter.new(config: { access_token: key }),
)
end

A pipeline that must never spend a call is the first two layers and run ->(env) { env } -- a policy said out loud rather than configured.

The conversation being compacted rides on env[:conversation], leaving env[:messages] to mean what it means to every other terminal app in Brute: the prompt going down, the reply coming back.

It answers #compact, so what comes out drops into Brute::Middleware::DefaultCompactionPipeline as that turn's compactor.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CompactionPipeline

Returns a new instance of CompactionPipeline.



41
42
43
44
# File 'lib/brute/turn/compaction_pipeline.rb', line 41

def initialize(&block)
  @pipeline = Pipeline.new
  @pipeline.instance_eval(&block) if block
end

Instance Method Details

#compact(messages, target:, events: Pipeline::NullSink.new, token_counter: nil) ⇒ Object

Answer a smaller conversation, or nil when nothing was given up.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/brute/turn/compaction_pipeline.rb', line 47

def compact(messages, target:, events: Pipeline::NullSink.new, token_counter: nil)
  env = {
    conversation:  messages,
    target:        target,
    token_counter: token_counter,
    applied:       [],
    messages:      Brute.log,
    events:        events,
    metadata:      {},
  }
  @pipeline.call(env)

  unless env[:applied].empty?
    env[:conversation]
  end
end