Class: EnhanceSwarm::AgentSpawner

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

Instance Method Summary collapse

Constructor Details

#initializeAgentSpawner

Returns a new instance of AgentSpawner.



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

def initialize
  @config = EnhanceSwarm.configuration
  @session_manager = SessionManager.new
  @resource_manager = ResourceManager.new
end

Instance Method Details

#claude_cli_available?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
# File 'lib/enhance_swarm/agent_spawner.rb', line 116

def claude_cli_available?
  @claude_cli_available ||= begin
    result = `claude --version 2>/dev/null`
    $?.success? && result.strip.length > 0
  rescue StandardError
    false
  end
end

#get_running_agentsObject



82
83
84
# File 'lib/enhance_swarm/agent_spawner.rb', line 82

def get_running_agents
  @session_manager.check_agent_processes
end

#spawn_agent(role:, task:, worktree: true) ⇒ Object



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/enhance_swarm/agent_spawner.rb', line 18

def spawn_agent(role:, task:, worktree: true)
  Logger.info("Spawning #{role} agent for task: #{task}")
  
  # Check resource limits before spawning
  resource_check = @resource_manager.can_spawn_agent?
  unless resource_check[:allowed]
    Logger.error("Cannot spawn agent - resource limits exceeded:")
    resource_check[:reasons].each { |reason| Logger.error("  - #{reason}") }
    return false
  end
  
  begin
    # Create worktree if requested
    worktree_path = nil
    if worktree
      worktree_path = create_agent_worktree(role)
      return false unless worktree_path
    end

    # Generate agent prompt
    prompt = build_agent_prompt(task, role, worktree_path)
    
    # Spawn the agent process
    pid = spawn_claude_process(prompt, role, worktree_path)
    return false unless pid

    # Register agent in session
    success = @session_manager.add_agent(role, pid, worktree_path, task)
    
    if success
      Logger.info("Successfully spawned #{role} agent (PID: #{pid})")
      { pid: pid, worktree_path: worktree_path, role: role }
    else
      Logger.error("Failed to register agent in session")
      cleanup_failed_spawn(pid, worktree_path)
      false
    end

  rescue StandardError => e
    Logger.error("Failed to spawn #{role} agent: #{e.message}")
    cleanup_failed_spawn(nil, worktree_path)
    false
  end
end

#spawn_multiple_agents(agents) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/enhance_swarm/agent_spawner.rb', line 63

def spawn_multiple_agents(agents)
  results = []
  
  agents.each_with_index do |agent_config, index|
    # Add jitter to prevent resource contention
    sleep(2 + rand(0..2)) if index > 0
    
    result = spawn_agent(
      role: agent_config[:role],
      task: agent_config[:task],
      worktree: agent_config.fetch(:worktree, true)
    )
    
    results << result if result
  end
  
  results
end

#stop_agent(pid) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/enhance_swarm/agent_spawner.rb', line 86

def stop_agent(pid)
  begin
    Process.kill('TERM', pid.to_i)
    @session_manager.update_agent_status(pid, 'stopped', Time.now.iso8601)
    Logger.info("Stopped agent with PID: #{pid}")
    true
  rescue Errno::ESRCH
    # Process already stopped
    @session_manager.update_agent_status(pid, 'stopped', Time.now.iso8601)
    true
  rescue StandardError => e
    Logger.error("Failed to stop agent (PID: #{pid}): #{e.message}")
    false
  end
end

#stop_all_agentsObject



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

def stop_all_agents
  active_agents = @session_manager.get_active_agents
  stopped_count = 0
  
  active_agents.each do |agent|
    if stop_agent(agent[:pid])
      stopped_count += 1
    end
  end
  
  Logger.info("Stopped #{stopped_count}/#{active_agents.length} agents")
  stopped_count
end