Class: RubynCode::Agent::LoopDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/agent/loop_detector.rb

Instance Method Summary collapse

Constructor Details

#initialize(window: 5, threshold: 3, name_window: 12, name_threshold: 6) ⇒ LoopDetector

Returns a new instance of LoopDetector.

Parameters:

  • window (Integer) (defaults to: 5)

    number of recent calls to keep in the sliding window

  • threshold (Integer) (defaults to: 3)

    number of identical signatures that indicate a stall

  • name_window (Integer) (defaults to: 12)

    larger window for tool name repetition detection

  • name_threshold (Integer) (defaults to: 6)

    how many times the same tool name in name_window triggers stall



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_messageString

A system-level nudge message to inject when a stall is detected. This tells the agent to try a different approach.

Returns:

  • (String)


56
57
58
59
60
# File 'lib/rubyn_code/agent/loop_detector.rb', line 56

def nudge_message
  '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.

Parameters:

  • tool_name (String)
  • tool_input (Hash, String, nil)


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.

Returns:

  • (Boolean)


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