Class: EnhanceSwarm::Orchestrator

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

Instance Method Summary collapse

Constructor Details

#initializeOrchestrator

Returns a new instance of Orchestrator.



16
17
18
19
20
21
22
23
# File 'lib/enhance_swarm/orchestrator.rb', line 16

def initialize
  @config = EnhanceSwarm.configuration
  @task_manager = TaskManager.new
  @session_manager = SessionManager.new
  @agent_spawner = AgentSpawner.new
  @process_monitor = ProcessMonitor.new
  @task_integration = TaskIntegration.new
end

Instance Method Details

#enhance(task_id: nil, dry_run: false, follow: false) ⇒ Object



25
26
27
28
29
30
31
32
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
68
69
70
71
72
73
74
75
76
77
# File 'lib/enhance_swarm/orchestrator.rb', line 25

def enhance(task_id: nil, dry_run: false, follow: false)
  puts 'šŸŽÆ ENHANCE Protocol Initiated'.colorize(:green)

  # Step 1: Identify task
  task = task_id ? @task_manager.find_task(task_id) : @task_manager.next_priority_task
  unless task
    puts 'No tasks available in backlog'.colorize(:yellow)
    return
  end

  puts "šŸ“‹ Task: #{task[:id]} - #{task[:title]}".colorize(:blue)

  if dry_run
    puts "\nšŸ” Dry run - would execute:".colorize(:yellow)
    show_execution_plan(task)
    return
  end

  # Step 2: Create or resume session
  unless @session_manager.session_exists?
    @session_manager.create_session(task[:description])
    puts 'šŸ“ Created new session'.colorize(:green)
  else
    puts 'šŸ“ Resuming existing session'.colorize(:blue)
  end

  # Step 3: Move task to active
  @task_manager.move_task(task[:id], 'active')
  puts 'āœ… Task moved to active'.colorize(:green)

  # Step 4: Break down and spawn agents with progress tracking
  agents = break_down_task(task)
  total_tokens = agents.sum { |agent| ProgressTracker.estimate_tokens_for_operation('spawn_agent') }
  
  if follow
    spawn_agents_with_streaming(agents)
  else
    ProgressTracker.track(total_steps: 100, estimated_tokens: total_tokens) do |tracker|
      # Spawn agents (0-50% progress)
      spawn_result = spawn_agents(agents, tracker)
      
      # Brief monitoring (50-100% progress)
      monitor_with_progress(tracker, @config.monitor_timeout)
    end
  end

  # Step 5: Continue with other work
  puts "\nšŸ’” Agents working in background. Check back later with:".colorize(:blue)
  puts '   enhance-swarm status'
  puts '   enhance-swarm monitor'

  # Return control to user for other work
end

#get_task_management_dataObject



172
173
174
# File 'lib/enhance_swarm/orchestrator.rb', line 172

def get_task_management_data
  @task_integration.get_kanban_data
end

#monitor_with_progress(tracker, timeout_seconds) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/enhance_swarm/orchestrator.rb', line 79

def monitor_with_progress(tracker, timeout_seconds)
  start_time = Time.now
  last_check = start_time
  check_interval = 5 # Check every 5 seconds
  
  while (Time.now - start_time) < timeout_seconds
    elapsed = Time.now - start_time
    progress = 50 + (elapsed / timeout_seconds * 50).to_i # 50-100% progress
    
    # Get current status using built-in monitor
    status = @process_monitor.status
    active_count = status[:active_agents]
    
    tracker.set_progress(progress,
                       message: "Monitoring #{active_count} active agents...",
                       operation: 'monitor',
                       details: { 
                         active_agents: active_count,
                         elapsed: "#{elapsed.round}s"
                       })
    
    sleep(check_interval)
  end
  
  tracker.set_progress(100, message: "Monitoring complete - agents running in background")
end

#setup_task_managementObject



176
177
178
# File 'lib/enhance_swarm/orchestrator.rb', line 176

def setup_task_management
  @task_integration.setup_task_management
end

#spawn_agents_with_streaming(agents) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/enhance_swarm/orchestrator.rb', line 106

def spawn_agents_with_streaming(agents)
  puts "\nšŸ¤– Spawning #{agents.count} agents with live output...".colorize(:yellow)
  
  # Use built-in agent spawner
  spawned_agents = @agent_spawner.spawn_multiple_agents(agents)
  
  return if spawned_agents.empty?
  
  puts "\nšŸ”“ Live output streaming started for #{spawned_agents.count} agents. Press Ctrl+C to stop watching.\n".colorize(:green)
  
  # Start streaming output for all agents
  begin
    OutputStreamer.stream_agents(spawned_agents)
  rescue NameError
    # Fallback: Use built-in monitoring if OutputStreamer doesn't exist
    puts "šŸ” Switching to built-in monitoring...".colorize(:blue)
    @process_monitor.watch(interval: 3)
  end
end

#spawn_single(task:, role:, worktree:) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
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
# File 'lib/enhance_swarm/orchestrator.rb', line 126

def spawn_single(task:, role:, worktree:)
  operation_id = "spawn_#{role}_#{Time.now.to_i}"
  Logger.log_operation(operation_id, 'started', { role: role, worktree: worktree })

  puts "šŸš€ Spawning #{role} agent...".colorize(:yellow)

  begin
    # Use built-in agent spawner
    result = @agent_spawner.spawn_agent(
      role: role,
      task: task,
      worktree: worktree && @config.worktree_enabled
    )
    
    if result
      puts 'āœ… Agent spawned successfully'.colorize(:green)
      puts "   šŸ“Š PID: #{result[:pid]}, Role: #{result[:role]}".colorize(:blue)
      if result[:worktree_path]
        puts "   šŸ“Š Worktree: #{File.basename(result[:worktree_path])}".colorize(:blue)
      end
      
      Logger.log_operation(operation_id, 'success', { role: role, pid: result[:pid] })
      
      # Wait a moment and check if the process is still running
      sleep(1)
      begin
        Process.getpgid(result[:pid])
        puts "   šŸ“Š Agent process confirmed running".colorize(:green)
      rescue Errno::ESRCH
        puts "   šŸ“Š Agent process completed quickly".colorize(:yellow)
      end
      
      result[:pid]
    else
      puts "āŒ Failed to spawn agent".colorize(:red)
      puts "   šŸ’” Try running with ENHANCE_SWARM_DEBUG=true for details".colorize(:yellow)
      Logger.log_operation(operation_id, 'failed', { role: role, error: 'Spawn failed' })
      false
    end
  rescue StandardError => e
    puts "āŒ Failed to spawn agent: #{e.message}".colorize(:red)
    Logger.log_operation(operation_id, 'failed', { role: role, error: e.message })
    false
  end
end