Class: Brute::Middleware::DoomLoopDetection

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

Overview

Detects when the agent is stuck repeating tool call patterns and injects a corrective warning into the context before the next LLM call.

Runs PRE-call: inspects the conversation history for repeating tool call patterns. If detected, talks a warning message into the context so the LLM sees it as input alongside the normal tool results.

Instance Method Summary collapse

Constructor Details

#initialize(app, threshold: 3) ⇒ DoomLoopDetection

Returns a new instance of DoomLoopDetection.



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

def initialize(app, threshold: 3)
  super(app)
  @detector = Brute::DoomLoopDetector.new(threshold: threshold)
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/brute/middleware/doom_loop_detection.rb', line 23

def call(env)
  ctx = env[:context]
  messages = ctx.messages.to_a

  if (reps = @detector.detect(messages))
    warning = @detector.warning_message(reps)
    # Inject the warning as a user message so the LLM sees it
    ctx.talk(warning)
    env[:metadata][:doom_loop_detected] = reps
  end

  @app.call(env)
end