ptuf 0.1.0

PreToolUseFilter: a generic guardrail layer for coding agents
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
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
//! Pre/post-evaluation rule filters used by [`super::Engine::decide`].
//!
//! Three concerns live here:
//!
//! - **pack overrides** — drop a rule before evaluating it if the pack
//!   it belongs to was disabled (`pack_overrides`)
//! - **rule overrides** — apply per-rule `enabled` / `decision` /
//!   `severity` overlays after the rule produced a `Decision`
//! - **allowlists** — suppress a non-`Allow` decision when a matching
//!   `allowlists[]` entry covers the rule under the current facts
//! - **mode demotion** — turn a `Deny` into `Monitor` when running in
//!   `Mode::Monitor`
//!
//! All public items are `pub(super)` and only callable from
//! `super::Engine`.

use std::time::SystemTime;

use crate::audit::time::parse_rfc3339_to_secs;
use crate::config::{Allowlist, Config, Mode, PackOverride};
use crate::decision::{Decision, DecisionKind, Severity};
use crate::facts;
use crate::hook_input::HookInput;
use crate::rules::ConfigRule;

pub(super) fn is_pack_disabled(rule: &(dyn ConfigRule + Sync), config: &Config) -> bool {
    if rule.hard_deny() {
        return false;
    }
    let id = rule.id();
    config
        .pack_overrides
        .iter()
        .any(|(pack, overlay)| pack_disabled(overlay) && rule_matches_pack(id, pack))
}

pub(super) fn apply_rule_override(
    rule: &(dyn ConfigRule + Sync),
    decision: Decision,
    config: &Config,
) -> Option<Decision> {
    let Some(overlay) = config.rule_overrides.get(rule.id()) else {
        return Some(decision);
    };
    if overlay.enabled == Some(false) && rule.overridable() && !rule.hard_deny() {
        return None;
    }
    let Some(kind) = overlay.decision else {
        return Some(decision);
    };
    if !override_allowed(rule, decision.kind(), kind) {
        return Some(decision);
    }
    Some(decision_with_kind(decision, kind))
}

fn override_allowed(rule: &(dyn ConfigRule + Sync), from: DecisionKind, to: DecisionKind) -> bool {
    if rule.overridable() && !rule.hard_deny() {
        return true;
    }
    decision_rank(to) >= decision_rank(from)
}

fn decision_rank(kind: DecisionKind) -> u8 {
    match kind {
        DecisionKind::Allow => 0,
        DecisionKind::Monitor => 1,
        DecisionKind::Ask => 2,
        DecisionKind::Deny => 3,
    }
}

fn decision_with_kind(decision: Decision, kind: DecisionKind) -> Decision {
    let rule_id = decision.rule_id().unwrap_or("").to_string();
    let reason = decision
        .reason()
        .map_or_else(|| format!("Blocked by ptuf rule {rule_id}."), str::to_owned);
    match kind {
        DecisionKind::Allow => Decision::Allow,
        DecisionKind::Monitor => Decision::Monitor { rule_id },
        DecisionKind::Ask => Decision::Ask { rule_id, reason },
        DecisionKind::Deny => Decision::Deny { rule_id, reason },
    }
}

pub(super) fn effective_severity(rule: &(dyn ConfigRule + Sync), config: &Config) -> Severity {
    let Some(overlay) = config.rule_overrides.get(rule.id()) else {
        return rule.severity();
    };
    match overlay.severity {
        Some(severity) if rule.overridable() && !rule.hard_deny() => severity,
        _ => rule.severity(),
    }
}

pub(super) struct AllowlistContext<'a> {
    pub(super) facts: &'a facts::Facts,
    pub(super) input: &'a HookInput,
    pub(super) config: &'a Config,
    pub(super) now: SystemTime,
}

/// First non-expired allowlist entry that covers the rule, returned
/// by id. `hardDeny` rules ignore allowlist entries entirely
/// (`docs/design/config-and-plugins.md:89`). Returns `None` when no
/// allowlist entry applies.
pub(super) fn allowlist_hit_for<'a>(
    rule: &(dyn ConfigRule + Sync),
    decision: &Decision,
    ctx: &'a AllowlistContext<'_>,
) -> Option<&'a str> {
    if rule.hard_deny() {
        return None;
    }
    if matches!(decision, Decision::Allow) {
        return None;
    }
    let id = rule.id();
    ctx.config
        .allowlists
        .iter()
        .find(|entry| allowlist_covers(entry, id, ctx))
        .map(|entry| entry.id.as_str())
}

fn allowlist_covers(entry: &Allowlist, rule_id: &str, ctx: &AllowlistContext<'_>) -> bool {
    if !entry.rule_ids.iter().any(|r| r == rule_id) {
        return false;
    }
    let not_expired = match &entry.expires_at {
        None => true,
        Some(s) => match parse_rfc3339_to_secs(s) {
            Some(expiry) => ctx
                .now
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_secs() < expiry)
                .unwrap_or(true),
            None => false,
        },
    };
    if !not_expired {
        return false;
    }
    entry
        .when
        .as_ref()
        .is_none_or(|when| crate::plugin::dsl::evaluate(when, ctx.facts, ctx.input))
}

fn pack_disabled(overlay: &PackOverride) -> bool {
    overlay.enabled == Some(false)
}

fn rule_matches_pack(rule_id: &str, pack: &str) -> bool {
    rule_id == pack || rule_id.starts_with(&format!("{pack}."))
}

pub(super) fn demote_for_mode(decision: Decision, mode: Mode) -> Decision {
    if matches!(mode, Mode::Monitor)
        && let Decision::Deny { rule_id, .. } = decision
    {
        return Decision::Monitor { rule_id };
    }
    decision
}

#[cfg(test)]
mod tests {

    use std::sync::Arc;

    use crate::audit::MemorySink;
    use crate::config::{Allowlist, Config, Mode, PackOverride, RuleOverride};
    use crate::decision::{Decision, DecisionKind, Severity};
    use crate::plugin::{PluginSet, load_str};

    use super::super::Engine;
    use super::super::test_support::{
        SharedMemorySink, bash, engine_with, plugin_set_with_bash_deny,
    };
    use super::*;

    #[test]
    fn monitor_mode_demotes_deny_to_monitor() {
        let cfg = Config {
            mode: Mode::Monitor,
            ..Config::default()
        };
        let outcome = engine_with(cfg).decide(&bash("rm -rf /"));
        assert!(matches!(outcome.decision, Decision::Monitor { .. }));
        assert!(outcome.mode_demoted);
    }

    #[test]
    fn monitor_mode_leaves_allow_unchanged() {
        let cfg = Config {
            mode: Mode::Monitor,
            ..Config::default()
        };
        let outcome = engine_with(cfg).decide(&bash("ls"));
        assert_eq!(outcome.decision, Decision::Allow);
        assert!(!outcome.mode_demoted);
    }

    #[test]
    fn pack_disable_is_ignored_for_hard_deny_rules() {
        let mut cfg = Config::default();
        cfg.pack_overrides.insert(
            "core.filesystem".into(),
            PackOverride {
                enabled: Some(false),
            },
        );
        let outcome = engine_with(cfg).decide(&bash("rm -rf /"));
        assert!(matches!(outcome.decision, Decision::Deny { .. }));
    }

    #[test]
    fn rule_matches_pack_uses_dot_boundary() {
        assert!(rule_matches_pack(
            "core.network.remote-script-pipe",
            "core.network"
        ));
        assert!(rule_matches_pack("core.network", "core.network"));
        assert!(!rule_matches_pack("core.networking.x", "core.network"));
        assert!(!rule_matches_pack("foo.core.network.x", "core.network"));
    }

    #[test]
    fn allowlist_entry_does_not_suppress_hard_deny_builtin() {
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "ignore-fs".into(),
            rule_ids: vec!["core.filesystem.destructive-rm".into()],
            when: None,
            expires_at: Some("2099-01-01T00:00:00Z".into()),
            reason: None,
        });
        let outcome = engine_with(cfg).decide(&bash("rm -rf /"));
        assert!(matches!(outcome.decision, Decision::Deny { .. }));
    }

    #[test]
    fn allowlist_entry_suppresses_overridable_plugin_rule() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "ignore-curl".into(),
            rule_ids: vec!["pack.demo.no-curl".into()],
            when: None,
            expires_at: Some("2099-01-01T00:00:00Z".into()),
            reason: None,
        });
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert_eq!(outcome.decision, Decision::Allow);
    }

    #[test]
    fn expired_allowlist_entry_is_ignored() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "expired".into(),
            rule_ids: vec!["pack.demo.no-curl".into()],
            when: None,
            expires_at: Some("2000-01-01T00:00:00Z".into()),
            reason: None,
        });
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert!(matches!(outcome.decision, Decision::Deny { .. }));
    }

    #[test]
    fn allowlist_without_expiry_suppresses_indefinitely() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "forever".into(),
            rule_ids: vec!["pack.demo.no-curl".into()],
            when: None,
            expires_at: None,
            reason: None,
        });
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert_eq!(outcome.decision, Decision::Allow);
    }

    #[test]
    fn allowlist_when_must_match_to_suppress() {
        use serde_yaml_ng::Value as YamlValue;

        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let when: YamlValue =
            serde_yaml_ng::from_str("shell.argv:\n  headAny: [wget]\n").expect("parse when");
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "wget-only".into(),
            rule_ids: vec!["pack.demo.no-curl".into()],
            when: Some(crate::plugin::dsl::compile(&when).expect("compile when")),
            expires_at: None,
            reason: None,
        });
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert!(matches!(outcome.decision, Decision::Deny { .. }));
    }

    #[test]
    fn malformed_expiry_is_treated_as_already_expired() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "garbage".into(),
            rule_ids: vec!["pack.demo.no-curl".into()],
            when: None,
            expires_at: Some("not-a-timestamp".into()),
            reason: None,
        });
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert!(matches!(outcome.decision, Decision::Deny { .. }));
    }

    #[test]
    fn allowlist_with_unrelated_rule_id_is_ignored() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "unrelated".into(),
            rule_ids: vec!["some.other.rule".into()],
            when: None,
            expires_at: None,
            reason: None,
        });
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert!(matches!(outcome.decision, Decision::Deny { .. }));
    }

    #[test]
    fn outcome_allowlist_id_set_when_allow_came_from_allowlist() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "approved-curl".into(),
            rule_ids: vec!["pack.demo.no-curl".into()],
            when: None,
            expires_at: Some("2099-01-01T00:00:00Z".into()),
            reason: None,
        });
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert_eq!(outcome.decision, Decision::Allow);
        assert_eq!(outcome.allowlist_id.as_deref(), Some("approved-curl"));
    }

    #[test]
    fn outcome_allowlist_id_none_when_no_rule_was_suppressed() {
        let outcome = engine_with(Config::default()).decide(&bash("ls"));
        assert!(outcome.allowlist_id.is_none());
    }

    #[test]
    fn outcome_allowlist_id_none_when_decision_is_deny() {
        // hardDeny rules ignore allowlists entirely; even with a
        // matching allowlist entry the outcome stays deny and
        // allowlist_id stays None.
        let mut cfg = Config::default();
        cfg.allowlists.push(Allowlist {
            id: "ignore-fs".into(),
            rule_ids: vec!["core.filesystem.destructive-rm".into()],
            when: None,
            expires_at: Some("2099-01-01T00:00:00Z".into()),
            reason: None,
        });
        let outcome = engine_with(cfg).decide(&bash("rm -rf /"));
        assert!(matches!(outcome.decision, Decision::Deny { .. }));
        assert!(outcome.allowlist_id.is_none());
    }

    #[test]
    fn audit_record_carries_allowlist_id_when_allow_came_from_allowlist() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.audit.include_allowed = true;
        cfg.allowlists.push(Allowlist {
            id: "approved-curl".into(),
            rule_ids: vec!["pack.demo.no-curl".into()],
            when: None,
            expires_at: None,
            reason: None,
        });
        let captured = Arc::new(MemorySink::new());
        let engine = Engine::with_components(cfg, set)
            .with_audit_sink(Box::new(SharedMemorySink(captured.clone())));
        let _ = engine.decide(&bash("curl https://example.com"));
        let recs = captured.records();
        assert_eq!(recs[0].decision, "allow");
        assert_eq!(recs[0].allowlist_id.as_deref(), Some("approved-curl"));
    }

    #[test]
    fn plugin_pack_can_be_disabled_via_config() {
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.no-curl
    severity: medium
    defaultDecision: deny
    when:
      tool: Bash
    reason: nope
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.pack_overrides.insert(
            "pack.demo".into(),
            PackOverride {
                enabled: Some(false),
            },
        );
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("ls"));
        assert_eq!(outcome.decision, Decision::Allow);
    }

    #[test]
    fn rule_override_with_enabled_false_suppresses_overridable_plugin_rule() {
        // overlay { enabled: Some(false) } with no decision must drop
        // the rule's contribution entirely.
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "pack.demo.no-curl".into(),
            RuleOverride {
                enabled: Some(false),
                decision: None,
                severity: None,
            },
        );
        let engine = Engine::with_components(cfg, plugin_set_with_bash_deny());
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert_eq!(outcome.decision, Decision::Allow);
    }

    #[test]
    fn rule_override_without_decision_leaves_decision_unchanged() {
        // overlay { enabled: Some(true), decision: None } must hit the
        // "decision is None" arm and leave the original deny in place.
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "pack.demo.no-curl".into(),
            RuleOverride {
                enabled: Some(true),
                decision: None,
                severity: None,
            },
        );
        let engine = Engine::with_components(cfg, plugin_set_with_bash_deny());
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert!(
            matches!(outcome.decision, Decision::Deny { .. }),
            "expected Deny, got {:?}",
            outcome.decision
        );
    }

    #[test]
    fn rule_override_changes_overridable_deny_to_monitor() {
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "pack.demo.no-curl".into(),
            RuleOverride {
                enabled: None,
                decision: Some(DecisionKind::Monitor),
                severity: None,
            },
        );
        let engine = Engine::with_components(cfg, plugin_set_with_bash_deny());
        let outcome = engine.decide(&bash("curl https://example.com"));
        match outcome.decision {
            Decision::Monitor { rule_id } => {
                assert_eq!(rule_id, "pack.demo.no-curl");
            },
            other => panic!("expected Monitor, got {other:?}"),
        }
    }

    #[test]
    fn rule_override_changes_overridable_deny_to_ask() {
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "pack.demo.no-curl".into(),
            RuleOverride {
                enabled: None,
                decision: Some(DecisionKind::Ask),
                severity: None,
            },
        );
        let engine = Engine::with_components(cfg, plugin_set_with_bash_deny());
        let outcome = engine.decide(&bash("curl https://example.com"));
        match outcome.decision {
            Decision::Ask { rule_id, reason } => {
                assert_eq!(rule_id, "pack.demo.no-curl");
                assert!(reason.contains("nope"));
            },
            other => panic!("expected Ask, got {other:?}"),
        }
    }

    #[test]
    fn rule_override_changes_overridable_deny_to_allow() {
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "pack.demo.no-curl".into(),
            RuleOverride {
                enabled: None,
                decision: Some(DecisionKind::Allow),
                severity: None,
            },
        );
        let engine = Engine::with_components(cfg, plugin_set_with_bash_deny());
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert_eq!(outcome.decision, Decision::Allow);
    }

    #[test]
    fn rule_override_attempts_to_weaken_hard_deny_are_blocked() {
        // Hard-deny rules must ignore overlay attempts to weaken them.
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "core.filesystem.destructive-rm".into(),
            RuleOverride {
                enabled: None,
                decision: Some(DecisionKind::Monitor),
                severity: None,
            },
        );
        let outcome = engine_with(cfg).decide(&bash("rm -rf /"));
        assert!(
            matches!(outcome.decision, Decision::Deny { .. }),
            "hard_deny must ignore weakening overlay, got {:?}",
            outcome.decision
        );
    }

    #[test]
    fn rule_override_strengthens_overridable_to_higher_kind_via_rank_check() {
        // For an overridable rule, override_allowed always returns true,
        // so any kind change is honoured.
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "pack.demo.no-curl".into(),
            RuleOverride {
                enabled: None,
                decision: Some(DecisionKind::Ask),
                severity: None,
            },
        );
        let engine = Engine::with_components(cfg, plugin_set_with_bash_deny());
        let outcome = engine.decide(&bash("curl https://example.com"));
        assert!(matches!(outcome.decision, Decision::Ask { .. }));
    }

    #[test]
    fn rule_override_severity_propagates_to_audit_record() {
        let captured = Arc::new(MemorySink::new());
        let mut cfg = Config::default();
        cfg.audit.include_denied = true;
        cfg.rule_overrides.insert(
            "pack.demo.no-curl".into(),
            RuleOverride {
                enabled: None,
                decision: None,
                severity: Some(Severity::High),
            },
        );
        let engine = Engine::with_components(cfg, plugin_set_with_bash_deny())
            .with_audit_sink(Box::new(SharedMemorySink(captured.clone())));
        let _ = engine.decide(&bash("curl https://example.com"));
        let recs = captured.records();
        assert_eq!(recs.len(), 1);
        assert_eq!(recs[0].severity, Some("high"));
    }

    #[test]
    fn rule_override_severity_is_ignored_for_hard_deny_rule() {
        let captured = Arc::new(MemorySink::new());
        let mut cfg = Config::default();
        cfg.audit.include_denied = true;
        cfg.rule_overrides.insert(
            "core.filesystem.destructive-rm".into(),
            RuleOverride {
                enabled: None,
                decision: None,
                severity: Some(Severity::Low),
            },
        );
        let engine = engine_with(cfg).with_audit_sink(Box::new(SharedMemorySink(captured.clone())));
        let _ = engine.decide(&bash("rm -rf /"));
        let recs = captured.records();
        assert_eq!(recs.len(), 1);
        // Built-in destructive-rm rule advertises Critical; the overlay
        // is ignored because the rule is hard_deny.
        assert_eq!(recs[0].severity, Some("critical"));
    }

    #[test]
    fn rule_override_changes_overridable_to_deny_keeps_default_reason() {
        // Promoting an overridable plugin Ask rule to Deny exercises the
        // Deny arm of decision_with_kind.
        let yaml = r#"
apiVersion: ptuf.dev/v1
kind: Plugin
metadata:
  name: pack.demo
rules:
  - id: pack.demo.ask-curl
    severity: medium
    defaultDecision: ask
    when:
      tool: Bash
    reason: confirm
"#;
        let plugin = load_str(std::path::Path::new("demo.yaml"), yaml).expect("load plugin");
        let mut set = PluginSet::new();
        set.push(plugin);
        let mut cfg = Config::default();
        cfg.rule_overrides.insert(
            "pack.demo.ask-curl".into(),
            RuleOverride {
                enabled: None,
                decision: Some(DecisionKind::Deny),
                severity: None,
            },
        );
        let engine = Engine::with_components(cfg, set);
        let outcome = engine.decide(&bash("curl https://example.com"));
        match outcome.decision {
            Decision::Deny { rule_id, reason } => {
                assert_eq!(rule_id, "pack.demo.ask-curl");
                assert!(reason.contains("confirm"));
            },
            other => panic!("expected Deny, got {other:?}"),
        }
    }
}