vallum 0.6.1

Security boundary between AI coding agents and your shell — redacts secrets, neutralizes prompt injection, sanitizes untrusted terminal output, audits every command.
Documentation
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
//! Per-command output optimizers: the `CommandOptimizer` trait and dispatch registry.

pub mod cargo;
pub mod docker;
pub mod file_list;
pub mod git_diff;
pub mod git_log;
pub mod git_status;
pub mod go_test;
pub mod grep;
pub mod kubectl;
pub mod make;
pub mod npm;
pub mod pytest;
pub mod terraform;

use std::sync::OnceLock;

pub trait CommandOptimizer {
    fn name(&self) -> &'static str;
    fn matches(&self, cmd: &str, args: &[String]) -> bool;
    fn optimize(&self, input: &str) -> Option<String>;
}

fn registry() -> &'static [Box<dyn CommandOptimizer + Send + Sync>] {
    static REG: OnceLock<Vec<Box<dyn CommandOptimizer + Send + Sync>>> = OnceLock::new();
    REG.get_or_init(|| {
        vec![
            Box::new(pytest::PytestOptimizer),
            Box::new(npm::NpmOptimizer),
            Box::new(cargo::CargoOptimizer),
            Box::new(git_status::GitStatusOptimizer),
            Box::new(git_diff::GitDiffOptimizer),
            Box::new(git_log::GitLogOptimizer),
            Box::new(docker::DockerOptimizer),
            Box::new(go_test::GoTestOptimizer),
            Box::new(make::MakeOptimizer),
            Box::new(kubectl::KubectlOptimizer),
            Box::new(terraform::TerraformOptimizer),
            Box::new(grep::GrepOptimizer),
            Box::new(file_list::FileListOptimizer),
        ]
    })
}

/// Names of all registered optimizers. Used by `vallum doctor` to validate
/// `[optimizer] disabled` entries against real optimizer names.
pub fn names() -> Vec<&'static str> {
    registry().iter().map(|o| o.name()).collect()
}

/// Shell metacharacters that make a `bash -c` script unsafe to word-split.
/// Any hit means we bail and match the invocation as-is (today's behavior).
const SHELL_METACHARACTERS: &[char] = &[
    '|', '&', ';', '<', '>', '(', ')', '$', '`', '\\', '"', '\'', '*', '?', '[', ']', '{', '}',
    '~', '#',
];

/// The Claude Code hook rewrites every Bash call to `bash -c '<original>'`,
/// which would otherwise hide the real command from optimizer matching.
/// Unwraps only the trivial case: exactly `["-c", script]` with no shell
/// metacharacters in the script.
fn unwrap_shell_invocation(cmd: &str, args: &[String]) -> Option<(String, Vec<String>)> {
    if !matches!(cmd, "bash" | "sh" | "zsh") {
        return None;
    }
    if args.len() != 2 || args[0] != "-c" {
        return None;
    }
    let script = &args[1];
    if script.chars().any(|c| SHELL_METACHARACTERS.contains(&c)) {
        return None;
    }
    let mut words = script.split_whitespace();
    let inner_cmd = words.next()?.to_string();
    let inner_args: Vec<String> = words.map(str::to_string).collect();
    Some((inner_cmd, inner_args))
}

pub fn dispatch(
    cmd: &str,
    args: &[String],
    input: &str,
    disabled: &[String],
) -> Option<(String, &'static str)> {
    let unwrapped = unwrap_shell_invocation(cmd, args);
    let (cmd, args) = match &unwrapped {
        Some((c, a)) => (c.as_str(), a.as_slice()),
        None => (cmd, args),
    };
    for opt in registry() {
        if disabled.iter().any(|d| d == opt.name()) {
            continue;
        }
        if opt.matches(cmd, args) {
            if let Some(out) = opt.optimize(input) {
                return Some((out, opt.name()));
            }
        }
    }
    None
}

/// Collapse maximal runs of "noise" lines (3 or more consecutive lines for which
/// `is_noise` returns true) into a single `[N <label> hidden]` marker, keeping
/// all other lines verbatim. Returns `None` if the input has fewer than
/// `min_lines` lines or nothing was collapsed (so callers pass through).
pub(crate) fn collapse_noise_runs(
    input: &str,
    min_lines: usize,
    is_noise: impl Fn(&str) -> bool,
    label: &str,
) -> Option<String> {
    let lines: Vec<&str> = input.lines().collect();
    if lines.len() < min_lines {
        return None;
    }

    let mut out = String::new();
    let mut collapsed_any = false;
    let mut i = 0;
    while i < lines.len() {
        if is_noise(lines[i]) {
            let start = i;
            while i < lines.len() && is_noise(lines[i]) {
                i += 1;
            }
            let run = i - start;
            if run >= 3 {
                out.push_str(&format!("[{} {} hidden]\n", run, label));
                collapsed_any = true;
            } else {
                for l in &lines[start..i] {
                    out.push_str(l);
                    out.push('\n');
                }
            }
        } else {
            out.push_str(lines[i]);
            out.push('\n');
            i += 1;
        }
    }

    if !collapsed_any {
        return None;
    }
    out.push_str("[summarized by vallum]\n");
    Some(out)
}

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

    struct AlwaysMatch;
    impl CommandOptimizer for AlwaysMatch {
        fn name(&self) -> &'static str {
            "always_match"
        }
        fn matches(&self, _cmd: &str, _args: &[String]) -> bool {
            true
        }
        fn optimize(&self, input: &str) -> Option<String> {
            Some(format!("OPT:{}", input))
        }
    }

    #[test]
    fn dispatch_matches_git_status_when_input_is_long() {
        let mut files = String::new();
        for i in 0..40 {
            files.push_str(&format!("\tmodified:   src/file_{}.rs\n", i));
        }
        let input = format!(
            "On branch main\nYour branch is up to date with 'origin/main'.\n\nChanges to be committed:\n{}\n",
            files
        );
        let result = dispatch("git", &["status".to_string()], &input, &[]);
        assert!(result.is_some());
        let (out, name) = result.unwrap();
        assert_eq!(name, "git_status");
        assert!(out.contains("[summarized by vallum]"));
    }

    #[test]
    fn dispatch_matches_cargo_test_when_output_is_long() {
        let input = concat!(
            "   Compiling dep_a v0.1.0\n",
            "   Compiling dep_b v0.1.0\n",
            "   Compiling dep_c v0.1.0\n",
            "   Compiling vallum v0.2.0 (/tmp/vallum)\n",
            "    Finished `test` profile [unoptimized + debuginfo] target(s) in 1.23s\n",
            "     Running unittests src/main.rs (target/debug/deps/vallum-abc)\n",
            "\n",
            "running 30 tests\n",
            "test alpha ... ok\n",
            "test beta ... ok\n",
            "test gamma ... ok\n",
            "test delta ... ok\n",
            "test result: ok. 30 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s\n"
        );

        let result = dispatch("cargo", &["test".to_string()], input, &[]);
        assert!(result.is_some());
        let (out, name) = result.unwrap();
        assert_eq!(name, "cargo");
        assert!(out.contains("Compiling: 4 crates hidden"));
        assert!(out.contains("test result: ok."));
        assert!(out.contains("[summarized by vallum]"));
    }

    #[test]
    fn dispatch_matches_pytest_when_output_is_long() {
        let input = concat!(
            "============================= test session starts =============================\n",
            "platform darwin -- Python 3.11.0, pytest-8.0.0, pluggy-1.0.0\n",
            "rootdir: /tmp/app\n",
            "plugins: anyio-4.0.0\n",
            "collected 12 items\n",
            "\n",
            "tests/test_alpha.py ........\n",
            "tests/test_beta.py ...F.\n",
            "\n",
            "=================================== FAILURES ===================================\n",
            "______________________________ test_example ___________________________________\n",
            "E   assert 1 == 2\n",
            "\n",
            "=========================== short test summary info ============================\n",
            "FAILED tests/test_beta.py::test_example - assert 1 == 2\n",
            "========================= 1 failed, 11 passed in 0.50s =========================\n"
        );

        let result = dispatch("pytest", &[], input, &[]);
        assert!(result.is_some());
        let (out, name) = result.unwrap();
        assert_eq!(name, "pytest");
        assert!(out.contains("collected 12 items"));
        assert!(out.contains("Progress: 2 lines hidden"));
        assert!(out.contains("1 failed, 11 passed"));
        assert!(out.contains("[summarized by vallum]"));
    }

    #[test]
    fn dispatch_matches_npm_test_when_output_is_long() {
        let input = concat!(
            "> app@1.0.0 test\n",
            "> jest\n",
            "\n",
            "PASS src/a.test.js\n",
            "PASS src/b.test.js\n",
            "PASS src/c.test.js\n",
            "PASS src/d.test.js\n",
            "PASS src/e.test.js\n",
            "\n",
            "Test Suites: 5 passed, 5 total\n",
            "Tests:       42 passed, 42 total\n",
            "Snapshots:   0 total\n",
            "Time:        1.234 s\n",
            "Ran all test suites.\n"
        );

        let result = dispatch("npm", &["test".to_string()], input, &[]);
        assert!(result.is_some());
        let (out, name) = result.unwrap();
        assert_eq!(name, "npm");
        assert!(out.contains("PASS: 5 lines hidden"));
        assert!(out.contains("Test Suites: 5 passed, 5 total"));
        assert!(out.contains("[summarized by vallum]"));
    }

    #[test]
    fn dispatch_matches_rg_output() {
        let mut input = String::new();
        for i in 0..35 {
            input.push_str(&format!("src/alpha.rs:{}:line\n", i + 1));
        }
        let result = dispatch("rg", &["line".to_string()], &input, &[]);
        assert!(result.is_some());
        assert_eq!(result.unwrap().1, "grep");
    }

    #[test]
    fn dispatch_returns_none_for_unknown_command() {
        let result = dispatch("ls", &[], "foo", &[]);
        assert!(result.is_none());
    }

    #[test]
    fn dispatch_skips_disabled_optimizer() {
        let mut files = String::new();
        for i in 0..40 {
            files.push_str(&format!("\tmodified:   src/file_{}.rs\n", i));
        }
        let input = format!(
            "On branch main\nYour branch is up to date with 'origin/main'.\n\nChanges to be committed:\n{}\n",
            files
        );
        let disabled = vec!["git_status".to_string()];
        let result = dispatch("git", &["status".to_string()], &input, &disabled);
        assert!(result.is_none(), "disabled optimizer must be skipped");
    }

    #[test]
    fn trait_dispatch_returns_name_and_output() {
        let opt = AlwaysMatch;
        assert!(opt.matches("anything", &[]));
        assert_eq!(opt.optimize("x").unwrap(), "OPT:x");
        assert_eq!(opt.name(), "always_match");
    }

    #[test]
    fn collapse_noise_runs_collapses_and_keeps_signal() {
        let input = "keep A\nnoise\nnoise\nnoise\nnoise\nkeep B\nnoise\nkeep C\n";
        let out = collapse_noise_runs(input, 4, |l| l == "noise", "noise lines").unwrap();
        assert!(out.contains("keep A"));
        assert!(out.contains("keep B"));
        assert!(out.contains("keep C"));
        assert!(out.contains("[4 noise lines hidden]"));
        // A run shorter than 3 is NOT collapsed.
        assert!(out.matches("noise").count() >= 1); // the single "noise" before "keep C" stays
        assert!(out.contains("[summarized by vallum]"));
    }

    #[test]
    fn collapse_noise_runs_passthrough_when_nothing_collapsed() {
        // No run of >=3 noise lines -> None (pass-through).
        let input = "a\nnoise\nb\nnoise\nc\n";
        assert!(collapse_noise_runs(input, 1, |l| l == "noise", "x").is_none());
    }

    #[test]
    fn collapse_noise_runs_passthrough_when_too_small() {
        let input = "noise\nnoise\nnoise\n";
        assert!(collapse_noise_runs(input, 10, |l| l == "noise", "x").is_none());
    }

    #[test]
    fn unwrap_shell_invocation_shapes() {
        let ok = unwrap_shell_invocation("bash", &["-c".into(), "git status".into()]);
        assert_eq!(ok, Some(("git".to_string(), vec!["status".to_string()])));
        assert!(unwrap_shell_invocation("sh", &["-c".into(), "cargo test".into()]).is_some());
        assert!(unwrap_shell_invocation("zsh", &["-c".into(), "ls -la".into()]).is_some());
        // Wrong shapes must not unwrap.
        assert!(unwrap_shell_invocation("bash", &["-c".into()]).is_none());
        assert!(unwrap_shell_invocation("bash", &["-lc".into(), "git status".into()]).is_none());
        assert!(unwrap_shell_invocation(
            "bash",
            &["-c".into(), "git status".into(), "extra".into()]
        )
        .is_none());
        assert!(unwrap_shell_invocation("python", &["-c".into(), "print(1)".into()]).is_none());
        assert!(unwrap_shell_invocation("bash", &["-c".into(), "".into()]).is_none());
    }

    #[test]
    fn dispatch_unwraps_bash_c_simple_command() {
        let mut files = String::new();
        for i in 0..40 {
            files.push_str(&format!("\tmodified:   src/file_{}.rs\n", i));
        }
        let input = format!(
            "On branch main\nYour branch is up to date with 'origin/main'.\n\nChanges to be committed:\n{}\n",
            files
        );
        let args = vec!["-c".to_string(), "git status".to_string()];
        let result = dispatch("bash", &args, &input, &[]);
        assert!(
            result.is_some(),
            "bash -c 'git status' must reach git_status"
        );
        assert_eq!(result.unwrap().1, "git_status");
    }

    #[test]
    fn dispatch_unwraps_bash_c_find_to_file_list() {
        let mut input = String::new();
        for i in 0..60 {
            input.push_str(&format!("./src/file_{:02}.rs\n", i));
        }
        let args = vec!["-c".to_string(), "find . -type f".to_string()];
        let result = dispatch("bash", &args, &input, &[]);
        assert!(result.is_some());
        assert_eq!(result.unwrap().1, "file_list");
    }

    #[test]
    fn dispatch_does_not_unwrap_metacharacter_scripts() {
        for script in [
            "git status | head",
            "echo 'hi'",
            "ls *.rs",
            "a; b",
            "x > y",
            "echo $HOME",
            "cd ~/src",
        ] {
            let args = vec!["-c".to_string(), script.to_string()];
            assert!(
                dispatch("bash", &args, "irrelevant", &[]).is_none(),
                "{script} must not unwrap"
            );
        }
    }

    use proptest::prelude::*;

    proptest! {
        #[test]
        fn prop_collapse_noise_runs_does_not_panic(
            s in "[\\s\\S]{0,500}",
            min_lines in 0usize..50,
        ) {
            let _ = collapse_noise_runs(&s, min_lines, |line| line.is_empty(), "noise");
        }

        #[test]
        fn prop_collapse_noise_runs_line_count_bounded(
            s in "[\\s\\S]{0,500}",
            min_lines in 0usize..50,
        ) {
            if let Some(out) = collapse_noise_runs(&s, min_lines, |line| line.is_empty(), "noise") {
                let in_lines = s.lines().count();
                let out_lines = out.lines().count();
                // A collapse can only equal or reduce the line count, plus one
                // trailing "[summarized by vallum]" marker.
                prop_assert!(out_lines <= in_lines + 2);
            }
        }
    }
}