minco-plugin-audit 1.7.0

Append-only audit event port and reference memory sink for Minco
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
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
//! Additive V2 contracts for durable, queryable and lifecycle-aware audit ledgers.

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use minco_core::DataClass;
use serde::{Deserialize, Serialize};
use std::{
    collections::{BTreeMap, BTreeSet},
    sync::Arc,
};
use tokio::sync::RwLock;
use uuid::Uuid;

/// Maximum encoded size of one V2 record before provider contact.
pub const MAX_AUDIT_RECORD_BYTES: usize = 64 * 1024;
/// Maximum records in one portable ledger batch.
pub const MAX_AUDIT_BATCH_RECORDS: usize = 100;
/// Maximum encoded payload in one portable ledger batch.
pub const MAX_AUDIT_BATCH_BYTES: usize = 4 * 1024 * 1024;
/// Maximum number of explicit related resources on one record.
pub const MAX_AUDIT_RELATED_RESOURCES: usize = 8;
/// Maximum changed fields on one record.
pub const MAX_AUDIT_CHANGED_FIELDS: usize = 128;
/// Maximum safe labels on one record.
pub const MAX_AUDIT_LABELS: usize = 32;
/// Maximum records returned by one resource-history query.
pub const MAX_AUDIT_PAGE_SIZE: usize = 100;
/// Maximum encoded size of one literal changed-field value.
pub const MAX_AUDIT_LITERAL_BYTES: usize = 4 * 1024;

const MIB: u64 = 1024 * 1024;

/// Opaque application resource identity. It never implies a database foreign key.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditResourceRef {
    pub resource_type: String,
    pub resource_id: String,
}

impl AuditResourceRef {
    pub fn new(resource_type: impl Into<String>, resource_id: impl Into<String>) -> Self {
        Self {
            resource_type: resource_type.into(),
            resource_id: resource_id.into(),
        }
    }
}

/// A bounded relationship used to gather child or adjacent action history.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditRelatedResource {
    pub relation: String,
    pub resource: AuditResourceRef,
}

/// Stable kind of principal responsible for an action.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditActorKind {
    Human,
    Service,
    System,
    Migration,
    DatabasePrincipal,
    Unknown,
}

/// Actor snapshot retained without a foreign key to an identity table.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditActor {
    pub kind: AuditActorKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subject: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub effective_subject: Option<String>,
}

impl AuditActor {
    pub fn human(subject: impl Into<String>) -> Self {
        Self {
            kind: AuditActorKind::Human,
            subject: Some(subject.into()),
            effective_subject: None,
        }
    }

    #[must_use]
    pub const fn unknown() -> Self {
        Self {
            kind: AuditActorKind::Unknown,
            subject: None,
            effective_subject: None,
        }
    }
}

/// Origin distinguishes authoritative semantic actions from supporting evidence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditOrigin {
    Application,
    Migration,
    DatabaseEvidence,
    Import,
}

/// A field value that makes privacy handling explicit in the record itself.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum AuditValue {
    Literal { value: serde_json::Value },
    Redacted,
    Digest { digest: AuditDigest },
    Omitted,
}

impl AuditValue {
    pub fn literal(value: impl Into<serde_json::Value>) -> Self {
        Self::Literal {
            value: value.into(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditDigestAlgorithm {
    Sha256,
}

/// Bounded digest used instead of retaining a sensitive raw value.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditDigest {
    pub algorithm: AuditDigestAlgorithm,
    pub value: String,
}

impl AuditDigest {
    pub fn sha256(value: impl Into<String>) -> Self {
        Self {
            algorithm: AuditDigestAlgorithm::Sha256,
            value: value.into(),
        }
    }

    fn validate(&self) -> Result<(), AuditLedgerError> {
        if self.value.len() != 64 || !self.value.bytes().all(|byte| byte.is_ascii_hexdigit()) {
            return Err(AuditLedgerError::InvalidRecord(
                "SHA-256 digest must contain exactly 64 hexadecimal bytes".into(),
            ));
        }
        Ok(())
    }
}

/// Before/after representation for one allowlisted field.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditFieldChange {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub before: Option<AuditValue>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub after: Option<AuditValue>,
}

/// Position inside one source transaction when multiple actions commit together.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditTransactionRef {
    pub id: Uuid,
    pub ordinal: u32,
}

/// Durable semantic action record. This is additive to the legacy `AuditEvent`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditRecordV2 {
    pub schema_version: u16,
    pub event_id: Uuid,
    pub tenant_scope: String,
    pub action: String,
    pub resource: AuditResourceRef,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub related_resources: Vec<AuditRelatedResource>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resource_revision: Option<u64>,
    pub actor: AuditActor,
    pub operation_id: String,
    pub correlation_id: Uuid,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub causation_id: Option<Uuid>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub idempotency_key_digest: Option<AuditDigest>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub transaction: Option<AuditTransactionRef>,
    pub occurred_at: DateTime<Utc>,
    pub recorded_at: DateTime<Utc>,
    pub origin: AuditOrigin,
    pub data_class: DataClass,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub changes: BTreeMap<String, AuditFieldChange>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub labels: BTreeMap<String, String>,
}

impl AuditRecordV2 {
    pub fn new(
        tenant_scope: impl Into<String>,
        action: impl Into<String>,
        resource: AuditResourceRef,
        actor: AuditActor,
        operation_id: impl Into<String>,
        correlation_id: Uuid,
    ) -> Self {
        let now = Utc::now();
        Self {
            schema_version: 2,
            event_id: Uuid::now_v7(),
            tenant_scope: tenant_scope.into(),
            action: action.into(),
            resource,
            related_resources: Vec::new(),
            resource_revision: None,
            actor,
            operation_id: operation_id.into(),
            correlation_id,
            causation_id: None,
            idempotency_key_digest: None,
            transaction: None,
            occurred_at: now,
            recorded_at: now,
            origin: AuditOrigin::Application,
            data_class: DataClass::Internal,
            changes: BTreeMap::new(),
            labels: BTreeMap::new(),
        }
    }

    /// Validates portable provider limits and returns the encoded byte size.
    pub fn validate(&self) -> Result<usize, AuditLedgerError> {
        if self.schema_version != 2 {
            return Err(AuditLedgerError::InvalidRecord(
                "schema_version must be 2".into(),
            ));
        }
        if self.event_id.is_nil() || self.correlation_id.is_nil() {
            return Err(AuditLedgerError::InvalidRecord(
                "event_id and correlation_id must be non-nil".into(),
            ));
        }
        if self.causation_id.is_some_and(|id| id.is_nil())
            || self
                .transaction
                .as_ref()
                .is_some_and(|item| item.id.is_nil())
        {
            return Err(AuditLedgerError::InvalidRecord(
                "causation and transaction IDs must be non-nil when present".into(),
            ));
        }
        validate_text("tenant_scope", &self.tenant_scope, 256)?;
        validate_text("action", &self.action, 128)?;
        validate_resource(&self.resource)?;
        validate_text("operation_id", &self.operation_id, 128)?;
        validate_optional_text("actor.subject", self.actor.subject.as_deref(), 512)?;
        validate_optional_text(
            "actor.effective_subject",
            self.actor.effective_subject.as_deref(),
            512,
        )?;
        if let Some(digest) = &self.idempotency_key_digest {
            digest.validate()?;
        }
        match self.actor.kind {
            AuditActorKind::Unknown
                if self.actor.subject.is_some() || self.actor.effective_subject.is_some() =>
            {
                return Err(AuditLedgerError::InvalidRecord(
                    "unknown actors cannot carry an attributed subject".into(),
                ));
            }
            AuditActorKind::Unknown => {}
            _ if self.actor.subject.is_none() => {
                return Err(AuditLedgerError::InvalidRecord(
                    "attributed actors require a stable subject".into(),
                ));
            }
            _ => {}
        }
        if self.related_resources.len() > MAX_AUDIT_RELATED_RESOURCES {
            return Err(AuditLedgerError::InvalidRecord(format!(
                "related_resources exceeds {MAX_AUDIT_RELATED_RESOURCES}"
            )));
        }
        let mut related = BTreeSet::new();
        for relation in &self.related_resources {
            validate_text("related_resources.relation", &relation.relation, 64)?;
            validate_resource(&relation.resource)?;
            let key = (
                relation.relation.as_str(),
                relation.resource.resource_type.as_str(),
                relation.resource.resource_id.as_str(),
            );
            if !related.insert(key) {
                return Err(AuditLedgerError::InvalidRecord(
                    "related_resources contains a duplicate".into(),
                ));
            }
        }
        if self.changes.len() > MAX_AUDIT_CHANGED_FIELDS {
            return Err(AuditLedgerError::InvalidRecord(format!(
                "changes exceeds {MAX_AUDIT_CHANGED_FIELDS}"
            )));
        }
        for (field, change) in &self.changes {
            validate_text("changes field", field, 128)?;
            if change.before == change.after {
                return Err(AuditLedgerError::InvalidRecord(format!(
                    "change {field} has identical before and after values"
                )));
            }
            validate_audit_value(change.before.as_ref(), self.data_class)?;
            validate_audit_value(change.after.as_ref(), self.data_class)?;
        }
        if self.labels.len() > MAX_AUDIT_LABELS {
            return Err(AuditLedgerError::InvalidRecord(format!(
                "labels exceeds {MAX_AUDIT_LABELS}"
            )));
        }
        for (key, value) in &self.labels {
            validate_text("label key", key, 64)?;
            validate_text("label value", value, 512)?;
        }
        let bytes = serde_json::to_vec(self)
            .map_err(|_| AuditLedgerError::Encoding)?
            .len();
        if bytes > MAX_AUDIT_RECORD_BYTES {
            return Err(AuditLedgerError::RecordTooLarge {
                bytes,
                maximum: MAX_AUDIT_RECORD_BYTES,
            });
        }
        Ok(bytes)
    }
}

fn validate_resource(resource: &AuditResourceRef) -> Result<(), AuditLedgerError> {
    validate_text("resource_type", &resource.resource_type, 128)?;
    validate_text("resource_id", &resource.resource_id, 512)
}

fn validate_text(name: &str, value: &str, maximum: usize) -> Result<(), AuditLedgerError> {
    if value.trim().is_empty() || value.len() > maximum || value.chars().any(char::is_control) {
        return Err(AuditLedgerError::InvalidRecord(format!(
            "{name} must contain between 1 and {maximum} non-control bytes"
        )));
    }
    Ok(())
}

fn validate_optional_text(
    name: &str,
    value: Option<&str>,
    maximum: usize,
) -> Result<(), AuditLedgerError> {
    value.map_or(Ok(()), |value| validate_text(name, value, maximum))
}

fn validate_audit_value(
    value: Option<&AuditValue>,
    data_class: DataClass,
) -> Result<(), AuditLedgerError> {
    match value {
        Some(AuditValue::Literal { value }) => {
            if data_class == DataClass::Secret {
                return Err(AuditLedgerError::InvalidRecord(
                    "secret-class audit changes must be redacted, digested or omitted".into(),
                ));
            }
            if value.is_array() || value.is_object() {
                return Err(AuditLedgerError::InvalidRecord(
                    "literal audit values must be scalar".into(),
                ));
            }
            let bytes = serde_json::to_vec(value)
                .map_err(|_| AuditLedgerError::Encoding)?
                .len();
            if bytes > MAX_AUDIT_LITERAL_BYTES {
                return Err(AuditLedgerError::InvalidRecord(format!(
                    "literal audit value exceeds {MAX_AUDIT_LITERAL_BYTES} bytes"
                )));
            }
        }
        Some(AuditValue::Digest { digest }) => digest.validate()?,
        Some(AuditValue::Redacted | AuditValue::Omitted) | None => {}
    }
    Ok(())
}

/// Stable ledger position independent of a physical table, file or segment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditCursor {
    pub occurred_at: DateTime<Utc>,
    pub event_id: Uuid,
}

impl From<&AuditRecordV2> for AuditCursor {
    fn from(record: &AuditRecordV2) -> Self {
        Self {
            occurred_at: record.occurred_at,
            event_id: record.event_id,
        }
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditSortDirection {
    OldestFirst,
    #[default]
    NewestFirst,
}

/// Bounded resource-history query. Authorization remains in the application use case.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditQuery {
    pub tenant_scope: String,
    pub resource: AuditResourceRef,
    #[serde(default)]
    pub include_related: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub relation: Option<String>,
    #[serde(default)]
    pub direction: AuditSortDirection,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub after: Option<AuditCursor>,
    pub limit: usize,
}

impl AuditQuery {
    pub fn for_resource(tenant_scope: impl Into<String>, resource: AuditResourceRef) -> Self {
        Self {
            tenant_scope: tenant_scope.into(),
            resource,
            include_related: false,
            relation: None,
            direction: AuditSortDirection::NewestFirst,
            after: None,
            limit: 50,
        }
    }

    pub fn validate(&self) -> Result<(), AuditLedgerError> {
        validate_text("tenant_scope", &self.tenant_scope, 256)?;
        validate_resource(&self.resource)?;
        validate_optional_text("relation", self.relation.as_deref(), 64)?;
        if self.relation.is_some() && !self.include_related {
            return Err(AuditLedgerError::InvalidQuery(
                "relation requires include_related".into(),
            ));
        }
        if self.limit == 0 || self.limit > MAX_AUDIT_PAGE_SIZE {
            return Err(AuditLedgerError::InvalidQuery(format!(
                "limit must be between 1 and {MAX_AUDIT_PAGE_SIZE}"
            )));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditPage {
    pub records: Vec<AuditRecordV2>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<AuditCursor>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditAppendReport {
    pub requested: usize,
    pub inserted: usize,
    pub duplicates: usize,
}

#[async_trait]
pub trait AuditLedgerWriter: Send + Sync + std::fmt::Debug {
    /// Atomically appends a bounded batch.
    ///
    /// Repeating the same ID and content is an idempotent duplicate. Reusing an
    /// ID for different content fails the complete batch.
    async fn append_batch(
        &self,
        records: &[AuditRecordV2],
    ) -> Result<AuditAppendReport, AuditLedgerError>;
}

#[async_trait]
pub trait AuditReader: Send + Sync + std::fmt::Debug {
    async fn list_resource_history(
        &self,
        query: &AuditQuery,
    ) -> Result<AuditPage, AuditLedgerError>;
}

/// Size thresholds for finite-disk ledgers. `None` means not applicable, not unchecked.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditSizePolicy {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub warn_at_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rotate_at_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reject_at_bytes: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub minimum_free_bytes: Option<u64>,
}

impl AuditSizePolicy {
    /// Initial `SQLite` recommendation: 80 MiB warning, 100 MiB rotation and
    /// 125 MiB hard stop. Deployments must separately size free-disk reserve.
    #[must_use]
    pub const fn sqlite_100_mib(minimum_free_bytes: u64) -> Self {
        Self {
            warn_at_bytes: Some(80 * MIB),
            rotate_at_bytes: Some(100 * MIB),
            reject_at_bytes: Some(125 * MIB),
            minimum_free_bytes: Some(minimum_free_bytes),
        }
    }

    fn validate(self) -> Result<(), AuditLedgerError> {
        let ordered = [
            self.warn_at_bytes,
            self.rotate_at_bytes,
            self.reject_at_bytes,
        ];
        if ordered.iter().any(Option::is_some) && ordered.iter().any(Option::is_none) {
            return Err(AuditLedgerError::InvalidLifecycle(
                "warn, rotate and reject byte thresholds must be declared together".into(),
            ));
        }
        if let [Some(warn), Some(rotate), Some(reject)] = ordered
            && (warn == 0 || warn >= rotate || rotate >= reject)
        {
            return Err(AuditLedgerError::InvalidLifecycle(
                "byte thresholds must satisfy 0 < warn < rotate < reject".into(),
            ));
        }
        Ok(())
    }
}

/// Retention requires archive proof before any provider may delete hot history.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditRetentionPolicy {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub archive_after_seconds: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub delete_after_seconds: Option<u64>,
    #[serde(default)]
    pub require_archive_receipt: bool,
}

impl AuditRetentionPolicy {
    fn validate(self) -> Result<(), AuditLedgerError> {
        if let Some(delete_after) = self.delete_after_seconds {
            let archive_after = self.archive_after_seconds.ok_or_else(|| {
                AuditLedgerError::InvalidLifecycle(
                    "delete_after_seconds requires archive_after_seconds".into(),
                )
            })?;
            if delete_after < archive_after || !self.require_archive_receipt {
                return Err(AuditLedgerError::InvalidLifecycle(
                    "deletion must follow archive and require an archive receipt".into(),
                ));
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditLifecyclePolicy {
    pub size: AuditSizePolicy,
    pub retention: AuditRetentionPolicy,
    pub maximum_pending_records: u64,
    pub maximum_pending_bytes: u64,
    pub maximum_oldest_pending_seconds: u64,
}

impl AuditLifecyclePolicy {
    #[must_use]
    pub const fn cloud_online() -> Self {
        Self {
            size: AuditSizePolicy {
                warn_at_bytes: None,
                rotate_at_bytes: None,
                reject_at_bytes: None,
                minimum_free_bytes: None,
            },
            retention: AuditRetentionPolicy {
                archive_after_seconds: None,
                delete_after_seconds: None,
                require_archive_receipt: false,
            },
            maximum_pending_records: 0,
            maximum_pending_bytes: 0,
            maximum_oldest_pending_seconds: 0,
        }
    }

    #[must_use]
    pub const fn sqlite_100_mib(minimum_free_bytes: u64) -> Self {
        Self {
            size: AuditSizePolicy::sqlite_100_mib(minimum_free_bytes),
            retention: AuditRetentionPolicy {
                archive_after_seconds: None,
                delete_after_seconds: None,
                require_archive_receipt: false,
            },
            maximum_pending_records: 100_000,
            maximum_pending_bytes: 64 * MIB,
            maximum_oldest_pending_seconds: 3_600,
        }
    }

    pub fn validate(self) -> Result<(), AuditLedgerError> {
        self.size.validate()?;
        self.retention.validate()?;
        let pending = [
            self.maximum_pending_records,
            self.maximum_pending_bytes,
            self.maximum_oldest_pending_seconds,
        ];
        if pending.iter().any(|value| *value > 0) && pending.contains(&0) {
            return Err(AuditLedgerError::InvalidLifecycle(
                "pending record, byte and age limits must be declared together".into(),
            ));
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditSegmentState {
    Active,
    Sealed,
    Archived,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditArchiveReceipt {
    pub archive_id: String,
    pub digest: AuditDigest,
    pub archived_at: DateTime<Utc>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditSegmentStatus {
    pub segment_id: u64,
    pub state: AuditSegmentState,
    pub record_count: u64,
    pub encoded_bytes: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub first: Option<AuditCursor>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last: Option<AuditCursor>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub archive_receipt: Option<AuditArchiveReceipt>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditStorageSnapshot {
    pub provider: String,
    pub hot_bytes: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub free_bytes: Option<u64>,
    pub pending_records: u64,
    pub pending_bytes: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub oldest_pending_seconds: Option<u64>,
    pub quarantined_records: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub archive_watermark: Option<AuditCursor>,
    pub segments: Vec<AuditSegmentStatus>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuditHealthSeverity {
    Healthy,
    Warning,
    RotationRequired,
    Critical,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuditStorageHealth {
    pub severity: AuditHealthSeverity,
    pub reasons: Vec<String>,
    pub snapshot: AuditStorageSnapshot,
}

#[async_trait]
pub trait AuditStorageInspector: Send + Sync + std::fmt::Debug {
    async fn storage_health(&self) -> Result<AuditStorageHealth, AuditLedgerError>;
}

/// Applies one provider-neutral severity model to a provider snapshot.
pub fn evaluate_storage_health(
    policy: AuditLifecyclePolicy,
    snapshot: AuditStorageSnapshot,
) -> Result<AuditStorageHealth, AuditLedgerError> {
    policy.validate()?;
    validate_text("storage provider", &snapshot.provider, 128)?;
    let mut severity = AuditHealthSeverity::Healthy;
    let mut reasons = Vec::new();
    let active_bytes = snapshot
        .segments
        .iter()
        .find(|segment| segment.state == AuditSegmentState::Active)
        .map_or(0, |segment| segment.encoded_bytes);
    if let Some(warn) = policy.size.warn_at_bytes
        && active_bytes >= warn
    {
        severity = severity.max(AuditHealthSeverity::Warning);
        reasons.push(format!("active segment reached warning threshold {warn}"));
    }
    if let Some(rotate) = policy.size.rotate_at_bytes
        && active_bytes >= rotate
    {
        severity = severity.max(AuditHealthSeverity::RotationRequired);
        reasons.push(format!(
            "active segment reached rotation threshold {rotate}"
        ));
    }
    if let Some(reject) = policy.size.reject_at_bytes
        && active_bytes >= reject
    {
        severity = AuditHealthSeverity::Critical;
        reasons.push(format!(
            "active segment reached rejection threshold {reject}"
        ));
    }
    if let (Some(free), Some(minimum)) = (snapshot.free_bytes, policy.size.minimum_free_bytes) {
        if free < minimum {
            severity = AuditHealthSeverity::Critical;
            reasons.push(format!(
                "free storage {free} is below required reserve {minimum}"
            ));
        } else if free < minimum.saturating_add(minimum / 4) {
            severity = severity.max(AuditHealthSeverity::Warning);
            reasons.push(format!(
                "free storage {free} is approaching required reserve {minimum}"
            ));
        }
    }
    evaluate_limit(
        snapshot.pending_records,
        policy.maximum_pending_records,
        "pending journal record",
        &mut severity,
        &mut reasons,
    );
    evaluate_limit(
        snapshot.pending_bytes,
        policy.maximum_pending_bytes,
        "pending journal byte",
        &mut severity,
        &mut reasons,
    );
    if let Some(age) = snapshot.oldest_pending_seconds {
        evaluate_limit(
            age,
            policy.maximum_oldest_pending_seconds,
            "oldest pending journal age",
            &mut severity,
            &mut reasons,
        );
    }
    if snapshot.quarantined_records > 0 {
        severity = severity.max(AuditHealthSeverity::Warning);
        reasons.push("quarantined audit records require attention".into());
    }
    Ok(AuditStorageHealth {
        severity,
        reasons,
        snapshot,
    })
}

fn evaluate_limit(
    value: u64,
    limit: u64,
    name: &str,
    severity: &mut AuditHealthSeverity,
    reasons: &mut Vec<String>,
) {
    if limit == 0 {
        return;
    }
    if value >= limit {
        *severity = AuditHealthSeverity::Critical;
        reasons.push(format!("{name} limit {limit} reached"));
    } else if value >= limit.saturating_sub(limit / 5) {
        *severity = (*severity).max(AuditHealthSeverity::Warning);
        reasons.push(format!("{name} is approaching limit {limit}"));
    }
}

#[derive(Clone)]
pub struct AuditLedgerServices {
    pub writer: Arc<dyn AuditLedgerWriter>,
    pub reader: Arc<dyn AuditReader>,
    pub inspector: Arc<dyn AuditStorageInspector>,
}

impl std::fmt::Debug for AuditLedgerServices {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("AuditLedgerServices")
            .finish_non_exhaustive()
    }
}

impl AuditLedgerServices {
    pub fn new<L>(ledger: Arc<L>) -> Self
    where
        L: AuditLedgerWriter + AuditReader + AuditStorageInspector + 'static,
    {
        Self {
            writer: ledger.clone(),
            reader: ledger.clone(),
            inspector: ledger,
        }
    }

    #[must_use]
    pub fn from_parts(
        writer: Arc<dyn AuditLedgerWriter>,
        reader: Arc<dyn AuditReader>,
        inspector: Arc<dyn AuditStorageInspector>,
    ) -> Self {
        Self {
            writer,
            reader,
            inspector,
        }
    }
}

#[derive(Debug, Clone)]
struct MemorySegment {
    status: AuditSegmentStatus,
    record_ids: Vec<Uuid>,
}

impl MemorySegment {
    const fn active(segment_id: u64) -> Self {
        Self {
            status: AuditSegmentStatus {
                segment_id,
                state: AuditSegmentState::Active,
                record_count: 0,
                encoded_bytes: 0,
                first: None,
                last: None,
                archive_receipt: None,
            },
            record_ids: Vec::new(),
        }
    }
}

#[derive(Debug)]
struct MemoryState {
    records: BTreeMap<Uuid, AuditRecordV2>,
    segments: Vec<MemorySegment>,
}

/// Deterministic segmented reference ledger used by tests and local composition.
#[derive(Debug)]
pub struct MemoryAuditLedger {
    policy: AuditLifecyclePolicy,
    state: RwLock<MemoryState>,
}

impl Default for MemoryAuditLedger {
    fn default() -> Self {
        Self::new(AuditLifecyclePolicy::cloud_online()).expect("static memory policy")
    }
}

impl MemoryAuditLedger {
    pub fn new(policy: AuditLifecyclePolicy) -> Result<Self, AuditLedgerError> {
        policy.validate()?;
        Ok(Self {
            policy,
            state: RwLock::new(MemoryState {
                records: BTreeMap::new(),
                segments: vec![MemorySegment::active(1)],
            }),
        })
    }

    pub async fn segment_statuses(&self) -> Vec<AuditSegmentStatus> {
        self.state
            .read()
            .await
            .segments
            .iter()
            .map(|segment| segment.status.clone())
            .collect()
    }

    pub async fn mark_archived(
        &self,
        segment_id: u64,
        receipt: AuditArchiveReceipt,
    ) -> Result<(), AuditLedgerError> {
        validate_text("archive_id", &receipt.archive_id, 512)?;
        receipt.digest.validate()?;
        let mut state = self.state.write().await;
        let segment = state
            .segments
            .iter_mut()
            .find(|segment| segment.status.segment_id == segment_id)
            .ok_or(AuditLedgerError::MissingSegment(segment_id))?;
        if segment.status.state != AuditSegmentState::Sealed {
            return Err(AuditLedgerError::InvalidSegmentState(segment_id));
        }
        segment.status.state = AuditSegmentState::Archived;
        segment.status.archive_receipt = Some(receipt);
        drop(state);
        Ok(())
    }
}

#[async_trait]
impl AuditLedgerWriter for MemoryAuditLedger {
    async fn append_batch(
        &self,
        records: &[AuditRecordV2],
    ) -> Result<AuditAppendReport, AuditLedgerError> {
        if records.is_empty() || records.len() > MAX_AUDIT_BATCH_RECORDS {
            return Err(AuditLedgerError::InvalidBatch(format!(
                "batch must contain between 1 and {MAX_AUDIT_BATCH_RECORDS} records"
            )));
        }
        let mut encoded_sizes = Vec::with_capacity(records.len());
        let mut total_bytes = 0usize;
        let mut request_records = BTreeMap::new();
        let mut duplicate_request_ids = 0usize;
        for record in records {
            let bytes = record.validate()?;
            total_bytes = total_bytes
                .checked_add(bytes)
                .ok_or_else(|| AuditLedgerError::InvalidBatch("batch bytes overflow".into()))?;
            if total_bytes > MAX_AUDIT_BATCH_BYTES {
                return Err(AuditLedgerError::BatchTooLarge {
                    bytes: total_bytes,
                    maximum: MAX_AUDIT_BATCH_BYTES,
                });
            }
            if let Some(existing) = request_records.insert(record.event_id, record) {
                if existing != record {
                    return Err(AuditLedgerError::EventConflict(record.event_id));
                }
                duplicate_request_ids += 1;
            } else {
                encoded_sizes.push((record.event_id, bytes));
            }
        }

        let mut state = self.state.write().await;
        let mut new_records = Vec::new();
        let mut duplicates = duplicate_request_ids;
        for (event_id, record) in &request_records {
            match state.records.get(event_id) {
                Some(existing) if existing == *record => duplicates += 1,
                Some(_) => return Err(AuditLedgerError::EventConflict(*event_id)),
                None => new_records.push((*event_id, (*record).clone())),
            }
        }

        let mut planned_segments = state.segments.clone();
        for (event_id, record) in &new_records {
            let bytes = encoded_sizes
                .iter()
                .find_map(|(id, bytes)| (id == event_id).then_some(*bytes))
                .expect("validated unique record size");
            let bytes = u64::try_from(bytes).map_err(|_| AuditLedgerError::Encoding)?;
            let active = planned_segments.last_mut().expect("active memory segment");
            if let Some(rotate) = self.policy.size.rotate_at_bytes
                && active.status.record_count > 0
                && active.status.encoded_bytes.saturating_add(bytes) > rotate
            {
                active.status.state = AuditSegmentState::Sealed;
                let next_id = active.status.segment_id.saturating_add(1);
                planned_segments.push(MemorySegment::active(next_id));
            }
            let active = planned_segments
                .last_mut()
                .expect("new active memory segment");
            let projected = active.status.encoded_bytes.saturating_add(bytes);
            if self
                .policy
                .size
                .reject_at_bytes
                .is_some_and(|reject| projected > reject)
            {
                return Err(AuditLedgerError::StorageLimitExceeded { bytes: projected });
            }
            let cursor = AuditCursor::from(record);
            active.status.first = active.status.first.or(Some(cursor));
            active.status.last = Some(cursor);
            active.status.record_count = active.status.record_count.saturating_add(1);
            active.status.encoded_bytes = projected;
            active.record_ids.push(*event_id);
        }

        let inserted = new_records.len();
        for (event_id, record) in new_records {
            state.records.insert(event_id, record);
        }
        state.segments = planned_segments;
        drop(state);
        Ok(AuditAppendReport {
            requested: records.len(),
            inserted,
            duplicates,
        })
    }
}

#[async_trait]
impl AuditReader for MemoryAuditLedger {
    async fn list_resource_history(
        &self,
        query: &AuditQuery,
    ) -> Result<AuditPage, AuditLedgerError> {
        query.validate()?;
        let mut records = {
            let state = self.state.read().await;
            state
                .records
                .values()
                .filter(|record| record.tenant_scope == query.tenant_scope)
                .filter(|record| {
                    if record.resource == query.resource {
                        return true;
                    }
                    query.include_related
                        && record.related_resources.iter().any(|related| {
                            related.resource == query.resource
                                && query
                                    .relation
                                    .as_ref()
                                    .is_none_or(|relation| relation == &related.relation)
                        })
                })
                .cloned()
                .collect::<Vec<_>>()
        };
        records.sort_by_key(|record| AuditCursor::from(record));
        if query.direction == AuditSortDirection::NewestFirst {
            records.reverse();
        }
        if let Some(after) = query.after {
            records.retain(|record| match query.direction {
                AuditSortDirection::OldestFirst => AuditCursor::from(record) > after,
                AuditSortDirection::NewestFirst => AuditCursor::from(record) < after,
            });
        }
        let has_more = records.len() > query.limit;
        records.truncate(query.limit);
        let next_cursor = has_more.then(|| {
            records
                .last()
                .map(AuditCursor::from)
                .expect("positive validated page limit")
        });
        Ok(AuditPage {
            records,
            next_cursor,
        })
    }
}

#[async_trait]
impl AuditStorageInspector for MemoryAuditLedger {
    async fn storage_health(&self) -> Result<AuditStorageHealth, AuditLedgerError> {
        let segments = {
            let state = self.state.read().await;
            state
                .segments
                .iter()
                .map(|segment| segment.status.clone())
                .collect::<Vec<_>>()
        };
        let hot_bytes = segments
            .iter()
            .filter(|segment| segment.state != AuditSegmentState::Archived)
            .map(|segment| segment.encoded_bytes)
            .sum();
        let archive_watermark = segments
            .iter()
            .filter(|segment| segment.state == AuditSegmentState::Archived)
            .filter_map(|segment| segment.last)
            .max();
        let snapshot = AuditStorageSnapshot {
            provider: "memory".into(),
            hot_bytes,
            free_bytes: None,
            pending_records: 0,
            pending_bytes: 0,
            oldest_pending_seconds: None,
            quarantined_records: 0,
            archive_watermark,
            segments,
        };
        evaluate_storage_health(self.policy, snapshot)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum AuditLedgerError {
    #[error("invalid audit record: {0}")]
    InvalidRecord(String),
    #[error("audit record is {bytes} bytes; maximum is {maximum}")]
    RecordTooLarge { bytes: usize, maximum: usize },
    #[error("invalid audit batch: {0}")]
    InvalidBatch(String),
    #[error("audit batch is {bytes} bytes; maximum is {maximum}")]
    BatchTooLarge { bytes: usize, maximum: usize },
    #[error("audit event ID {0} was reused for different content")]
    EventConflict(Uuid),
    #[error("invalid audit query: {0}")]
    InvalidQuery(String),
    #[error("invalid audit lifecycle policy: {0}")]
    InvalidLifecycle(String),
    #[error("audit storage hard limit would be exceeded at {bytes} bytes")]
    StorageLimitExceeded { bytes: u64 },
    #[error("audit segment {0} does not exist")]
    MissingSegment(u64),
    #[error("audit segment {0} is not sealed and cannot be archived")]
    InvalidSegmentState(u64),
    #[error("audit encoding failed")]
    Encoding,
    #[error("audit ledger operation failed")]
    Infrastructure,
    #[error("invalid audit journal entry")]
    InvalidJournalEntry,
    #[error("invalid audit journal claim")]
    InvalidJournalClaim,
    #[error("audit journal claim was lost")]
    JournalClaimLost,
}

impl AuditLedgerError {
    #[must_use]
    pub const fn is_permanent(&self) -> bool {
        !matches!(self, Self::Infrastructure)
    }

    #[must_use]
    pub const fn stable_code(&self) -> &'static str {
        match self {
            Self::InvalidRecord(_) => "AUDIT-INVALID-RECORD",
            Self::RecordTooLarge { .. } => "AUDIT-RECORD-TOO-LARGE",
            Self::InvalidBatch(_) => "AUDIT-INVALID-BATCH",
            Self::BatchTooLarge { .. } => "AUDIT-BATCH-TOO-LARGE",
            Self::EventConflict(_) => "AUDIT-EVENT-CONFLICT",
            Self::InvalidQuery(_) => "AUDIT-INVALID-QUERY",
            Self::InvalidLifecycle(_) => "AUDIT-INVALID-LIFECYCLE",
            Self::StorageLimitExceeded { .. } => "AUDIT-STORAGE-LIMIT",
            Self::MissingSegment(_) => "AUDIT-MISSING-SEGMENT",
            Self::InvalidSegmentState(_) => "AUDIT-INVALID-SEGMENT",
            Self::Encoding => "AUDIT-ENCODING",
            Self::Infrastructure => "AUDIT-INFRASTRUCTURE",
            Self::InvalidJournalEntry => "AUDIT-INVALID-JOURNAL",
            Self::InvalidJournalClaim => "AUDIT-INVALID-CLAIM",
            Self::JournalClaimLost => "AUDIT-CLAIM-LOST",
        }
    }
}

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

    fn record(index: u32) -> AuditRecordV2 {
        let occurred_at = DateTime::from_timestamp(1_800_000_000 + i64::from(index), 0).unwrap();
        let mut record = AuditRecordV2::new(
            "tenant-one",
            "order.status_changed",
            AuditResourceRef::new("order", "order-one"),
            AuditActor::human("user-one"),
            "updateOrder",
            Uuid::from_u128(1),
        );
        record.event_id = Uuid::from_u128(10_000 + u128::from(index));
        record.occurred_at = occurred_at;
        record.recorded_at = occurred_at + TimeDelta::seconds(1);
        record.resource_revision = Some(u64::from(index));
        record.changes.insert(
            "status".into(),
            AuditFieldChange {
                before: Some(AuditValue::literal(format!("before-{index}"))),
                after: Some(AuditValue::literal(format!("after-{index}"))),
            },
        );
        record
    }

    fn tiny_rotation_policy() -> AuditLifecyclePolicy {
        AuditLifecyclePolicy {
            size: AuditSizePolicy {
                warn_at_bytes: Some(1),
                rotate_at_bytes: Some(1_200),
                reject_at_bytes: Some(64 * 1024),
                minimum_free_bytes: None,
            },
            retention: AuditRetentionPolicy::default(),
            maximum_pending_records: 0,
            maximum_pending_bytes: 0,
            maximum_oldest_pending_seconds: 0,
        }
    }

    #[test]
    fn record_rejects_unbounded_or_noop_changes() {
        let mut invalid = record(1);
        invalid.changes.insert(
            "same".into(),
            AuditFieldChange {
                before: Some(AuditValue::Redacted),
                after: Some(AuditValue::Redacted),
            },
        );
        assert!(matches!(
            invalid.validate(),
            Err(AuditLedgerError::InvalidRecord(_))
        ));

        let mut oversized = record(2);
        oversized
            .labels
            .insert("large".into(), "x".repeat(MAX_AUDIT_RECORD_BYTES));
        assert!(matches!(
            oversized.validate(),
            Err(AuditLedgerError::InvalidRecord(_) | AuditLedgerError::RecordTooLarge { .. })
        ));
    }

    #[tokio::test]
    async fn batch_append_is_idempotent_and_conflicts_fail_before_writes() {
        let ledger = MemoryAuditLedger::default();
        let first = record(1);
        let second = record(2);
        let report = ledger
            .append_batch(&[first.clone(), second.clone()])
            .await
            .unwrap();
        assert_eq!(report.inserted, 2);
        assert_eq!(report.duplicates, 0);

        let duplicate = ledger
            .append_batch(std::slice::from_ref(&first))
            .await
            .unwrap();
        assert_eq!(duplicate.inserted, 0);
        assert_eq!(duplicate.duplicates, 1);

        let mut conflict = first.clone();
        conflict.action = "order.deleted".into();
        let third = record(3);
        assert!(matches!(
            ledger.append_batch(&[third, conflict]).await,
            Err(AuditLedgerError::EventConflict(id)) if id == first.event_id
        ));
        let page = ledger
            .list_resource_history(&AuditQuery::for_resource(
                "tenant-one",
                AuditResourceRef::new("order", "order-one"),
            ))
            .await
            .unwrap();
        assert_eq!(page.records.len(), 2);
    }

    #[tokio::test]
    async fn concurrent_duplicate_delivery_creates_one_record() {
        let ledger = Arc::new(MemoryAuditLedger::default());
        let action = record(1);
        let left = {
            let ledger = ledger.clone();
            let action = action.clone();
            tokio::spawn(async move { ledger.append_batch(&[action]).await.unwrap() })
        };
        let right = {
            let ledger = ledger.clone();
            tokio::spawn(async move { ledger.append_batch(&[action]).await.unwrap() })
        };
        let left = left.await.unwrap();
        let right = right.await.unwrap();
        assert_eq!(left.inserted + right.inserted, 1);
        assert_eq!(left.duplicates + right.duplicates, 1);
    }

    #[tokio::test]
    async fn cursor_pages_cross_rotated_and_archived_segments_without_gaps() {
        let ledger = MemoryAuditLedger::new(tiny_rotation_policy()).unwrap();
        let records = (1..=8).map(record).collect::<Vec<_>>();
        ledger.append_batch(&records).await.unwrap();
        let segments = ledger.segment_statuses().await;
        assert!(segments.len() >= 2);
        assert_eq!(segments.last().unwrap().state, AuditSegmentState::Active);

        let sealed = segments
            .iter()
            .find(|segment| segment.state == AuditSegmentState::Sealed)
            .unwrap();
        ledger
            .mark_archived(
                sealed.segment_id,
                AuditArchiveReceipt {
                    archive_id: "archive/orders/segment-1".into(),
                    digest: AuditDigest::sha256("a".repeat(64)),
                    archived_at: Utc::now(),
                },
            )
            .await
            .unwrap();

        let mut query =
            AuditQuery::for_resource("tenant-one", AuditResourceRef::new("order", "order-one"));
        query.direction = AuditSortDirection::OldestFirst;
        query.limit = 3;
        let mut revisions = Vec::new();
        loop {
            let page = ledger.list_resource_history(&query).await.unwrap();
            revisions.extend(
                page.records
                    .iter()
                    .map(|record| record.resource_revision.unwrap()),
            );
            let Some(cursor) = page.next_cursor else {
                break;
            };
            query.after = Some(cursor);
        }
        assert_eq!(revisions, (1_u32..=8).map(u64::from).collect::<Vec<_>>());
        assert!(
            ledger
                .storage_health()
                .await
                .unwrap()
                .snapshot
                .archive_watermark
                .is_some()
        );
    }

    #[tokio::test]
    async fn related_resource_query_is_explicit_and_relation_scoped() {
        let ledger = MemoryAuditLedger::default();
        let direct = record(1);
        let mut related = record(2);
        related.resource = AuditResourceRef::new("shift", "shift-one");
        related.related_resources.push(AuditRelatedResource {
            relation: "order".into(),
            resource: AuditResourceRef::new("order", "order-one"),
        });
        ledger.append_batch(&[direct, related]).await.unwrap();

        let mut query =
            AuditQuery::for_resource("tenant-one", AuditResourceRef::new("order", "order-one"));
        assert_eq!(
            ledger
                .list_resource_history(&query)
                .await
                .unwrap()
                .records
                .len(),
            1
        );
        query.include_related = true;
        query.relation = Some("order".into());
        assert_eq!(
            ledger
                .list_resource_history(&query)
                .await
                .unwrap()
                .records
                .len(),
            2
        );
    }

    #[test]
    fn lifecycle_rejects_unsafe_deletion_and_incoherent_size_thresholds() {
        let invalid_delete = AuditLifecyclePolicy {
            size: AuditSizePolicy::default(),
            retention: AuditRetentionPolicy {
                archive_after_seconds: Some(60),
                delete_after_seconds: Some(120),
                require_archive_receipt: false,
            },
            maximum_pending_records: 0,
            maximum_pending_bytes: 0,
            maximum_oldest_pending_seconds: 0,
        };
        assert!(matches!(
            invalid_delete.validate(),
            Err(AuditLedgerError::InvalidLifecycle(_))
        ));

        let invalid_sizes = AuditLifecyclePolicy {
            size: AuditSizePolicy {
                warn_at_bytes: Some(100),
                rotate_at_bytes: Some(50),
                reject_at_bytes: Some(200),
                minimum_free_bytes: None,
            },
            ..AuditLifecyclePolicy::cloud_online()
        };
        assert!(matches!(
            invalid_sizes.validate(),
            Err(AuditLedgerError::InvalidLifecycle(_))
        ));
    }

    #[test]
    fn sensitive_values_require_bounded_explicit_representation() {
        let mut nested = record(1);
        nested.changes.insert(
            "profile".into(),
            AuditFieldChange {
                before: None,
                after: Some(AuditValue::literal(serde_json::json!({"token": "unsafe"}))),
            },
        );
        assert!(matches!(
            nested.validate(),
            Err(AuditLedgerError::InvalidRecord(_))
        ));

        let mut secret = record(2);
        secret.data_class = DataClass::Secret;
        assert!(matches!(
            secret.validate(),
            Err(AuditLedgerError::InvalidRecord(_))
        ));
        secret.changes.values_mut().for_each(|change| {
            change.before = Some(AuditValue::Redacted);
            change.after = Some(AuditValue::Digest {
                digest: AuditDigest::sha256("b".repeat(64)),
            });
        });
        secret.validate().unwrap();

        let mut unknown = record(3);
        unknown.actor = AuditActor {
            kind: AuditActorKind::Unknown,
            subject: Some("claimed-user".into()),
            effective_subject: None,
        };
        assert!(matches!(
            unknown.validate(),
            Err(AuditLedgerError::InvalidRecord(_))
        ));
    }

    #[test]
    fn storage_health_warns_early_and_fails_at_disk_or_backlog_limits() {
        let policy = AuditLifecyclePolicy {
            size: AuditSizePolicy {
                warn_at_bytes: Some(100),
                rotate_at_bytes: Some(200),
                reject_at_bytes: Some(300),
                minimum_free_bytes: Some(1_000),
            },
            retention: AuditRetentionPolicy::default(),
            maximum_pending_records: 100,
            maximum_pending_bytes: 1_000,
            maximum_oldest_pending_seconds: 100,
        };
        let snapshot = AuditStorageSnapshot {
            provider: "sqlite".into(),
            hot_bytes: 150,
            free_bytes: Some(2_000),
            pending_records: 80,
            pending_bytes: 200,
            oldest_pending_seconds: Some(10),
            quarantined_records: 0,
            archive_watermark: None,
            segments: vec![AuditSegmentStatus {
                segment_id: 1,
                state: AuditSegmentState::Active,
                record_count: 1,
                encoded_bytes: 150,
                first: None,
                last: None,
                archive_receipt: None,
            }],
        };
        let warning = evaluate_storage_health(policy, snapshot.clone()).unwrap();
        assert_eq!(warning.severity, AuditHealthSeverity::Warning);
        assert!(
            warning
                .reasons
                .iter()
                .any(|reason| reason.contains("approaching limit"))
        );

        let critical = evaluate_storage_health(
            policy,
            AuditStorageSnapshot {
                free_bytes: Some(999),
                pending_records: 100,
                ..snapshot
            },
        )
        .unwrap();
        assert_eq!(critical.severity, AuditHealthSeverity::Critical);
    }
}