Class: EnhanceSwarm::InterruptHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/enhance_swarm/interrupt_handler.rb

Constant Summary collapse

STUCK_THRESHOLD =

10 minutes

600
MEMORY_THRESHOLD =

1GB in MB

1000
RESPONSE_TIMEOUT =

30 seconds for user response

30

Instance Method Summary collapse

Constructor Details

#initialize(notification_manager = nil) ⇒ InterruptHandler

Returns a new instance of InterruptHandler.



11
12
13
14
15
16
# File 'lib/enhance_swarm/interrupt_handler.rb', line 11

def initialize(notification_manager = nil)
  @notification_manager = notification_manager || NotificationManager.instance
  @interrupts_enabled = true
  @monitoring_active = false
  @user_responses = {}
end

Instance Method Details

#disable_interrupts!Object



151
152
153
154
# File 'lib/enhance_swarm/interrupt_handler.rb', line 151

def disable_interrupts!
  @interrupts_enabled = false
  puts "🔇 Interrupts disabled - agents will run without intervention".colorize(:yellow)
end

#enable_interrupts!Object

Enable/disable interrupts



146
147
148
149
# File 'lib/enhance_swarm/interrupt_handler.rb', line 146

def enable_interrupts!
  @interrupts_enabled = true
  puts "✅ Interrupts enabled - will prompt for stuck/failed agents".colorize(:green)
end

#handle_agent_excessive_memory(agent) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/enhance_swarm/interrupt_handler.rb', line 69

def handle_agent_excessive_memory(agent)
  return unless @interrupts_enabled

  memory_mb = agent[:memory_mb]
  return unless memory_mb > MEMORY_THRESHOLD

  agent_id = agent[:id]
  role = agent[:role]

  @notification_manager.intervention_needed(
    "Agent '#{role}' using #{memory_mb}MB memory",
    agent_id,
    [
      "enhance-swarm restart #{agent_id}",
      "enhance-swarm kill #{agent_id}",
      "Continue monitoring"
    ]
  )

  response = prompt_user_with_timeout(
    "Agent '#{role}' using #{memory_mb}MB. Action? [r]estart/[k]ill/[c]ontinue",
    timeout: RESPONSE_TIMEOUT,
    default: 'c'
  )

  case response.downcase
  when 'r', 'restart'
    restart_agent(agent)
  when 'k', 'kill'
    kill_agent(agent)
  else
    puts "Continuing to monitor agent '#{role}'...".colorize(:yellow)
  end
end

#handle_agent_stuck(agent) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/enhance_swarm/interrupt_handler.rb', line 33

def handle_agent_stuck(agent)
  return unless @interrupts_enabled

  agent_id = agent[:id]
  role = agent[:role]
  last_activity = agent[:last_activity]
  current_task = agent[:current_task]

  # Calculate how long stuck
  time_stuck = Time.now - last_activity
  
  # Only interrupt if stuck for significant time
  return unless time_stuck > STUCK_THRESHOLD

  # Show stuck notification
  @notification_manager.agent_stuck(agent_id, role, last_activity, current_task)
  
  # Prompt user for action with timeout
  response = prompt_user_with_timeout(
    "Agent '#{role}' stuck for #{format_duration(time_stuck)}. Restart? [y/N]",
    timeout: RESPONSE_TIMEOUT,
    default: 'n'
  )

  case response.downcase
  when 'y', 'yes'
    restart_agent(agent)
  when 'k', 'kill'
    kill_agent(agent)
  when 'd', 'debug'
    debug_agent(agent)
  else
    puts "Continuing to monitor agent '#{role}'...".colorize(:yellow)
  end
end

#handle_coordination_conflict(agents, conflict_type, details = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/enhance_swarm/interrupt_handler.rb', line 104

def handle_coordination_conflict(agents, conflict_type, details = {})
  return unless @interrupts_enabled

  case conflict_type
  when :file_conflict
    handle_file_conflict(agents, details)
  when :dependency_deadlock
    handle_dependency_deadlock(agents, details)
  when :resource_contention
    handle_resource_contention(agents, details)
  end
end

#handle_critical_error(agent, error, context = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/enhance_swarm/interrupt_handler.rb', line 117

def handle_critical_error(agent, error, context = {})
  return unless @interrupts_enabled

  agent_id = agent[:id]
  role = agent[:role]

  # Analyze error for suggested fixes
  suggestions = analyze_error_for_suggestions(error, context)

  @notification_manager.agent_failed(agent_id, role, error.message, suggestions)

  if suggestions.any?
    puts "\nError analysis complete. Choose an action:".colorize(:red)
    suggestions.each_with_index do |suggestion, index|
      puts "  #{index + 1}. #{suggestion}".colorize(:yellow)
    end
    puts "  c. Enter custom command".colorize(:blue)

    response = prompt_user_with_timeout(
      "Choose [1-#{suggestions.length}] or [c]ustom:",
      timeout: RESPONSE_TIMEOUT,
      default: '1'
    )

    execute_error_recovery(agent, response, suggestions)
  end
end

#interrupts_enabled?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/enhance_swarm/interrupt_handler.rb', line 156

def interrupts_enabled?
  @interrupts_enabled
end

#start_monitoring(agents) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/enhance_swarm/interrupt_handler.rb', line 18

def start_monitoring(agents)
  return if @monitoring_active

  @monitoring_active = true
  @monitoring_thread = Thread.new do
    monitor_for_interrupts(agents)
  end
end

#stop_monitoringObject



27
28
29
30
31
# File 'lib/enhance_swarm/interrupt_handler.rb', line 27

def stop_monitoring
  @monitoring_active = false
  @monitoring_thread&.kill
  @monitoring_thread = nil
end