diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index 92716cc41b..a6a5e77107 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -1623,6 +1623,26 @@ dependencies = [ "thiserror 2.0.18", ] +[[package]] +name = "codex-command" +version = "0.0.0" +dependencies = [ + "anyhow", + "base64 0.22.1", + "codex-protocol", + "codex-utils-absolute-path", + "once_cell", + "pretty_assertions", + "regex", + "serde", + "serde_json", + "shlex", + "tree-sitter", + "tree-sitter-bash", + "url", + "which", +] + [[package]] name = "codex-common" version = "0.0.0" @@ -1660,6 +1680,7 @@ dependencies = [ "codex-arg0", "codex-async-utils", "codex-client", + "codex-command", "codex-core", "codex-execpolicy", "codex-file-search", @@ -1733,8 +1754,6 @@ dependencies = [ "tracing", "tracing-subscriber", "tracing-test", - "tree-sitter", - "tree-sitter-bash", "url", "uuid", "walkdir", diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index eebc652d47..101023f4a3 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -16,6 +16,7 @@ members = [ "cloud-tasks-client", "cli", "common", + "command", "core", "hooks", "secrets", @@ -82,6 +83,7 @@ codex-chatgpt = { path = "chatgpt" } codex-cli = { path = "cli"} codex-client = { path = "codex-client" } codex-common = { path = "common" } +codex-command = { path = "command" } codex-core = { path = "core" } codex-hooks = { path = "hooks" } codex-secrets = { path = "secrets" } diff --git a/codex-rs/command/BUILD.bazel b/codex-rs/command/BUILD.bazel new file mode 100644 index 0000000000..8662048026 --- /dev/null +++ b/codex-rs/command/BUILD.bazel @@ -0,0 +1,6 @@ +load("//:defs.bzl", "codex_rust_crate") + +codex_rust_crate( + name = "command", + crate_name = "codex_command", +) diff --git a/codex-rs/command/Cargo.toml b/codex-rs/command/Cargo.toml new file mode 100644 index 0000000000..61e3868691 --- /dev/null +++ b/codex-rs/command/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "codex-command" +version.workspace = true +edition.workspace = true +license.workspace = true + +[lints] +workspace = true + +[dependencies] +base64 = { workspace = true } +codex-protocol = { workspace = true } +codex-utils-absolute-path = { workspace = true } +once_cell = { workspace = true } +regex = { workspace = true } +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +shlex = { workspace = true } +tree-sitter = { workspace = true } +tree-sitter-bash = { workspace = true } +url = { workspace = true } +which = { workspace = true } + +[dev-dependencies] +anyhow = { workspace = true } +pretty_assertions = { workspace = true } diff --git a/codex-rs/core/src/bash.rs b/codex-rs/command/src/bash.rs similarity index 99% rename from codex-rs/core/src/bash.rs rename to codex-rs/command/src/bash.rs index bb0ae7fe90..a3286b2252 100644 --- a/codex-rs/core/src/bash.rs +++ b/codex-rs/command/src/bash.rs @@ -5,8 +5,8 @@ use tree_sitter::Parser; use tree_sitter::Tree; use tree_sitter_bash::LANGUAGE as BASH; -use crate::shell::ShellType; -use crate::shell::detect_shell_type; +use crate::shell_detect::ShellType; +use crate::shell_detect::detect_shell_type; /// Parse the provided bash source using tree-sitter-bash, returning a Tree on /// success or None if parsing failed. diff --git a/codex-rs/core/src/command_safety/is_dangerous_command.rs b/codex-rs/command/src/command_safety/is_dangerous_command.rs similarity index 100% rename from codex-rs/core/src/command_safety/is_dangerous_command.rs rename to codex-rs/command/src/command_safety/is_dangerous_command.rs diff --git a/codex-rs/core/src/command_safety/is_safe_command.rs b/codex-rs/command/src/command_safety/is_safe_command.rs similarity index 100% rename from codex-rs/core/src/command_safety/is_safe_command.rs rename to codex-rs/command/src/command_safety/is_safe_command.rs diff --git a/codex-rs/core/src/command_safety/mod.rs b/codex-rs/command/src/command_safety/mod.rs similarity index 100% rename from codex-rs/core/src/command_safety/mod.rs rename to codex-rs/command/src/command_safety/mod.rs diff --git a/codex-rs/core/src/command_safety/powershell_parser.ps1 b/codex-rs/command/src/command_safety/powershell_parser.ps1 similarity index 100% rename from codex-rs/core/src/command_safety/powershell_parser.ps1 rename to codex-rs/command/src/command_safety/powershell_parser.ps1 diff --git a/codex-rs/core/src/command_safety/windows_dangerous_commands.rs b/codex-rs/command/src/command_safety/windows_dangerous_commands.rs similarity index 100% rename from codex-rs/core/src/command_safety/windows_dangerous_commands.rs rename to codex-rs/command/src/command_safety/windows_dangerous_commands.rs diff --git a/codex-rs/core/src/command_safety/windows_safe_commands.rs b/codex-rs/command/src/command_safety/windows_safe_commands.rs similarity index 100% rename from codex-rs/core/src/command_safety/windows_safe_commands.rs rename to codex-rs/command/src/command_safety/windows_safe_commands.rs diff --git a/codex-rs/command/src/lib.rs b/codex-rs/command/src/lib.rs new file mode 100644 index 0000000000..215c30fd7d --- /dev/null +++ b/codex-rs/command/src/lib.rs @@ -0,0 +1,11 @@ +//! Command parsing and safety utilities shared across Codex crates. + +mod shell_detect; + +pub mod bash; +pub mod command_safety; +pub mod parse_command; +pub mod powershell; + +pub use command_safety::is_dangerous_command; +pub use command_safety::is_safe_command; diff --git a/codex-rs/core/src/parse_command.rs b/codex-rs/command/src/parse_command.rs similarity index 100% rename from codex-rs/core/src/parse_command.rs rename to codex-rs/command/src/parse_command.rs diff --git a/codex-rs/core/src/powershell.rs b/codex-rs/command/src/powershell.rs similarity index 92% rename from codex-rs/core/src/powershell.rs rename to codex-rs/command/src/powershell.rs index 148605b4b3..4d3b553333 100644 --- a/codex-rs/core/src/powershell.rs +++ b/codex-rs/command/src/powershell.rs @@ -1,18 +1,16 @@ use std::path::PathBuf; -#[cfg(any(windows, test))] use codex_utils_absolute_path::AbsolutePathBuf; -use crate::shell::ShellType; -use crate::shell::detect_shell_type; +use crate::shell_detect::ShellType; +use crate::shell_detect::detect_shell_type; const POWERSHELL_FLAGS: &[&str] = &["-nologo", "-noprofile", "-command", "-c"]; /// Prefixed command for powershell shell calls to force UTF-8 console output. -pub(crate) const UTF8_OUTPUT_PREFIX: &str = - "[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;\n"; +pub const UTF8_OUTPUT_PREFIX: &str = "[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;\n"; -pub(crate) fn prefix_powershell_script_with_utf8(command: &[String]) -> Vec { +pub fn prefix_powershell_script_with_utf8(command: &[String]) -> Vec { let Some((_, script)) = extract_powershell_command(command) else { return command.to_vec(); }; @@ -84,8 +82,7 @@ pub(crate) fn try_find_powershellish_executable_blocking() -> Option Option { +pub fn try_find_powershell_executable_blocking() -> Option { try_find_powershellish_executable_in_path(&["powershell.exe"]) } @@ -99,8 +96,7 @@ pub(crate) fn try_find_powershell_executable_blocking() -> Option Option { +pub fn try_find_pwsh_executable_blocking() -> Option { if let Some(ps_home) = std::process::Command::new("cmd") .args(["/C", "pwsh", "-NoProfile", "-Command", "$PSHOME"]) .output() @@ -126,7 +122,6 @@ pub(crate) fn try_find_pwsh_executable_blocking() -> Option { try_find_powershellish_executable_in_path(&["pwsh.exe"]) } -#[cfg(any(windows, test))] fn try_find_powershellish_executable_in_path(candidates: &[&str]) -> Option { for candidate in candidates { let Ok(resolved_path) = which::which(candidate) else { @@ -147,7 +142,6 @@ fn try_find_powershellish_executable_in_path(candidates: &[&str]) -> Option bool { // This test works for both powershell.exe and pwsh.exe. std::process::Command::new(powershell_or_pwsh_exe) diff --git a/codex-rs/command/src/shell_detect.rs b/codex-rs/command/src/shell_detect.rs new file mode 100644 index 0000000000..34322a06d0 --- /dev/null +++ b/codex-rs/command/src/shell_detect.rs @@ -0,0 +1,32 @@ +use std::path::Path; +use std::path::PathBuf; + +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +pub(crate) enum ShellType { + Zsh, + Bash, + PowerShell, + Sh, + Cmd, +} + +pub(crate) fn detect_shell_type(shell_path: &PathBuf) -> Option { + match shell_path.as_os_str().to_str() { + Some("zsh") => Some(ShellType::Zsh), + Some("sh") => Some(ShellType::Sh), + Some("cmd") => Some(ShellType::Cmd), + Some("bash") => Some(ShellType::Bash), + Some("pwsh") => Some(ShellType::PowerShell), + Some("powershell") => Some(ShellType::PowerShell), + _ => { + let shell_name = shell_path.file_stem(); + if let Some(shell_name) = shell_name { + let shell_name_path = Path::new(shell_name); + if shell_name_path != Path::new(shell_path) { + return detect_shell_type(&shell_name_path.to_path_buf()); + } + } + None + } + } +} diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index 4142a72d75..2a86148c43 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -33,6 +33,7 @@ codex-app-server-protocol = { workspace = true } codex-apply-patch = { workspace = true } codex-async-utils = { workspace = true } codex-client = { workspace = true } +codex-command = { workspace = true } codex-execpolicy = { workspace = true } codex-file-search = { workspace = true } codex-git = { workspace = true } @@ -106,8 +107,6 @@ tokio-tungstenite = { workspace = true } toml = { workspace = true } toml_edit = { workspace = true } tracing = { workspace = true, features = ["log"] } -tree-sitter = { workspace = true } -tree-sitter-bash = { workspace = true } url = { workspace = true } uuid = { workspace = true, features = ["serde", "v4", "v5"] } which = { workspace = true } diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 7378fe3e6f..cd9ac777b0 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -10,7 +10,6 @@ pub mod api_bridge; mod apply_patch; mod apps; pub mod auth; -pub mod bash; mod client; mod client_common; pub mod codex; @@ -21,7 +20,6 @@ pub use codex_thread::CodexThread; pub use codex_thread::ThreadConfigSnapshot; mod agent; mod codex_delegate; -mod command_safety; pub mod config; pub mod config_loader; pub mod connectors; @@ -51,10 +49,8 @@ mod memories; mod mentions; mod message_history; mod model_provider_info; -pub mod parse_command; pub mod path_utils; pub mod personality_migration; -pub mod powershell; mod proposed_plan_parser; pub mod sandboxing; mod session_prefix; @@ -131,11 +127,14 @@ mod state; mod tasks; mod user_shell_command; pub mod util; +pub use codex_command::bash; +pub use codex_command::is_dangerous_command; +pub use codex_command::is_safe_command; +pub use codex_command::parse_command; +pub use codex_command::powershell; pub use apply_patch::CODEX_APPLY_PATCH_ARG1; pub use client::X_CODEX_TURN_METADATA_HEADER; -pub use command_safety::is_dangerous_command; -pub use command_safety::is_safe_command; pub use exec_policy::ExecPolicyError; pub use exec_policy::check_execpolicy_for_warnings; pub use exec_policy::load_exec_policy;