Class: AgentHarness::CommandExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/command_executor.rb

Overview

Executes shell commands with timeout support

Provides a clean interface for running CLI commands with proper error handling, timeout support, and result capture.

Examples:

Basic usage

executor = AgentHarness::CommandExecutor.new
result = executor.execute(["claude", "--print", "--prompt", "Hello"])
puts result.stdout

With timeout

result = executor.execute("claude --print", timeout: 300)

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ CommandExecutor

Returns a new instance of CommandExecutor.



34
35
36
# File 'lib/agent_harness/command_executor.rb', line 34

def initialize(logger: nil)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



32
33
34
# File 'lib/agent_harness/command_executor.rb', line 32

def logger
  @logger
end

Instance Method Details

#available?(binary) ⇒ Boolean

Check if a binary is available

Parameters:

  • binary (String)

    binary name

Returns:

  • (Boolean)

    true if available



86
87
88
# File 'lib/agent_harness/command_executor.rb', line 86

def available?(binary)
  !which(binary).nil?
end

#execute(command, timeout: nil, env: {}, stdin_data: nil) ⇒ Result

Execute a command with optional timeout

Parameters:

  • command (Array<String>, String)

    command to execute

  • timeout (Integer, nil) (defaults to: nil)

    timeout in seconds

  • env (Hash) (defaults to: {})

    environment variables

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

    data to send to stdin

Returns:

  • (Result)

    execution result

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/agent_harness/command_executor.rb', line 46

def execute(command, timeout: nil, env: {}, stdin_data: nil)
  cmd_array = normalize_command(command)
  cmd_string = cmd_array.shelljoin

  log_debug("Executing command", command: cmd_string, timeout: timeout)

  start_time = Time.now

  stdout, stderr, status = if timeout
    execute_with_timeout(cmd_array, timeout: timeout, env: env, stdin_data: stdin_data)
  else
    execute_without_timeout(cmd_array, env: env, stdin_data: stdin_data)
  end

  duration = Time.now - start_time

  Result.new(
    stdout: stdout,
    stderr: stderr,
    exit_code: status.exitstatus,
    duration: duration
  )
end

#which(binary) ⇒ String?

Check if a binary exists in PATH

Parameters:

  • binary (String)

    binary name

Returns:

  • (String, nil)

    full path or nil



74
75
76
77
78
79
80
# File 'lib/agent_harness/command_executor.rb', line 74

def which(binary)
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    full_path = File.join(path, binary)
    return full_path if File.executable?(full_path)
  end
  nil
end