Skip to main content

vtcode_core/tools/pty/
command_utils.rs

1use std::path::Path;
2
3/// Check if a command uses cargo, which requires exclusive file lock access
4pub fn is_cargo_command(program: &str) -> bool {
5    let name = Path::new(program)
6        .file_name()
7        .and_then(|value| value.to_str())
8        .unwrap_or(program)
9        .to_ascii_lowercase();
10    name == "cargo"
11}
12
13/// Check if a command string (potentially passed via shell -c) is a cargo command
14pub fn is_cargo_command_string(command: &str) -> bool {
15    let trimmed = command.trim();
16    trimmed.starts_with("cargo ") || trimmed == "cargo"
17}
18
19/// Commands that use lock files and need serialization.
20/// These commands access lock files (Cargo.lock, package-lock.json, etc.)
21/// and can cause "blocking waiting for file lock" errors if run concurrently.
22const LOCKFILE_COMMANDS: &[&str] = &[
23    "cargo",    // Cargo.lock
24    "npm",      // package-lock.json
25    "pnpm",     // pnpm-lock.yaml
26    "yarn",     // yarn.lock
27    "bun",      // bun.lockb
28    "bunx",     // bun global cache lock
29    "go",       // go.sum
30    "gradle",   // gradle.lockfile
31    "mvn",      // uses local repo locks
32    "pip",      // pip can have concurrent install issues
33    "pip3",     // pip3 same as pip
34    "poetry",   // poetry.lock
35    "composer", // composer.lock (PHP)
36    "bundler",  // Gemfile.lock (Ruby)
37    "bundle",   // Gemfile.lock (Ruby)
38];
39
40fn normalize_program_name(program: &str) -> String {
41    Path::new(program)
42        .file_name()
43        .and_then(|value| value.to_str())
44        .unwrap_or(program)
45        .to_ascii_lowercase()
46}
47
48/// Check if a command requires serialization due to lock file access.
49/// These commands should not be run concurrently within the same workspace
50/// to prevent lock file contention errors.
51pub fn is_lockfile_command(program: &str) -> bool {
52    let name = normalize_program_name(program);
53    LOCKFILE_COMMANDS.contains(&name.as_str())
54}
55
56/// Check if a command should be serialized because it is long-running.
57pub fn is_long_running_command(program: &str) -> bool {
58    let name = normalize_program_name(program);
59    is_development_toolchain_command(&name) || is_lockfile_command(&name)
60}
61
62/// Check if a command string (potentially passed via shell -c) is a lockfile command
63pub fn is_lockfile_command_string(command: &str) -> bool {
64    let trimmed = command.trim();
65    LOCKFILE_COMMANDS
66        .iter()
67        .any(|&cmd| trimmed.starts_with(&format!("{cmd} ")) || trimmed == cmd)
68}
69
70/// Check if a command string (potentially passed via shell -c) is long-running
71pub fn is_long_running_command_string(command: &str) -> bool {
72    let trimmed = command.trim();
73    if trimmed.is_empty() {
74        return false;
75    }
76    if is_lockfile_command_string(trimmed) {
77        return true;
78    }
79    let first = trimmed.split_whitespace().next().unwrap_or_default();
80    is_development_toolchain_command(first)
81}
82
83pub(super) fn is_shell_program(program: &str) -> bool {
84    let name = Path::new(program)
85        .file_name()
86        .and_then(|value| value.to_str())
87        .unwrap_or(program)
88        .to_ascii_lowercase();
89    matches!(
90        name.as_str(),
91        "bash"
92            | "sh"
93            | "zsh"
94            | "fish"
95            | "dash"
96            | "ash"
97            | "busybox"
98            | "ksh"
99            | "ksh93"
100            | "mksh"
101            | "tcsh"
102            | "csh"
103            | "posh"
104            | "yash"
105            | "oksh"
106            | "loksh"
107            | "oil"
108            | "osh"
109            | "nu"
110            | "nushell"
111            | "elvish"
112            | "pwsh"
113    )
114}
115
116pub(super) fn is_sandbox_wrapper_program(program: &str, args: &[String]) -> bool {
117    let name = Path::new(program)
118        .file_name()
119        .and_then(|value| value.to_str())
120        .unwrap_or(program)
121        .to_ascii_lowercase();
122    if name == "sandbox-exec" {
123        return true;
124    }
125
126    args.iter().any(|arg| {
127        matches!(
128            arg.as_str(),
129            "--sandbox-policy" | "--sandbox-policy-cwd" | "--seccomp-profile" | "--resource-limits"
130        )
131    })
132}
133
134// Note: resolve_fallback_shell moved to tools::shell module
135
136/// Resolve program path - if program doesn't exist in PATH, return None to signal shell fallback.
137/// This allows the shell to find programs installed in user-specific directories.
138pub fn is_development_toolchain_command(program: &str) -> bool {
139    let name = Path::new(program)
140        .file_name()
141        .and_then(|value| value.to_str())
142        .unwrap_or(program)
143        .to_ascii_lowercase();
144    matches!(
145        name.as_str(),
146        "cargo"
147            | "rustc"
148            | "rustup"
149            | "rustfmt"
150            | "clippy"
151            | "npm"
152            | "node"
153            | "yarn"
154            | "pnpm"
155            | "bun"
156            | "bunx"
157            | "go"
158            | "python"
159            | "python3"
160            | "pip"
161            | "pip3"
162            | "java"
163            | "javac"
164            | "mvn"
165            | "gradle"
166            | "make"
167            | "cmake"
168            | "gcc"
169            | "g++"
170            | "clang"
171            | "clang++"
172            | "which"
173    )
174}
175
176#[cfg(test)]
177mod tests {
178    use super::*;
179
180    #[test]
181    fn test_is_cargo_command() {
182        assert!(is_cargo_command("cargo"));
183        assert!(is_cargo_command("Cargo"));
184        assert!(is_cargo_command("/usr/bin/cargo"));
185        assert!(is_cargo_command("~/.cargo/bin/cargo"));
186        assert!(!is_cargo_command("rustc"));
187        assert!(!is_cargo_command("npm"));
188        assert!(!is_cargo_command("cargo-watch"));
189    }
190
191    #[test]
192    fn test_is_cargo_command_string() {
193        assert!(is_cargo_command_string("cargo check"));
194        assert!(is_cargo_command_string("cargo test --lib"));
195        assert!(is_cargo_command_string("cargo build --release"));
196        assert!(is_cargo_command_string("  cargo clippy  "));
197        assert!(is_cargo_command_string("cargo"));
198        assert!(!is_cargo_command_string("rustc --version"));
199        assert!(!is_cargo_command_string("npm install"));
200        assert!(!is_cargo_command_string("echo cargo"));
201    }
202
203    #[test]
204    fn test_is_long_running_command() {
205        assert!(is_long_running_command("cargo"));
206        assert!(is_long_running_command("cmake"));
207        assert!(is_long_running_command("composer"));
208        assert!(is_long_running_command("bundle"));
209        assert!(is_long_running_command("poetry"));
210        assert!(!is_long_running_command("rg"));
211    }
212
213    #[test]
214    fn test_is_long_running_command_string() {
215        assert!(is_long_running_command_string("cargo test"));
216        assert!(is_long_running_command_string(" cmake --build ."));
217        assert!(is_long_running_command_string("composer install"));
218        assert!(is_long_running_command_string("bundle exec rake"));
219        assert!(is_long_running_command_string("poetry install"));
220        assert!(!is_long_running_command_string("rg \"fn main\" ."));
221    }
222
223    #[test]
224    fn test_is_sandbox_wrapper_program() {
225        assert!(is_sandbox_wrapper_program("sandbox-exec", &[]));
226        assert!(is_sandbox_wrapper_program("vtcode-linux-sandbox", &["--sandbox-policy".to_string()]));
227        assert!(!is_sandbox_wrapper_program("bash", &["-lc".to_string(), "ls".to_string()]));
228    }
229}