dirge-agent 0.11.0

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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
//! The registered dirge policy set.
//!
//! Deciders are listed here in precedence order (the order they must
//! be registered in [`super::Engine::new`]); the first to claim a
//! resource sets its base effect:
//!
//! 1. [`PromptDenyPolicy`]   — frontmatter `deny_tools`; terminal Deny, beats Yolo.
//! 2. [`YoloPolicy`]         — `mode == Yolo`; terminal Allow.
//! 3. [`ConfiguredDenyPolicy`] — configured `deny` (last-match), terminal above
//!    session-allow so a session grant can't override it (dirge-ct16). Below
//!    Yolo: Yolo's documented "all rules off" is preserved.
//! 4. [`SessionAllowlistPolicy`] — user "allow always"; terminal Allow.
//! 5. [`ConfiguredRulePolicy`]   — user rules, last-match-wins inside (the
//!    allow/ask cases; a last-match `deny` is pre-empted by `ConfiguredDeny`).
//! 6. [`BuiltinAllowPolicy`]     — read-only ops, memory/skill, dev-null, in-cwd writes.
//! 7. [`ExternalDirPolicy`]      — out-of-cwd paths → external_directory rule or Ask.
//! 8. [`DefaultActionPolicy`]    — the configured default (always claims; terminal).
//!
//! Accept-mode loosening is NOT a decider — it's a post-Stage-A base
//! coercion in `Engine::authorize` (it relaxes a base `Ask`→`Allow`,
//! the one place a mode loosens).
//!
//! Modes are folded into the deciders (not a separate stage): because
//! explicit user rules (ConfiguredRule) outrank the builtin/default
//! deciders, an explicit `allow` survives Restrictive automatically —
//! the provenance question that plagued the old `check`/`check_path`
//! Restrictive logic dissolves into precedence order.
//!
//! The sole Stage-B modifier is [`LoopGuardPolicy`], which only ever
//! tightens (never gates an already-allowed op) and hard-denies a
//! genuine retry loop.

use super::policy::{Decider, Modifier, PolicyCtx};
use super::types::{AccessRequest, Effect, Operation, Refined, Resource, Verdict};
use crate::permission::SecurityMode;
use crate::permission::pattern::Pattern;

/// Which operations a configured rule governs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpMatch {
    Any,
    One(Operation),
}

impl OpMatch {
    fn matches(self, op: Operation) -> bool {
        match self {
            OpMatch::Any => true,
            OpMatch::One(o) => o == op,
        }
    }
}

/// One configured authorization rule: "for this operation class
/// (optionally narrowed to a concrete tool) and resource pattern,
/// apply this effect." The ordered list reads top-to-bottom as the
/// precedence (last match wins within the list).
///
/// `tool` lets the legacy per-tool config (`grep`, `read`, …) map
/// faithfully even though several tools share an [`Operation`] — a
/// `grep` rule narrows to `tool == "grep"` so it doesn't also gate
/// `read`. The breaking op-based config (Phase 4) leaves `tool: None`.
#[derive(Debug, Clone)]
pub struct Rule {
    pub op: OpMatch,
    pub tool: Option<String>,
    pub pattern: Pattern,
    pub effect: Effect,
    pub original: String,
}

impl Rule {
    fn matches(&self, req: &AccessRequest, op: Operation, resource: &Resource) -> bool {
        self.op.matches(op)
            && self.tool.as_deref().is_none_or(|t| t == req.tool)
            && resource
                .match_candidates()
                .iter()
                .any(|k| self.pattern.matches(k))
    }
}

/// The last (top-to-bottom precedence) configured rule that matches this
/// claim, or `None`. "Last-match-wins" is the configured-rule semantics,
/// shared by [`ConfiguredRulePolicy`] and [`ConfiguredDenyPolicy`] so both
/// agree on which single rule governs a claim.
fn last_match<'r>(
    rules: &'r [Rule],
    req: &AccessRequest,
    op: Operation,
    resource: &Resource,
) -> Option<&'r Rule> {
    rules.iter().rfind(|r| r.matches(req, op, resource))
}

/// Whether a resource is a filesystem path OUTSIDE the working directory
/// (and not `/dev/null`). Governs the external-directory deciders.
fn is_external_path(resource: &Resource) -> bool {
    matches!(
        resource,
        Resource::Path {
            in_cwd: false,
            dev_null: false,
            ..
        }
    )
}

// ---------------------------------------------------------------------------
// Stage A — deciders
// ---------------------------------------------------------------------------

/// Frontmatter `deny_tools`: the active prompt forbids a tool/op. Runs
/// first so it beats even Yolo's blanket allow. Reads `ctx.prompt_deny`
/// (tool names, case-insensitive). For MCP resources the concrete
/// `server:tool` name is probed too.
pub struct PromptDenyPolicy;

impl Decider for PromptDenyPolicy {
    fn id(&self) -> &'static str {
        "prompt-deny"
    }
    fn applies_to(&self, _: Operation, _: &Resource) -> bool {
        true
    }
    fn decide(
        &self,
        req: &AccessRequest,
        _op: Operation,
        resource: &Resource,
        ctx: &PolicyCtx,
    ) -> Option<Verdict> {
        let denied = |name: &str| ctx.prompt_deny.iter().any(|d| d.eq_ignore_ascii_case(name));
        let hit =
            denied(&req.tool) || matches!(resource, Resource::Mcp { name, .. } if denied(name));
        hit.then(|| {
            Verdict::new(
                Effect::Deny,
                format!(
                    "tool {:?} denied by the active prompt's deny_tools",
                    req.tool
                ),
            )
        })
    }
}

/// `--yolo` / `mode: yolo`: allow everything (after prompt-deny).
pub struct YoloPolicy;

impl Decider for YoloPolicy {
    fn id(&self) -> &'static str {
        "yolo"
    }
    fn applies_to(&self, _: Operation, _: &Resource) -> bool {
        true
    }
    fn decide(
        &self,
        req: &AccessRequest,
        _op: Operation,
        _: &Resource,
        _: &PolicyCtx,
    ) -> Option<Verdict> {
        (req.mode == SecurityMode::Yolo).then(|| Verdict::new(Effect::Allow, "yolo mode"))
    }
}

/// Session "allow always" grants, scoped by [`Operation`]. Op-scoping
/// is what lets one Edit grant cover write/edit/apply_patch without the
/// old allowlist mirroring.
pub struct SessionAllowlistPolicy;

impl Decider for SessionAllowlistPolicy {
    fn id(&self) -> &'static str {
        "session-allow"
    }
    fn applies_to(&self, _: Operation, _: &Resource) -> bool {
        true
    }
    fn decide(
        &self,
        _req: &AccessRequest,
        op: Operation,
        resource: &Resource,
        ctx: &PolicyCtx,
    ) -> Option<Verdict> {
        resource
            .match_candidates()
            .iter()
            .any(|k| ctx.allowlist.allows(op, k))
            .then(|| Verdict::new(Effect::Allow, "allowed for this session"))
    }
}

/// User-configured rules, last-match-wins within the ordered list.
pub struct ConfiguredRulePolicy {
    pub rules: Vec<Rule>,
}

impl Decider for ConfiguredRulePolicy {
    fn id(&self) -> &'static str {
        "configured-rule"
    }
    fn applies_to(&self, op: Operation, resource: &Resource) -> bool {
        // Coarse: op + pattern (the tool narrowing is applied in
        // `decide`, which has the full request). A false positive here
        // only means `decide` runs and returns None — harmless.
        let cands = resource.match_candidates();
        self.rules
            .iter()
            .any(|r| r.op.matches(op) && cands.iter().any(|k| r.pattern.matches(k)))
    }
    fn decide(
        &self,
        req: &AccessRequest,
        op: Operation,
        resource: &Resource,
        _: &PolicyCtx,
    ) -> Option<Verdict> {
        last_match(&self.rules, req, op, resource)
            .map(|r| Verdict::new(r.effect, format!("rule {:?}{:?}", r.original, r.effect)))
    }
}

/// dirge-ct16: configured `deny` rules made TERMINAL above session-allow
/// (and the allow/ask deciders). Registered between [`YoloPolicy`] and
/// [`SessionAllowlistPolicy`], so an explicit user deny can no longer be
/// silently overridden by a (broad, one-keypress) session allow-always
/// grant — while Yolo's documented "all rules off" behavior is preserved
/// (this sits BELOW Yolo).
///
/// Honors last-match-wins: it only claims when the LAST matching rule is a
/// `Deny`, so a later `allow` overriding an earlier `deny` is respected and
/// NOT turned terminal. Covers both the main rule list and
/// `external_directory` rules (for external paths) — both previously sat
/// below session-allow and shared the bypass. Main rules outrank
/// `external_directory` (mirroring the `ConfiguredRule` < `ExternalDir`
/// decider order): when a main rule matches, it alone decides terminality.
pub struct ConfiguredDenyPolicy {
    pub rules: Vec<Rule>,
    pub ext_rules: Vec<Rule>,
}

impl Decider for ConfiguredDenyPolicy {
    fn id(&self) -> &'static str {
        "configured-deny"
    }
    fn applies_to(&self, _: Operation, _: &Resource) -> bool {
        true // claims only on a last-match Deny; cheap to evaluate in decide
    }
    fn decide(
        &self,
        req: &AccessRequest,
        op: Operation,
        resource: &Resource,
        _: &PolicyCtx,
    ) -> Option<Verdict> {
        // Main rules govern any op/resource and outrank external_directory:
        // if a main rule matches, it alone decides terminality (we do NOT
        // fall through to ext rules — that would let an ext deny beat a
        // main allow, reversing precedence).
        if let Some(r) = last_match(&self.rules, req, op, resource) {
            return (r.effect == Effect::Deny).then(|| {
                Verdict::new(
                    Effect::Deny,
                    format!("rule {:?} → Deny (terminal)", r.original),
                )
            });
        }
        // No main rule matched: an external_directory deny on an external
        // mutating path is likewise terminal above session-allow.
        if matches!(op, Operation::Edit)
            && is_external_path(resource)
            && let Some(r) = last_match(&self.ext_rules, req, op, resource)
        {
            return (r.effect == Effect::Deny).then(|| {
                Verdict::new(
                    Effect::Deny,
                    format!("external_directory {:?} → Deny (terminal)", r.original),
                )
            });
        }
        None
    }
}

/// Built-in transparent allows: read-only observation anywhere, memory
/// reads (and writes outside Restrictive), skill reads (and writes
/// outside Restrictive), `/dev/null`, and in-cwd file writes/edits.
pub struct BuiltinAllowPolicy;

impl BuiltinAllowPolicy {
    /// The effect this policy would contribute, or `None` to pass.
    fn effect_for(op: Operation, req: &AccessRequest, resource: &Resource) -> Option<Effect> {
        let restrictive = req.mode == SecurityMode::Restrictive;
        match op {
            // Observation is always transparent — even in Restrictive,
            // even outside the cwd (the old global `read **` allow).
            Operation::Read => Some(Effect::Allow),

            // Memory/skill: reads always allowed; writes allowed except
            // under Restrictive, where they confirm.
            Operation::Memory | Operation::Skill => {
                let is_read = matches!(resource, Resource::Bareword(a) if is_read_action(op, a));
                if is_read || !restrictive {
                    Some(Effect::Allow)
                } else {
                    Some(Effect::Ask)
                }
            }

            // File mutation: dev-null is a harmless bit-bucket; in-cwd
            // writes are trusted (confirmed under Restrictive).
            Operation::Edit => match resource {
                Resource::Path { dev_null: true, .. } => Some(Effect::Allow),
                Resource::Path { in_cwd: true, .. } => Some(if restrictive {
                    Effect::Ask
                } else {
                    Effect::Allow
                }),
                _ => None,
            },

            // Internal/meta tools have no external effect.
            Operation::Meta => Some(Effect::Allow),

            // Execute / Network / Mcp / Agent (recursive task) / Plugin
            // are never builtin-allowed; Other (unknown) falls to
            // configured rules or the default.
            Operation::Execute
            | Operation::Network
            | Operation::Mcp
            | Operation::Agent
            | Operation::Plugin
            | Operation::Other => None,
        }
    }
}

impl Decider for BuiltinAllowPolicy {
    fn id(&self) -> &'static str {
        "builtin-allow"
    }
    fn applies_to(&self, op: Operation, resource: &Resource) -> bool {
        // Cheap structural check; the precise effect (and the
        // Restrictive Ask) is computed in `decide`.
        matches!(
            op,
            Operation::Read | Operation::Memory | Operation::Skill | Operation::Meta
        ) || matches!(
            (op, resource),
            (Operation::Edit, Resource::Path { in_cwd: true, .. })
                | (Operation::Edit, Resource::Path { dev_null: true, .. })
        )
    }
    fn decide(
        &self,
        req: &AccessRequest,
        op: Operation,
        resource: &Resource,
        _: &PolicyCtx,
    ) -> Option<Verdict> {
        Self::effect_for(op, req, resource).map(|e| {
            let why = match e {
                Effect::Allow => "built-in allow",
                _ => "restrictive mode confirms writes",
            };
            Verdict::new(e, why)
        })
    }
}

/// Read-only action names for memory/skill (everything else is a write).
fn is_read_action(op: Operation, action: &str) -> bool {
    match op {
        Operation::Memory => action == "view",
        Operation::Skill => action == "load" || action == "list",
        _ => false,
    }
}

/// Paths outside the working directory: honor `external_directory`
/// rules, otherwise require confirmation. Only claims external paths.
pub struct ExternalDirPolicy {
    pub rules: Vec<Rule>,
}

impl Decider for ExternalDirPolicy {
    fn id(&self) -> &'static str {
        "external-dir"
    }
    fn applies_to(&self, op: Operation, resource: &Resource) -> bool {
        // Only governs mutating access to external paths; external
        // reads are already allowed by BuiltinAllow (higher precedence).
        matches!(op, Operation::Edit) && is_external_path(resource)
    }
    fn decide(
        &self,
        req: &AccessRequest,
        op: Operation,
        resource: &Resource,
        _: &PolicyCtx,
    ) -> Option<Verdict> {
        if !is_external_path(resource) {
            return None;
        }
        let matched = last_match(&self.rules, req, op, resource);
        match matched {
            Some(r) => Some(Verdict::new(
                r.effect,
                format!("external_directory {:?}{:?}", r.original, r.effect),
            )),
            None => Some(Verdict::new(Effect::Ask, "outside the working directory")),
        }
    }
}

/// The configured default — always claims, so every resource has a
/// base effect. Demotes a default of Allow to Ask under Restrictive.
pub struct DefaultActionPolicy {
    pub default: Effect,
}

impl Decider for DefaultActionPolicy {
    fn id(&self) -> &'static str {
        "default"
    }
    fn applies_to(&self, _: Operation, _: &Resource) -> bool {
        true
    }
    fn decide(
        &self,
        req: &AccessRequest,
        _op: Operation,
        _: &Resource,
        _: &PolicyCtx,
    ) -> Option<Verdict> {
        let eff = if req.mode == SecurityMode::Restrictive && self.default == Effect::Allow {
            Effect::Ask
        } else {
            self.default
        };
        Some(Verdict::new(eff, "default action"))
    }
}

// ---------------------------------------------------------------------------
// Stage B — modifiers (monotone, tighten-only)
// ---------------------------------------------------------------------------

/// Breaks genuine retry loops WITHOUT ever gating an allowed op. It
/// only acts when the base effect is already `Ask` (the op was going
/// to prompt anyway) and the same (op, resource) has been prompted
/// `threshold`+ times before — then it hard-denies. Reads-only,
/// in-cwd writes, memory/skill, dev-null (all `Allow`) are structurally
/// untouched. The counter is bumped in `Engine::commit`, never here.
pub struct LoopGuardPolicy {
    pub threshold: u32,
}

impl Modifier for LoopGuardPolicy {
    fn id(&self) -> &'static str {
        "loop-guard"
    }
    fn applies_to(&self, _: Operation, _: &Resource) -> bool {
        true // gated on `current == Ask` inside refine
    }
    fn refine(
        &self,
        req: &AccessRequest,
        op: Operation,
        resource: &Resource,
        current: Effect,
        ctx: &PolicyCtx,
    ) -> Refined {
        // NEVER touch an allowed (or already-denied) op. Only an op
        // that is going to prompt can be a retry loop.
        if current != Effect::Ask {
            return Refined::noop(current);
        }
        let prior = ctx.repeat.prior(op, resource.match_key());
        if prior >= self.threshold {
            let preview: String = resource.match_key().chars().take(60).collect();
            Refined::tighten(
                current,
                Effect::Deny,
                "loop-guard",
                format!(
                    "Doom loop: repeated identical {} call ({preview}) prompted {prior}× — breaking retry loop",
                    req.tool
                ),
            )
        } else {
            Refined::noop(current)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::permission::engine::Engine;
    use std::path::PathBuf;

    fn engine(default: Effect) -> Engine {
        Engine::new(
            vec![
                Box::new(PromptDenyPolicy),
                Box::new(YoloPolicy),
                Box::new(ConfiguredDenyPolicy {
                    rules: vec![],
                    ext_rules: vec![],
                }),
                Box::new(SessionAllowlistPolicy),
                Box::new(ConfiguredRulePolicy { rules: vec![] }),
                Box::new(BuiltinAllowPolicy),
                Box::new(ExternalDirPolicy { rules: vec![] }),
                Box::new(DefaultActionPolicy { default }),
            ],
            vec![Box::new(LoopGuardPolicy { threshold: 3 })],
            PolicyCtx::default(),
        )
    }

    fn req(op: Operation, mode: SecurityMode, resources: Vec<Resource>) -> AccessRequest {
        AccessRequest {
            tool: format!("{op:?}").to_lowercase(),
            claims: resources
                .into_iter()
                .map(|r| crate::permission::engine::types::Claim::new(op, r))
                .collect(),
            mode,
            display_input: String::new(),
        }
    }
    fn path(p: &str, in_cwd: bool, dev_null: bool) -> Resource {
        Resource::Path {
            raw: p.to_string(),
            resolved: PathBuf::from(p),
            in_cwd,
            dev_null,
        }
    }
    fn cmd(s: &str) -> Resource {
        Resource::Command {
            raw: s.to_string(),
            head: s.split_whitespace().next().unwrap_or("").to_string(),
        }
    }
    fn url(u: &str) -> Resource {
        Resource::Url(u.to_string())
    }
    fn word(s: &str) -> Resource {
        Resource::Bareword(s.to_string())
    }

    #[test]
    fn read_always_allowed_even_external_and_restrictive() {
        for mode in [SecurityMode::Standard, SecurityMode::Restrictive] {
            let e = engine(Effect::Ask);
            let d = e.authorize(&req(
                Operation::Read,
                mode,
                vec![path("/etc/x", false, false)],
            ));
            assert_eq!(d.effect, Effect::Allow, "read external in {mode:?}");
            assert_eq!(d.deciding.unwrap().policy, "builtin-allow");
        }
    }

    #[test]
    fn in_cwd_write_allowed_standard_confirms_restrictive() {
        let e = engine(Effect::Ask);
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/proj/x", true, false)],
        ));
        assert_eq!(d.effect, Effect::Allow);
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Restrictive,
            vec![path("/proj/x", true, false)],
        ));
        assert_eq!(d.effect, Effect::Ask);
    }

    #[test]
    fn external_write_asks_dev_null_allows() {
        let e = engine(Effect::Ask);
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/etc/x", false, false)],
        ));
        assert_eq!(d.effect, Effect::Ask);
        assert_eq!(d.deciding.unwrap().policy, "external-dir");

        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/dev/null", false, true)],
        ));
        assert_eq!(d.effect, Effect::Allow);
    }

    #[test]
    fn memory_skill_transparent_standard_writes_confirm_restrictive() {
        let e = engine(Effect::Ask);
        // standard: all actions allow
        for (op, action) in [
            (Operation::Memory, "add"),
            (Operation::Memory, "view"),
            (Operation::Skill, "create:x"),
            (Operation::Skill, "load"),
        ] {
            let d = e.authorize(&req(op, SecurityMode::Standard, vec![word(action)]));
            assert_eq!(d.effect, Effect::Allow, "{op:?} {action} standard");
        }
        // restrictive: reads allow, writes ask
        let d = e.authorize(&req(
            Operation::Memory,
            SecurityMode::Restrictive,
            vec![word("view")],
        ));
        assert_eq!(d.effect, Effect::Allow);
        let d = e.authorize(&req(
            Operation::Memory,
            SecurityMode::Restrictive,
            vec![word("add")],
        ));
        assert_eq!(d.effect, Effect::Ask);
        let d = e.authorize(&req(
            Operation::Skill,
            SecurityMode::Restrictive,
            vec![word("load")],
        ));
        assert_eq!(d.effect, Effect::Allow);
        let d = e.authorize(&req(
            Operation::Skill,
            SecurityMode::Restrictive,
            vec![word("create:x")],
        ));
        assert_eq!(d.effect, Effect::Ask);
    }

    #[test]
    fn execute_falls_to_default_ask_not_builtin() {
        let e = engine(Effect::Ask);
        let d = e.authorize(&req(
            Operation::Execute,
            SecurityMode::Standard,
            vec![cmd("rm -rf x")],
        ));
        assert_eq!(d.effect, Effect::Ask);
        assert_eq!(d.deciding.unwrap().policy, "default");
    }

    #[test]
    fn prompt_deny_beats_yolo() {
        let mut e = engine(Effect::Ask);
        e.ctx_mut().prompt_deny = vec!["execute".to_string()];
        let d = e.authorize(&req(
            Operation::Execute,
            SecurityMode::Yolo,
            vec![cmd("anything")],
        ));
        assert_eq!(d.effect, Effect::Deny);
        assert_eq!(d.deciding.unwrap().policy, "prompt-deny");
    }

    #[test]
    fn yolo_allows_otherwise() {
        let e = engine(Effect::Ask);
        let d = e.authorize(&req(
            Operation::Execute,
            SecurityMode::Yolo,
            vec![cmd("rm -rf /")],
        ));
        assert_eq!(d.effect, Effect::Allow);
        assert_eq!(d.deciding.unwrap().policy, "yolo");
    }

    #[test]
    fn accept_coerces_default_but_not_execute() {
        let e = engine(Effect::Ask);
        // a meta op with no builtin claim would default to Ask; accept → Allow
        let d = e.authorize(&req(
            Operation::Network,
            SecurityMode::Accept,
            vec![Resource::Url("http://x".into())],
        ));
        // Network is high-risk → accept does NOT coerce
        assert_eq!(d.effect, Effect::Ask);
        // Execute also not coerced
        let d = e.authorize(&req(
            Operation::Execute,
            SecurityMode::Accept,
            vec![cmd("x")],
        ));
        assert_eq!(d.effect, Effect::Ask);
    }

    /// Concise rule constructor for tests (`tool: None`, op-based).
    fn rule(op: OpMatch, pattern: Pattern, effect: Effect) -> Rule {
        let original = pattern.original.clone();
        Rule {
            op,
            tool: None,
            pattern,
            effect,
            original,
        }
    }

    /// Build an engine whose ConfiguredRulePolicy carries `rules`.
    fn engine_with_rules(rules: Vec<Rule>) -> Engine {
        Engine::new(
            vec![
                Box::new(PromptDenyPolicy),
                Box::new(YoloPolicy),
                Box::new(ConfiguredDenyPolicy {
                    rules: rules.clone(),
                    ext_rules: vec![],
                }),
                Box::new(SessionAllowlistPolicy),
                Box::new(ConfiguredRulePolicy { rules }),
                Box::new(BuiltinAllowPolicy),
                Box::new(ExternalDirPolicy { rules: vec![] }),
                Box::new(DefaultActionPolicy {
                    default: Effect::Ask,
                }),
            ],
            vec![Box::new(LoopGuardPolicy { threshold: 3 })],
            PolicyCtx::default(),
        )
    }

    #[test]
    fn configured_rule_last_match_wins_and_beats_builtin() {
        let e = engine_with_rules(vec![
            rule(
                OpMatch::One(Operation::Execute),
                Pattern::new_command("cargo *"),
                Effect::Allow,
            ),
            rule(
                OpMatch::One(Operation::Read),
                Pattern::new("/secret/**"),
                Effect::Deny,
            ),
        ]);
        // explicit allow on execute cargo (would otherwise default to Ask)
        let d = e.authorize(&req(
            Operation::Execute,
            SecurityMode::Standard,
            vec![cmd("cargo test")],
        ));
        assert_eq!(d.effect, Effect::Allow);
        assert_eq!(d.deciding.unwrap().policy, "configured-rule");
        // explicit deny on read beats builtin-allow (rule has higher precedence)
        let d = e.authorize(&req(
            Operation::Read,
            SecurityMode::Standard,
            vec![path("/secret/k", false, false)],
        ));
        assert_eq!(d.effect, Effect::Deny);
        // The deny is now decided by the terminal configured-deny decider
        // (dirge-ct16), which sits above session-allow.
        assert_eq!(d.deciding.unwrap().policy, "configured-deny");
    }

    #[test]
    fn last_match_wins_within_rule_list() {
        // two matching rules; the later one wins
        let e = engine_with_rules(vec![
            rule(OpMatch::Any, Pattern::new("/proj/**"), Effect::Deny),
            rule(
                OpMatch::One(Operation::Edit),
                Pattern::new("/proj/ok/**"),
                Effect::Allow,
            ),
        ]);
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/proj/ok/a.rs", true, false)],
        ));
        assert_eq!(d.effect, Effect::Allow);
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/proj/no/a.rs", true, false)],
        ));
        assert_eq!(d.effect, Effect::Deny);
    }

    #[test]
    fn loop_guard_never_gates_allowed_only_hard_denies_ask_retries() {
        // builtin-allowed read repeated many times: stays Allowed.
        let mut e = engine(Effect::Ask);
        let r = req(
            Operation::Read,
            SecurityMode::Standard,
            vec![path("/proj/x", true, false)],
        );
        for _ in 0..6 {
            let d = e.authorize(&r);
            assert_eq!(d.effect, Effect::Allow, "repeated read must never prompt");
            e.commit(&r, &d);
        }
        // an Ask op retried past threshold escalates to Deny.
        let mut e = engine(Effect::Ask);
        let r = req(
            Operation::Execute,
            SecurityMode::Standard,
            vec![cmd("frobnicate")],
        );
        let mut effects = vec![];
        for _ in 0..5 {
            let d = e.authorize(&r);
            effects.push(d.effect);
            e.commit(&r, &d);
        }
        // first `threshold` (3) are Ask, then hard-Deny.
        assert_eq!(effects[0], Effect::Ask);
        assert_eq!(effects[1], Effect::Ask);
        assert_eq!(effects[2], Effect::Ask);
        assert_eq!(
            effects[3],
            Effect::Deny,
            "4th identical Ask retry hard-denied"
        );
        assert_eq!(effects[4], Effect::Deny);
    }

    #[test]
    fn approved_repeats_never_trip_loop_guard() {
        // A repeated Ask op that the user APPROVES each time (modeled by
        // `note_allowed` after each commit) must keep prompting forever —
        // it never accrues toward the hard-deny.
        let mut e = engine(Effect::Ask);
        let r = req(
            Operation::Network,
            SecurityMode::Standard,
            vec![url("https://example.com/x")],
        );
        for i in 0..10 {
            let d = e.authorize(&r);
            assert_eq!(d.effect, Effect::Ask, "iteration {i}: must keep asking");
            e.commit(&r, &d);
            e.note_allowed(&r); // user approved this prompt
        }
    }

    #[test]
    fn denials_still_hard_deny_even_after_an_earlier_approval() {
        // approve once (resets), then 4 denials → still hard-denies, so
        // the protection against a genuinely-stuck loop is preserved.
        let mut e = engine(Effect::Ask);
        let r = req(
            Operation::Execute,
            SecurityMode::Standard,
            vec![cmd("frobnicate")],
        );
        let d = e.authorize(&r);
        e.commit(&r, &d);
        e.note_allowed(&r); // approved → counter reset to 0
        let mut effects = vec![];
        for _ in 0..5 {
            let d = e.authorize(&r);
            effects.push(d.effect);
            e.commit(&r, &d); // denied at the prompt → no note_allowed
        }
        assert_eq!(
            effects[3],
            Effect::Deny,
            "hard-deny still trips on pure denials"
        );
    }

    // ---- dirge-ct16: configured `deny` is terminal above session-allow ----

    #[test]
    fn configured_deny_beats_session_allow() {
        // A configured DENY must not be overridable by a (broad) session
        // allow-always grant. Scenario: user denies edit of /etc/secret/**
        // in config, then allow-always'd a sibling path → a broad /etc/**
        // session grant. The narrow deny must still hold.
        let mut e = engine_with_rules(vec![rule(
            OpMatch::One(Operation::Edit),
            Pattern::new("/etc/secret/**"),
            Effect::Deny,
        )]);
        e.allow_always(Operation::Edit, "/etc/**");
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/etc/secret/k", false, false)],
        ));
        assert_eq!(
            d.effect,
            Effect::Deny,
            "a session allow-always must not override a configured deny",
        );
        assert_eq!(d.deciding.unwrap().policy, "configured-deny");
    }

    #[test]
    fn configured_deny_respects_last_match_allow() {
        // Last-match-wins is preserved: a later `allow` overriding an
        // earlier `deny` must NOT be turned terminal by the deny decider.
        let e = engine_with_rules(vec![
            rule(OpMatch::Any, Pattern::new("/proj/**"), Effect::Deny),
            rule(
                OpMatch::One(Operation::Edit),
                Pattern::new("/proj/ok/**"),
                Effect::Allow,
            ),
        ]);
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/proj/ok/a.rs", true, false)],
        ));
        assert_eq!(
            d.effect,
            Effect::Allow,
            "later allow still wins by last-match"
        );
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Standard,
            vec![path("/proj/no/a.rs", true, false)],
        ));
        assert_eq!(d.effect, Effect::Deny);
        assert_eq!(d.deciding.unwrap().policy, "configured-deny");
    }

    #[test]
    fn yolo_still_overrides_configured_deny() {
        // We deliberately kept config-deny BELOW yolo: yolo's documented
        // "all rules off" contract (and its startup warning) is preserved.
        let e = engine_with_rules(vec![rule(
            OpMatch::One(Operation::Edit),
            Pattern::new("/etc/**"),
            Effect::Deny,
        )]);
        let d = e.authorize(&req(
            Operation::Edit,
            SecurityMode::Yolo,
            vec![path("/etc/x", false, false)],
        ));
        assert_eq!(d.effect, Effect::Allow);
        assert_eq!(d.deciding.unwrap().policy, "yolo");
    }
}