Class: Brute::Middleware::MaxIterations

Inherits:
Object
  • Object
show all
Defined in:
lib/brute/middleware/010_max_iterations.rb

Overview

Guards against runaway tool loops by capping the number of iterations.

When the limit is reached, injects a user message into the session stating that maximum iterations have been reached. This causes ToolResultLoop to exit its loop naturally (last message is not :tool).

Constant Summary collapse

DEFAULT_MAX_ITERATIONS =
100

Instance Method Summary collapse

Constructor Details

#initialize(app, max_iterations: DEFAULT_MAX_ITERATIONS) ⇒ MaxIterations

Returns a new instance of MaxIterations.



18
19
20
21
# File 'lib/brute/middleware/010_max_iterations.rb', line 18

def initialize(app, max_iterations: DEFAULT_MAX_ITERATIONS)
  @app = app
  @max_iterations = max_iterations
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/brute/middleware/010_max_iterations.rb', line 23

def call(env)
  if max_iterations_reached?(env)
    env[:messages] << RubyLLM::Message.new(
      role: :user,
      content: "Maximum iterations reached.",
    )
  else
    @app.call(env)
  end
end