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<Child> { ... }
fn spawn_agent(&self, config: &AgentSpawnConfig) -> Result<AgentChildHandle> { ... }
fn command_exists(&self, command: &str) -> bool { ... }
}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<Child>
fn spawn( &self, command: &str, args: &[&str], env: &[(String, String)], workdir: Option<&Path>, ) -> Result<Child>
Spawn a process with stdin input and return the child handle.
This method is used when you need to write to the process’s stdin
or stream its output in real-time. Unlike execute(), this returns
a Child handle for direct interaction.
§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 Child handle that can be used to interact with the process.
§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 uses the spawn() method with additional
configuration for agent-specific needs. Mock implementations should
override this to return mock results without spawning real processes.