Class: SwarmCLI::V3::RebootTool

Inherits:
SwarmSDK::V3::Tools::Base
  • Object
show all
Defined in:
lib/swarm_cli/v3/reboot_tool.rb

Overview

Tool that triggers a process reboot with continuation message.

Writes a signal file scoped to the current PID. The CLI checks for pending signals after each agent.ask() completes and calls Kernel.exec to replace the process with a fresh Ruby invocation.

Signal files are PID-scoped (+signal_<pid>.json+) so multiple CLI sessions sharing the same agent directory don’t interfere.

Examples:

CLI integration

RebootTool.session_pid = Process.pid
SwarmSDK::V3::Tools::Registry.register(:Reboot, RebootTool)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory:) ⇒ RebootTool

Returns a new instance of RebootTool.

Parameters:

  • directory (String)

    Agent working directory



84
85
86
87
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 84

def initialize(directory:)
  super()
  @directory = File.expand_path(directory)
end

Class Attribute Details

.session_pidInteger?

PID of the CLI session that owns this tool instance. Set by the CLI at startup.

Returns:

  • (Integer, nil)


28
29
30
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 28

def session_pid
  @session_pid
end

Class Method Details

.consume_signal(directory, pid) ⇒ Hash?

Read and delete the signal file, returning the parsed data.

Parameters:

  • directory (String)

    Agent working directory

  • pid (Integer)

    Process ID whose signal to consume

Returns:

  • (Hash, nil)

    Signal data or nil if no signal exists



44
45
46
47
48
49
50
51
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 44

def consume_signal(directory, pid)
  path = signal_path_for(directory, pid)
  return unless File.exist?(path)

  data = JSON.parse(File.read(path), symbolize_names: true)
  File.delete(path)
  data
end

.creation_requirementsArray<Symbol>

Returns Constructor requirements.

Returns:

  • (Array<Symbol>)

    Constructor requirements



20
21
22
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 20

def creation_requirements
  [:directory]
end

.signal_path_for(directory, pid) ⇒ String

Compute the signal file path for a given PID.

Parameters:

  • directory (String)

    Agent working directory

  • pid (Integer)

    Process ID

Returns:

  • (String)

    Absolute path to the signal file



58
59
60
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 58

def signal_path_for(directory, pid)
  File.join(File.expand_path(directory), ".swarm", "reboot", "signal_#{pid}.json")
end

.signal_pending?(directory, pid) ⇒ Boolean

Check if a reboot signal is pending for the given PID.

Parameters:

  • directory (String)

    Agent working directory

  • pid (Integer)

    Process ID to check

Returns:

  • (Boolean)


35
36
37
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 35

def signal_pending?(directory, pid)
  File.exist?(signal_path_for(directory, pid))
end

Instance Method Details

#execute(continuation_message:, reason: nil) ⇒ String

Write a reboot signal file for the CLI to detect.

Parameters:

  • continuation_message (String)

    Message to resume with after reboot

  • reason (String, nil) (defaults to: nil)

    Brief reason for the reboot

Returns:

  • (String)

    Confirmation message



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 99

def execute(continuation_message:, reason: nil)
  pid = self.class.session_pid
  return error("session_pid not set — Reboot tool requires CLI integration") unless pid

  if continuation_message.nil? || continuation_message.strip.empty?
    return validation_error("continuation_message is required")
  end

  signal = {
    continuation_message: continuation_message,
    reason: reason,
    timestamp: Time.now.iso8601,
    pid: pid,
  }

  path = self.class.signal_path_for(@directory, pid)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, JSON.pretty_generate(signal))

  "Reboot signal written. The process will restart after this response completes. " \
    "Your continuation message has been saved and will be sent as the first prompt after reboot."
end

#nameString

Returns Tool display name.

Returns:

  • (String)

    Tool display name



90
91
92
# File 'lib/swarm_cli/v3/reboot_tool.rb', line 90

def name
  "Reboot"
end