pub struct CodexBuilder { /* private fields */ }Expand description
Builder for creating a Codex client.
All options are optional. By default the builder discovers the codex
binary via PATH.
Implementations§
Source§impl CodexBuilder
impl CodexBuilder
Sourcepub fn tested_cli_version_range(self, min: CliVersion, max: CliVersion) -> Self
pub fn tested_cli_version_range(self, min: CliVersion, max: CliVersion) -> Self
Override the tested-against CLI version range.
Defaults to the range this crate declares and verifies in CI. Set this only when you have validated a different range yourself; widening it does not make the wrapper work against versions it was not tested on.
Sourcepub fn binary(self, path: impl Into<PathBuf>) -> Self
pub fn binary(self, path: impl Into<PathBuf>) -> Self
Set an explicit path to the codex binary (skips PATH lookup).
Sourcepub fn working_dir(self, path: impl Into<PathBuf>) -> Self
pub fn working_dir(self, path: impl Into<PathBuf>) -> Self
Set the working directory for all commands.
Sourcepub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
Set a single environment variable for child processes.
Sourcepub fn envs(
self,
vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
) -> Self
pub fn envs( self, vars: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>, ) -> Self
Set multiple environment variables for child processes.
Sourcepub fn clear_env(self) -> Self
pub fn clear_env(self) -> Self
Clear the inherited environment before applying variables supplied by
env and envs.
By default, child processes inherit the wrapper process’s environment.
This opt-in is call-order independent: explicit variables survive
whether they are added before or after clear_env.
This controls the direct Codex child’s environment. It is not OS or same-user isolation: the child can still access files, process metadata, sockets, and other resources allowed to its user and sandbox.
Sourcepub fn timeout_secs(self, seconds: u64) -> Self
pub fn timeout_secs(self, seconds: u64) -> Self
Set the command timeout in seconds.
Sourcepub fn termination_grace(self, duration: Duration) -> Self
pub fn termination_grace(self, duration: Duration) -> Self
How long a cancelled run’s process group gets to exit before it is killed. Defaults to five seconds.
Applies to awaited exec cancellation and timeouts, buffered or streaming.
These send the configured termination_signal,
wait this long, send SIGKILL, and await the direct child. Output received
during shutdown is drained and discarded without extending this budget.
A dropped future does not use it: Drop cannot wait, so it kills
immediately.
App-server shutdown waits up to this long after closing stdin. With Unix process groups enabled, it can wait up to this long again after sending the configured termination signal (SIGTERM by default). Shutdown returns an error with the reaped status when the server needs a stop signal, even if it then exits with code zero.
Sourcepub fn termination_signal(self, signal: TerminationSignal) -> Self
pub fn termination_signal(self, signal: TerminationSignal) -> Self
Select the Unix signal used before the hard-kill fallback.
Defaults to TerminationSignal::Terminate. Use
TerminationSignal::Interrupt to let the CLI handle Ctrl-C and clean
up its tools. This does not guarantee cleanup of detached descendants.
termination_grace controls the wait after
this signal. The signal setting is ignored with process groups disabled
or on non-Unix.
Dropping a future remains an immediate hard kill.
Sourcepub fn process_group(self, enabled: bool) -> Self
pub fn process_group(self, enabled: bool) -> Self
Whether each run gets its own process group. On by default.
With a group of its own, cancellation reaches the CLI and descendants that remain in that group. Tools that create a new group or session need cooperative cleanup by the CLI; group signaling cannot reach them.
Opting out puts the child in the parent’s group, so a terminal Ctrl-C reaches processes in the terminal’s foreground group. A wrapper-side kill reaches only the direct child, skipping the soft signal and grace. This suits a terminal-attached host that shells out synchronously and treats the terminal as the supervisor.
Matches claude-wrapper’s option of the same name. No effect on
non-unix targets, which have no process groups.
Sourcepub fn output_limit(self, max_bytes: usize) -> Self
pub fn output_limit(self, max_bytes: usize) -> Self
Stop capturing a stream once it passes max_bytes.
Without a ceiling both captured streams are read to EOF, so peak memory for a run is whatever the child decides to print. A consumer cannot add this itself: by the time it holds a result the bytes are already resident, and for a JSON run already parsed.
With a ceiling set, each buffer stays under max_bytes, so a run
holds at most twice that across stdout and stderr. Passing it stops
the run the way cancellation does and fails with
Error::OutputLimitExceeded, which carries no captured bytes: a
prefix returned as though it were the whole result is the failure
this prevents.
Off by default, so existing behavior is unchanged until a host opts in.
Sourcepub fn on_spawn(self, observer: SpawnObserver) -> Self
pub fn on_spawn(self, observer: SpawnObserver) -> Self
Observe every child this client spawns, at spawn time.
The observer fires before the run produces output, including for buffered, streaming, stdin, cancellable, and retried runs. A retry fires once per attempt because each attempt is a distinct process.
The observer runs inline on the spawning thread and must not block. A supervisor can use it to write a pidfile or send the receipt to a channel before the child can be orphaned.
Sourcepub fn die_with_parent(self, enabled: bool) -> Self
pub fn die_with_parent(self, enabled: bool) -> Self
Ask the kernel to kill spawned children when this process dies.
This uses PR_SET_PDEATHSIG on Linux. Check
die_with_parent_supported
before relying on it; other targets accept the option as a no-op. A
portable supervisor should combine this with on_spawn
and reconcile recorded children after a restart.
The post-fork hook closes the parent-death race by rechecking the parent after arming the signal. This is off by default because it is appropriate for supervised work but not for deliberately backgrounded CLI processes.
Sourcepub fn arg(self, arg: impl Into<String>) -> Self
pub fn arg(self, arg: impl Into<String>) -> Self
Append a raw global argument passed before any subcommand.
When an exec command has a typed rollout budget, conflicting global
--enable/--disable rollout_budget arguments are suppressed at final
assembly. Codex applies feature toggles after config regardless of argv
order, so retaining one could silently defeat the typed protection.
Sourcepub fn config(self, key_value: impl Into<String>) -> Self
pub fn config(self, key_value: impl Into<String>) -> Self
Add a global config override (-c key=value).
Sourcepub fn enable(self, feature: impl Into<String>) -> Self
pub fn enable(self, feature: impl Into<String>) -> Self
Enable a feature flag globally (--enable <name>).
A rollout_budget toggle is suppressed for an exec command that has a
typed RolloutBudgetConfig.
Sourcepub fn disable(self, feature: impl Into<String>) -> Self
pub fn disable(self, feature: impl Into<String>) -> Self
Disable a feature flag globally (--disable <name>).
A rollout_budget toggle is suppressed for an exec command that has a
typed RolloutBudgetConfig.
Sourcepub fn retry(self, policy: RetryPolicy) -> Self
pub fn retry(self, policy: RetryPolicy) -> Self
Set a default RetryPolicy for all commands.