Class: EnhanceSwarm::ControlAgent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_description, config = nil) ⇒ ControlAgent

Returns a new instance of ControlAgent.



10
11
12
13
14
15
16
17
18
# File 'lib/enhance_swarm/control_agent.rb', line 10

def initialize(task_description, config = nil)
  @task = task_description
  @config = config || EnhanceSwarm.configuration
  @status = 'initializing'
  @worker_agents = {}
  @start_time = Time.now
  @communication_file = create_communication_file
  @control_process = nil
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/enhance_swarm/control_agent.rb', line 8

def config
  @config
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



8
9
10
# File 'lib/enhance_swarm/control_agent.rb', line 8

def start_time
  @start_time
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/enhance_swarm/control_agent.rb', line 8

def status
  @status
end

#taskObject (readonly)

Returns the value of attribute task.



8
9
10
# File 'lib/enhance_swarm/control_agent.rb', line 8

def task
  @task
end

#worker_agentsObject (readonly)

Returns the value of attribute worker_agents.



8
9
10
# File 'lib/enhance_swarm/control_agent.rb', line 8

def worker_agents
  @worker_agents
end

Instance Method Details

#current_statusObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/enhance_swarm/control_agent.rb', line 49

def current_status
  return default_status unless File.exist?(@communication_file)
  
  begin
    content = File.read(@communication_file)
    return default_status if content.strip.empty?
    
    # Parse the latest status update from Control Agent
    lines = content.split("\n").reject(&:empty?)
    latest_status = lines.last
    
    JSON.parse(latest_status)
  rescue JSON::ParserError, StandardError => e
    Logger.warn("Failed to parse control agent status: #{e.message}")
    default_status
  end
end

#start_coordinationObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/enhance_swarm/control_agent.rb', line 20

def start_coordination
  Logger.info("Starting Control Agent for task: #{@task}")
  @status = 'starting'
  
  # Spawn the Control Agent as a Claude process
  spawn_control_agent
  
  # Monitor and coordinate
  coordinate_agents
end

#stop_coordinationObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/enhance_swarm/control_agent.rb', line 31

def stop_coordination
  Logger.info("Stopping Control Agent coordination")
  @status = 'stopping'
  
  # Terminate control agent process
  if @control_process
    begin
      Process.kill('TERM', @control_process)
      Process.wait(@control_process)
    rescue Errno::ESRCH, Errno::ECHILD
      # Process already terminated
    end
  end
  
  # Cleanup
  cleanup_resources
end

#track_progress_with_streamer(streamer = nil) ⇒ Object

Enhanced progress tracking integration



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
105
# File 'lib/enhance_swarm/control_agent.rb', line 80

def track_progress_with_streamer(streamer = nil)
  return unless streamer
  
  Thread.new do
    while @status == 'coordinating'
      status = current_status
      
      # Update progress tracker
      progress = status['progress_percentage'] || 0
      message = status['message'] || 'Coordinating agents...'
      
      streamer.set_progress(progress,
                          message: message,
                          details: {
                            operation: 'control_coordination',
                            phase: status['phase'],
                            active_agents: status['active_agents']&.length || 0,
                            completed_agents: status['completed_agents']&.length || 0
                          })
      
      break if %w[completed failed].include?(status['status'])
      
      sleep(2)
    end
  end
end

#worker_agent_summaryObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/enhance_swarm/control_agent.rb', line 67

def worker_agent_summary
  status = current_status
  {
    total: status['active_agents'].length + status['completed_agents'].length,
    active: status['active_agents'].length,
    completed: status['completed_agents'].length,
    progress: status['progress_percentage'] || 0,
    phase: status['phase'] || 'unknown',
    estimated_completion: status['estimated_completion']
  }
end