Class: Aidp::Interfaces::TtyCommandExecutor

Inherits:
Object
  • Object
show all
Includes:
CommandExecutorInterface
Defined in:
lib/aidp/interfaces/command_executor_interface.rb

Overview

TtyCommandExecutor wraps TTY::Command for command execution. This adapter provides the standard implementation used by AIDP.

Examples:

Creating an executor with a logger

logger = AidpLoggerAdapter.new
executor = TtyCommandExecutor.new(logger: logger)

Constant Summary collapse

TERMINATION_GRACE_PERIOD =
0.1

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, component_name: "command_executor") ⇒ TtyCommandExecutor

Returns a new instance of TtyCommandExecutor.

Parameters:

  • logger (LoggerInterface) (defaults to: nil)

    optional logger for debug output

  • component_name (String) (defaults to: "command_executor")

    component name for logging



135
136
137
138
# File 'lib/aidp/interfaces/command_executor_interface.rb', line 135

def initialize(logger: nil, component_name: "command_executor")
  @logger = logger
  @component_name = component_name
end

Instance Method Details

#execute(command, args: [], input: nil, timeout: nil, **options) ⇒ CommandResult

Execute a shell command using TTY::Command.

Parameters:

  • command (String)

    the command to execute

  • args (Array<String>) (defaults to: [])

    command-line arguments

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

    input to pass to stdin (or file path to read from)

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

    timeout in seconds

  • options (Hash)

    additional options passed to TTY::Command#run

Returns:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/aidp/interfaces/command_executor_interface.rb', line 148

def execute(command, args: [], input: nil, timeout: nil, **options)
  require "open3"

  log_debug("executing_command", command: command, args: args, timeout: timeout)

  start_time = Time.now

  begin
    result = execute_command(command, args, resolve_input(input), timeout, options)

    duration = Time.now - start_time
    log_debug("command_completed",
      command: command,
      exit_status: result.exit_status,
      duration: duration.round(2))

    CommandResult.new(
      stdout: result.out,
      stderr: result.err,
      exit_status: result.exit_status
    )
  rescue CommandTimeoutError
    duration = Time.now - start_time
    log_debug("command_timeout",
      command: command,
      timeout: timeout,
      duration: duration.round(2))
    raise CommandTimeoutError.new(command: command, timeout: timeout)
  rescue Errno::ENOENT, Errno::EACCES => e
    # Command not found or not executable
    duration = Time.now - start_time
    log_debug("command_not_found",
      command: command,
      error: e.message,
      duration: duration.round(2))
    raise CommandExecutionError.new(command: command, original_error: e)
  rescue => e
    duration = Time.now - start_time
    log_debug("command_failed",
      command: command,
      error: e.message,
      duration: duration.round(2))
    raise CommandExecutionError.new(command: command, original_error: e)
  end
end