Class: RubynCode::Agent::LoopDetector
- Inherits:
-
Object
- Object
- RubynCode::Agent::LoopDetector
- Defined in:
- lib/rubyn_code/agent/loop_detector.rb
Instance Method Summary collapse
-
#initialize(window: 5, threshold: 3, name_window: 12, name_threshold: 6) ⇒ LoopDetector
constructor
A new instance of LoopDetector.
-
#nudge_message ⇒ String
A system-level nudge message to inject when a stall is detected.
-
#record(tool_name, tool_input) ⇒ void
Record a tool invocation.
-
#reset! ⇒ void
Clear recorded history.
-
#stalled? ⇒ Boolean
Returns true when the same tool call signature appears at least
thresholdtimes within the current sliding window.
Constructor Details
#initialize(window: 5, threshold: 3, name_window: 12, name_threshold: 6) ⇒ LoopDetector
Returns a new instance of LoopDetector.
12 13 14 15 16 17 18 19 20 |
# File 'lib/rubyn_code/agent/loop_detector.rb', line 12 def initialize(window: 5, threshold: 3, name_window: 12, name_threshold: 6) @window = window @threshold = threshold @name_window = name_window @name_threshold = name_threshold @history = [] @name_history = [] @file_edits = Hash.new(0) end |
Instance Method Details
#nudge_message ⇒ String
A system-level nudge message to inject when a stall is detected. This tells the agent to try a different approach.
56 57 58 59 60 |
# File 'lib/rubyn_code/agent/loop_detector.rb', line 56 def 'You appear to be repeating the same tool call without making progress. ' \ 'Please try a different approach, use a different tool, or ask the user ' \ 'for clarification. Do not repeat the same action.' end |
#record(tool_name, tool_input) ⇒ void
This method returns an undefined value.
Record a tool invocation. The signature is derived from the tool name and a stable hash of the input so that identical calls are detected regardless of key ordering.
29 30 31 32 33 |
# File 'lib/rubyn_code/agent/loop_detector.rb', line 29 def record(tool_name, tool_input) record_signature(tool_name, tool_input) record_tool_name(tool_name) record_file_edit(tool_name, tool_input) end |
#reset! ⇒ void
This method returns an undefined value.
Clear recorded history.
46 47 48 49 50 |
# File 'lib/rubyn_code/agent/loop_detector.rb', line 46 def reset! @history.clear @name_history.clear @file_edits.clear end |
#stalled? ⇒ Boolean
Returns true when the same tool call signature appears at least threshold times within the current sliding window.
39 40 41 |
# File 'lib/rubyn_code/agent/loop_detector.rb', line 39 def stalled? exact_call_repeated? || tool_name_repeated? || file_over_edited? end |