Class: Brute::Compaction::Middleware::Strategy

Inherits:
Middleware::Base show all
Defined in:
lib/brute/compaction/middleware/strategy.rb

Overview

One strategy in the ladder.

The stack order is the policy. A layer that got the conversation under target never calls the next one, so the strategies that cost nothing sit at the top and the one that spends a model call is only reached when they could not get there.

A subclass answers #strategy and #rewrite, and nothing else. The rules every strategy has to keep are here: it is only asked while the conversation is over target, and what it answers is only taken when it actually made the conversation smaller -- otherwise the pipeline above would write the same size back on every step, forever.

The same class serves as a layer or as the terminal, which is what the ladder is built out of: the strategies that cost nothing are used, and the one that spends a model call is run, reached only when they could not get under target.

Brute::Turn::CompactionPipeline.new do
use Brute::Compaction::Middleware::ToolResults
use Brute::Compaction::Middleware::SlidingWindow
run Brute::Compaction::Middleware::Summary.new(summarize: summarize)
end

Direct Known Subclasses

SlidingWindow, ToolResults

Constant Summary

Constants included from Hooks

Hooks::AFTER_LLM_EVENT, Hooks::AFTER_TOOL_EVENT, Hooks::APPROVE_TOOL_EVENT, Hooks::BEFORE_LLM_EVENT, Hooks::BEFORE_TOOL_EVENT, Hooks::COMPACTED_EVENT, Hooks::COMPACT_DURATION_EVENT, Hooks::DURATION_EVENT, Hooks::ENTER_EVENT, Hooks::EXIT_EVENT, Hooks::FARADAY_ERROR_EVENT, Hooks::LLM_DURATION_EVENT, Hooks::LLM_FAILURE_EVENT, Hooks::MIDDLEWARE_ADDED_EVENT, Hooks::OPEN_ROUTER_SERVER_ERROR_EVENT, Hooks::STANDARD_ERROR_EVENT, Hooks::TOOL_DURATION_EVENT, Hooks::TURN_DURATION_EVENT, Hooks::TURN_END_EVENT, Hooks::TURN_START_EVENT

Instance Method Summary collapse

Methods inherited from Middleware::Base

#initialize

Methods included from Hooks

new

Constructor Details

This class inherits a constructor from Brute::Middleware::Base

Instance Method Details

#call(env) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/brute/compaction/middleware/strategy.rb', line 35

def call(env)
  @env = env

  if Brute::Compaction.over_target?(env)
    apply(env)
  end

  # Under target now, so the rest of the ladder is not needed.
  if Brute::Compaction.over_target?(env)
    @app.call(env)
  end

  env
end

#rewrite(_conversation, target:) ⇒ Object

Answer a smaller conversation, or nil to decline. Build a new list: the one handed over belongs to the caller.

Raises:

  • (NotImplementedError)


55
# File 'lib/brute/compaction/middleware/strategy.rb', line 55

def rewrite(_conversation, target:) = raise(NotImplementedError, "#{self.class} must answer #rewrite")

#strategyObject

The name this strategy compacts under, recorded on what it produces.

Raises:

  • (NotImplementedError)


51
# File 'lib/brute/compaction/middleware/strategy.rb', line 51

def strategy = raise(NotImplementedError, "#{self.class} must answer #strategy")