Class: Brute::Loop::DoomLoopDetector
- Inherits:
-
Object
- Object
- Brute::Loop::DoomLoopDetector
- Defined in:
- lib/brute/loop/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
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#detect(messages) ⇒ Object
Extracts tool call signatures from the context’s message buffer and checks for repeating patterns at the tail.
-
#initialize(threshold: DEFAULT_THRESHOLD) ⇒ DoomLoopDetector
constructor
A new instance of DoomLoopDetector.
-
#warning_message(repetitions) ⇒ Object
Build a human-readable warning message for the agent.
Constructor Details
#initialize(threshold: DEFAULT_THRESHOLD) ⇒ DoomLoopDetector
Returns a new instance of DoomLoopDetector.
18 19 20 |
# File 'lib/brute/loop/doom_loop.rb', line 18 def initialize(threshold: DEFAULT_THRESHOLD) @threshold = threshold end |
Instance Attribute Details
#threshold ⇒ Object (readonly)
Returns the value of attribute threshold.
16 17 18 |
# File 'lib/brute/loop/doom_loop.rb', line 16 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.
26 27 28 29 30 31 |
# File 'lib/brute/loop/doom_loop.rb', line 26 def detect() signatures = extract_signatures() return nil if signatures.size < @threshold check_repeating_pattern(signatures) end |
#warning_message(repetitions) ⇒ Object
Build a human-readable warning message for the agent.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/brute/loop/doom_loop.rb', line 34 def (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 |