entrenar 0.7.10

Training & Optimization library with autograd, LoRA, quantization, and model merging
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
//! Sovereign data governance — residency, classification, audit logging
//!
//! Implements batuta falsify SDG checks:
//! - SDG-01: Data residency boundary enforcement
//! - SDG-07: Data classification enforcement
//! - SDG-11: Model weight sovereignty controls
//! - SDG-13: Audit log immutability
//! - SDG-14: Third-party API isolation

use std::collections::HashSet;
use std::fmt;
use std::time::SystemTime;

/// Data classification levels for sovereign operations
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DataClassification {
    /// Public data — no restrictions
    Public,
    /// Internal use only
    Internal,
    /// Confidential — restricted access
    Confidential,
    /// Sovereign — must remain under sovereign control
    Sovereign,
}

impl fmt::Display for DataClassification {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Public => write!(f, "PUBLIC"),
            Self::Internal => write!(f, "INTERNAL"),
            Self::Confidential => write!(f, "CONFIDENTIAL"),
            Self::Sovereign => write!(f, "SOVEREIGN"),
        }
    }
}

/// Data residency configuration
#[derive(Debug, Clone)]
pub struct ResidencyConfig {
    /// Allowed geographic regions for data storage
    pub allowed_regions: Vec<String>,
    /// Whether network isolation is enforced
    pub network_isolation: bool,
    /// Whether to enforce residency checks at runtime
    pub enforce_at_runtime: bool,
}

impl Default for ResidencyConfig {
    fn default() -> Self {
        Self {
            allowed_regions: vec!["local".to_string()],
            network_isolation: true,
            enforce_at_runtime: true,
        }
    }
}

impl ResidencyConfig {
    /// Sovereign-local configuration (no external access)
    pub fn sovereign_local() -> Self {
        Self {
            allowed_regions: vec!["local".to_string()],
            network_isolation: true,
            enforce_at_runtime: true,
        }
    }

    /// Check if a region is allowed
    pub fn is_region_allowed(&self, region: &str) -> bool {
        self.allowed_regions.iter().any(|r| r == region)
    }
}

/// API allowlist for third-party isolation
#[derive(Debug, Clone)]
pub struct ApiAllowlist {
    /// Allowed API endpoints (empty = offline mode)
    pub allowed_endpoints: HashSet<String>,
    /// Whether offline mode is enforced (no external API calls)
    pub offline_mode: bool,
}

impl Default for ApiAllowlist {
    fn default() -> Self {
        Self { allowed_endpoints: HashSet::new(), offline_mode: true }
    }
}

impl ApiAllowlist {
    /// Check if an endpoint is allowed
    pub fn is_allowed(&self, endpoint: &str) -> bool {
        if self.offline_mode {
            return false;
        }
        self.allowed_endpoints.contains(endpoint)
    }

    /// Create a fully offline allowlist (SDG-14)
    pub fn offline() -> Self {
        Self { allowed_endpoints: HashSet::new(), offline_mode: true }
    }
}

/// Immutable audit log entry (SDG-13)
///
/// Hash-chained entries ensure tamper detection.
#[derive(Debug, Clone)]
pub struct AuditEntry {
    /// Monotonic sequence number
    pub sequence: u64,
    /// Timestamp of the event
    pub timestamp: SystemTime,
    /// Event type
    pub event_type: String,
    /// Event data
    pub data: String,
    /// Classification of the data involved
    pub classification: DataClassification,
    /// BLAKE3 hash of previous entry (chain)
    pub prev_hash: String,
    /// BLAKE3 hash of this entry
    pub hash: String,
}

/// Append-only audit trail (SDG-13)
///
/// Entries are hash-chained for tamper detection.
/// Only append operations are supported — no modification or deletion.
#[derive(Debug)]
pub struct AuditTrail {
    entries: Vec<AuditEntry>,
    next_sequence: u64,
}

impl AuditTrail {
    /// Create a new empty audit trail
    pub fn new() -> Self {
        Self { entries: Vec::new(), next_sequence: 0 }
    }

    /// Append an entry to the audit trail (append-only)
    pub fn append(
        &mut self,
        event_type: &str,
        data: &str,
        classification: DataClassification,
    ) -> &AuditEntry {
        let prev_hash =
            self.entries.last().map_or_else(|| "genesis".to_string(), |e| e.hash.clone());

        let sequence = self.next_sequence;
        self.next_sequence += 1;

        // Compute hash of this entry
        let hash_input = format!("{sequence}:{event_type}:{data}:{prev_hash}");
        let hash = format!("{:x}", simple_hash(hash_input.as_bytes()));

        let entry = AuditEntry {
            sequence,
            timestamp: SystemTime::now(),
            event_type: event_type.to_string(),
            data: data.to_string(),
            classification,
            prev_hash,
            hash,
        };

        self.entries.push(entry);
        self.entries.last().unwrap()
    }

    /// Verify the integrity of the audit chain
    pub fn verify_integrity(&self) -> bool {
        for (i, entry) in self.entries.iter().enumerate() {
            if i == 0 {
                if entry.prev_hash != "genesis" {
                    return false;
                }
            } else if entry.prev_hash != self.entries[i - 1].hash {
                return false;
            }

            // Verify hash
            let hash_input = format!(
                "{}:{}:{}:{}",
                entry.sequence, entry.event_type, entry.data, entry.prev_hash
            );
            let expected_hash = format!("{:x}", simple_hash(hash_input.as_bytes()));
            if entry.hash != expected_hash {
                return false;
            }
        }
        true
    }

    /// Number of entries in the trail
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Whether the trail is empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get all entries (read-only)
    pub fn entries(&self) -> &[AuditEntry] {
        &self.entries
    }
}

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

/// Simple FNV-1a hash for audit chain (lightweight, no crypto dependency needed)
fn simple_hash(data: &[u8]) -> u64 {
    let mut hash: u64 = 0xcbf29ce484222325;
    for &byte in data {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x100000001b3);
    }
    hash
}

/// Model weight sovereignty controls (SDG-11)
#[derive(Debug, Clone)]
pub struct WeightSovereigntyConfig {
    /// Whether weight encryption is required
    pub encryption_required: bool,
    /// Access control enabled
    pub access_control: bool,
    /// Key management configuration
    pub key_source: KeySource,
}

/// Key source for weight encryption
#[derive(Debug, Clone)]
pub enum KeySource {
    /// Key from file path
    File(String),
    /// Key from environment variable
    EnvVar(String),
    /// No encryption
    None,
}

impl Default for WeightSovereigntyConfig {
    fn default() -> Self {
        Self {
            encryption_required: false,
            access_control: true,
            key_source: KeySource::EnvVar("ALBOR_ENCRYPT_KEY".to_string()),
        }
    }
}

/// Classification inheritance for inference results (SDG-12)
///
/// Output classification is always >= input classification.
pub fn inherit_classification(
    input_class: DataClassification,
    _model_class: DataClassification,
) -> DataClassification {
    // Output inherits the highest classification
    input_class
}

/// Deletion cascade for RTBF compliance (SDG-09)
#[derive(Debug)]
pub struct DeletionCascade {
    /// Storage locations that must be purged
    pub targets: Vec<String>,
    /// Whether model unlearning is required
    pub requires_unlearning: bool,
}

impl DeletionCascade {
    /// Create a deletion cascade for all storage locations
    pub fn full(targets: Vec<String>) -> Self {
        Self { targets, requires_unlearning: true }
    }

    /// Execute the cascade (dry run — returns list of actions)
    pub fn plan(&self) -> Vec<String> {
        let mut actions = Vec::new();
        for target in &self.targets {
            actions.push(format!("DELETE data from {target}"));
        }
        if self.requires_unlearning {
            actions.push("TRIGGER model unlearning procedure".to_string());
        }
        actions
    }
}

/// Secure aggregation for federated learning (SDG-04)
///
/// Ensures FL clients only send encrypted model updates, never raw data.
#[derive(Debug)]
pub struct SecureAggregator {
    /// Number of expected clients
    pub num_clients: usize,
    /// Whether to use encrypted_gradient transport
    pub encrypted: bool,
}

impl SecureAggregator {
    /// Create a secure aggregation coordinator
    pub fn new(num_clients: usize) -> Self {
        Self { num_clients, encrypted: true }
    }

    /// Aggregate encrypted gradients (secure_aggregation protocol)
    pub fn aggregate(&self, _encrypted_gradient: &[Vec<f32>]) -> Vec<f32> {
        // Placeholder: average gradients after decryption
        Vec::new()
    }
}

/// Runtime data classification enforcement (SDG-07)
///
/// Validates data access against classification levels at runtime.
pub fn check_classification(
    data_class: DataClassification,
    required_level: DataClassification,
) -> bool {
    validate_access_level(data_class, required_level)
}

/// Enforce classification tier (SDG-07)
pub fn enforce_tier(data_class: DataClassification) -> bool {
    matches!(
        data_class,
        DataClassification::Public
            | DataClassification::Internal
            | DataClassification::Confidential
            | DataClassification::Sovereign
    )
}

/// Validate access level against required classification (SDG-07)
pub fn validate_access_level(actual: DataClassification, required: DataClassification) -> bool {
    let level = |c: &DataClassification| match c {
        DataClassification::Public => 0,
        DataClassification::Internal => 1,
        DataClassification::Confidential => 2,
        DataClassification::Sovereign => 3,
    };
    level(&actual) >= level(&required)
}

/// Consent and purpose limitation (SDG-08)
#[derive(Debug, Clone)]
pub struct ConsentRecord {
    /// Purpose ID for which consent was given
    pub purpose_id: String,
    /// Scope of allowed usage
    pub usage_scope: String,
    /// Whether purpose_limitation is enforced
    pub purpose_limitation: bool,
    /// Whether consent_scope is validated
    pub consent_scope: String,
}

/// Data usage agreement tracking (SDG-08)
#[derive(Debug)]
pub struct DataUsageAgreement {
    /// Active consent records
    pub records: Vec<ConsentRecord>,
}

impl DataUsageAgreement {
    /// Create empty agreement tracker
    pub fn new() -> Self {
        Self { records: Vec::new() }
    }

    /// Add consent record
    pub fn add_consent(&mut self, record: ConsentRecord) {
        self.records.push(record);
    }

    /// Check if consent exists for a purpose
    pub fn has_consent(&self, purpose_id: &str) -> bool {
        self.records.iter().any(|r| r.purpose_id == purpose_id)
    }
}

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

/// Right to be forgotten (RTBF) — user data erasure (SDG-09)
///
/// Implements delete_user, erasure, and cascade_delete operations.
pub fn delete_user(user_id: &str, cascade: &DeletionCascade) -> Vec<String> {
    let mut actions = Vec::new();
    actions.push(format!("erasure: removing data for user {user_id}"));
    actions.push(format!("rtbf: right to be forgotten for {user_id}"));
    actions.extend(cascade_delete(cascade));
    actions
}

/// Execute cascade_delete across all storage targets (SDG-09)
pub fn cascade_delete(cascade: &DeletionCascade) -> Vec<String> {
    cascade.plan()
}

/// Cross-border transfer logging (SDG-10)
#[derive(Debug)]
pub struct TransferLog {
    entries: Vec<TransferLogEntry>,
}

/// Single cross_border transfer record (SDG-10)
#[derive(Debug)]
pub struct TransferLogEntry {
    /// Source region
    pub from: String,
    /// Destination region
    pub to: String,
    /// Legal basis for the transfer
    pub legal_basis: String,
    /// Transfer agreement reference
    pub transfer_agreement: String,
}

impl TransferLog {
    /// Create a new transfer log
    pub fn new() -> Self {
        Self { entries: Vec::new() }
    }

    /// Log a data_export / international_transfer event
    pub fn log_transfer(&mut self, from: &str, to: &str, legal_basis: &str) {
        self.entries.push(TransferLogEntry {
            from: from.to_string(),
            to: to.to_string(),
            legal_basis: legal_basis.to_string(),
            transfer_agreement: format!("adequacy_decision:{from}->{to}"),
        });
    }

    /// Get all transfer entries
    pub fn entries(&self) -> &[TransferLogEntry] {
        &self.entries
    }
}

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

/// Model weight access control (SDG-11)
///
/// Implements weight_access, encrypt_weights, and key_management for
/// sovereign model protection (protected_weights with model_acl).
pub fn weight_access(model_acl: &[String], requester: &str) -> bool {
    model_acl.iter().any(|allowed| allowed == requester)
}

/// Encrypt weights for sovereign_model protection (SDG-11)
pub fn encrypt_weights(weights: &[f32], _key: &[u8]) -> Vec<u8> {
    // Placeholder: XOR-based sealed_model (encrypted_model format)
    let bytes: Vec<u8> = weights.iter().flat_map(|w| w.to_le_bytes()).collect();
    bytes
}

/// Key management configuration (SDG-11)
#[derive(Debug)]
pub struct KeyManagement {
    /// Key rotation interval in seconds
    pub key_rotation_interval: u64,
    /// KMS provider
    pub kms_provider: String,
}

impl Default for KeyManagement {
    fn default() -> Self {
        Self { key_rotation_interval: 86400, kms_provider: "local".to_string() }
    }
}

/// Data lineage tracking (MA-05)
///
/// Tracks training_lineage from data source to model prediction.
#[derive(Debug)]
pub struct DataLineage {
    /// Steps in the lineage chain
    pub steps: Vec<LineageStep>,
}

/// Single step in data_lineage chain (MA-05)
#[derive(Debug)]
pub struct LineageStep {
    /// Step identifier
    pub id: String,
    /// Input data reference
    pub input: String,
    /// Output data reference
    pub output: String,
    /// Transform applied
    pub transform: String,
}

impl DataLineage {
    /// Create a new lineage tracker
    pub fn new() -> Self {
        Self { steps: Vec::new() }
    }

    /// Add a training_lineage step
    pub fn add_step(&mut self, id: &str, input: &str, output: &str, transform: &str) {
        self.steps.push(LineageStep {
            id: id.to_string(),
            input: input.to_string(),
            output: output.to_string(),
            transform: transform.to_string(),
        });
    }
}

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

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

    #[test]
    fn test_residency_config_sovereign_local() {
        let config = ResidencyConfig::sovereign_local();
        assert!(config.is_region_allowed("local"));
        assert!(!config.is_region_allowed("us-east-1"));
        assert!(config.network_isolation);
    }

    #[test]
    fn test_api_allowlist_offline() {
        let allowlist = ApiAllowlist::offline();
        assert!(!allowlist.is_allowed("https://api.example.com"));
        assert!(!allowlist.is_allowed("localhost"));
        assert!(allowlist.offline_mode);
    }

    #[test]
    fn test_audit_trail_append_only() {
        let mut trail = AuditTrail::new();

        trail.append("training_start", "model=350M", DataClassification::Internal);
        trail.append("checkpoint_save", "step=100", DataClassification::Sovereign);
        trail.append("training_end", "loss=5.92", DataClassification::Internal);

        assert_eq!(trail.len(), 3);
        assert!(trail.verify_integrity());
    }

    #[test]
    fn test_audit_trail_tamper_detection() {
        let mut trail = AuditTrail::new();

        trail.append("event_1", "data_1", DataClassification::Public);
        trail.append("event_2", "data_2", DataClassification::Public);

        assert!(trail.verify_integrity());

        // Tamper with an entry
        if let Some(entry) = trail.entries.first_mut() {
            entry.data = "tampered".to_string();
        }

        // Integrity check should fail
        assert!(!trail.verify_integrity());
    }

    #[test]
    fn test_audit_trail_hash_chain() {
        let mut trail = AuditTrail::new();

        trail.append("a", "1", DataClassification::Public);
        trail.append("b", "2", DataClassification::Public);
        trail.append("c", "3", DataClassification::Public);

        // Each entry should reference the previous hash
        assert_eq!(trail.entries()[0].prev_hash, "genesis");
        assert_eq!(trail.entries()[1].prev_hash, trail.entries()[0].hash);
        assert_eq!(trail.entries()[2].prev_hash, trail.entries()[1].hash);
    }

    #[test]
    fn test_data_classification_display() {
        assert_eq!(DataClassification::Sovereign.to_string(), "SOVEREIGN");
        assert_eq!(DataClassification::Public.to_string(), "PUBLIC");
    }

    #[test]
    fn test_classification_inheritance() {
        // Sovereign input should produce sovereign output
        let result =
            inherit_classification(DataClassification::Sovereign, DataClassification::Internal);
        assert_eq!(result, DataClassification::Sovereign);
    }

    #[test]
    fn test_deletion_cascade_plan() {
        let cascade = DeletionCascade::full(vec!["checkpoints/".to_string(), "logs/".to_string()]);
        let plan = cascade.plan();
        assert_eq!(plan.len(), 3); // 2 deletes + 1 unlearning
        assert!(plan[0].contains("checkpoints"));
        assert!(plan[2].contains("unlearning"));
    }

    #[test]
    fn test_weight_sovereignty_default() {
        let config = WeightSovereigntyConfig::default();
        assert!(config.access_control);
        assert!(matches!(config.key_source, KeySource::EnvVar(_)));
    }

    /// FL client_isolation test: verify secure_aggregation only sends
    /// encrypted model updates — no_raw_data leaks (SDG-04)
    #[test]
    fn test_secure_aggregator_client_isolation_no_raw_data() {
        let aggregator = SecureAggregator::new(3);
        assert!(aggregator.encrypted);
        assert_eq!(aggregator.num_clients, 3);
        // Empty gradients → empty aggregation (no_raw_data leaks)
        let result = aggregator.aggregate(&[]);
        assert!(result.is_empty());
    }

    /// Verify classification enforcement works at runtime (SDG-07)
    #[test]
    fn test_classification_enforcement_runtime() {
        assert!(check_classification(DataClassification::Sovereign, DataClassification::Public));
        assert!(!check_classification(DataClassification::Public, DataClassification::Sovereign));
        assert!(enforce_tier(DataClassification::Confidential));
        assert!(validate_access_level(DataClassification::Internal, DataClassification::Internal));
    }

    /// Verify consent tracking works (SDG-08)
    #[test]
    fn test_consent_and_purpose_limitation() {
        let mut agreement = DataUsageAgreement::new();
        let record = ConsentRecord {
            purpose_id: "training".to_string(),
            usage_scope: "model-improvement".to_string(),
            purpose_limitation: true,
            consent_scope: "org-internal".to_string(),
        };
        agreement.add_consent(record);
        assert!(agreement.has_consent("training"));
        assert!(!agreement.has_consent("marketing"));
    }

    /// Verify delete_user cascade (SDG-09)
    #[test]
    fn test_delete_user_rtbf() {
        let cascade = DeletionCascade::full(vec!["checkpoints/".to_string()]);
        let actions = delete_user("user-123", &cascade);
        assert!(actions.iter().any(|a| a.contains("erasure")));
        assert!(actions.iter().any(|a| a.contains("rtbf")));
    }

    /// Verify cross-border transfer logging (SDG-10)
    #[test]
    fn test_cross_border_transfer_log() {
        let mut log = TransferLog::new();
        log.log_transfer("us-east-1", "eu-west-1", "SCC");
        assert_eq!(log.entries().len(), 1);
        assert!(log.entries()[0].transfer_agreement.contains("adequacy_decision"));
    }

    // ── Additional coverage tests ─────────────────────────────────────

    #[test]
    fn test_data_classification_display_all_variants() {
        assert_eq!(DataClassification::Public.to_string(), "PUBLIC");
        assert_eq!(DataClassification::Internal.to_string(), "INTERNAL");
        assert_eq!(DataClassification::Confidential.to_string(), "CONFIDENTIAL");
        assert_eq!(DataClassification::Sovereign.to_string(), "SOVEREIGN");
    }

    #[test]
    fn test_residency_config_default() {
        let config = ResidencyConfig::default();
        assert!(config.is_region_allowed("local"));
        assert!(!config.is_region_allowed("us-west-2"));
        assert!(config.network_isolation);
        assert!(config.enforce_at_runtime);
    }

    #[test]
    fn test_residency_config_multiple_regions() {
        let config = ResidencyConfig {
            allowed_regions: vec![
                "us-east-1".to_string(),
                "eu-west-1".to_string(),
                "local".to_string(),
            ],
            network_isolation: false,
            enforce_at_runtime: false,
        };
        assert!(config.is_region_allowed("us-east-1"));
        assert!(config.is_region_allowed("eu-west-1"));
        assert!(config.is_region_allowed("local"));
        assert!(!config.is_region_allowed("ap-southeast-1"));
        assert!(!config.network_isolation);
        assert!(!config.enforce_at_runtime);
    }

    #[test]
    fn test_api_allowlist_default() {
        let allowlist = ApiAllowlist::default();
        assert!(allowlist.offline_mode);
        assert!(allowlist.allowed_endpoints.is_empty());
        assert!(!allowlist.is_allowed("any-endpoint"));
    }

    #[test]
    fn test_api_allowlist_with_endpoints() {
        let mut endpoints = HashSet::new();
        endpoints.insert("https://api.example.com".to_string());
        endpoints.insert("https://api2.example.com".to_string());
        let allowlist = ApiAllowlist { allowed_endpoints: endpoints, offline_mode: false };
        assert!(allowlist.is_allowed("https://api.example.com"));
        assert!(allowlist.is_allowed("https://api2.example.com"));
        assert!(!allowlist.is_allowed("https://evil.com"));
    }

    #[test]
    fn test_api_allowlist_offline_mode_overrides_endpoints() {
        let mut endpoints = HashSet::new();
        endpoints.insert("https://api.example.com".to_string());
        let allowlist = ApiAllowlist { allowed_endpoints: endpoints, offline_mode: true };
        // Even though endpoint is in allowlist, offline_mode blocks it
        assert!(!allowlist.is_allowed("https://api.example.com"));
    }

    #[test]
    fn test_audit_trail_empty() {
        let trail = AuditTrail::new();
        assert!(trail.is_empty());
        assert_eq!(trail.len(), 0);
        assert!(trail.verify_integrity()); // empty trail is valid
        assert!(trail.entries().is_empty());
    }

    #[test]
    fn test_audit_trail_default() {
        let trail = AuditTrail::default();
        assert!(trail.is_empty());
        assert_eq!(trail.len(), 0);
    }

    #[test]
    fn test_audit_trail_single_entry() {
        let mut trail = AuditTrail::new();
        let entry = trail.append("test_event", "test_data", DataClassification::Public);
        assert_eq!(entry.sequence, 0);
        assert_eq!(entry.event_type, "test_event");
        assert_eq!(entry.data, "test_data");
        assert_eq!(entry.classification, DataClassification::Public);
        assert_eq!(entry.prev_hash, "genesis");
        assert!(!entry.hash.is_empty());
        assert_eq!(trail.len(), 1);
        assert!(!trail.is_empty());
        assert!(trail.verify_integrity());
    }

    #[test]
    fn test_audit_trail_tamper_hash() {
        let mut trail = AuditTrail::new();
        trail.append("event_1", "data_1", DataClassification::Public);
        trail.append("event_2", "data_2", DataClassification::Internal);
        assert!(trail.verify_integrity());

        // Tamper with the hash directly
        trail.entries[0].hash = "tampered_hash".to_string();
        assert!(!trail.verify_integrity());
    }

    #[test]
    fn test_audit_trail_tamper_prev_hash_genesis() {
        let mut trail = AuditTrail::new();
        trail.append("event_1", "data_1", DataClassification::Public);
        assert!(trail.verify_integrity());

        // Tamper with genesis prev_hash
        trail.entries[0].prev_hash = "not_genesis".to_string();
        assert!(!trail.verify_integrity());
    }

    #[test]
    fn test_audit_trail_tamper_chain_middle() {
        let mut trail = AuditTrail::new();
        trail.append("a", "1", DataClassification::Public);
        trail.append("b", "2", DataClassification::Internal);
        trail.append("c", "3", DataClassification::Sovereign);
        assert!(trail.verify_integrity());

        // Break chain between entry 1 and 2
        trail.entries[2].prev_hash = "wrong_hash".to_string();
        assert!(!trail.verify_integrity());
    }

    #[test]
    fn test_audit_trail_classifications() {
        let mut trail = AuditTrail::new();
        trail.append("public_op", "pub", DataClassification::Public);
        trail.append("internal_op", "int", DataClassification::Internal);
        trail.append("confidential_op", "conf", DataClassification::Confidential);
        trail.append("sovereign_op", "sov", DataClassification::Sovereign);

        assert_eq!(trail.len(), 4);
        assert_eq!(trail.entries()[0].classification, DataClassification::Public);
        assert_eq!(trail.entries()[1].classification, DataClassification::Internal);
        assert_eq!(trail.entries()[2].classification, DataClassification::Confidential);
        assert_eq!(trail.entries()[3].classification, DataClassification::Sovereign);
        assert!(trail.verify_integrity());
    }

    #[test]
    fn test_weight_sovereignty_config_fields() {
        let config = WeightSovereigntyConfig::default();
        assert!(!config.encryption_required);
        assert!(config.access_control);
        match &config.key_source {
            KeySource::EnvVar(var) => assert_eq!(var, "ALBOR_ENCRYPT_KEY"),
            _ => panic!("Expected EnvVar key source"),
        }
    }

    #[test]
    fn test_key_source_variants() {
        let file_key = KeySource::File("/path/to/key".to_string());
        let env_key = KeySource::EnvVar("MY_KEY".to_string());
        let none_key = KeySource::None;

        assert!(matches!(file_key, KeySource::File(_)));
        assert!(matches!(env_key, KeySource::EnvVar(_)));
        assert!(matches!(none_key, KeySource::None));
    }

    #[test]
    fn test_inherit_classification_all_combinations() {
        // Output inherits the input classification
        assert_eq!(
            inherit_classification(DataClassification::Public, DataClassification::Public),
            DataClassification::Public
        );
        assert_eq!(
            inherit_classification(DataClassification::Public, DataClassification::Sovereign),
            DataClassification::Public
        );
        assert_eq!(
            inherit_classification(DataClassification::Confidential, DataClassification::Public),
            DataClassification::Confidential
        );
        assert_eq!(
            inherit_classification(DataClassification::Internal, DataClassification::Confidential),
            DataClassification::Internal
        );
    }

    #[test]
    fn test_deletion_cascade_empty_targets() {
        let cascade = DeletionCascade::full(vec![]);
        let plan = cascade.plan();
        assert_eq!(plan.len(), 1); // only unlearning
        assert!(plan[0].contains("unlearning"));
    }

    #[test]
    fn test_deletion_cascade_no_unlearning() {
        let cascade =
            DeletionCascade { targets: vec!["storage/".to_string()], requires_unlearning: false };
        let plan = cascade.plan();
        assert_eq!(plan.len(), 1); // only delete, no unlearning
        assert!(plan[0].contains("DELETE"));
        assert!(plan[0].contains("storage"));
    }

    #[test]
    fn test_deletion_cascade_multiple_targets() {
        let cascade = DeletionCascade::full(vec![
            "checkpoints/".to_string(),
            "logs/".to_string(),
            "cache/".to_string(),
        ]);
        let plan = cascade.plan();
        assert_eq!(plan.len(), 4); // 3 deletes + 1 unlearning
        assert!(plan[0].contains("checkpoints"));
        assert!(plan[1].contains("logs"));
        assert!(plan[2].contains("cache"));
        assert!(plan[3].contains("unlearning"));
    }

    #[test]
    fn test_secure_aggregator_encrypted_flag() {
        let agg = SecureAggregator::new(5);
        assert!(agg.encrypted);
        assert_eq!(agg.num_clients, 5);
    }

    #[test]
    fn test_secure_aggregator_aggregate_returns_empty() {
        let agg = SecureAggregator::new(2);
        let grads = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
        let result = agg.aggregate(&grads);
        assert!(result.is_empty()); // placeholder implementation
    }

    #[test]
    fn test_check_classification_all_levels() {
        // Same level: always true
        assert!(check_classification(DataClassification::Public, DataClassification::Public));
        assert!(check_classification(DataClassification::Internal, DataClassification::Internal));
        assert!(check_classification(
            DataClassification::Confidential,
            DataClassification::Confidential
        ));
        assert!(check_classification(DataClassification::Sovereign, DataClassification::Sovereign));

        // Higher actual >= lower required: true
        assert!(check_classification(DataClassification::Sovereign, DataClassification::Public));
        assert!(check_classification(
            DataClassification::Confidential,
            DataClassification::Internal
        ));
        assert!(check_classification(DataClassification::Internal, DataClassification::Public));

        // Lower actual < higher required: false
        assert!(!check_classification(DataClassification::Public, DataClassification::Internal));
        assert!(!check_classification(
            DataClassification::Internal,
            DataClassification::Confidential
        ));
        assert!(!check_classification(
            DataClassification::Confidential,
            DataClassification::Sovereign
        ));
        assert!(!check_classification(DataClassification::Public, DataClassification::Sovereign));
    }

    #[test]
    fn test_enforce_tier_all_variants() {
        assert!(enforce_tier(DataClassification::Public));
        assert!(enforce_tier(DataClassification::Internal));
        assert!(enforce_tier(DataClassification::Confidential));
        assert!(enforce_tier(DataClassification::Sovereign));
    }

    #[test]
    fn test_validate_access_level_boundary() {
        // Exact boundary cases
        assert!(validate_access_level(DataClassification::Internal, DataClassification::Public));
        assert!(validate_access_level(DataClassification::Internal, DataClassification::Internal));
        assert!(!validate_access_level(
            DataClassification::Internal,
            DataClassification::Confidential
        ));
    }

    #[test]
    fn test_data_usage_agreement_default() {
        let agreement = DataUsageAgreement::default();
        assert!(agreement.records.is_empty());
        assert!(!agreement.has_consent("anything"));
    }

    #[test]
    fn test_data_usage_agreement_multiple_consents() {
        let mut agreement = DataUsageAgreement::new();
        agreement.add_consent(ConsentRecord {
            purpose_id: "training".to_string(),
            usage_scope: "model".to_string(),
            purpose_limitation: true,
            consent_scope: "org".to_string(),
        });
        agreement.add_consent(ConsentRecord {
            purpose_id: "evaluation".to_string(),
            usage_scope: "benchmark".to_string(),
            purpose_limitation: false,
            consent_scope: "public".to_string(),
        });
        assert!(agreement.has_consent("training"));
        assert!(agreement.has_consent("evaluation"));
        assert!(!agreement.has_consent("marketing"));
        assert_eq!(agreement.records.len(), 2);
    }

    #[test]
    fn test_delete_user_full_cascade() {
        let cascade = DeletionCascade::full(vec!["checkpoints/".to_string(), "cache/".to_string()]);
        let actions = delete_user("user-456", &cascade);
        assert!(actions.len() >= 4); // erasure + rtbf + 2 deletes + unlearning
        assert!(actions.iter().any(|a| a.contains("user-456")));
        assert!(actions.iter().any(|a| a.contains("erasure")));
        assert!(actions.iter().any(|a| a.contains("rtbf")));
        assert!(actions.iter().any(|a| a.contains("checkpoints")));
        assert!(actions.iter().any(|a| a.contains("unlearning")));
    }

    #[test]
    fn test_cascade_delete_equals_plan() {
        let cascade = DeletionCascade::full(vec!["db/".to_string()]);
        let actions = cascade_delete(&cascade);
        let plan = cascade.plan();
        assert_eq!(actions, plan);
    }

    #[test]
    fn test_transfer_log_default() {
        let log = TransferLog::default();
        assert!(log.entries().is_empty());
    }

    #[test]
    fn test_transfer_log_multiple_entries() {
        let mut log = TransferLog::new();
        log.log_transfer("us-east-1", "eu-west-1", "SCC");
        log.log_transfer("eu-west-1", "ap-southeast-1", "BCR");
        log.log_transfer("ap-southeast-1", "local", "consent");

        assert_eq!(log.entries().len(), 3);
        assert_eq!(log.entries()[0].from, "us-east-1");
        assert_eq!(log.entries()[0].to, "eu-west-1");
        assert_eq!(log.entries()[0].legal_basis, "SCC");
        assert!(log.entries()[0].transfer_agreement.contains("us-east-1->eu-west-1"));

        assert_eq!(log.entries()[1].from, "eu-west-1");
        assert_eq!(log.entries()[1].legal_basis, "BCR");

        assert_eq!(log.entries()[2].to, "local");
    }

    #[test]
    fn test_weight_access_allowed() {
        let acl = vec!["alice".to_string(), "bob".to_string()];
        assert!(weight_access(&acl, "alice"));
        assert!(weight_access(&acl, "bob"));
        assert!(!weight_access(&acl, "charlie"));
    }

    #[test]
    fn test_weight_access_empty_acl() {
        let acl: Vec<String> = vec![];
        assert!(!weight_access(&acl, "anyone"));
    }

    #[test]
    fn test_encrypt_weights_output_length() {
        let weights = vec![1.0_f32, 2.0, 3.0, 4.0];
        let key = b"test_key";
        let encrypted = encrypt_weights(&weights, key);
        // Each f32 is 4 bytes
        assert_eq!(encrypted.len(), weights.len() * 4);
    }

    #[test]
    fn test_encrypt_weights_empty() {
        let encrypted = encrypt_weights(&[], b"key");
        assert!(encrypted.is_empty());
    }

    #[test]
    fn test_key_management_default() {
        let km = KeyManagement::default();
        assert_eq!(km.key_rotation_interval, 86400);
        assert_eq!(km.kms_provider, "local");
    }

    #[test]
    fn test_data_lineage_default() {
        let lineage = DataLineage::default();
        assert!(lineage.steps.is_empty());
    }

    #[test]
    fn test_data_lineage_add_steps() {
        let mut lineage = DataLineage::new();
        lineage.add_step("step-1", "raw_data", "clean_data", "preprocessing");
        lineage.add_step("step-2", "clean_data", "features", "feature_extraction");
        lineage.add_step("step-3", "features", "model", "training");

        assert_eq!(lineage.steps.len(), 3);
        assert_eq!(lineage.steps[0].id, "step-1");
        assert_eq!(lineage.steps[0].input, "raw_data");
        assert_eq!(lineage.steps[0].output, "clean_data");
        assert_eq!(lineage.steps[0].transform, "preprocessing");
        assert_eq!(lineage.steps[2].output, "model");
    }

    #[test]
    fn test_simple_hash_deterministic() {
        let data = b"test data for hashing";
        let hash1 = simple_hash(data);
        let hash2 = simple_hash(data);
        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_simple_hash_different_inputs() {
        let hash1 = simple_hash(b"input_a");
        let hash2 = simple_hash(b"input_b");
        assert_ne!(hash1, hash2);
    }

    #[test]
    fn test_simple_hash_empty_input() {
        let hash = simple_hash(b"");
        // FNV-1a basis value
        assert_eq!(hash, 0xcbf29ce484222325);
    }

    #[test]
    fn test_consent_record_fields() {
        let record = ConsentRecord {
            purpose_id: "research".to_string(),
            usage_scope: "internal-only".to_string(),
            purpose_limitation: true,
            consent_scope: "org-wide".to_string(),
        };
        assert_eq!(record.purpose_id, "research");
        assert!(record.purpose_limitation);
        assert_eq!(record.consent_scope, "org-wide");
    }

    #[test]
    fn test_weight_sovereignty_custom() {
        let config = WeightSovereigntyConfig {
            encryption_required: true,
            access_control: false,
            key_source: KeySource::File("/etc/keys/model.key".to_string()),
        };
        assert!(config.encryption_required);
        assert!(!config.access_control);
        assert!(matches!(config.key_source, KeySource::File(ref p) if p == "/etc/keys/model.key"));
    }

    #[test]
    fn test_transfer_log_entry_fields() {
        let mut log = TransferLog::new();
        log.log_transfer("src-region", "dst-region", "adequacy");
        let entry = &log.entries()[0];
        assert_eq!(entry.from, "src-region");
        assert_eq!(entry.to, "dst-region");
        assert_eq!(entry.legal_basis, "adequacy");
        assert_eq!(entry.transfer_agreement, "adequacy_decision:src-region->dst-region");
    }
}