pub trait ProcessExecutor:
Send
+ Sync
+ Debug {
// Required method
fn execute(
&self,
command: &str,
args: &[&str],
env: &[(String, String)],
workdir: Option<&Path>,
) -> Result<ProcessOutput>;
// Provided methods
fn spawn(
&self,
command: &str,
args: &[&str],
env: &[(String, String)],
workdir: Option<&Path>,
) -> Result<SpawnedProcess> { ... }
fn spawn_agent(&self, config: &AgentSpawnConfig) -> Result<AgentChildHandle> { ... }
fn command_exists(&self, command: &str) -> bool { ... }
fn get_child_process_info(&self, parent_pid: u32) -> ChildProcessInfo { ... }
fn kill_process_group(&self, _pgid: u32) -> Result<()> { ... }
}Expand description
Trait for executing external processes.
This trait abstracts process execution to allow dependency injection.
Production code uses RealProcessExecutor which calls actual commands.
Test code can use MockProcessExecutor to control process behavior.
Only external process execution is abstracted. Internal code logic is never mocked.
Required Methods§
Sourcefn execute(
&self,
command: &str,
args: &[&str],
env: &[(String, String)],
workdir: Option<&Path>,
) -> Result<ProcessOutput>
fn execute( &self, command: &str, args: &[&str], env: &[(String, String)], workdir: Option<&Path>, ) -> Result<ProcessOutput>
Execute a command with given arguments and return its output.
§Arguments
command- The program to executeargs- Command-line arguments to pass to the programenv- Environment variables to set for the process (optional)workdir- Working directory for the process (optional)
§Returns
Returns a ProcessOutput containing exit status, stdout, and stderr.
§Errors
Returns an error if command cannot be spawned or if output capture fails.
Provided Methods§
Sourcefn spawn(
&self,
command: &str,
args: &[&str],
env: &[(String, String)],
workdir: Option<&Path>,
) -> Result<SpawnedProcess>
fn spawn( &self, command: &str, args: &[&str], env: &[(String, String)], workdir: Option<&Path>, ) -> Result<SpawnedProcess>
Spawn a process with stdin input and return a handle for interaction.
This method is used when you need to write to the process’s stdin.
Unlike execute(), this returns a SpawnedProcess handle that exposes
only the domain-relevant surface (stdin writing and process completion).
§Arguments
command- The program to executeargs- Command-line arguments to pass to the programenv- Environment variables to set for the process (optional)workdir- Working directory for the process (optional)
§Returns
Returns a SpawnedProcess handle for writing stdin and waiting.
§Errors
Returns an error if command cannot be spawned.
Sourcefn spawn_agent(&self, config: &AgentSpawnConfig) -> Result<AgentChildHandle>
fn spawn_agent(&self, config: &AgentSpawnConfig) -> Result<AgentChildHandle>
Spawn an agent process with streaming output support.
This method is specifically designed for spawning AI agent subprocesses
that need to output streaming JSON in real-time. Unlike spawn(), this
returns a handle with boxed stdout for trait object compatibility.
§Arguments
config- Agent spawn configuration including command, args, env, prompt, etc.
§Returns
Returns an AgentChildHandle with stdout, stderr, and the child process.
§Errors
Returns an error if the agent cannot be spawned.
§Default Implementation
The default implementation spawns the agent command directly. Mock implementations should override this to return mock results without spawning real processes.
Sourcefn command_exists(&self, command: &str) -> bool
fn command_exists(&self, command: &str) -> bool
Sourcefn get_child_process_info(&self, parent_pid: u32) -> ChildProcessInfo
fn get_child_process_info(&self, parent_pid: u32) -> ChildProcessInfo
their cumulative CPU time.
Used by the idle-timeout monitor to determine whether child processes are actively working (CPU time advancing between consecutive checks) versus merely existing (stalled, zombie, or idle daemon).
Default implementation: parses ps output for PID, PPID, and cputime
columns on Unix platforms. Returns ChildProcessInfo::NONE on non-Unix.
Any execution error is treated as “no children” to avoid blocking the
timeout system. If ps is unavailable or fails unexpectedly, a one-time
warning is emitted to stderr so operators can diagnose reduced protection
against false-positive idle kills.
Sourcefn kill_process_group(&self, _pgid: u32) -> Result<()>
fn kill_process_group(&self, _pgid: u32) -> Result<()>
Kill an entire process group by sending SIGKILL to all members.
Uses kill(-pid, SIGKILL) to send the signal to all processes in the
process group identified by pgid. This is a best-effort fire-and-forget
call; errors are ignored because the primary process has already exited.
The default implementation is a no-op so existing mock implementations
continue to compile without change. The RealProcessExecutor override
issues the actual SIGKILL.