Class: EnhanceSwarm::ProcessMonitor

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

Instance Method Summary collapse

Constructor Details

#initializeProcessMonitor

Returns a new instance of ProcessMonitor.



9
10
11
# File 'lib/enhance_swarm/process_monitor.rb', line 9

def initialize
  @session_manager = SessionManager.new
end

Instance Method Details

#cleanup_completed_agentsObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/enhance_swarm/process_monitor.rb', line 175

def cleanup_completed_agents
  all_agents = @session_manager.get_all_agents
  completed_agents = all_agents.select { |a| a[:status] == 'completed' }
  
  cleanup_count = 0
  completed_agents.each do |agent|
    if cleanup_agent_worktree(agent[:worktree_path])
      @session_manager.remove_agent(agent[:pid])
      cleanup_count += 1
    end
  end

  if cleanup_count > 0
    puts "๐Ÿงน Cleaned up #{cleanup_count} completed agents".colorize(:green)
  end

  cleanup_count
end

#display_agents_table(agents) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/enhance_swarm/process_monitor.rb', line 91

def display_agents_table(agents)
  return if agents.empty?

  # Table headers
  printf "   %-10s %-8s %-12s %-10s %s\n", "ROLE", "PID", "STATUS", "RUNTIME", "WORKTREE"
  puts "   " + "-" * 70

  agents.each do |agent|
    runtime = calculate_runtime(agent[:start_time], agent[:completion_time])
    worktree = agent[:worktree_path] ? File.basename(agent[:worktree_path]) : 'none'
    
    status_color = case agent[:status]
                  when 'running' then :green
                  when 'completed' then :blue
                  when 'failed' then :red
                  when 'stopped' then :yellow
                  else :white
                  end

    role = agent[:role].upcase.ljust(10)
    pid = agent[:pid].to_s.ljust(8)
    status = agent[:status].ljust(12)
    
    printf "   %-10s %-8s ", role, pid
    print status.colorize(status_color)
    printf " %-10s %s\n", runtime, worktree
  end
end

#display_statusObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/enhance_swarm/process_monitor.rb', line 44

def display_status
  status_data = status

  unless status_data[:session_exists]
    puts "๐Ÿ“Š No active enhance-swarm session found".colorize(:yellow)
    puts "   Run 'enhance-swarm enhance' to start a new session"
    return
  end

  puts "๐Ÿ“Š EnhanceSwarm Session Status".colorize(:blue)
  puts "=" * 50

  # Session info
  puts "\n๐ŸŽฏ Session: #{status_data[:session_id]}".colorize(:green)
  puts "   Started: #{format_time(status_data[:start_time])}"
  puts "   Task: #{status_data[:task_description] || 'No description'}" if status_data[:task_description]

  # Agent summary
  puts "\n๐Ÿ“ˆ Agents Summary:".colorize(:blue)
  puts "   Total: #{status_data[:total_agents]}"
  puts "   Active: #{status_data[:active_agents]}".colorize(:green)
  puts "   Completed: #{status_data[:completed_agents]}".colorize(:green)
  puts "   Stopped: #{status_data[:stopped_agents]}".colorize(:yellow)
  puts "   Failed: #{status_data[:failed_agents]}".colorize(:red) if status_data[:failed_agents] > 0

  # Active agents details
  if status_data[:active_agents] > 0
    puts "\n๐Ÿค– Active Agents:".colorize(:yellow)
    status_data[:active_agent_details].each do |agent|
      runtime = calculate_runtime(agent[:start_time])
      puts "   #{agent[:role].upcase.ljust(8)} PID: #{agent[:pid].to_s.ljust(6)} Runtime: #{runtime}".colorize(:yellow)
      puts "   #{' ' * 10} Worktree: #{agent[:worktree_path]}" if agent[:worktree_path]
    end
  end

  # All agents table if requested or if there are completed/failed agents
  if status_data[:total_agents] > status_data[:active_agents]
    puts "\n๐Ÿ“‹ All Agents:".colorize(:blue)
    display_agents_table(status_data[:agents])
  end

  # Git worktrees
  display_worktree_status

  puts ""
end

#display_worktree_statusObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/enhance_swarm/process_monitor.rb', line 120

def display_worktree_status
  begin
    worktrees = get_git_worktrees
    enhance_worktrees = worktrees.select { |wt| wt[:path].include?('.enhance_swarm/worktrees') }
    
    if enhance_worktrees.any?
      puts "\n๐ŸŒณ Git Worktrees:".colorize(:blue)
      enhance_worktrees.each do |worktree|
        branch = worktree[:branch] || 'detached'
        puts "   #{File.basename(worktree[:path]).ljust(20)} #{branch}".colorize(:cyan)
      end
    end
  rescue StandardError => e
    Logger.warn("Could not get worktree status: #{e.message}")
  end
end

#statusObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/enhance_swarm/process_monitor.rb', line 13

def status
  session_status = @session_manager.session_status
  
  unless session_status[:exists]
    return {
      session_exists: false,
      active_agents: 0,
      total_agents: 0,
      message: 'No active session found'
    }
  end

  # Check and update process statuses
  active_agents = @session_manager.check_agent_processes
  all_agents = @session_manager.get_all_agents

  {
    session_exists: true,
    session_id: session_status[:session_id],
    start_time: session_status[:start_time],
    task_description: session_status[:task_description],
    active_agents: active_agents.length,
    total_agents: all_agents.length,
    completed_agents: all_agents.count { |a| a[:status] == 'completed' },
    failed_agents: all_agents.count { |a| a[:status] == 'failed' },
    stopped_agents: all_agents.count { |a| a[:status] == 'stopped' },
    agents: all_agents,
    active_agent_details: active_agents
  }
end

#watch(interval: 5, timeout: nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/enhance_swarm/process_monitor.rb', line 137

def watch(interval: 5, timeout: nil)
  start_time = Time.now
  check_count = 0

  puts "๐Ÿ” Watching enhance-swarm agents (Ctrl+C to stop)...".colorize(:yellow)
  puts ""

  begin
    loop do
      check_count += 1
      elapsed = Time.now - start_time

      # Check timeout
      if timeout && elapsed > timeout
        puts "\nโฑ๏ธ  Watch timeout reached (#{timeout}s)".colorize(:blue)
        break
      end

      # Clear screen and show status
      system('clear') || system('cls')
      
      puts "[Check #{check_count}] #{Time.now.strftime('%H:%M:%S')} (#{elapsed.round}s elapsed)".colorize(:gray)
      display_status

      # Check if all agents completed
      status_data = status
      if status_data[:session_exists] && status_data[:active_agents] == 0 && status_data[:total_agents] > 0
        puts "โœ… All agents completed!".colorize(:green)
        break
      end

      sleep interval
    end
  rescue Interrupt
    puts "\n\n๐Ÿ‘‹ Watch stopped by user".colorize(:yellow)
  end
end