llmman 0.1.689

Run any agent on any model, models stored as OCI images
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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! The Claude Code `PreToolUse` hook behind `launch claude --optimizer tk`.
//!
//! For a Bash call that is one plain command with a filter in this tree, the
//! hook answers with `updatedInput`: the same command line behind
//! `<llmman> tk `. Only text is added in front, so quoting survives. For
//! anything else, and on any error, it says nothing and exits 0.
//!
//! Claude Code checks permission rules against the rewritten command, so:
//!
//! - A program is left alone when a deny or ask rule in a settings file
//!   could apply to it, or when a settings file cannot be read.
//! - The hook never approves a command itself, except that an allow rule the
//!   user wrote carries over, and so do plain `git status|diff|log`, which
//!   Claude Code runs unprompted.
//!
//! Rules from managed policy, MDM or `--disallowedTools` are not visible.

use std::io::Read;
use std::path::{Path, PathBuf};

use serde_json::{json, Value};

use super::shell::{quote, simple_words, trim};

/// The most of the hook's input that is read.
const MAX_INPUT: u64 = 1 << 20;
/// A command longer than this (in bytes), before or after the rewrite, is
/// left alone: Claude Code always asks about one (2.1.214 on), and an
/// `allow` from here would skip that.
const MAX_COMMAND: usize = 10_000;

/// What `--settings` is given to register the hook: one `PreToolUse` entry
/// for the Bash tool that runs `<exe> tk hook`.
pub(crate) fn settings_json(exe: &str) -> String {
    json!({
        "hooks": {
            "PreToolUse": [{
                "matcher": "Bash",
                "hooks": [{
                    "type": "command",
                    "command": format!("{} tk hook", quote(exe)),
                    "timeout": 10
                }]
            }]
        }
    })
    .to_string()
}

/// `llmman tk hook`: reads the hook input on stdin, and prints the answer
/// when there is one. Never fails and never panics out.
pub fn run() {
    serve(|input, exe| {
        answer(input, exe, &|cwd| load_rules(&settings_files(cwd)?)).map(|a| a + "\n")
    });
}

/// `llmman tk compact`: reads `{"command", "output", "exitCode"}` on stdin
/// and prints the output compacted, or nothing. For an agent that checks
/// permissions on the command a hook rewrote; reads no settings.
pub fn compact() {
    serve(compact_output);
}

/// Prints what `answer` makes of stdin. Never fails, never panics out, and
/// says nothing when `LLMMAN_TK` is off.
fn serve(answer: impl FnOnce(&str, &str) -> Option<String> + std::panic::UnwindSafe) {
    std::panic::set_hook(Box::new(|_| {}));
    let _ = std::panic::catch_unwind(|| {
        if super::is_off(std::env::var("LLMMAN_TK").ok().as_deref()) {
            return;
        }
        let mut input = String::new();
        if std::io::stdin()
            .take(MAX_INPUT)
            .read_to_string(&mut input)
            .is_err()
        {
            return;
        }
        let Some(exe) = std::env::current_exe()
            .ok()
            .filter(|p| p.is_file())
            .and_then(|p| p.to_str().map(String::from))
        else {
            return;
        };
        if let Some(out) = answer(&input, &exe) {
            print!("{out}");
        }
    });
}

/// The permission rules of the settings files, by decision.
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct Rules {
    deny: Vec<String>,
    ask: Vec<String>,
    allow: Vec<String>,
}

/// The settings files Claude Code reads rules from: the user's, the
/// project's (shared and local), and the managed policy files. `None` when
/// a managed policy directory exists but cannot be listed.
fn settings_files(cwd: Option<&Path>) -> Option<Vec<PathBuf>> {
    let mut files = Vec::new();
    let user = std::env::var_os("CLAUDE_CONFIG_DIR")
        .map(PathBuf::from)
        .or_else(|| dirs::home_dir().map(|h| h.join(".claude")));
    files.extend(user.map(|d| d.join("settings.json")));
    let mut projects: Vec<PathBuf> = Vec::new();
    projects.extend(std::env::var_os("CLAUDE_PROJECT_DIR").map(PathBuf::from));
    projects.extend(cwd.map(Path::to_path_buf));
    projects.dedup();
    for project in projects {
        files.push(project.join(".claude/settings.json"));
        files.push(project.join(".claude/settings.local.json"));
    }
    for managed in [
        "/etc/claude-code",
        "/Library/Application Support/ClaudeCode",
    ] {
        let managed = Path::new(managed);
        files.push(managed.join("managed-settings.json"));
        files.extend(dropins(&managed.join("managed-settings.d"))?);
    }
    Some(files)
}

/// The `.json` files in `dir`, sorted; none when it does not exist. `None`
/// when it cannot be listed, so the rules are unknown.
fn dropins(dir: &Path) -> Option<Vec<PathBuf>> {
    let entries = match std::fs::read_dir(dir) {
        Ok(entries) => entries,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Some(Vec::new()),
        Err(_) => return None,
    };
    let mut files = Vec::new();
    for entry in entries {
        let path = entry.ok()?.path();
        if path.extension().is_some_and(|x| x == "json") {
            files.push(path);
        }
    }
    files.sort();
    Some(files)
}

/// The rules in `files`, which need not exist. `None` when one that does
/// exist cannot be read or understood: then nothing is known about what
/// Claude Code will enforce.
fn load_rules(files: &[PathBuf]) -> Option<Rules> {
    let mut rules = Rules::default();
    for file in files {
        let text = match std::fs::read_to_string(file) {
            Ok(text) => text,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(_) => return None,
        };
        let doc: Value = serde_json::from_str(&text).ok()?;
        let Some(permissions) = doc.as_object()?.get("permissions") else {
            continue;
        };
        for (key, into) in [
            ("deny", &mut rules.deny),
            ("ask", &mut rules.ask),
            ("allow", &mut rules.allow),
        ] {
            match permissions.get(key) {
                None => {}
                Some(list) => into.extend(
                    list.as_array()?
                        .iter()
                        .filter_map(Value::as_str)
                        .map(String::from),
                ),
            }
        }
    }
    Some(rules)
}

/// A permission rule as `(tool, content)`: `Bash(git push:*)` is
/// `("Bash", Some("git push:*"))`, `Bash` is `("Bash", None)`.
fn split_rule(rule: &str) -> (&str, Option<&str>) {
    match rule.trim().split_once('(') {
        Some((tool, rest)) => (tool.trim(), rest.trim_end().strip_suffix(')')),
        None => (rule.trim(), None),
    }
}

/// The part of a Bash rule before its first space, without a `:*` suffix:
/// the program it is about. A `*` left in it is a wildcard.
fn rule_program(content: &str) -> &str {
    let first = content.split_whitespace().next().unwrap_or("");
    first.trim_end_matches(":*")
}

impl Rules {
    /// Whether a deny or ask rule could apply to `program`, or a rule is
    /// about files in a way a wrapped command could dodge. Deliberately
    /// wider than Claude Code's own matching: a doubt is a yes.
    fn restricts(&self, program: &str) -> bool {
        self.deny.iter().chain(&self.ask).any(|rule| {
            let (tool, content) = split_rule(rule);
            match tool {
                "Bash" => {
                    let Some(content) = content else { return true };
                    let named = rule_program(content);
                    named.is_empty()
                        || named.contains(['*', '=', '?', '['])
                        || super::program_name(named) == program
                }
                "Read" | "Grep" | "Glob" | "Edit" | "Write" | "NotebookEdit" => true,
                _ => false,
            }
        })
    }

    /// Whether an allow rule written for `command` covers it: `Bash`, a
    /// `Bash(<command>)`, or a `Bash(<prefix>:*)` / `Bash(<prefix> *)`
    /// whose prefix ends where a word does.
    fn allows(&self, command: &str) -> bool {
        self.allow.iter().any(|rule| match split_rule(rule) {
            ("Bash", None) => true,
            ("Bash", Some(content)) => {
                let content = content.trim();
                if content == command {
                    return true;
                }
                let prefix = content
                    .strip_suffix(":*")
                    .or_else(|| content.strip_suffix(" *"));
                prefix.is_some_and(|p| {
                    !p.is_empty()
                        && !p.contains('*')
                        && command
                            .strip_prefix(p)
                            .is_some_and(|rest| rest.is_empty() || rest.starts_with([' ', '\t']))
                })
            }
            _ => false,
        })
    }
}

/// `git status`, `diff` and `log` run without a prompt in Claude Code's
/// default mode. Only a command that is that subcommand and arguments that
/// stay inside the working tree counts: no option before the subcommand
/// (`-c` and `-C` can run programs or read elsewhere), and no path that
/// leaves the directory.
fn is_plain_read_only(words: &[String]) -> bool {
    let program = words.first().map(|w| super::program_name(w));
    let sub = words.get(1).map(String::as_str);
    program == Some("git")
        && words[0] == "git"
        && matches!(sub, Some("status" | "diff" | "log"))
        && words[2..].iter().all(|a| {
            !a.starts_with('/')
                && !a.starts_with('~')
                && !a.split('/').any(|part| part == "..")
                && !a.starts_with("--output")
                && !a.starts_with("--ext-diff")
                && !a.starts_with("--open-files-in-pager")
        })
}

/// `raw` trimmed, and its words, if `llmman tk` would filter it.
fn candidate(raw: &str) -> Option<(&str, Vec<String>)> {
    if raw.len() > MAX_COMMAND {
        return None;
    }
    let command = trim(raw);
    let words = simple_words(command)?;
    super::is_filtered(&words).then_some((command, words))
}

/// `command` behind `<exe> tk `, unless that is too long to keep.
fn wrapped(command: &str, exe: &str) -> Option<String> {
    let rewritten = format!("{} tk {command}", quote(exe));
    (rewritten.len() <= MAX_COMMAND).then_some(rewritten)
}

/// Only a command that succeeded is compacted: the streams arrive as one, so
/// a filter cannot tell the error text from the rest. `None` if nothing is saved.
fn compact_output(input: &str, _exe: &str) -> Option<String> {
    let call: Value = serde_json::from_str(input).ok()?;
    if call.get("exitCode")?.as_i64()? != 0 {
        return None;
    }
    let output = call.get("output")?.as_str()?;
    let (_, words) = candidate(call.get("command")?.as_str()?)?;
    let compact = super::render(super::plan(&words), 0, output, "");
    let text = compact.stdout + &compact.stderr;
    (text.len() < output.len()).then_some(text)
}

/// The hook's stdout for `input`, or `None` to say nothing. `rules` reads
/// the permission rules given the call's working directory.
pub(crate) fn answer(
    input: &str,
    exe: &str,
    rules: &dyn Fn(Option<&Path>) -> Option<Rules>,
) -> Option<String> {
    let call: Value = serde_json::from_str(input).ok()?;
    if call
        .get("hook_event_name")
        .is_some_and(|e| e != "PreToolUse")
        || call.get("tool_name")? != "Bash"
    {
        return None;
    }
    let tool_input = call.get("tool_input")?.as_object()?;
    let raw = tool_input.get("command")?.as_str()?;
    let (command, words) = candidate(raw)?;
    let program = super::program_name(&words[0]);
    let cwd = call.get("cwd").and_then(Value::as_str).map(Path::new);
    let rules = rules(cwd)?;
    if rules.restricts(program) {
        return None;
    }
    let rewritten = wrapped(command, exe)?;
    let mut updated = tool_input.clone();
    updated.insert("command".into(), Value::String(rewritten));
    let mut output = json!({
        "hookEventName": "PreToolUse",
        "updatedInput": updated,
    });
    if rules.allows(command) || is_plain_read_only(&words) {
        output["permissionDecision"] = json!("allow");
        output["permissionDecisionReason"] =
            json!("llmman tk: the rewritten command keeps the decision for the original");
    }
    Some(json!({ "hookSpecificOutput": output }).to_string())
}

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

    const EXE: &str = "/opt/llm man/llmman";

    fn call(tool: &str, command: &str) -> String {
        json!({
            "session_id": "s",
            "cwd": "/work/app",
            "hook_event_name": "PreToolUse",
            "tool_name": tool,
            "tool_input": { "command": command, "description": "d", "timeout": 5000 },
        })
        .to_string()
    }

    fn no_rules(_: Option<&Path>) -> Option<Rules> {
        Some(Rules::default())
    }

    fn with_rules(deny: &[&str], ask: &[&str], allow: &[&str]) -> Rules {
        let own = |l: &[&str]| l.iter().map(|s| s.to_string()).collect();
        Rules {
            deny: own(deny),
            ask: own(ask),
            allow: own(allow),
        }
    }

    fn parsed(answer: Option<String>) -> Value {
        serde_json::from_str(&answer.expect("an answer")).unwrap()
    }

    #[test]
    fn a_supported_simple_command_is_wrapped_with_the_original_text_intact() {
        let out = parsed(answer(&call("Bash", "cargo test --lib"), EXE, &no_rules));
        let hook = &out["hookSpecificOutput"];
        assert_eq!(hook["hookEventName"], "PreToolUse");
        assert_eq!(
            hook["updatedInput"]["command"],
            "'/opt/llm man/llmman' tk cargo test --lib"
        );
        // The rest of the input is carried over: updatedInput replaces it all.
        assert_eq!(hook["updatedInput"]["description"], "d");
        assert_eq!(hook["updatedInput"]["timeout"], 5000);
        // Nothing is approved on the user's behalf.
        assert!(hook.get("permissionDecision").is_none());
    }

    fn compacted(command: &str, output: &str, code: i64) -> Option<String> {
        let input = json!({ "command": command, "output": output, "exitCode": code });
        compact_output(&input.to_string(), EXE)
    }

    const STATUS: &str = "On branch main\nYour branch is up to date with 'origin/main'.\n\nChanges not staged for commit:\n  (use \"git add <file>...\" to update what will be committed)\n  (use \"git restore <file>...\" to discard changes in working directory)\n\tmodified:   a.rs\n\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n";

    #[test]
    fn compact_shortens_what_a_filtered_command_printed_and_nothing_else() {
        let text = compacted("git status", STATUS, 0).expect("compacted");
        assert!(text.len() < STATUS.len());
        assert!(text.contains("a.rs"), "{text}");
        assert!(!text.contains("(use \"git add <file>"), "{text}");
        assert_eq!(compacted("echo hi", "hi\n", 0), None);
        assert_eq!(compacted("git status", STATUS, 1), None);
        assert_eq!(compacted("git status | cat", STATUS, 0), None);
        assert_eq!(compact_output("not json", EXE), None);
        assert_eq!(compact_output(r#"{"command":"git status"}"#, EXE), None);
    }

    #[test]
    fn quoting_in_the_original_survives_byte_for_byte() {
        for command in [
            r#"git commit -m "fix: a 'quoted' thing""#,
            r#"git commit -m 'it'\''s'"#,
            r#"grep -rn "a\|b" src"#,
            "git commit -m 'one\ntwo'",
            "git log --author='A B' -n 3",
        ] {
            let out = parsed(answer(&call("Bash", command), EXE, &no_rules));
            let rewritten = out["hookSpecificOutput"]["updatedInput"]["command"]
                .as_str()
                .unwrap()
                .to_string();
            assert_eq!(rewritten, format!("'/opt/llm man/llmman' tk {command}"));
        }
    }

    #[test]
    fn what_cannot_be_wrapped_safely_is_left_alone() {
        for command in [
            "git status | head",
            "git status && git diff",
            "git diff > out.patch",
            "git log $(git rev-parse HEAD)",
            "git commit -F - <<EOF\nmsg\nEOF",
            "FOO=1 cargo test",
            "(cd sub && cargo test)",
            "cd sub; cargo test",
            "echo hi",
            "ls -la",
            "cat Cargo.toml",
            "git rev-parse HEAD",
            "git status --porcelain",
            "git log --oneline",
            "cargo build --message-format=json",
            "rg --json foo",
            "grep -c x f",
            "grep -o x f",
            "git commit",
            "",
        ] {
            assert_eq!(
                answer(&call("Bash", command), EXE, &no_rules),
                None,
                "{command:?}"
            );
        }
    }

    #[test]
    fn an_already_wrapped_command_is_not_wrapped_again() {
        for command in [
            "llmman tk cargo test",
            "'/opt/llm man/llmman' tk cargo test",
            "/usr/bin/llmman tk git status",
        ] {
            assert_eq!(
                answer(&call("Bash", command), EXE, &no_rules),
                None,
                "{command}"
            );
        }
    }

    #[test]
    fn other_tools_and_malformed_input_get_no_answer() {
        assert_eq!(answer(&call("Read", "cargo test"), EXE, &no_rules), None);
        assert_eq!(answer(&call("Edit", "git status"), EXE, &no_rules), None);
        for input in [
            "",
            "not json",
            "[]",
            "{}",
            r#"{"tool_name":"Bash"}"#,
            r#"{"tool_name":"Bash","tool_input":{}}"#,
            r#"{"tool_name":"Bash","tool_input":{"command":7}}"#,
            r#"{"tool_name":"Bash","tool_input":"git status"}"#,
            r#"{"tool_name":"Bash","tool_input":{"command":"git status"},"hook_event_name":"PostToolUse"}"#,
        ] {
            assert_eq!(answer(input, EXE, &no_rules), None, "{input:?}");
        }
        // A command Claude Code always asks about is not touched, nor is one
        // the rewrite would make so.
        let long = format!("git commit -m '{}'", "x".repeat(MAX_COMMAND));
        assert_eq!(answer(&call("Bash", &long), EXE, &no_rules), None);
        let padded = format!("git status{}", " ".repeat(MAX_COMMAND));
        assert_eq!(answer(&call("Bash", &padded), EXE, &no_rules), None);
        let near = format!("git commit -m '{}'", "x".repeat(MAX_COMMAND - 20));
        assert!(near.len() <= MAX_COMMAND);
        assert_eq!(answer(&call("Bash", &near), EXE, &no_rules), None);
    }

    #[test]
    fn a_unicode_space_at_the_end_is_part_of_the_command() {
        let command = "git diff -- notes\u{a0}";
        let out = parsed(answer(&call("Bash", command), EXE, &no_rules));
        assert_eq!(
            out["hookSpecificOutput"]["updatedInput"]["command"],
            format!("'/opt/llm man/llmman' tk {command}")
        );
        // Blanks a shell ignores are not.
        let out = parsed(answer(&call("Bash", "\n git status \n"), EXE, &no_rules));
        assert_eq!(
            out["hookSpecificOutput"]["updatedInput"]["command"],
            "'/opt/llm man/llmman' tk git status"
        );
    }

    #[test]
    fn a_glob_is_left_for_claude_code_to_judge() {
        for command in ["git diff *", "git diff --[o]utput=x", "git log -- src/?.rs"] {
            assert_eq!(
                answer(&call("Bash", command), EXE, &no_rules),
                None,
                "{command}"
            );
        }
        assert!(answer(&call("Bash", "git diff -- '*.rs'"), EXE, &no_rules).is_some());
    }

    #[test]
    fn unreadable_rules_mean_no_rewrite() {
        let none = |_: Option<&Path>| None;
        assert_eq!(answer(&call("Bash", "cargo test"), EXE, &none), None);
    }

    #[test]
    fn a_deny_or_ask_rule_that_could_apply_keeps_the_program_unwrapped() {
        let run = |rules: Rules, command: &str| {
            answer(&call("Bash", command), EXE, &move |_| {
                Some(Rules {
                    deny: rules.deny.clone(),
                    ask: rules.ask.clone(),
                    allow: rules.allow.clone(),
                })
            })
        };
        for (deny, ask) in [
            (&["Bash(git push:*)"][..], &[][..]),
            (&["Bash(git push *)"], &[]),
            (&["Bash(git:*)"], &[]),
            (&["Bash(/usr/bin/git push:*)"], &[]),
            (&[], &["Bash(git commit:*)"]),
            (&["Bash"], &[]),
            (&["Bash(*)"], &[]),
            (&["Bash(*push*)"], &[]),
            (&["Bash(g*)"], &[]),
            (&["Bash(gi* status)"], &[]),
            (&["Bash(git*)"], &[]),
            (&["Bash(FOO=1 git:*)"], &[]),
            (&["Read(./.env)"], &[]),
            (&["Edit(src/**)"], &[]),
        ] {
            assert_eq!(
                run(with_rules(deny, ask, &[]), "git status"),
                None,
                "{deny:?} {ask:?}"
            );
        }
        // A rule about another program, or another tool, does not matter.
        for (deny, ask) in [
            (&["Bash(rm:*)"][..], &[][..]),
            (&["Bash(curl:*)"], &["Bash(npm publish:*)"]),
            (&["WebFetch(domain:example.com)"], &[]),
            (&["mcp__x__y"], &[]),
        ] {
            assert!(
                run(with_rules(deny, ask, &[]), "git status").is_some(),
                "{deny:?} {ask:?}"
            );
        }
    }

    #[test]
    fn an_allow_rule_for_the_original_carries_over_as_an_allow() {
        let decision = |allow: &[&str], command: &str| {
            let allow: Vec<String> = allow.iter().map(|s| s.to_string()).collect();
            let out = parsed(answer(&call("Bash", command), EXE, &move |_| {
                Some(Rules {
                    deny: Vec::new(),
                    ask: Vec::new(),
                    allow: allow.clone(),
                })
            }));
            out["hookSpecificOutput"]
                .get("permissionDecision")
                .map(|d| d.as_str().unwrap().to_string())
        };
        assert_eq!(
            decision(&["Bash(cargo test:*)"], "cargo test --lib"),
            Some("allow".into())
        );
        assert_eq!(
            decision(&["Bash(cargo test *)"], "cargo test"),
            Some("allow".into())
        );
        assert_eq!(
            decision(&["Bash(cargo test)"], "cargo test"),
            Some("allow".into())
        );
        assert_eq!(decision(&["Bash"], "cargo test"), Some("allow".into()));
        // The prefix has to end at a word: `cargo t` does not allow `cargo test`.
        assert_eq!(decision(&["Bash(cargo t:*)"], "cargo test"), None);
        assert_eq!(decision(&["Bash(cargo test)"], "cargo test --lib"), None);
        assert_eq!(decision(&["Bash(go test:*)"], "cargo test"), None);
        assert_eq!(decision(&["Bash(cargo*:*)"], "cargo test"), None);
        assert_eq!(decision(&["Read(x)"], "cargo test"), None);
        assert_eq!(decision(&[], "cargo test"), None);
    }

    #[test]
    fn plain_read_only_git_is_allowed_and_nothing_else_is() {
        let decision = |command: &str| {
            let out = parsed(answer(&call("Bash", command), EXE, &no_rules));
            out["hookSpecificOutput"]
                .get("permissionDecision")
                .is_some()
        };
        for command in [
            "git status",
            "git diff",
            "git diff HEAD~1 -- src/a.rs",
            "git log -n 5",
        ] {
            assert!(decision(command), "{command}");
        }
        for command in [
            "git -C ../other status",
            "git -c core.pager=x diff",
            "git diff ../other/file",
            "git diff -- /etc/passwd",
            "git diff ~/x",
            "git diff --ext-diff",
            "git commit -m x",
            "git push",
            "git pull",
            "cargo test",
            "grep -rn x src",
            "pytest",
            "npm test",
            "docker ps",
        ] {
            assert!(!decision(command), "{command}");
        }
    }

    #[test]
    fn registered_settings_run_this_binary_for_bash_only() {
        let settings: Value = serde_json::from_str(&settings_json(EXE)).unwrap();
        let entry = &settings["hooks"]["PreToolUse"][0];
        assert_eq!(entry["matcher"], "Bash");
        assert_eq!(entry["hooks"][0]["type"], "command");
        assert_eq!(
            entry["hooks"][0]["command"],
            "'/opt/llm man/llmman' tk hook"
        );
        assert_eq!(
            settings.as_object().unwrap().len(),
            1,
            "hooks and nothing else"
        );
    }

    #[test]
    fn rules_are_read_from_every_settings_file_that_exists() {
        let dir = tempfile::tempdir().unwrap();
        let write = |name: &str, body: &str| {
            let p = dir.path().join(name);
            std::fs::write(&p, body).unwrap();
            p
        };
        let user = write(
            "user.json",
            r#"{"permissions":{"deny":["Bash(git push:*)"],"allow":["Bash(cargo test:*)"],"defaultMode":"default"},"model":"x"}"#,
        );
        let project = write(
            "project.json",
            r#"{"permissions":{"ask":["Bash(npm publish:*)"]}}"#,
        );
        let none = write("none.json", r#"{"hooks":{}}"#);
        let missing = dir.path().join("absent.json");
        let rules = load_rules(&[user, project, none, missing.clone()]).unwrap();
        assert_eq!(
            rules,
            with_rules(
                &["Bash(git push:*)"],
                &["Bash(npm publish:*)"],
                &["Bash(cargo test:*)"]
            )
        );
        assert_eq!(load_rules(&[missing]).unwrap(), Rules::default());
    }

    #[test]
    fn a_settings_file_that_cannot_be_understood_means_unknown_rules() {
        let dir = tempfile::tempdir().unwrap();
        for (i, body) in ["{", "[]", r#"{"permissions":{"deny":"Bash"}}"#, ""]
            .iter()
            .enumerate()
        {
            let p = dir.path().join(format!("{i}.json"));
            std::fs::write(&p, body).unwrap();
            assert_eq!(load_rules(&[p]), None, "{body:?}");
        }
        // A directory where a file should be cannot be read either.
        let sub = dir.path().join("sub.json");
        std::fs::create_dir(&sub).unwrap();
        assert_eq!(load_rules(&[sub]), None);
    }

    #[test]
    fn a_managed_directory_that_cannot_be_listed_means_unknown_rules() {
        let dir = tempfile::tempdir().unwrap();
        for name in ["b.json", "a.json", "c.txt"] {
            std::fs::write(dir.path().join(name), "{}").unwrap();
        }
        let listed = dropins(dir.path()).unwrap();
        let names: Vec<_> = listed.iter().map(|p| p.file_name().unwrap()).collect();
        assert_eq!(names, ["a.json", "b.json"]);
        // No directory is no rules; a file where it should be is not that.
        assert_eq!(dropins(&dir.path().join("absent")), Some(Vec::new()));
        assert_eq!(dropins(&dir.path().join("a.json")), None);
    }

    #[test]
    fn the_project_and_user_files_are_among_those_read() {
        let files = settings_files(Some(Path::new("/work/app"))).unwrap();
        assert!(files.contains(&PathBuf::from("/work/app/.claude/settings.json")));
        assert!(files.contains(&PathBuf::from("/work/app/.claude/settings.local.json")));
        assert!(files.contains(&PathBuf::from("/etc/claude-code/managed-settings.json")));
        assert!(files.iter().any(|f| f.ends_with(".claude/settings.json")));
    }
}