Class: ClaudeSwarm::Orchestrator

Inherits:
Object
  • Object
show all
Includes:
SystemUtils
Defined in:
lib/claude_swarm/orchestrator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SystemUtils

#last_status, #system!, #system_with_pid!

Constructor Details

#initialize(configuration, mcp_generator, vibe: false, prompt: nil, interactive_prompt: nil, stream_logs: false, debug: false, restore_session_path: nil, worktree: nil, session_id: nil) ⇒ Orchestrator

Returns a new instance of Orchestrator.



9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/claude_swarm/orchestrator.rb', line 9

def initialize(configuration, mcp_generator, vibe: false, prompt: nil, interactive_prompt: nil, stream_logs: false, debug: false,
  restore_session_path: nil, worktree: nil, session_id: nil)
  @config = configuration
  @generator = mcp_generator
  @vibe = vibe
  @non_interactive_prompt = prompt
  @interactive_prompt = interactive_prompt
  @stream_logs = stream_logs
  @debug = debug
  @restore_session_path = restore_session_path
  @provided_session_id = session_id
  # Store worktree option for later use
  @worktree_option = worktree
  @needs_worktree_manager = worktree.is_a?(String) || worktree == "" ||
    configuration.instances.values.any? { |inst| !inst[:worktree].nil? }
  # Store modified instances after worktree setup
  @modified_instances = nil
  # Track start time for runtime calculation
  @start_time = nil
  # Track transcript tailing thread
  @transcript_thread = nil

  # Set environment variable for prompt mode to suppress output
  ENV["CLAUDE_SWARM_PROMPT"] = "1" if @non_interactive_prompt

  # Initialize session path
  if @restore_session_path
    # Use existing session path for restoration
    @session_path = @restore_session_path
    @session_log_path = File.join(@session_path, "session.log")
  else
    # Generate new session path
    session_params = { working_dir: @config.base_dir }
    session_params[:session_id] = @provided_session_id if @provided_session_id
    @session_path = SessionPath.generate(**session_params)
    SessionPath.ensure_directory(@session_path)
    @session_log_path = File.join(@session_path, "session.log")

    # Extract session ID from path (the timestamp part)
    @session_id = File.basename(@session_path)

  end
  ENV["CLAUDE_SWARM_SESSION_PATH"] = @session_path

  # Initialize components that depend on session path
  @process_tracker = ProcessTracker.new(@session_path)
  @settings_generator = SettingsGenerator.new(@config)

  # Initialize WorktreeManager if needed
  if @needs_worktree_manager && !@restore_session_path
    cli_option = @worktree_option.is_a?(String) && !@worktree_option.empty? ? @worktree_option : nil
    @worktree_manager = WorktreeManager.new(cli_option, session_id: @session_id)
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/claude_swarm/orchestrator.rb', line 7

def config
  @config
end

#session_log_pathObject (readonly)

Returns the value of attribute session_log_path.



7
8
9
# File 'lib/claude_swarm/orchestrator.rb', line 7

def session_log_path
  @session_log_path
end

#session_pathObject (readonly)

Returns the value of attribute session_path.



7
8
9
# File 'lib/claude_swarm/orchestrator.rb', line 7

def session_path
  @session_path
end

Instance Method Details

#startObject



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

def start
  # Track start time
  @start_time = Time.now

  # Setup signal handlers for graceful shutdown
  setup_signal_handlers do
    @signal_received = true
    cleanup_all
  end

  begin
    start_internal
  rescue StandardError => e
    # Ensure cleanup happens even on unexpected errors
    cleanup_all
    raise e
  end
end