Class: Brute::DoomLoopDetector

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

Overview

Detects when the agent is stuck in a repeating pattern of tool calls.

Two types of loops are detected:

1. Consecutive identical calls: [A, A, A] — same tool + same args
2. Repeating sequences: [A,B,C, A,B,C, A,B,C] — a pattern cycling

When detected, a warning is injected into the context so the LLM can course-correct.

Constant Summary collapse

DEFAULT_THRESHOLD =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold: DEFAULT_THRESHOLD) ⇒ DoomLoopDetector

Returns a new instance of DoomLoopDetector.



17
18
19
# File 'lib/brute/doom_loop.rb', line 17

def initialize(threshold: DEFAULT_THRESHOLD)
  @threshold = threshold
end

Instance Attribute Details

#thresholdObject (readonly)

Returns the value of attribute threshold.



15
16
17
# File 'lib/brute/doom_loop.rb', line 15

def threshold
  @threshold
end

Instance Method Details

#detect(messages) ⇒ Object

Extracts tool call signatures from the context’s message buffer and checks for repeating patterns at the tail.

Returns the repetition count if a loop is found, nil otherwise.



25
26
27
28
29
30
# File 'lib/brute/doom_loop.rb', line 25

def detect(messages)
  signatures = extract_signatures(messages)
  return nil if signatures.size < @threshold

  check_repeating_pattern(signatures)
end

#warning_message(repetitions) ⇒ Object

Build a human-readable warning message for the agent.



33
34
35
36
37
38
39
40
41
42
# File 'lib/brute/doom_loop.rb', line 33

def warning_message(repetitions)
  <<~MSG
    SYSTEM NOTICE: Doom loop detected — the same tool call pattern has repeated #{repetitions} times.
    You are stuck in a loop and not making progress. Stop and try a fundamentally different approach:
    - Re-read the file to check your changes actually applied
    - Try a different tool or strategy
    - Break the problem into smaller steps
    - If a command keeps failing, investigate why before retrying
  MSG
end