kirino 0.7.7

Zero-trust authentication and authorization framework with RBAC and dynamic risk assessment.
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
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
use anyhow::Result;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::{
    collections::{HashMap, VecDeque},
    sync::Arc,
};
use tokio::sync::RwLock;

type AlertHook = Box<dyn Fn(AuditAlert) + Send + Sync>;

#[derive(Debug, Clone, Serialize, Deserialize)]
/// One recorded authorization decision - the unit of audit evidence.
///
/// Appended by [`AuditLogger::log`] before any rule runs, so a decision is on
/// record even when no [`AuditRule`] matches. `granted == false` is the signal
/// policy rules and analyzers key on. The sink, not the caller, owns identity:
/// [`AuditSink::append`] assigns `id`, so treat any pre-set value as advisory.
pub struct AuditEntry {
    /// Sink-assigned identifier, unique within one [`AuditSink`] instance.
    pub id: u64,
    /// Opaque id of the acting subject; the join key for per-subject queries.
    pub subject_id: String,
    /// Subject kind (for example `user` or `agent`); attribution, not a trust claim.
    pub subject_type: String,
    /// The permission that was checked, in the checker's own wire form.
    pub permission: String,
    /// Entry point that requested the check; correlates a denial burst with one API surface.
    pub endpoint: String,
    /// The decision. `false` (a denial) is the security-relevant case rules act on.
    pub granted: bool,
    /// Decision time (UTC). Recency queries sort on this field, not on `id`.
    pub created_at: DateTime<Utc>,
    /// Risk verdict when the entry was scored; `None` means unscored, never "safe".
    pub verdict: Option<AuditVerdict>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Dynamic-authorization verdict attached to an [`AuditEntry`].
///
/// Produced by `rbac::dynamic` (risk score plus the autonomy level it mapped to).
/// It is advisory evidence only and never grants anything by itself.
pub struct AuditVerdict {
    /// Autonomy level the risk score mapped to (see `rbac::dynamic::verdict`).
    pub autonomy_level: String,
    /// Overall risk in `[0.0, 1.0]`; [`AuditEntry::is_high_risk`] cuts at 0.6.
    pub risk_score: f64,
    /// Per-factor breakdown backing `risk_score`; kept so alerts stay explainable.
    pub sub_scores: AuditSubScores,
    /// Human-readable reasons for the score. Never put credentials, tokens or
    /// password material here: audit entries are persisted and exported.
    pub evidence: Vec<String>,
    /// Mitigation applied to the decision, if any; `None` means it stood unmitigated.
    pub mitigation: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// The per-factor contributions that produced an [`AuditVerdict`] risk score.
///
/// Kept for attribution: an investigator needs to know whether risk came from
/// trust decay, resource sensitivity, domain mismatch, delegation weight or
/// anomaly - not just the total.
pub struct AuditSubScores {
    /// Risk contributed by the delegator's privilege level.
    pub delegator_weight: f64,
    /// Risk contributed by trust decay; `1.0 - trust_penalty` is the trust score
    /// compared by [`AuditCondition::TrustBelow`].
    pub trust_penalty: f64,
    /// Risk contributed by the target resource's sensitivity category.
    pub sensitivity: f64,
    /// Risk contributed by acting outside the delegator's task domain.
    pub domain_mismatch: f64,
    /// Risk contributed by behavioral anomaly detection against the subject baseline.
    pub anomaly: f64,
}

impl AuditEntry {
    /// Whether this entry records a denial.
    ///
    /// Denials are what alert rules and [`AuditCondition::Denied`] act on, and what
    /// [`AuditAnalysisResult::denied_rate`] counts; prefer this predicate over
    /// reading `granted` directly.
    #[must_use]
    pub fn is_denied(&self) -> bool {
        !self.granted
    }

    /// Whether the entry carries a risk verdict at or above the high-risk cut (0.6).
    ///
    /// Triage helper only: the authoritative threshold is the one on each
    /// deployment's [`AuditCondition::HighRisk`] rule, which may differ. An entry
    /// with no verdict is never high risk - unscored is not the same as safe.
    #[must_use]
    pub fn is_high_risk(&self) -> bool {
        self.verdict.as_ref().is_some_and(|v| v.risk_score >= 0.6)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Side effect a matched [`AuditRule`] asks for.
///
/// Actions are observability and response only: they run after the decision has
/// been recorded and must never feed back into [`AuditEntry::granted`]. Keep
/// alert text and `params` free of credentials - alerts are exported to
/// operators and log sinks.
pub enum AuditAction {
    /// Emit a human-facing alert carrying `message` at `severity`.
    Alert {
        message: String,
        severity: AuditSeverity,
    },
    /// Notify a specific `target` (webhook, queue, chat channel) with `message`.
    Notify { target: String, message: String },
    /// Request an automated response named `action` with `params`.
    ///
    /// Deliberately free-form: the audit subsystem records the request, it does
    /// not execute countermeasures.
    Countermeasure {
        action: String,
        params: HashMap<String, String>,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
/// Triage level of an alert; carries no authorization weight on its own.
pub enum AuditSeverity {
    /// Informational - recorded, not paged.
    Info,
    /// Needs operator attention at the next opportunity.
    Warning,
    /// Urgent; assume an active or attempted compromise until triaged.
    Critical,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// A policy rule: when `condition` holds and the cooldown has elapsed, `action` runs.
///
/// Rules observe already-recorded decisions, so a disabled or broken rule
/// degrades detection but never enforcement. `cooldown_secs` suppresses per rule
/// id, not per subject, and a suppressed match is not re-evaluated: a long
/// cooldown can hide a later, more severe match for the same rule.
pub struct AuditRule {
    /// Stable rule id; the cooldown key and what [`AuditAlert::rule_id`] reports.
    pub id: String,
    /// Operator-facing rule name, used in alert text and panic logs.
    pub name: String,
    /// Disabled rules are skipped entirely: no evaluation and no cooldown update.
    pub enabled: bool,
    /// Predicate selecting the entries this rule reacts to.
    pub condition: AuditCondition,
    /// What to do when the rule fires; it cannot change the triggering decision.
    pub action: AuditAction,
    /// Minimum seconds between two firings of this rule id; `0` fires on every match.
    pub cooldown_secs: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Predicate over a single [`AuditEntry`], evaluated by [`AuditPolicyEngine::evaluate`].
///
/// Every variant except [`AuditCondition::RapidDenials`] is a pure function of one
/// entry. An unscored entry (`verdict: None`) fails each risk-based condition, so
/// treat a missing verdict as "unknown, not safe" when writing rules.
pub enum AuditCondition {
    /// Matches every denial.
    Denied,
    /// Matches entries whose `risk_score` is at or above `threshold`.
    ///
    /// The threshold lives on the rule so deployments can tune it without touching
    /// the scoring engine; it is unrelated to [`AuditEntry::is_high_risk`]'s 0.6.
    HighRisk { threshold: f64 },
    /// Matches entries whose resource-sensitivity sub-score is at or above `min_weight`.
    CategorySensitive { min_weight: f64 },
    /// Matches entries whose domain-mismatch sub-score is at or above `min_weight`.
    DomainMismatch { min_weight: f64 },
    /// Matches when the subject accumulated `min_count` denials within `window_secs`.
    ///
    /// The one history-dependent condition: it needs the [`AuditSink`] the policy
    /// engine was built with, and [`AuditCondition::evaluate`] alone always returns
    /// `false` for it. An engine without a sink therefore never fires it - a silent
    /// detection gap, not a fail-closed default.
    RapidDenials { window_secs: u64, min_count: u32 },
    /// Matches when the entry's trust score (`1.0 - trust_penalty`) is at or below `threshold`.
    ///
    /// Note the polarity: a low trust score matches. An unscored entry has no trust
    /// reading and never matches.
    TrustBelow { threshold: f64 },
    /// Combines sub-conditions with `operator` (all / any).
    ///
    /// Degenerate cases follow `Iterator::all`/`any`: an empty `All` matches
    /// everything, an empty `Any` matches nothing.
    Composite {
        conditions: Vec<AuditCondition>,
        operator: LogicalOp,
    },
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
/// Boolean connective for [`AuditCondition::Composite`].
pub enum LogicalOp {
    /// Fire only when every sub-condition matches.
    All,
    /// Fire when at least one sub-condition matches.
    Any,
}

impl AuditCondition {
    /// Evaluate the condition against one entry; `true` means the rule fires.
    ///
    /// Pure and side-effect free: it must not touch the sink, which is why
    /// [`AuditCondition::RapidDenials`] always returns `false` here and is resolved
    /// by [`AuditPolicyEngine::evaluate`] instead. Keep it pure - the condition may
    /// be evaluated for entries that are never persisted.
    #[must_use]
    pub fn evaluate(&self, entry: &AuditEntry) -> bool {
        match self {
            AuditCondition::Denied => entry.is_denied(),
            AuditCondition::HighRisk { threshold } => entry
                .verdict
                .as_ref()
                .is_some_and(|v| v.risk_score >= *threshold),
            AuditCondition::CategorySensitive { min_weight } => entry
                .verdict
                .as_ref()
                .is_some_and(|v| v.sub_scores.sensitivity >= *min_weight),
            AuditCondition::DomainMismatch { min_weight } => entry
                .verdict
                .as_ref()
                .is_some_and(|v| v.sub_scores.domain_mismatch >= *min_weight),
            // RapidDenials cannot be evaluated at the condition level because it
            // requires access to the AuditSink. Evaluation happens in
            // InMemoryAuditPolicyEngine::evaluate which has access to the sink.
            AuditCondition::RapidDenials { .. } => false,
            AuditCondition::TrustBelow { threshold } => entry.verdict.as_ref().is_some_and(|v| {
                let trust = 1.0 - v.sub_scores.trust_penalty;
                trust <= *threshold
            }),
            AuditCondition::Composite {
                conditions,
                operator,
            } => match operator {
                LogicalOp::All => conditions.iter().all(|c| c.evaluate(entry)),
                LogicalOp::Any => conditions.iter().any(|c| c.evaluate(entry)),
            },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// A fired rule: what matched, what to do about it, and the evidence.
///
/// Delivered to hooks registered with [`AuditLogger::on_alert`]. A hook is
/// arbitrary caller code, so it must not be able to fail the audit write, and its
/// output is never the authorization decision.
pub struct AuditAlert {
    /// Id of the rule that fired (also its cooldown key).
    pub rule_id: String,
    /// Name of the rule that fired, for operator-facing text.
    pub rule_name: String,
    /// The action the rule asked for; dispatching it is the consumer's job.
    pub action: AuditAction,
    /// Snapshot of the matching entry, so the alert stays meaningful after retention
    /// evicts the original from the sink.
    pub triggering_entry: Box<AuditEntry>,
    /// When the rule fired (UTC), not when the underlying decision was made.
    pub created_at: DateTime<Utc>,
}

/// Storage for audit entries.
///
/// Implementations must attempt to persist every appended entry and must not fail
/// or block the caller: `append` returns nothing, so a sink that cannot write is
/// expected to record that internally (log/metric) rather than drop it silently.
/// `query` and `count` must agree with what `append` accepted; retention belongs to
/// the sink, so callers cannot assume an entry survives forever.
#[async_trait::async_trait]
pub trait AuditSink: Send + Sync {
    /// Persist one decision. Idempotence is not required; the sink assigns `id`.
    async fn append(&self, entry: AuditEntry);
    /// Return matching entries newest first; `filter.limit` is applied here.
    async fn query(&self, filter: &AuditFilter) -> Vec<AuditEntry>;
    /// Count matching entries without materializing them.
    async fn count(&self, filter: &AuditFilter) -> u64;
}

#[derive(Debug, Clone, Default)]
/// Selector for [`AuditSink::query`] and [`AuditSink::count`].
///
/// Every field is optional and `Default` matches everything; the fields combine
/// conjunctively (AND), there is no OR form. `min_risk` compares an unscored
/// entry against 0.0, so a bound above zero excludes entries with no verdict.
pub struct AuditFilter {
    /// Restrict to one subject id.
    pub subject_id: Option<String>,
    /// Restrict to grants (`true`) or denials (`false`).
    pub granted: Option<bool>,
    /// Restrict to one permission string (exact match, no wildcards).
    pub permission: Option<String>,
    /// Inclusive lower bound on `created_at`.
    pub since: Option<DateTime<Utc>>,
    /// Inclusive upper bound on `created_at`.
    pub until: Option<DateTime<Utc>>,
    /// Inclusive lower bound on `risk_score`; an entry with no verdict reads as 0.0.
    pub min_risk: Option<f64>,
    /// Cap on returned entries, applied after sorting newest first; `count` ignores it.
    pub limit: Option<usize>,
}

/// Evaluates [`AuditRule`]s against entries and reports the ones that fired.
///
/// Implementations own their rule state, including per-rule cooldown timestamps,
/// so two engines loaded with the same rules can both fire - cooldown is local,
/// not global. Rule changes made through this trait are not persisted.
#[async_trait::async_trait]
pub trait AuditPolicyEngine: Send + Sync {
    /// Evaluate every enabled rule against `entry` and return the alerts that fired.
    ///
    /// Must not mutate the entry: it has already been persisted by the caller.
    async fn evaluate(&self, entry: &AuditEntry) -> Vec<AuditAlert>;
    /// Append a rule. A duplicate `id` is not rejected, and each copy keeps its own
    /// cooldown slot.
    async fn add_rule(&self, rule: AuditRule);
    /// Remove every rule with `rule_id`; `Ok(false)` means nothing matched.
    async fn remove_rule(&self, rule_id: &str) -> Result<bool>;
    /// Snapshot of the current rules; mutating the returned vector does not affect
    /// the engine.
    async fn list_rules(&self) -> Vec<AuditRule>;
}

/// Computes aggregate statistics over a batch of entries.
///
/// Read-only and advisory: analysis summarizes what the sink already holds and must
/// never be used as an authorization input.
#[async_trait::async_trait]
pub trait AuditAnalyzer: Send + Sync {
    /// Summarize one batch. The batch is a snapshot; results do not update later.
    async fn analyze(&self, entries: &[AuditEntry]) -> AuditAnalysisResult;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Summary an [`AuditAnalyzer`] produced over one batch of entries.
///
/// A one-shot snapshot, not a live view. `top_risk_entries` is capped by the
/// analyzer (10 for [`DefaultAuditAnalyzer`]), so it is not the full denial set.
pub struct AuditAnalysisResult {
    /// Number of entries in the analyzed batch.
    pub total_entries: u64,
    /// Denials in the batch.
    pub denied_count: u64,
    /// `denied_count / total_entries`, or 0.0 for an empty batch.
    pub denied_rate: f64,
    /// Entries at or above the analyzer's high-risk cut (0.6).
    pub high_risk_count: u64,
    /// Per-subject rollup, keyed by subject id.
    pub by_subject: HashMap<String, SubjectStats>,
    /// Decision counts per permission string, grants and denials combined.
    pub by_permission: HashMap<String, u64>,
    /// Highest-risk entries, descending, capped by the analyzer.
    pub top_risk_entries: Vec<AuditEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
/// Per-subject aggregate inside an [`AuditAnalysisResult`].
pub struct SubjectStats {
    /// Decisions recorded for this subject in the batch.
    pub total: u64,
    /// Denials among them.
    pub denied: u64,
    /// Mean risk across the subject's entries (unscored entries count as 0.0).
    pub avg_risk: f64,
    /// Highest risk seen for this subject.
    pub max_risk: f64,
}

// Retention cap for the in-memory sink. Matches the same 10k bound used by
// `TtlPermissionCache` and the login rate limiter, so one process's in-memory
// security state has a single, predictable ceiling.
const DEFAULT_MAX_AUDIT_ENTRIES: usize = 10000;

/// Bounded in-memory [`AuditSink`] backed by a ring buffer.
///
/// On overflow the oldest entries are dropped, so this sink is neither durable nor
/// tamper-evident: it is for tests, examples and single-process deployments that
/// ship entries elsewhere. Size `max_entries` so the retention window covers the
/// longest investigation you need; evidence beyond it is gone.
pub struct InMemoryAuditSink {
    entries: RwLock<VecDeque<AuditEntry>>,
    next_id: std::sync::atomic::AtomicU64,
    max_entries: usize,
}

impl InMemoryAuditSink {
    /// Create a sink retaining the default maximum of 10 000 entries.
    #[must_use]
    pub fn new() -> Self {
        Self {
            entries: RwLock::new(VecDeque::new()),
            next_id: std::sync::atomic::AtomicU64::new(1),
            max_entries: DEFAULT_MAX_AUDIT_ENTRIES,
        }
    }

    /// Create a sink retaining at most `max_entries` entries, evicting oldest first.
    ///
    /// The bound is the whole retention policy: there is no time-based expiry, so a
    /// quiet system keeps entries indefinitely.
    #[must_use]
    pub fn with_max_entries(max_entries: usize) -> Self {
        Self {
            entries: RwLock::new(VecDeque::new()),
            next_id: std::sync::atomic::AtomicU64::new(1),
            max_entries,
        }
    }
}

impl Default for InMemoryAuditSink {
    fn default() -> Self {
        Self::new()
    }
}

fn matches_filter(entry: &AuditEntry, filter: &AuditFilter) -> bool {
    if let Some(ref sid) = filter.subject_id {
        if entry.subject_id != *sid {
            return false;
        }
    }
    if let Some(g) = filter.granted {
        if entry.granted != g {
            return false;
        }
    }
    if let Some(ref perm) = filter.permission {
        if entry.permission != *perm {
            return false;
        }
    }
    if let Some(since) = filter.since {
        if entry.created_at < since {
            return false;
        }
    }
    if let Some(until) = filter.until {
        if entry.created_at > until {
            return false;
        }
    }
    if let Some(min_risk) = filter.min_risk {
        let risk = entry.verdict.as_ref().map_or(0.0, |v| v.risk_score);
        if risk < min_risk {
            return false;
        }
    }
    true
}

#[async_trait::async_trait]
impl AuditSink for InMemoryAuditSink {
    async fn append(&self, mut entry: AuditEntry) {
        let mut entries = self.entries.write().await;
        let id = self
            .next_id
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        entry.id = id;
        entries.push_back(entry);
        while entries.len() > self.max_entries {
            entries.pop_front();
        }
    }

    async fn query(&self, filter: &AuditFilter) -> Vec<AuditEntry> {
        let entries = self.entries.read().await;
        let mut result: Vec<AuditEntry> = entries
            .iter()
            .filter(|e| matches_filter(e, filter))
            .cloned()
            .collect();

        result.sort_by_key(|b| std::cmp::Reverse(b.created_at));
        if let Some(limit) = filter.limit {
            result.truncate(limit);
        }
        result
    }

    async fn count(&self, filter: &AuditFilter) -> u64 {
        let entries = self.entries.read().await;
        entries.iter().filter(|e| matches_filter(e, filter)).count() as u64
    }
}

/// Rule engine with in-memory rules, cooldown state and an optional [`AuditSink`].
///
/// Holds no persistence - [`AuditPolicyEngine::add_rule`] affects this instance
/// only. Without a sink the history-dependent [`AuditCondition::RapidDenials`] can
/// never fire, so a deployment relying on it must use
/// [`InMemoryAuditPolicyEngine::with_sink`].
pub struct InMemoryAuditPolicyEngine {
    rules: RwLock<Vec<AuditRule>>,
    last_triggered: RwLock<HashMap<String, DateTime<Utc>>>,
    sink: Option<Arc<dyn AuditSink>>,
}

impl InMemoryAuditPolicyEngine {
    /// Create an engine with no rules and no sink (RapidDenials cannot fire).
    #[must_use]
    pub fn new() -> Self {
        Self {
            rules: RwLock::new(Vec::new()),
            last_triggered: RwLock::new(HashMap::new()),
            sink: None,
        }
    }

    /// Create an engine that resolves history-dependent conditions against `sink`.
    ///
    /// The sink is also the source RapidDenials counts from, so it should be the same
    /// sink the decisions are written to; otherwise the rule counts a different history.
    #[must_use]
    pub fn with_sink(sink: Arc<dyn AuditSink>) -> Self {
        Self {
            rules: RwLock::new(Vec::new()),
            last_triggered: RwLock::new(HashMap::new()),
            sink: Some(sink),
        }
    }
}

impl Default for InMemoryAuditPolicyEngine {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl AuditPolicyEngine for InMemoryAuditPolicyEngine {
    async fn evaluate(&self, entry: &AuditEntry) -> Vec<AuditAlert> {
        let rules_snapshot = {
            let rules = self.rules.read().await;
            rules.clone()
        };

        let mut last_triggered_snapshot = {
            let last_triggered = self.last_triggered.read().await;
            last_triggered.clone()
        };

        let now = Utc::now();
        let mut alerts = Vec::new();

        for rule in &rules_snapshot {
            if !rule.enabled {
                continue;
            }

            if let Some(&last_time) = last_triggered_snapshot.get(&rule.id) {
                let elapsed_secs = (now - last_time).num_seconds();
                if elapsed_secs < 0 || (elapsed_secs as u64) < rule.cooldown_secs {
                    continue;
                }
            }

            let matched = match &rule.condition {
                AuditCondition::RapidDenials {
                    window_secs,
                    min_count,
                } => {
                    if let Some(ref sink) = self.sink {
                        let window_secs_i64 = i64::try_from(*window_secs).unwrap_or(i64::MAX);
                        let since = now - chrono::Duration::seconds(window_secs_i64);
                        let filter = AuditFilter {
                            subject_id: Some(entry.subject_id.clone()),
                            granted: Some(false),
                            since: Some(since),
                            ..Default::default()
                        };
                        let query_len = sink.count(&filter).await as usize;
                        query_len >= (*min_count as usize)
                    } else {
                        false
                    }
                }
                other => other.evaluate(entry),
            };

            if matched {
                last_triggered_snapshot.insert(rule.id.clone(), now);
                alerts.push(AuditAlert {
                    rule_id: rule.id.clone(),
                    rule_name: rule.name.clone(),
                    action: rule.action.clone(),
                    triggering_entry: Box::new(entry.clone()),
                    created_at: now,
                });
            }
        }

        {
            let mut last_triggered = self.last_triggered.write().await;
            for (id, time) in &last_triggered_snapshot {
                last_triggered.insert(id.clone(), *time);
            }
        }

        alerts
    }

    async fn add_rule(&self, rule: AuditRule) {
        let mut rules = self.rules.write().await;
        rules.push(rule);
    }

    async fn remove_rule(&self, rule_id: &str) -> Result<bool> {
        let mut rules = self.rules.write().await;
        let before = rules.len();
        rules.retain(|r| r.id != rule_id);
        Ok(rules.len() < before)
    }

    async fn list_rules(&self) -> Vec<AuditRule> {
        let rules = self.rules.read().await;
        rules.clone()
    }
}

/// Stateless [`AuditAnalyzer`] computing the bundled summary statistics.
///
/// Counts denials and high-risk entries (risk >= 0.6) and keeps the ten riskiest
/// entries, so the result is a triage aid rather than a complete denial inventory.
pub struct DefaultAuditAnalyzer;

impl DefaultAuditAnalyzer {
    /// Create the analyzer; it holds no state, so one instance serves every query.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl Default for DefaultAuditAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait::async_trait]
impl AuditAnalyzer for DefaultAuditAnalyzer {
    async fn analyze(&self, entries: &[AuditEntry]) -> AuditAnalysisResult {
        let total = entries.len() as u64;
        let denied_count = entries.iter().filter(|e| e.is_denied()).count() as u64;
        let high_risk_count = entries.iter().filter(|e| e.is_high_risk()).count() as u64;

        let mut by_subject: HashMap<String, SubjectStats> = HashMap::new();
        let mut by_permission: HashMap<String, u64> = HashMap::new();
        let mut top_risk: Vec<AuditEntry> = entries.to_vec();

        for entry in entries {
            let stats = by_subject
                .entry(entry.subject_id.clone())
                .or_insert_with(|| SubjectStats {
                    total: 0,
                    denied: 0,
                    avg_risk: 0.0,
                    max_risk: 0.0,
                });
            stats.total += 1;
            if entry.is_denied() {
                stats.denied += 1;
            }
            let risk = entry.verdict.as_ref().map_or(0.0, |v| v.risk_score);
            stats.avg_risk += risk;
            stats.max_risk = stats.max_risk.max(risk);

            *by_permission.entry(entry.permission.clone()).or_insert(0) += 1;
        }

        for stats in by_subject.values_mut() {
            if stats.total > 0 {
                #[allow(clippy::cast_precision_loss)]
                {
                    stats.avg_risk /= stats.total as f64;
                }
            }
        }

        top_risk.sort_by(|a, b| {
            let ra = a.verdict.as_ref().map_or(0.0, |v| v.risk_score);
            let rb = b.verdict.as_ref().map_or(0.0, |v| v.risk_score);
            rb.partial_cmp(&ra).unwrap_or(std::cmp::Ordering::Equal)
        });
        top_risk.truncate(10);

        AuditAnalysisResult {
            total_entries: total,
            denied_count,
            denied_rate: if total > 0 {
                #[allow(clippy::cast_precision_loss)]
                {
                    denied_count as f64 / total as f64
                }
            } else {
                0.0
            },
            high_risk_count,
            by_subject,
            by_permission,
            top_risk_entries: top_risk,
        }
    }
}

/// Audit front door: persist a decision, evaluate policy, dispatch alerts.
///
/// [`AuditLogger::log`] appends to the sink first, so evidence survives a slow or
/// panicking policy engine and hook. Hooks run synchronously on the caller's task;
/// a panicking hook is caught and logged, and the remaining hooks still run.
/// Cloning shares sink, engine, analyzer and hook list - the clone is a handle,
/// not a copy of the state.
pub struct AuditLogger {
    sink: Arc<dyn AuditSink>,
    policy_engine: Option<Arc<dyn AuditPolicyEngine>>,
    analyzer: Option<Arc<dyn AuditAnalyzer>>,
    alert_hooks: Arc<RwLock<Vec<AlertHook>>>,
}

impl AuditLogger {
    /// Build a logger over `sink`, with no policy engine and no analyzer yet.
    pub fn new(sink: impl AuditSink + 'static) -> Self {
        Self {
            sink: Arc::new(sink),
            policy_engine: None,
            analyzer: None,
            alert_hooks: Arc::new(RwLock::new(Vec::new())),
        }
    }

    /// Attach the rule engine whose alerts [`AuditLogger::log`] returns and dispatches.
    #[must_use]
    pub fn with_policy_engine(mut self, engine: impl AuditPolicyEngine + 'static) -> Self {
        self.policy_engine = Some(Arc::new(engine));
        self
    }

    /// Attach the analyzer used by [`AuditLogger::analyze_recent`].
    #[must_use]
    pub fn with_analyzer(mut self, analyzer: impl AuditAnalyzer + 'static) -> Self {
        self.analyzer = Some(Arc::new(analyzer));
        self
    }

    /// Register a callback invoked for every fired alert.
    ///
    /// Hooks run while the logger's hook lock is held, so a blocking hook delays every
    /// later [`AuditLogger::log`] call. A panicking hook is caught and logged; it can
    /// never poison the audit path. Registering is not idempotent.
    pub async fn on_alert(&self, hook: impl Fn(AuditAlert) + Send + Sync + 'static) {
        let mut hooks = self.alert_hooks.write().await;
        hooks.push(Box::new(hook));
    }

    /// Record `entry` and return the alerts its rules produced.
    ///
    /// The entry is appended before rules run, so the audit write does not depend on
    /// policy success. Returned alerts have also been handed to the hooks. The logger
    /// reports; it never grants or denies - the caller owns the decision.
    #[must_use]
    pub async fn log(&self, entry: AuditEntry) -> Vec<AuditAlert> {
        self.sink.append(entry.clone()).await;

        let mut alerts = Vec::new();
        if let Some(ref engine) = self.policy_engine {
            let fired = engine.evaluate(&entry).await;
            if !fired.is_empty() {
                let hooks = self.alert_hooks.read().await;
                for alert in &fired {
                    for hook in hooks.iter() {
                        if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                            hook(alert.clone());
                        }))
                        .is_err()
                        {
                            tracing::error!(target: "kirino::audit", "alert hook panicked for rule '{}'", alert.rule_name);
                        }
                    }
                }
                alerts = fired;
            }
        }

        alerts
    }

    /// Read matching entries from the sink, newest first.
    #[must_use]
    pub async fn query(&self, filter: &AuditFilter) -> Vec<AuditEntry> {
        self.sink.query(filter).await
    }

    /// Count matching entries in the sink.
    #[must_use]
    pub async fn count(&self, filter: &AuditFilter) -> u64 {
        self.sink.count(filter).await
    }

    /// Query the sink and analyze the result; `None` when no analyzer is attached.
    ///
    /// Runs over one snapshot, so entries appended afterwards are not reflected and
    /// `filter.limit` bounds the analysis window.
    #[must_use]
    pub async fn analyze_recent(&self, filter: &AuditFilter) -> Option<AuditAnalysisResult> {
        let analyzer = self.analyzer.as_ref()?;
        let entries = self.sink.query(filter).await;
        Some(analyzer.analyze(&entries).await)
    }
}

impl Clone for AuditLogger {
    fn clone(&self) -> Self {
        Self {
            sink: self.sink.clone(),
            policy_engine: self.policy_engine.clone(),
            analyzer: self.analyzer.clone(),
            alert_hooks: self.alert_hooks.clone(),
        }
    }
}

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

    fn make_entry(subject: &str, permission: &str, granted: bool, risk: f64) -> AuditEntry {
        AuditEntry {
            id: 0,
            subject_id: subject.to_string(),
            subject_type: "user".to_string(),
            permission: permission.to_string(),
            endpoint: "/api/test".to_string(),
            granted,
            created_at: Utc::now(),
            verdict: Some(AuditVerdict {
                autonomy_level: if granted { "L4" } else { "L0" }.to_string(),
                risk_score: risk,
                sub_scores: AuditSubScores {
                    delegator_weight: 0.0,
                    trust_penalty: 1.0 - risk,
                    sensitivity: 0.5,
                    domain_mismatch: 0.0,
                    anomaly: 0.0,
                },
                evidence: vec![],
                mitigation: None,
            }),
        }
    }

    #[tokio::test]
    async fn test_sink_append_and_query() {
        let sink = InMemoryAuditSink::new();
        sink.append(make_entry("user1", "read", true, 0.1)).await;
        sink.append(make_entry("user2", "write", false, 0.8)).await;
        sink.append(make_entry("user1", "delete", false, 0.9)).await;

        let all = sink.query(&AuditFilter::default()).await;
        assert_eq!(all.len(), 3);

        let user1 = sink
            .query(&AuditFilter {
                subject_id: Some("user1".to_string()),
                ..Default::default()
            })
            .await;
        assert_eq!(user1.len(), 2);

        let denied = sink
            .query(&AuditFilter {
                granted: Some(false),
                ..Default::default()
            })
            .await;
        assert_eq!(denied.len(), 2);

        let high = sink
            .query(&AuditFilter {
                min_risk: Some(0.7),
                ..Default::default()
            })
            .await;
        assert_eq!(high.len(), 2);
    }

    #[tokio::test]
    async fn test_sink_count() {
        let sink = InMemoryAuditSink::new();
        sink.append(make_entry("u1", "read", true, 0.1)).await;
        sink.append(make_entry("u2", "write", false, 0.8)).await;

        let count = sink
            .count(&AuditFilter {
                granted: Some(false),
                ..Default::default()
            })
            .await;
        assert_eq!(count, 1);
    }

    #[tokio::test]
    async fn test_filter_limit() {
        let sink = InMemoryAuditSink::new();
        for i in 0..10 {
            sink.append(make_entry(&format!("u{}", i), "read", true, 0.1))
                .await;
        }
        let result = sink
            .query(&AuditFilter {
                limit: Some(3),
                ..Default::default()
            })
            .await;
        assert_eq!(result.len(), 3);
    }

    #[tokio::test]
    async fn test_sink_max_entries_eviction() {
        let sink = InMemoryAuditSink::with_max_entries(5);
        for i in 0..10 {
            sink.append(make_entry(&format!("u{}", i), "read", true, 0.1))
                .await;
        }
        let all = sink.query(&AuditFilter::default()).await;
        assert_eq!(all.len(), 5);
        let mut subjects: Vec<String> = all.iter().map(|e| e.subject_id.clone()).collect();
        subjects.sort();
        assert_eq!(subjects, vec!["u5", "u6", "u7", "u8", "u9"]);
    }

    #[tokio::test]
    async fn test_policy_engine_rules() {
        let engine = InMemoryAuditPolicyEngine::new();
        engine
            .add_rule(AuditRule {
                id: "r1".to_string(),
                name: "deny-alert".to_string(),
                enabled: true,
                condition: AuditCondition::Denied,
                action: AuditAction::Alert {
                    message: "denied".to_string(),
                    severity: AuditSeverity::Warning,
                },
                cooldown_secs: 0,
            })
            .await;

        let rules = engine.list_rules().await;
        assert_eq!(rules.len(), 1);

        let alerts = engine.evaluate(&make_entry("u1", "read", true, 0.1)).await;
        assert!(alerts.is_empty());

        let alerts = engine
            .evaluate(&make_entry("u1", "write", false, 0.8))
            .await;
        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].rule_id, "r1");
    }

    #[tokio::test]
    async fn test_policy_engine_cooldown() {
        let engine = InMemoryAuditPolicyEngine::new();
        engine
            .add_rule(AuditRule {
                id: "r1".to_string(),
                name: "deny".to_string(),
                enabled: true,
                condition: AuditCondition::Denied,
                action: AuditAction::Alert {
                    message: "denied".to_string(),
                    severity: AuditSeverity::Warning,
                },
                cooldown_secs: 3600,
            })
            .await;

        let a1 = engine
            .evaluate(&make_entry("u1", "write", false, 0.8))
            .await;
        assert_eq!(a1.len(), 1);

        let a2 = engine
            .evaluate(&make_entry("u1", "write", false, 0.9))
            .await;
        assert!(a2.is_empty());
    }

    #[tokio::test]
    async fn test_policy_engine_composite() {
        let engine = InMemoryAuditPolicyEngine::new();
        engine
            .add_rule(AuditRule {
                id: "r1".to_string(),
                name: "high-risk-denial".to_string(),
                enabled: true,
                condition: AuditCondition::Composite {
                    conditions: vec![
                        AuditCondition::Denied,
                        AuditCondition::HighRisk { threshold: 0.7 },
                    ],
                    operator: LogicalOp::All,
                },
                action: AuditAction::Alert {
                    message: "critical".to_string(),
                    severity: AuditSeverity::Critical,
                },
                cooldown_secs: 0,
            })
            .await;

        let alerts = engine.evaluate(&make_entry("u1", "read", true, 0.1)).await;
        assert!(alerts.is_empty());

        let alerts = engine
            .evaluate(&make_entry("u1", "write", false, 0.5))
            .await;
        assert!(alerts.is_empty());

        let alerts = engine
            .evaluate(&make_entry("u1", "write", false, 0.9))
            .await;
        assert_eq!(alerts.len(), 1);
    }

    #[tokio::test]
    async fn test_analyzer() {
        let analyzer = DefaultAuditAnalyzer::new();
        let entries = vec![
            make_entry("u1", "read", true, 0.1),
            make_entry("u1", "write", false, 0.8),
            make_entry("u2", "read", true, 0.2),
            make_entry("u2", "delete", false, 0.9),
            make_entry("u2", "write", false, 0.7),
        ];

        let result = analyzer.analyze(&entries).await;
        assert_eq!(result.total_entries, 5);
        assert_eq!(result.denied_count, 3);
        assert!((result.denied_rate - 0.6).abs() < 1e-10);
        assert_eq!(result.high_risk_count, 3);

        let u1 = result.by_subject.get("u1").unwrap();
        assert_eq!(u1.total, 2);
        assert_eq!(u1.denied, 1);

        let u2 = result.by_subject.get("u2").unwrap();
        assert_eq!(u2.total, 3);
        assert_eq!(u2.denied, 2);
        assert!((u2.max_risk - 0.9).abs() < 1e-10);

        assert_eq!(result.top_risk_entries.len(), 5);
        let top_risk = result.top_risk_entries[0]
            .verdict
            .as_ref()
            .unwrap()
            .risk_score;
        assert!((top_risk - 0.9).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_audit_logger_full_pipeline() {
        let logger = AuditLogger::new(InMemoryAuditSink::new())
            .with_policy_engine(InMemoryAuditPolicyEngine::new())
            .with_analyzer(DefaultAuditAnalyzer::new());

        let _ = logger.log(make_entry("u1", "read", true, 0.1)).await;
        let _ = logger.log(make_entry("u1", "write", false, 0.8)).await;
        let _ = logger.log(make_entry("u2", "delete", false, 0.9)).await;

        let all = logger.query(&AuditFilter::default()).await;
        assert_eq!(all.len(), 3);

        let result = logger
            .analyze_recent(&AuditFilter::default())
            .await
            .unwrap();
        assert_eq!(result.total_entries, 3);
        assert_eq!(result.denied_count, 2);
    }

    #[tokio::test]
    async fn test_audit_logger_with_alert_hook() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        let counter = Arc::new(AtomicUsize::new(0));
        let counter_clone = counter.clone();

        let engine = InMemoryAuditPolicyEngine::new();
        engine
            .add_rule(AuditRule {
                id: "r1".to_string(),
                name: "deny".to_string(),
                enabled: true,
                condition: AuditCondition::Denied,
                action: AuditAction::Alert {
                    message: "denied".to_string(),
                    severity: AuditSeverity::Warning,
                },
                cooldown_secs: 0,
            })
            .await;

        let logger = AuditLogger::new(InMemoryAuditSink::new()).with_policy_engine(engine);

        logger
            .on_alert(move |_alert| {
                counter_clone.fetch_add(1, Ordering::SeqCst);
            })
            .await;

        let _ = logger.log(make_entry("u1", "read", true, 0.1)).await;
        let _ = logger.log(make_entry("u1", "write", false, 0.8)).await;

        assert_eq!(counter.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn test_remove_rule() {
        let engine = InMemoryAuditPolicyEngine::new();
        engine
            .add_rule(AuditRule {
                id: "r1".to_string(),
                name: "deny".to_string(),
                enabled: true,
                condition: AuditCondition::Denied,
                action: AuditAction::Alert {
                    message: "denied".to_string(),
                    severity: AuditSeverity::Warning,
                },
                cooldown_secs: 0,
            })
            .await;

        assert_eq!(engine.list_rules().await.len(), 1);
        assert!(engine.remove_rule("r1").await.unwrap());
        assert!(engine.list_rules().await.is_empty());
    }

    #[tokio::test]
    async fn test_trust_below_condition() {
        let cond = AuditCondition::TrustBelow { threshold: 0.5 };
        let entry_low_trust = make_entry("u1", "read", true, 0.1);
        assert!(cond.evaluate(&entry_low_trust));

        let entry_high_trust = make_entry("u1", "read", true, 0.9);
        assert!(!cond.evaluate(&entry_high_trust));
    }

    #[tokio::test]
    async fn test_rapid_denials_with_sink() {
        let sink = Arc::new(InMemoryAuditSink::new());
        let engine = InMemoryAuditPolicyEngine::with_sink(sink.clone());
        engine
            .add_rule(AuditRule {
                id: "rapid".to_string(),
                name: "rapid-denials".to_string(),
                enabled: true,
                condition: AuditCondition::RapidDenials {
                    window_secs: 60,
                    min_count: 3,
                },
                action: AuditAction::Alert {
                    message: "rapid denials detected".to_string(),
                    severity: AuditSeverity::Critical,
                },
                cooldown_secs: 0,
            })
            .await;

        let denied_entry = make_entry("u1", "write", false, 0.8);

        sink.append(make_entry("u1", "write", false, 0.8)).await;
        sink.append(make_entry("u1", "write", false, 0.7)).await;
        let alerts = engine.evaluate(&denied_entry).await;
        assert!(alerts.is_empty());

        sink.append(make_entry("u1", "write", false, 0.9)).await;
        let alerts = engine.evaluate(&denied_entry).await;
        assert_eq!(alerts.len(), 1);
        assert_eq!(alerts[0].rule_id, "rapid");
    }

    #[tokio::test]
    async fn test_rapid_denials_without_sink() {
        let engine = InMemoryAuditPolicyEngine::new();
        engine
            .add_rule(AuditRule {
                id: "rapid".to_string(),
                name: "rapid".to_string(),
                enabled: true,
                condition: AuditCondition::RapidDenials {
                    window_secs: 60,
                    min_count: 1,
                },
                action: AuditAction::Alert {
                    message: "rapid".to_string(),
                    severity: AuditSeverity::Critical,
                },
                cooldown_secs: 0,
            })
            .await;

        let entry = make_entry("u1", "write", false, 0.8);
        let alerts = engine.evaluate(&entry).await;
        assert!(alerts.is_empty());
    }

    #[test]
    fn test_category_sensitive_condition() {
        let cond = AuditCondition::CategorySensitive { min_weight: 0.4 };
        let entry = make_entry("u1", "write", true, 0.3);
        assert!(cond.evaluate(&entry));

        let entry_low = {
            let mut e = make_entry("u1", "write", true, 0.3);
            if let Some(ref mut v) = e.verdict {
                v.sub_scores.sensitivity = 0.1;
            }
            e
        };
        assert!(!cond.evaluate(&entry_low));
    }

    #[test]
    fn test_domain_mismatch_condition() {
        let cond = AuditCondition::DomainMismatch { min_weight: 0.3 };
        let entry = {
            let mut e = make_entry("u1", "write", true, 0.3);
            if let Some(ref mut v) = e.verdict {
                v.sub_scores.domain_mismatch = 0.5;
            }
            e
        };
        assert!(cond.evaluate(&entry));

        let entry_low = make_entry("u1", "write", true, 0.3);
        assert!(!cond.evaluate(&entry_low));
    }

    #[test]
    fn test_composite_any_operator() {
        let cond = AuditCondition::Composite {
            conditions: vec![
                AuditCondition::Denied,
                AuditCondition::HighRisk { threshold: 0.9 },
            ],
            operator: LogicalOp::Any,
        };

        let denied_entry = make_entry("u1", "write", false, 0.1);
        assert!(cond.evaluate(&denied_entry));

        let high_risk_entry = make_entry("u1", "write", true, 0.95);
        assert!(cond.evaluate(&high_risk_entry));

        let normal_entry = make_entry("u1", "write", true, 0.1);
        assert!(!cond.evaluate(&normal_entry));
    }

    #[test]
    fn test_disabled_rule_not_evaluated() {
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_time()
            .build()
            .unwrap();
        rt.block_on(async {
            let engine = InMemoryAuditPolicyEngine::new();
            engine
                .add_rule(AuditRule {
                    id: "disabled-rule".to_string(),
                    name: "should-not-fire".to_string(),
                    enabled: false,
                    condition: AuditCondition::Denied,
                    action: AuditAction::Alert {
                        message: "should not fire".to_string(),
                        severity: AuditSeverity::Warning,
                    },
                    cooldown_secs: 0,
                })
                .await;

            let entry = make_entry("u1", "write", false, 0.5);
            let alerts = engine.evaluate(&entry).await;
            assert!(alerts.is_empty());
        });
    }

    #[test]
    fn test_entry_without_verdict() {
        let entry = AuditEntry {
            id: 1,
            subject_id: "u1".to_string(),
            subject_type: "user".to_string(),
            permission: "read".to_string(),
            endpoint: "/test".to_string(),
            granted: false,
            created_at: Utc::now(),
            verdict: None,
        };

        let cond = AuditCondition::HighRisk { threshold: 0.5 };
        assert!(!cond.evaluate(&entry));

        let cond = AuditCondition::CategorySensitive { min_weight: 0.1 };
        assert!(!cond.evaluate(&entry));

        let cond = AuditCondition::DomainMismatch { min_weight: 0.1 };
        assert!(!cond.evaluate(&entry));

        let cond = AuditCondition::TrustBelow { threshold: 0.5 };
        assert!(!cond.evaluate(&entry));

        assert!(AuditCondition::Denied.evaluate(&entry));
    }
}