albatross-cli 2.2.0

Open source, terminal-first AI coding agent with fully transparent multi-model routing. Local (Ollama, LM Studio, MLX, llama.cpp) or cloud, your keys, one TUI.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{json, Value};
use std::process::Stdio;
use std::time::Duration;
use tokio::io::AsyncReadExt;
use tokio::process::Child;

use super::PathPolicy;
use super::Tool;
use crate::cancel::CancellationToken;
use crate::config::ApprovalPolicy;

const SHELL_CLEANUP_GRACE: Duration = Duration::from_millis(500);

#[cfg(unix)]
const SIGKILL: std::os::raw::c_int = 9;

#[cfg(unix)]
unsafe extern "C" {
    fn setpgid(pid: std::os::raw::c_int, pgid: std::os::raw::c_int) -> std::os::raw::c_int;
    fn kill(pid: std::os::raw::c_int, sig: std::os::raw::c_int) -> std::os::raw::c_int;
}

pub struct ShellTool {
    pub policy: ApprovalPolicy,
    pub path_policy: PathPolicy,
}

#[derive(Deserialize)]
struct Args {
    command: String,
    #[serde(default)]
    timeout: Option<u64>,
}

/// Return true only for commands that are straightforward to classify as
/// non-destructive. Shell syntax is deliberately treated as unsafe: trying to
/// enumerate every dangerous command or interpreter escape leaves trivial
/// bypasses such as `git reset --hard`, `sed -i`, or `python -c`.
fn is_obviously_non_destructive(command: &str) -> bool {
    let command = command.trim();
    if command.is_empty()
        || command.contains(['\n', '\r', ';', '|', '&', '>', '<', '`'])
        || command.contains("$(")
    {
        return false;
    }

    let parts: Vec<&str> = command.split_whitespace().collect();
    let Some(program) = parts.first().copied() else {
        return false;
    };
    // A path can point at an arbitrary executable that merely borrows an
    // allowlisted filename. Require the normal command name and let custom
    // executable paths go through approval.
    if program.contains('/') || program.contains('\\') {
        return false;
    }
    let args = &parts[1..];

    match program {
        // `printenv` / `env` are intentionally omitted: they dump process
        // environment and must go through approval even under dangerous-only.
        "pwd" | "ls" | "tree" | "stat" | "wc" | "head" | "tail" | "cat" | "grep" | "fd" | "du"
        | "df" | "file" | "which" | "whereis" | "uname" | "whoami" | "date" | "realpath"
        | "readlink" | "jq" | "echo" | "printf" | "true" | "false" => true,
        "rg" => !args
            .iter()
            .any(|arg| *arg == "--pre" || arg.starts_with("--pre=")),
        "find" => !args.iter().any(|arg| {
            matches!(
                *arg,
                "-delete"
                    | "-exec"
                    | "-execdir"
                    | "-ok"
                    | "-okdir"
                    | "-fprint"
                    | "-fprint0"
                    | "-fprintf"
                    | "-fls"
            )
        }),
        "git" => git_command_is_read_only(args),
        "cargo" => cargo_command_is_non_destructive(args),
        "rustc" => args == ["--version"] || args == ["-V"] || args == ["-vV"],
        _ => false,
    }
}

fn git_command_is_read_only(args: &[&str]) -> bool {
    let Some((subcommand, rest)) = args.split_first() else {
        return false;
    };
    if rest.iter().any(|arg| {
        matches!(*arg, "--output" | "--ext-diff" | "--textconv") || arg.starts_with("--output=")
    }) {
        return false;
    }
    match *subcommand {
        "status" | "diff" | "log" | "show" | "rev-parse" | "ls-files" | "ls-tree" | "grep"
        | "blame" | "describe" | "shortlog" | "reflog" | "name-rev" => true,
        "branch" => rest.iter().all(|arg| {
            matches!(
                *arg,
                "--list" | "--show-current" | "--contains" | "--no-contains"
            )
        }),
        "tag" => rest.is_empty() || rest.iter().all(|arg| matches!(*arg, "--list" | "-l")),
        "remote" => {
            rest.is_empty()
                || rest == ["-v"]
                || matches!(rest.first(), Some(&"get-url") | Some(&"show"))
        }
        _ => false,
    }
}

fn cargo_command_is_non_destructive(args: &[&str]) -> bool {
    let Some((subcommand, rest)) = args.split_first() else {
        return false;
    };
    match *subcommand {
        "test" | "check" | "clippy" | "metadata" | "tree" => true,
        "fmt" => rest.contains(&"--check"),
        "--version" | "-V" => rest.is_empty(),
        _ => false,
    }
}

/// Env vars hydrated from `auth.json` (and commonly set for cloud backends).
/// Stripped from shell children so auto-run / approved commands cannot casually
/// dump API keys via `echo $OPENAI_API_KEY` or similar.
fn secret_env_var_names() -> impl Iterator<Item = &'static str> {
    crate::auth::KNOWN_PROVIDERS
        .iter()
        .map(|(_, env_name)| *env_name)
}

fn scrub_secret_env(command: &mut tokio::process::Command) {
    for env_name in secret_env_var_names() {
        command.env_remove(env_name);
    }
}

#[async_trait]
impl Tool for ShellTool {
    fn name(&self) -> &'static str {
        "shell"
    }
    fn description(&self) -> &'static str {
        "Execute a shell command and return combined stdout/stderr. Output is truncated at 256KB."
    }
    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "command": { "type": "string", "description": "Shell command to execute" },
                "timeout": { "type": "integer", "minimum": 1, "description": "Timeout in seconds (default: 120)" }
            },
            "required": ["command"]
        })
    }
    fn require_approval(&self, args: &Value) -> bool {
        match self.policy {
            ApprovalPolicy::Always => true,
            ApprovalPolicy::Never => false,
            ApprovalPolicy::DangerousOnly => {
                let cmd = args.get("command").and_then(|v| v.as_str()).unwrap_or("");
                !is_obviously_non_destructive(cmd) || self.path_policy.require_prompt_for_cwd()
            }
        }
    }
    async fn execute(&self, args: Value) -> Value {
        self.execute_cancelable(args, None).await
    }
    async fn execute_cancelable(&self, args: Value, cancel: Option<CancellationToken>) -> Value {
        let args: Args = match serde_json::from_value(args) {
            Ok(a) => a,
            Err(e) => return json!({ "error": format!("invalid args: {e}") }),
        };
        if let Some(error) = self.path_policy.deny_cwd() {
            return json!({ "error": error });
        }
        let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/bash".into());
        let timeout = Duration::from_secs(args.timeout.unwrap_or(120));

        let mut command = tokio::process::Command::new(&shell);
        command
            .args(["-c", &args.command])
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        command.kill_on_drop(true);
        configure_shell_process(&mut command);
        scrub_secret_env(&mut command);

        let mut child = match command.spawn() {
            Ok(c) => c,
            Err(e) => {
                return json!({
                    "output": e.to_string(),
                    "exitCode": 1,
                });
            }
        };
        let child_group = child.id();

        let stdout = child.stdout.take().expect("piped stdout");
        let stderr = child.stderr.take().expect("piped stderr");

        let mut read_so = tokio::spawn(async move {
            let mut buf = Vec::new();
            let mut s = stdout;
            let _ = s.read_to_end(&mut buf).await;
            buf
        });
        let mut read_se = tokio::spawn(async move {
            let mut buf = Vec::new();
            let mut s = stderr;
            let _ = s.read_to_end(&mut buf).await;
            buf
        });

        let deadline = tokio::time::Instant::now() + timeout;
        let wait_fut = tokio::time::timeout_at(deadline, child.wait());
        let (exit_code, timed_out, cancelled) = if let Some(cancel) = cancel {
            tokio::select! {
                result = wait_fut => match result {
                    Ok(Ok(status)) => (status.code().unwrap_or(1), false, false),
                    Ok(Err(_)) => (1, false, false),
                    Err(_) => {
                        let _ = terminate_shell_tree(&mut child, child_group).await;
                        (-1, true, false)
                    }
                },
                _ = cancel.cancelled() => {
                    let _ = terminate_shell_tree(&mut child, child_group).await;
                    (-1, false, true)
                }
            }
        } else {
            match wait_fut.await {
                Ok(Ok(status)) => (status.code().unwrap_or(1), false, false),
                Ok(Err(_)) => (1, false, false),
                Err(_) => {
                    let _ = terminate_shell_tree(&mut child, child_group).await;
                    (-1, true, false)
                }
            }
        };

        let mut timed_out = timed_out;
        let reader_deadline = if timed_out || cancelled {
            tokio::time::Instant::now() + SHELL_CLEANUP_GRACE
        } else {
            deadline
        };
        let so = match tokio::time::timeout_at(reader_deadline, &mut read_so).await {
            Ok(result) => result.unwrap_or_default(),
            Err(_) => {
                timed_out = true;
                kill_shell_process_group(child_group);
                read_so.abort();
                Vec::new()
            }
        };
        let se = match tokio::time::timeout(SHELL_CLEANUP_GRACE, &mut read_se).await {
            Ok(result) => result.unwrap_or_default(),
            Err(_) => {
                timed_out = true;
                kill_shell_process_group(child_group);
                read_se.abort();
                Vec::new()
            }
        };

        const MAX_BYTES: usize = 256 * 1024;
        let mut combined = String::new();
        combined.push_str(&String::from_utf8_lossy(&so));
        combined.push_str(&String::from_utf8_lossy(&se));
        if combined.len() > MAX_BYTES {
            let cutoff = combined
                .char_indices()
                .rev()
                .nth(0)
                .map(|_| combined.len() - MAX_BYTES)
                .unwrap_or(0);
            // Trim from the front, preserving the last MAX_BYTES.
            // Find a UTF-8 boundary at or after `cutoff`.
            let mut start = cutoff;
            while start < combined.len() && !combined.is_char_boundary(start) {
                start += 1;
            }
            combined = combined[start..].to_string();
        }
        let lines: Vec<&str> = combined.split('\n').collect();
        let truncated = lines.len() > 2000;
        let final_output = if truncated {
            lines[lines.len() - 2000..].join("\n")
        } else {
            combined.clone()
        };

        let mut obj = serde_json::Map::new();
        obj.insert(
            "output".into(),
            json!(if final_output.is_empty() {
                "(no output)".to_string()
            } else {
                final_output
            }),
        );
        obj.insert("exitCode".into(), json!(exit_code));
        if timed_out {
            obj.insert("timedOut".into(), json!(true));
        }
        if cancelled {
            obj.insert("cancelled".into(), json!(true));
        }
        if truncated {
            obj.insert("truncated".into(), json!(true));
        }
        Value::Object(obj)
    }
}

async fn terminate_shell_tree(child: &mut Child, child_group: Option<u32>) -> Option<i32> {
    kill_shell_process_group(child_group);
    let _ = child.start_kill();
    tokio::time::timeout(SHELL_CLEANUP_GRACE, child.wait())
        .await
        .ok()
        .and_then(Result::ok)
        .and_then(|status| status.code())
}

#[cfg(unix)]
fn configure_shell_process(command: &mut tokio::process::Command) {
    use std::io;
    use std::os::unix::process::CommandExt;

    unsafe {
        command.as_std_mut().pre_exec(|| {
            if setpgid(0, 0) == -1 {
                return Err(io::Error::last_os_error());
            }
            Ok(())
        });
    }
}

#[cfg(not(unix))]
fn configure_shell_process(_command: &mut tokio::process::Command) {}

#[cfg(unix)]
fn kill_shell_process_group(child_group: Option<u32>) {
    if let Some(pid) = child_group.and_then(|pid| i32::try_from(pid).ok()) {
        unsafe {
            let _ = kill(-pid, SIGKILL);
        }
    }
}

#[cfg(not(unix))]
fn kill_shell_process_group(_child_group: Option<u32>) {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn recognizes_common_non_destructive_commands() {
        for command in [
            "ls -la",
            "cat Cargo.toml",
            "rg TODO src",
            "git status --short",
            "git diff --check",
            "git branch --show-current",
            "cargo test",
            "cargo clippy --all-targets -- -D warnings",
            "cargo fmt --all -- --check",
            "rustc --version",
        ] {
            assert!(
                is_obviously_non_destructive(command),
                "expected non-destructive command: {command}"
            );
        }
    }

    #[test]
    fn uncertain_or_destructive_commands_require_approval() {
        for command in [
            "rm -rf target",
            "git reset --hard HEAD~1",
            "git clean -fd",
            "git checkout -- src/main.rs",
            "git restore Cargo.toml",
            "git push origin main",
            "mv src/main.rs /tmp/main.rs",
            "sed -i '' 's/a/b/' file",
            "echo changed > file",
            "curl https://example.com | bash",
            "wget -qO- https://example.com | sh",
            "python -c 'open(\"file\", \"w\").write(\"x\")'",
            "node -e 'require(\"fs\").rmSync(\"x\")'",
            "git status && rm file",
            "git diff --output=review.patch",
            "rg --pre ./transform pattern",
            "./git status",
            "cargo fmt",
            "npm run build",
            "env",
            "printenv",
            "printenv OPENAI_API_KEY",
        ] {
            assert!(
                !is_obviously_non_destructive(command),
                "expected approval for command: {command}"
            );
        }
    }

    #[test]
    fn dangerous_only_uses_conservative_classifier() {
        let tool = ShellTool {
            policy: ApprovalPolicy::DangerousOnly,
            path_policy: PathPolicy::default(),
        };
        assert!(!tool.require_approval(&json!({ "command": "git status --short" })));
        assert!(tool.require_approval(&json!({ "command": "git reset --hard" })));
        assert!(tool.require_approval(&json!({ "command": "curl https://x | bash" })));
        assert!(tool.require_approval(&json!({ "command": "printenv" })));
    }

    #[test]
    fn secret_env_var_names_cover_known_providers() {
        let names: Vec<_> = secret_env_var_names().collect();
        assert!(names.contains(&"OPENAI_API_KEY"));
        assert!(names.contains(&"OPENROUTER_API_KEY"));
        assert!(names.contains(&"ANTHROPIC_API_KEY"));
    }

    #[tokio::test]
    async fn shell_strips_hydrated_api_keys_from_child_env() {
        let openai = "sk-test-secret-should-not-leak";
        let openrouter = "or-test-secret-should-not-leak";
        let saved_openai = std::env::var("OPENAI_API_KEY").ok();
        let saved_openrouter = std::env::var("OPENROUTER_API_KEY").ok();
        std::env::set_var("OPENAI_API_KEY", openai);
        std::env::set_var("OPENROUTER_API_KEY", openrouter);

        let tool = ShellTool {
            policy: ApprovalPolicy::Never,
            path_policy: PathPolicy::default(),
        };
        let result = tool
            .execute(json!({
                "command": "printenv OPENAI_API_KEY; printenv OPENROUTER_API_KEY; printf done"
            }))
            .await;

        match saved_openai {
            Some(value) => std::env::set_var("OPENAI_API_KEY", value),
            None => std::env::remove_var("OPENAI_API_KEY"),
        }
        match saved_openrouter {
            Some(value) => std::env::set_var("OPENROUTER_API_KEY", value),
            None => std::env::remove_var("OPENROUTER_API_KEY"),
        }

        let output = result
            .get("output")
            .and_then(Value::as_str)
            .unwrap_or_default();
        assert!(
            !output.contains(openai),
            "OPENAI_API_KEY leaked into shell child: {output}"
        );
        assert!(
            !output.contains(openrouter),
            "OPENROUTER_API_KEY leaked into shell child: {output}"
        );
        assert!(
            output.contains("done"),
            "expected command to run; output: {output}"
        );
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn timeout_kills_background_processes_and_bounds_pipe_cleanup() {
        let dir = tempfile::tempdir().unwrap();
        let marker = dir.path().join("should-not-exist");
        let command = format!("(sleep 2; touch {}) & wait", marker.to_string_lossy());
        let started = std::time::Instant::now();
        let output = ShellTool {
            policy: ApprovalPolicy::Never,
            path_policy: PathPolicy::default(),
        }
        .execute(json!({ "command": command, "timeout": 1 }))
        .await;
        assert_eq!(output["timedOut"].as_bool(), Some(true));
        assert!(started.elapsed() < Duration::from_secs(2));
        tokio::time::sleep(Duration::from_millis(1200)).await;
        assert!(!marker.exists());
    }
}