Class: SwarmSDK::Swarm::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/swarm/executor.rb

Overview

Handles swarm execution orchestration

Extracted from Swarm#execute to reduce complexity and eliminate code duplication. The core execution loop, error handling, and cleanup logic are unified here.

Stop Mechanism

Supports hard-stop via swarm.stop using IO.pipe for thread-safe signaling:

  1. swarm.stop writes to pipe and sets @stop_requested
  2. A listener task reads from the pipe (async-aware I/O)
  3. Listener calls barrier.stop within the Async reactor
  4. All child tasks receive Async::Stop exception
  5. execute_in_task catches Async::Stop, sets interrupted flag, emits events

Instance Method Summary collapse

Constructor Details

#initialize(swarm) ⇒ Executor

Returns a new instance of Executor.



19
20
21
22
# File 'lib/swarm_sdk/swarm/executor.rb', line 19

def initialize(swarm)
  @swarm = swarm
  @interrupted_result = nil
end

Instance Method Details

#run(prompt, wait:, logs:, has_logging:, original_fiber_storage:) ⇒ Result, Async::Task

Execute the swarm with a prompt

Parameters:

  • prompt (String)

    User prompt

  • wait (Boolean)

    Block until completion (true) or return task (false)

  • logs (Array)

    Log collection array

  • has_logging (Boolean)

    Whether logging is enabled

  • original_fiber_storage (Hash)

    Original Fiber storage values to restore

Returns:

  • (Result, Async::Task)

    Result if wait: true, Async::Task if wait: false



32
33
34
35
36
37
38
39
# File 'lib/swarm_sdk/swarm/executor.rb', line 32

def run(prompt, wait:, logs:, has_logging:, original_fiber_storage:)
  @original_fiber_storage = original_fiber_storage
  if wait
    run_blocking(prompt, logs: logs, has_logging: has_logging)
  else
    run_async(prompt, logs: logs, has_logging: has_logging)
  end
end