diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index cf5f961a62..8794f54af0 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -4450,7 +4450,7 @@ checksum = "3af6b589e163c5a788fab00ce0c0366f6efbb9959c2f9874b224936af7fce7e1" dependencies = [ "base64", "indexmap 2.12.0", - "quick-xml", + "quick-xml 0.38.0", "serde", "time", ] @@ -7093,7 +7093,7 @@ version = "0.31.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c66a47e840dc20793f2264eb4b3e4ecb4b75d91c0dd4af04b456128e0bdd449d" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "rustix 1.0.8", "wayland-backend", "wayland-scanner", @@ -7105,7 +7105,7 @@ version = "0.32.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efa790ed75fbfd71283bd2521a1cfdc022aabcc28bdcff00851f9e4ae88d9901" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "wayland-backend", "wayland-client", "wayland-scanner", @@ -7117,7 +7117,7 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efd94963ed43cf9938a090ca4f7da58eb55325ec8200c3848963e98dc25b78ec" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.10.0", "wayland-backend", "wayland-client", "wayland-protocols", @@ -7726,7 +7726,7 @@ dependencies = [ "os_pipe", "rustix 0.38.44", "tempfile", - "thiserror 2.0.16", + "thiserror 2.0.17", "tree_magic_mini", "wayland-backend", "wayland-client", diff --git a/codex-rs/core/src/bash.rs b/codex-rs/core/src/bash.rs index e85ce76a5e..7232cacd1b 100644 --- a/codex-rs/core/src/bash.rs +++ b/codex-rs/core/src/bash.rs @@ -120,6 +120,20 @@ pub fn parse_shell_lc_plain_commands(command: &[String]) -> Option Option { + let tree = try_parse_shell(command)?; + let commands = try_parse_word_only_commands_sequence(&tree, command)?; + let [command_words] = commands.as_slice() else { + return None; + }; + let (_, script) = extract_bash_command(command_words)?; + Some(script.to_string()) +} + fn parse_plain_command_from_node(cmd: tree_sitter::Node, src: &str) -> Option> { if cmd.kind() != "command" { return None; @@ -258,4 +272,19 @@ mod tests { let parsed = parse_shell_lc_plain_commands(&command).unwrap(); assert_eq!(parsed, vec![vec!["ls".to_string()]]); } + + #[test] + fn extracts_bash_lc_script_from_string() { + let script = extract_shell_lc_script_from_str(r#"bash -lc "ls -a""#).unwrap(); + assert_eq!(script, "ls -a"); + + let zsh_script = extract_shell_lc_script_from_str("zsh -lc 'echo hello && pwd'").unwrap(); + assert_eq!(zsh_script, "echo hello && pwd"); + } + + #[test] + fn ignores_non_bash_lc_strings() { + assert!(extract_shell_lc_script_from_str("echo hi").is_none()); + assert!(extract_shell_lc_script_from_str("bash -lc 'ls'; echo hi").is_none()); + } } diff --git a/codex-rs/core/src/features.rs b/codex-rs/core/src/features.rs index e34128f362..8a399d6552 100644 --- a/codex-rs/core/src/features.rs +++ b/codex-rs/core/src/features.rs @@ -29,6 +29,9 @@ pub enum Stage { pub enum Feature { /// Use the single unified PTY-backed exec tool. UnifiedExec, + /// Use the shell command tool that takes `command` as a single string of + /// shell instead of an array of args passed to `execvp(3)`. + ShellCommandTool, /// Enable experimental RMCP features such as OAuth login. RmcpClient, /// Include the freeform apply_patch tool. @@ -250,6 +253,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::Experimental, default_enabled: false, }, + FeatureSpec { + id: Feature::ShellCommandTool, + key: "shell_command_tool", + stage: Stage::Experimental, + default_enabled: false, + }, FeatureSpec { id: Feature::RmcpClient, key: "rmcp_client", diff --git a/codex-rs/core/src/shell.rs b/codex-rs/core/src/shell.rs index 197c00bad2..4a8c6f2a51 100644 --- a/codex-rs/core/src/shell.rs +++ b/codex-rs/core/src/shell.rs @@ -41,6 +41,28 @@ impl Shell { Shell::Unknown => None, } } + + pub fn derive_shell_invocation(&self, command: &str) -> Vec { + match self { + Shell::Zsh(zsh) => vec![ + zsh.shell_path.clone(), + "-lc".to_string(), + command.to_string(), + ], + Shell::Bash(bash) => vec![ + bash.shell_path.clone(), + "-lc".to_string(), + command.to_string(), + ], + Shell::PowerShell(ps) => vec![ + ps.exe.clone(), + "-NoProfile".to_string(), + "-Command".to_string(), + command.to_string(), + ], + Shell::Unknown => shlex::split(command).unwrap_or_else(|| vec![command.to_string()]), + } + } } #[cfg(unix)] diff --git a/codex-rs/core/src/tasks/user_shell.rs b/codex-rs/core/src/tasks/user_shell.rs index 28a84f23f3..3e09c921ce 100644 --- a/codex-rs/core/src/tasks/user_shell.rs +++ b/codex-rs/core/src/tasks/user_shell.rs @@ -63,27 +63,7 @@ impl SessionTask for UserShellCommandTask { // Execute the user's script under their default shell when known; this // allows commands that use shell features (pipes, &&, redirects, etc.). // We do not source rc files or otherwise reformat the script. - let shell_invocation = match session.user_shell() { - crate::shell::Shell::Zsh(zsh) => vec![ - zsh.shell_path.clone(), - "-lc".to_string(), - self.command.clone(), - ], - crate::shell::Shell::Bash(bash) => vec![ - bash.shell_path.clone(), - "-lc".to_string(), - self.command.clone(), - ], - crate::shell::Shell::PowerShell(ps) => vec![ - ps.exe.clone(), - "-NoProfile".to_string(), - "-Command".to_string(), - self.command.clone(), - ], - crate::shell::Shell::Unknown => { - shlex::split(&self.command).unwrap_or_else(|| vec![self.command.clone()]) - } - }; + let shell_invocation = session.user_shell().derive_shell_invocation(&self.command); let call_id = Uuid::new_v4().to_string(); let raw_command = self.command.clone(); diff --git a/codex-rs/core/src/tools/handlers/mod.rs b/codex-rs/core/src/tools/handlers/mod.rs index 187b444414..dcf848e376 100644 --- a/codex-rs/core/src/tools/handlers/mod.rs +++ b/codex-rs/core/src/tools/handlers/mod.rs @@ -19,6 +19,7 @@ pub use mcp::McpHandler; pub use mcp_resource::McpResourceHandler; pub use plan::PlanHandler; pub use read_file::ReadFileHandler; +pub use shell::ShellCommandHandler; pub use shell::ShellHandler; pub use test_sync::TestSyncHandler; pub use unified_exec::UnifiedExecHandler; diff --git a/codex-rs/core/src/tools/handlers/shell.rs b/codex-rs/core/src/tools/handlers/shell.rs index b97242a9a3..21e063019e 100644 --- a/codex-rs/core/src/tools/handlers/shell.rs +++ b/codex-rs/core/src/tools/handlers/shell.rs @@ -1,14 +1,17 @@ use async_trait::async_trait; +use codex_protocol::models::ShellCommandToolCallParams; use codex_protocol::models::ShellToolCallParams; use std::sync::Arc; use crate::apply_patch; use crate::apply_patch::InternalApplyPatchInvocation; use crate::apply_patch::convert_apply_patch_to_protocol; +use crate::bash::extract_shell_lc_script_from_str; use crate::codex::TurnContext; use crate::exec::ExecParams; use crate::exec_env::create_env; use crate::function_tool::FunctionCallError; +use crate::shell::Shell; use crate::tools::context::ToolInvocation; use crate::tools::context::ToolOutput; use crate::tools::context::ToolPayload; @@ -25,6 +28,8 @@ use crate::tools::sandboxing::ToolCtx; pub struct ShellHandler; +pub struct ShellCommandHandler; + impl ShellHandler { fn to_exec_params(params: ShellToolCallParams, turn_context: &TurnContext) -> ExecParams { ExecParams { @@ -39,6 +44,38 @@ impl ShellHandler { } } +impl ShellCommandHandler { + fn to_exec_params( + params: ShellCommandToolCallParams, + session: &crate::codex::Session, + turn_context: &TurnContext, + ) -> ExecParams { + let shell = session.user_shell(); + let script = script_for_shell(shell, params.command.as_str()); + + ExecParams { + command: shell.derive_shell_invocation(&script), + cwd: turn_context.resolve_path(params.workdir.clone()), + timeout_ms: params.timeout_ms, + env: create_env(&turn_context.shell_environment_policy), + with_escalated_permissions: params.with_escalated_permissions, + justification: params.justification, + arg0: None, + } + } +} + +/// If `raw_command` is of the form ` -lc "