agent-commander
Rust bindings and CLI tools for controlling AI coding agents through their native command-line interfaces.
This crate provides the same core behavior as the JavaScript package: command building, process control, isolation wrappers, JSON streaming helpers, model aliasing, and read-only planning mode.
Install
Install the CLI binaries:
CLI
Common options:
--tool <name>:claude,codex,opencode,qwen,gemini, oragent--working-directory <path>: directory where the agent command runs--prompt <text>and--system-prompt <text>: user and system prompts--prompt-file <path>: read prompt input from a file for stdin-based tools--model <name>: tool-specific model alias or full model name--read-onlyor--plan-only: enforce native planning/no-write mode when supported--approve-each(alias--permission-mode ask): approve each command, relaying native permission prompts as normalized NDJSON (supported foragentandclaude)--tool-executable <path>: override the native executable for any supported tool--tool-env <KEY=VALUE>: add an environment variable to the native tool process, repeatable--tool-arg <arg>: append a raw native tool argument, repeatable--skip-default-safety-flags: suppress default autonomous safety bypass flags, including Qwen/Gemini--yolo--isolation <mode>:none,screen, ordocker--dry-run: print the command without executing it
Claude-specific options include --append-system-prompt, --fallback-model, --session-id, --fork-session, --verbose, and --replay-user-messages.
Library
use ;
async
result.metadata is a normalized summary for all six supported tools. It includes success and error classification, session ID, usage-limit reset details, result summary, cost estimates, stream token usage, optional model usage, and sub-agent call summaries. result.usage exposes the aggregated stream token usage as JSON for parity with the JavaScript package.
For large generated prompts, set prompt_file or let the controller create a temporary prompt file automatically for claude, codex, opencode, agent, qwen, and gemini.
For parity with fast-moving native CLIs, set raw executable, environment, and argument overrides for any supported tool on AgentOptions:
let mut controller = agent?;
Completion, usage and cancellation
For direct JSON runs, metadata.success requires native completion and a successful process exit. Empty or unfinished streams return incomplete_stream; Agent's default NDJSON mode uses the same check. Protocol parsing and usage use stdout, while plain_output retains stderr diagnostics. Parent session metadata excludes Claude subagent-scoped results.
stop() waits for a direct process to finish. controller.cancel().await? interrupts it and collects the final result. Unix cancellation signals the process group; Windows cancellation signals the immediate child. A native CLI that ignores SIGTERM needs a caller-managed termination policy. Process pipes drain concurrently from start, including when attached output is disabled. Set AGENT_COMMANDER_DEBUG=1 for cancellation diagnostics.
Usage preserves native final totals without adding repeated assistant snapshots. Codex input_tokens now counts fresh input; add cache_read_tokens to recover inclusive input. Serializable usage structs expose cache and reasoning fields, and Qwen/Gemini results now include usage too.
Bundled aliases track a recorded Hive Mind catalogue; exact model IDs pass through for caller-managed live discovery. Omitting model leaves selection to the native CLI. Claude's opus execution alias stays rolling. Common resume works for all six tools; Agent adds --no-fork, and OpenCode uses --session.
See the parity audit for the feature matrix and regression evidence.
Shared Behavior
JavaScript and Rust expose the same core concepts:
- Tool selection and model alias mapping
none,screen, anddockerisolation modes- Dry-run command preview
- JSON/NDJSON output parsing for tools that support it
- Read-only planning mode for tools with enforceable native restrictions
- Per-command approval (ask mode) with a normalized
permission_request/permission_responserelay for tools with a drivable native handshake
See shared concepts for behavior that should stay aligned across both packages, including the per-command approval parity table.
Release Flow
Rust releases use changelog fragments in rust/changelog.d/:
bump: patch
-
The GitHub Release workflow publishes language-specific releases with rust_ tags and [Rust] vX.Y.Z release names.
Test
Interactive Terminal Capture
Use tui::capture_agent_tui when a test needs the client's real terminal
interface rather than its headless JSON mode. The capture drives text, control
keys, and resizes through a PTY, then returns an unrolled transcript, normalized
message/tool-call events, settled frames, and asciicast-compatible replay data.
When artifact_directory is set it also writes a transcript, frame data, a
static snapshot, a cast, and an animated SVG suitable for CI artifacts.
use ;
let capture = capture_agent_tui?;
println!;
# Ok::