amont-agent 2.32.0

A guard that inspects a shell command before Claude Code runs it
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! `symbol-grep-not-lsp` — a text search for a symbol, over code LSP serves.
//!
//! ```sh
//! grep -n "fn lease_is_stale" -A25 crates/relais/src/admission/mod.rs
//! grep -rn "EMAIL_PX" ../ --include=*.ts -l
//! rg -t rust agent_types
//! ```
//!
//! Measured with `amont-agent backtest` (2026-10-11): 46.8 to 59.0 searches
//! of this shape per 1,000 Bash calls over the last four weeks, and 4.5 per
//! 1,000 tool calls through the Grep tool in the last one. A text search for
//! a symbol finds strings, comments and same-named symbols alike, and
//! returns lines without what they mean; `workspaceSymbol` and
//! `findReferences` return the definition and its uses.
//!
//! `examine` reads the shape: `grep`, `egrep`, `rg` or `git grep`, not fed
//! from a pipe, with an identifier-shaped pattern ([`symbol`]) and a code
//! target — a code file operand, an include or type naming code, or a
//! recursive search with no file operand that is not code. A pipe filter
//! (`cmd | grep foo`), a count (`-c`), an inverted match (`-v`), a
//! case-insensitive one (`-i`), patterns from a file (`-f`), several
//! patterns (`-e a -e b`) or a `$VAR` path are text work or unknowable, and
//! stay silent. `confirm` asks [`crate::lsp::serves`] about the target. The
//! Grep tool is judged by the hook with [`symbol`] and [`grep_tool_targets_code`].

use crate::rules::{Confirmed, Context, Evidence, Examine, Finding, Rule, Stance, Trend};
use crate::shell::{Parsed, Simple, Word};

pub const RULE: Rule = Rule {
    id: "symbol-grep-not-lsp",
    default_stance: Stance::Advise,
    max_stance: Stance::Deny,
    evidence: Evidence {
        // `amont-agent backtest`, per 1,000 Bash calls, weeks from 09-07:
        // 25.4, 46.8, 55.9, 59.2, 54.4. Hand-labelled samples of 50 hits:
        // 34 real symbol lookups before the shape test (lowercase words,
        // `-i`, `$` paths), 44 after. The Grep tool (4.5 per 1,000 tool calls
        // in W41) is not replayed; fixtures check it.
        per_1000: 54.4,
        measured: "2026-10-11",
        trend: Trend::Flat(5),
    },
    examine: Examine::Legacy(examine),
    confirm: Some(confirm),
};

/// Comment markers: text, whatever their shape.
const STOP: &[&str] = &["TODO", "FIXME", "XXX", "HACK", "NOTE"];

/// Declaration keywords a symbol search may start with.
const KEYWORDS: &[&str] = &[
    "pub fn",
    "fn",
    "def",
    "func",
    "class",
    "struct",
    "impl",
    "interface",
    "type",
    "const",
    "let",
    "enum",
    "trait",
];

/// An identifier of three characters or more, optionally behind one
/// declaration keyword and inside word boundaries: `agent_types`,
/// `\bFileOp\b`, `fn lease_is_stale`, `def parse(`. Anything with regex
/// alternation, a class, a path or a space elsewhere is text.
///
/// Without a keyword the word must look like a symbol — an uppercase letter
/// or an underscore. In a hand-labelled sample of 50 hits (2026-10-11), the
/// lowercase words (`sandbox`, `derive`, `inline`, `intent`) were nearly all
/// searches for text; the camelCase, PascalCase and snake_case ones were
/// symbols.
pub fn symbol(pattern: &str) -> bool {
    let mut p = pattern.trim();
    for open in ["\\b", "\\<", "^"] {
        p = p.strip_prefix(open).unwrap_or(p);
    }
    for close in ["\\b", "\\>", "\\(", "("] {
        p = p.strip_suffix(close).unwrap_or(p);
    }
    let mut declared = false;
    for kw in KEYWORDS {
        if let Some(rest) = p.strip_prefix(kw).and_then(|r| r.strip_prefix(' ')) {
            p = rest;
            declared = true;
            break;
        }
    }
    let shaped = declared || p.contains('_') || p.chars().any(|c| c.is_ascii_uppercase());
    let mut chars = p.chars();
    let starts = chars
        .next()
        .is_some_and(|c| c.is_ascii_alphabetic() || c == '_');
    starts
        && p.len() >= 3
        && p.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
        && shaped
        && !STOP.contains(&p)
}

/// Where a search looks, as far as the command says.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Target {
    /// A code file operand.
    File(String),
    /// A directory operand, or the cwd when `None`.
    Dir(Option<String>),
}

/// A symbol search, read from one clause.
#[derive(Debug)]
pub struct Search<'a> {
    pub pattern: &'a str,
    pub target: Target,
    /// `git -C <dir>`: where the search runs, before any operand.
    pub run_in: Option<&'a str>,
}

/// Flags that take the next word (or the rest of a short cluster) as a value.
fn takes_value(program: &str, flag: &str) -> bool {
    let grep: &[&str] = &[
        "-A",
        "-B",
        "-C",
        "-e",
        "-f",
        "-m",
        "-d",
        "-D",
        "--include",
        "--exclude",
        "--exclude-dir",
        "--max-count",
        "--regexp",
        "--file",
        "--context",
        "--after-context",
        "--before-context",
        "--color",
        "--colour",
        "--label",
    ];
    let rg: &[&str] = &[
        "-e",
        "-f",
        "-g",
        "-t",
        "-T",
        "-A",
        "-B",
        "-C",
        "-m",
        "-j",
        "-M",
        "--type",
        "--type-not",
        "--type-add",
        "--glob",
        "--iglob",
        "--max-count",
        "--regexp",
        "--file",
        "--context",
        "--after-context",
        "--before-context",
        "--threads",
        "--max-columns",
        "--color",
        "--colors",
        "--sort",
        "--sortr",
        "--max-depth",
        "--max-filesize",
    ];
    let git: &[&str] = &[
        "-A",
        "-B",
        "-C",
        "-e",
        "-f",
        "-m",
        "--max-count",
        "--context",
        "--after-context",
        "--before-context",
        "--max-depth",
        "--threads",
        "--color",
        "--open-files-in-pager",
    ];
    match program {
        "rg" => rg.contains(&flag),
        "git grep" => git.contains(&flag),
        _ => grep.contains(&flag),
    }
}

/// Short flags that may carry their value in the same word (`-A25`, `-trust`).
fn short_with_value(program: &str) -> &'static [char] {
    match program {
        "rg" => &['e', 'f', 'g', 't', 'T', 'A', 'B', 'C', 'm', 'j', 'M'],
        _ => &['A', 'B', 'C', 'e', 'f', 'm', 'd', 'D'],
    }
}

/// One clause read as a symbol search, or `None`.
pub fn search(cmd: &Simple) -> Option<Search<'_>> {
    if cmd.prev.is_some_and(|c| c.is_pipe()) || cmd.heredoc {
        return None;
    }
    let raw = cmd.program()?;
    let name = raw.rsplit('/').next().unwrap_or(raw);
    let args: &[Word] = cmd.args();
    let (program, args, run_in) = match name {
        "grep" | "egrep" => (name, args, None),
        "rg" => ("rg", args, None),
        "git" => {
            let mut run_in = None;
            let mut i = 0;
            while let Some(w) = args.get(i) {
                match w.text.as_str() {
                    "-C" => {
                        run_in = args.get(i + 1).map(|w| w.text.as_str());
                        i += 2;
                    }
                    "-c" | "--git-dir" | "--work-tree" | "--namespace" => i += 2,
                    "grep" => break,
                    t if t.starts_with('-') => i += 1,
                    _ => return None,
                }
            }
            ("git grep", args.get(i + 1..)?, run_in)
        }
        _ => return None,
    };

    let mut patterns: Vec<&str> = Vec::new();
    let mut selectors: Vec<&str> = Vec::new();
    let mut operands: Vec<&Word> = Vec::new();
    let mut recursive = program != "grep" && program != "egrep";
    let mut silenced = false;
    let mut after_ddash = false;
    let mut i = 0;
    while let Some(w) = args.get(i) {
        i += 1;
        let t = w.text.as_str();
        // A `$VAR` or `$(…)` names something this rule cannot see.
        if w.expanded || t.contains('$') {
            return None;
        }
        // A word whose quotes start after its leading dash is still a flag:
        // `--include='*.{ts,tsx}'`.
        let flag = t.starts_with('-') && t != "-" && (!w.quoted || w.raw.starts_with('-'));
        if after_ddash || !flag {
            operands.push(w);
            continue;
        }
        if t == "--" {
            after_ddash = true;
            continue;
        }
        if let Some(long) = t.strip_prefix("--") {
            let (flag, inline) = match long.split_once('=') {
                Some((f, v)) => (format!("--{f}"), Some(v)),
                None => (t.to_string(), None),
            };
            let value = if takes_value(program, &flag) {
                match inline {
                    Some(v) => Some(v),
                    None => {
                        i += 1;
                        args.get(i - 1).map(|w| w.text.as_str())
                    }
                }
            } else {
                None
            };
            match flag.as_str() {
                "--regexp" => patterns.extend(value),
                "--file" | "--count" | "--invert-match" | "--count-matches" | "--ignore-case" => {
                    silenced = true
                }
                "--include" | "--glob" | "--iglob" | "--type" => selectors.extend(value),
                "--recursive" | "--dereference-recursive" => recursive = true,
                _ => {}
            }
            continue;
        }
        // A short cluster: `-rn`, `-A25`, `-e pat`, `-trust`.
        let cluster = &t[1..];
        for (k, c) in cluster.char_indices() {
            if short_with_value(program).contains(&c) {
                let rest = &cluster[k + c.len_utf8()..];
                let value = if rest.is_empty() {
                    i += 1;
                    args.get(i - 1).map(|w| w.text.as_str())
                } else {
                    Some(rest)
                };
                match c {
                    'e' => patterns.extend(value),
                    'f' => silenced = true,
                    'g' | 't' if program == "rg" => selectors.extend(value),
                    _ => {}
                }
                break;
            }
            match c {
                'v' | 'c' | 'i' => silenced = true,
                'r' | 'R' => recursive = true,
                _ => {}
            }
        }
    }
    if silenced || patterns.len() > 1 {
        return None;
    }
    let mut operands = operands.into_iter();
    let pattern = match patterns.first() {
        Some(p) => *p,
        None => operands.next()?.text.as_str(),
    };
    if !symbol(pattern) {
        return None;
    }
    let paths: Vec<&str> = operands.map(|w| w.text.as_str()).collect();
    if let Some(code) = paths.iter().find(|p| crate::lsp::is_code(p)) {
        return Some(Search {
            pattern,
            target: Target::File((*code).to_string()),
            run_in,
        });
    }
    let dir = paths.first().map(|p| (*p).to_string());
    if !selectors.is_empty() {
        return selectors
            .iter()
            .any(|s| crate::lsp::names_code(s))
            .then_some(Search {
                pattern,
                target: Target::Dir(dir),
                run_in,
            });
    }
    // A plain grep with no path reads stdin; one with only file operands
    // that are not code searches text.
    if !recursive || paths.iter().any(|p| file_shaped(p)) {
        return None;
    }
    Some(Search {
        pattern,
        target: Target::Dir(dir),
        run_in,
    })
}

/// A last component with an extension: a file, not a directory.
fn file_shaped(path: &str) -> bool {
    let name = path
        .trim_end_matches('/')
        .rsplit('/')
        .next()
        .unwrap_or(path);
    if name.chars().all(|c| c == '.') {
        return false;
    }
    name.get(1..).is_some_and(|rest| rest.contains('.'))
}

fn examine(parsed: &Parsed) -> Option<Finding> {
    parsed.judgeable().find_map(|cmd| {
        let s = search(cmd)?;
        let (reason, remedy) = phrase(s.pattern);
        Some(Finding {
            reason,
            remedy,
            span: cmd.at..cmd.end,
        })
    })
}

/// The search the finding was made from, read again from the parse.
pub fn search_at(parsed: &Parsed, at: usize) -> Option<Search<'_>> {
    parsed.judgeable().find(|c| c.at == at).and_then(search)
}

fn confirm(ctx: &Context, f: &Finding) -> Confirmed {
    let Some(s) = search_at(ctx.parsed, f.span.start) else {
        return Confirmed::No("the command no longer matches");
    };
    let mut base = ctx.cwd_at(f.span.start);
    if let Some(dir) = s.run_in {
        base = base.join(expand_home(dir));
    }
    let target = match &s.target {
        Target::File(p) | Target::Dir(Some(p)) => base.join(expand_home(p)),
        Target::Dir(None) => base,
    };
    match crate::lsp::serves(&target) {
        Ok(()) => Confirmed::Yes,
        Err(why) => Confirmed::No(why),
    }
}

fn expand_home(p: &str) -> std::path::PathBuf {
    match (p.strip_prefix("~/"), std::env::var_os("HOME")) {
        (Some(rest), Some(home)) => std::path::PathBuf::from(home).join(rest),
        _ => std::path::PathBuf::from(p),
    }
}

/// What a Grep tool call's `path` names, as the filesystem says: a path's
/// spelling does not tell a file from a directory (`C:\repo\.`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GrepScope {
    /// A regular file, by its file name.
    File(String),
    /// A directory, or the cwd when `path` is empty.
    Dir,
    /// Nothing is there: a mistyped path searches nothing.
    Missing,
}

/// A Grep tool call that searches code: a code file, or a directory with a
/// glob or type naming code, or with neither.
pub fn grep_tool_targets_code(scope: &GrepScope, glob: Option<&str>, kind: Option<&str>) -> bool {
    match scope {
        GrepScope::File(name) => crate::lsp::is_code(name),
        GrepScope::Missing => false,
        GrepScope::Dir => {
            let selectors: Vec<&str> = glob.into_iter().chain(kind).collect();
            selectors.is_empty() || selectors.iter().any(|s| crate::lsp::names_code(s))
        }
    }
}

/// What the hook says: the mechanism, then the way through.
pub fn phrase(pattern: &str) -> (String, String) {
    (
        format!(
            "`{pattern}` is a symbol: a text search finds strings, comments and same-named \
             symbols alike, and returns lines without what they mean."
        ),
        "Ask LSP: workspaceSymbol for the definition; findReferences or incomingCalls for \
         its uses. Keep grep for text LSP cannot see: strings, comments, config, logs."
            .to_string(),
    )
}

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

    fn fires(command: &str) -> bool {
        examine(&lex(command)).is_some()
    }

    fn target(command: &str) -> Target {
        let parsed = lex(command);
        let cmd = parsed.judgeable().next().expect("a clause");
        search(cmd).expect("a search").target
    }

    #[test]
    fn symbols_are_identifiers() {
        for p in [
            "agent_types",
            "\\bFileOp\\b",
            "fn lease_is_stale",
            "def parse(",
            "pub fn run",
            "EMAIL_PX",
        ] {
            assert!(symbol(p), "{p}");
        }
        for p in [
            "TODO",
            "FIXME",
            "sandbox",
            "derive",
            "ab",
            "foo\\|bar",
            "a.b",
            "fn .*",
            "some words",
            "^pub mod",
            "x-y",
            "[A-Z]+",
            "",
        ] {
            assert!(!symbol(p), "{p}");
        }
    }

    #[test]
    fn a_symbol_search_over_code_fires() {
        assert!(fires(
            "grep -n \"fn lease_is_stale\" -A25 crates/relais/src/admission/mod.rs"
        ));
        assert!(fires("grep -rn agent_types src/"));
        assert!(fires("grep -rn EMAIL_PX ../ --include=*.ts -l"));
        assert!(fires(
            "grep -rn EMAIL_PX ../ --include '*.ts' --exclude-dir node_modules"
        ));
        assert!(fires("rg agent_types"));
        assert!(fires("rg -t rust agent_types crates"));
        assert!(fires("rg -g '*.rs' FileOp"));
        assert!(fires("git grep -n FileOp"));
        assert!(fires("git -C ../relais grep -n FileOp -- crates"));
        assert!(fires("cd /x && grep -n parse_args src/main.go | head"));
        assert!(fires("grep -e FileOp -r src"));
        assert!(
            fires("grep -rn FileOp --include='*.{ts,tsx}' ."),
            "a quoted include"
        );
        assert!(fires("grep -rn FileOp .."), "a parent directory");
    }

    #[test]
    fn text_work_is_silent() {
        assert!(!fires("cargo test | grep FileOp"), "a pipe filter");
        assert!(!fires("grep FileOp"), "stdin");
        assert!(!fires("grep -c FileOp src/hook.rs"), "a count");
        assert!(!fires("grep -rv FileOp src"), "inverted");
        assert!(!fires("grep -rn -f pats.txt src"), "patterns from a file");
        assert!(!fires("grep -rn -e FileOp -e Bash src"), "several patterns");
        assert!(!fires("grep -rn TODO src"), "a comment marker");
        assert!(!fires("grep -n FileOp notes.md"), "not code");
        assert!(
            !fires("grep -rn FileOp docs/rules.md CHANGELOG.md"),
            "text files"
        );
        assert!(!fires("grep -rn 'foo\\|bar' src"), "alternation");
        assert!(!fires("rg -t md FileOp"), "a type that is not code");
        assert!(
            !fires("grep -rn FileOp . --include='*.md'"),
            "an include that is not code"
        );
        assert!(!fires("grep -n \"$X\" src/a.rs"), "unknowable");
        assert!(!fires("git log --grep FileOp"), "not git grep");
        assert!(!fires("grep -rn sandbox src"), "a plain lowercase word");
        assert!(!fires("grep -rni FileOp src"), "case-insensitive");
        assert!(
            !fires("grep -rln FileOp $R/src"),
            "a path behind a variable"
        );
    }

    #[test]
    fn the_target_is_the_code_file_or_the_directory() {
        assert_eq!(
            target("grep -n FileOp src/payload.rs"),
            Target::File("src/payload.rs".into())
        );
        assert_eq!(
            target("grep -rn FileOp src"),
            Target::Dir(Some("src".into()))
        );
        assert_eq!(target("rg FileOp"), Target::Dir(None));
        let parsed = lex("git -C ../relais grep FileOp");
        let s = search(parsed.judgeable().next().expect("clause")).expect("search");
        assert_eq!(s.run_in, Some("../relais"));
    }

    #[test]
    fn the_grep_tool_targets_code_by_file_glob_or_type() {
        let file = |n: &str| GrepScope::File(n.to_string());
        assert!(grep_tool_targets_code(&file("hook.rs"), None, None));
        assert!(!grep_tool_targets_code(&file("x.yaml"), None, None));
        assert!(grep_tool_targets_code(&GrepScope::Dir, None, None));
        assert!(grep_tool_targets_code(
            &GrepScope::Dir,
            Some("**/*.{ts,tsx}"),
            None
        ));
        assert!(grep_tool_targets_code(&GrepScope::Dir, None, Some("rust")));
        assert!(!grep_tool_targets_code(
            &GrepScope::Dir,
            Some("**/*macro*"),
            None
        ));
        assert!(!grep_tool_targets_code(&GrepScope::Dir, None, Some("yaml")));
        assert!(!grep_tool_targets_code(
            &GrepScope::Missing,
            None,
            Some("rust")
        ));
    }

    #[test]
    fn the_advice_stays_under_sixty_words() {
        let (reason, remedy) = phrase("lease_is_stale");
        let words = reason.split_whitespace().count() + remedy.split_whitespace().count();
        assert!(words <= 60, "{words} words");
    }
}