ai-model-vault 4.6.1

DEPRECATED - renamed to `ironvault`. This crate is no longer maintained; 4.6.1 is identical to 4.6.0 apart from this notice.
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
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
//! Federated vault synchronization
//!
//! Enables synchronization of model versions across multiple vault instances.
//! Uses a peer-to-peer protocol with eventual consistency.
//!
//! ## Architecture
//!
//! - **Nodes**: Independent vault instances that can sync with each other
//! - **Sync Protocol**: Delta-based replication using vector clocks
//! - **Conflict Resolution**: Last-writer-wins with version lineage preservation
//! - **Transport**: HTTPS with mutual TLS authentication
//!
//! ## Security
//!
//! - All transfers are encrypted end-to-end
//! - Node authentication via certificate pinning
//! - Audit logs record all sync operations

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
use zeroize::Zeroize;

use crate::error::{Result, VaultError};

/// Federation node identifier
pub type NodeId = String;

/// Vector clock for causal ordering
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct VectorClock {
    /// Map of node ID to logical timestamp
    pub timestamps: HashMap<NodeId, u64>,
}

impl VectorClock {
    /// Create new vector clock
    pub fn new() -> Self {
        Self::default()
    }

    /// Increment the clock for a node
    pub fn increment(&mut self, node_id: &str) {
        *self.timestamps.entry(node_id.to_string()).or_insert(0) += 1;
    }

    /// Merge with another vector clock (take max of each)
    pub fn merge(&mut self, other: &VectorClock) {
        for (node, ts) in &other.timestamps {
            let entry = self.timestamps.entry(node.clone()).or_insert(0);
            *entry = (*entry).max(*ts);
        }
    }

    /// Check if this clock is causally before or concurrent with another
    pub fn compare(&self, other: &VectorClock) -> ClockComparison {
        let mut dominated = true;
        let mut dominates = true;

        for (node, ts) in &self.timestamps {
            let other_ts = other.timestamps.get(node).copied().unwrap_or(0);
            if *ts > other_ts {
                dominated = false;
            }
            if *ts < other_ts {
                dominates = false;
            }
        }

        for (node, ts) in &other.timestamps {
            if !self.timestamps.contains_key(node) && *ts > 0 {
                dominates = false;
            }
        }

        match (dominates, dominated) {
            (true, true) => ClockComparison::Equal,
            (true, false) => ClockComparison::After,
            (false, true) => ClockComparison::Before,
            (false, false) => ClockComparison::Concurrent,
        }
    }
}

/// Result of comparing two vector clocks
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClockComparison {
    /// Clocks are equal
    Equal,
    /// First clock happened before second
    Before,
    /// First clock happened after second
    After,
    /// Clocks are concurrent (no causal relationship)
    Concurrent,
}

/// Federation node configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FederationConfig {
    /// This node's unique ID
    pub node_id: NodeId,
    /// This node's display name
    pub node_name: String,
    /// List of peer nodes to sync with
    pub peers: Vec<PeerConfig>,
    /// Sync interval in seconds (0 to disable auto-sync)
    pub sync_interval_secs: u64,
    /// Whether to auto-resolve conflicts
    pub auto_resolve_conflicts: bool,
    /// Maximum concurrent syncs
    pub max_concurrent_syncs: usize,
}

impl Default for FederationConfig {
    fn default() -> Self {
        Self {
            node_id: Uuid::new_v4().to_string(),
            node_name: hostname::get()
                .map(|h| h.to_string_lossy().into_owned())
                .unwrap_or_else(|_| "unknown".into()),
            peers: Vec::new(),
            sync_interval_secs: 300, // 5 minutes
            auto_resolve_conflicts: true,
            max_concurrent_syncs: 4,
        }
    }
}

/// Peer node configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PeerConfig {
    /// Peer's node ID
    pub node_id: NodeId,
    /// Peer's display name
    pub name: String,
    /// Peer's sync endpoint URL
    pub endpoint: String,
    /// Optional API key for authentication
    pub api_key: Option<String>,
    /// Whether sync is enabled for this peer
    pub enabled: bool,
}

impl Drop for PeerConfig {
    fn drop(&mut self) {
        if let Some(ref mut key) = self.api_key {
            key.zeroize();
        }
    }
}

/// Sync state for a model
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelSyncState {
    /// Model name
    pub name: String,
    /// Vector clock for this model
    pub clock: VectorClock,
    /// Last sync time per peer
    pub last_sync: HashMap<NodeId, DateTime<Utc>>,
    /// Known versions across all nodes
    pub known_versions: HashSet<String>, // checkpoint IDs
    /// Pending uploads
    pub pending_upload: HashSet<String>,
    /// Pending downloads
    pub pending_download: HashSet<(NodeId, String)>, // (node_id, checkpoint_id)
}

/// Sync manifest exchanged between nodes
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncManifest {
    /// Source node ID
    pub source_node: NodeId,
    /// Manifest timestamp
    pub timestamp: DateTime<Utc>,
    /// Model states
    pub models: Vec<ModelManifestEntry>,
    /// Node's vector clock
    pub clock: VectorClock,
}

/// Single model entry in sync manifest
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelManifestEntry {
    /// Model name
    pub name: String,
    /// Available versions
    pub versions: Vec<VersionManifestEntry>,
    /// Model's vector clock
    pub clock: VectorClock,
}

/// Version entry in manifest
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersionManifestEntry {
    /// Version number
    pub version: u32,
    /// Checkpoint ID
    pub checkpoint_id: String,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// SHA-256 checksum
    pub checksum: String,
    /// Size in bytes
    pub size_bytes: u64,
    /// Parent checkpoint ID (for lineage)
    pub parent_id: Option<String>,
    /// Originating node
    pub origin_node: NodeId,
}

/// Sync operation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncResult {
    /// Peer node ID
    pub peer_id: NodeId,
    /// Timestamp
    pub timestamp: DateTime<Utc>,
    /// Duration in milliseconds
    pub duration_ms: u64,
    /// Models synced
    pub models_synced: u32,
    /// Versions uploaded
    pub versions_uploaded: u32,
    /// Versions downloaded
    pub versions_downloaded: u32,
    /// Conflicts detected
    pub conflicts: Vec<SyncConflict>,
    /// Errors encountered
    pub errors: Vec<String>,
}

/// Sync conflict details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncConflict {
    /// Model name
    pub model: String,
    /// Local version
    pub local_version: String,
    /// Remote version
    pub remote_version: String,
    /// Remote node
    pub remote_node: NodeId,
    /// Resolution (if auto-resolved)
    pub resolution: Option<ConflictResolution>,
}

/// Conflict resolution strategy
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConflictResolution {
    /// Keep local version
    KeepLocal,
    /// Use remote version
    UseRemote,
    /// Keep both as separate branches
    Branch {
        local_name: String,
        remote_name: String,
    },
    /// Manual resolution required
    Manual,
}

/// Federation manager
pub struct FederationManager {
    config: FederationConfig,
    state: Arc<RwLock<FederationState>>,
    http_client: reqwest::Client,
}

/// Internal federation state
struct FederationState {
    /// Sync state per model
    models: HashMap<String, ModelSyncState>,
    /// Global vector clock
    clock: VectorClock,
    /// Sync history
    history: Vec<SyncResult>,
    /// State file path
    state_file: PathBuf,
}

impl FederationManager {
    /// Create new federation manager
    pub fn new(config: FederationConfig, state_dir: PathBuf) -> Result<Self> {
        let state_file = state_dir.join("federation_state.json");

        // Load existing state or create new
        let federation_state = if state_file.exists() {
            let contents = std::fs::read_to_string(&state_file)?;
            let loaded: SavedFederationState = serde_json::from_str(&contents)?;
            FederationState {
                models: loaded.models,
                clock: loaded.clock,
                history: loaded.history,
                state_file,
            }
        } else {
            FederationState {
                models: HashMap::new(),
                clock: VectorClock::new(),
                history: Vec::new(),
                state_file,
            }
        };

        let http_client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(300))
            .build()
            .map_err(|e| {
                VaultError::IoError(std::io::Error::other(format!("HTTP client error: {e}")))
            })?;

        Ok(Self {
            config,
            state: Arc::new(RwLock::new(federation_state)),
            http_client,
        })
    }

    /// Get this node's ID
    pub fn node_id(&self) -> &str {
        &self.config.node_id
    }

    /// Get configured peers
    pub fn peers(&self) -> &[PeerConfig] {
        &self.config.peers
    }

    /// Add a peer
    pub fn add_peer(&mut self, peer: PeerConfig) {
        self.config.peers.push(peer);
    }

    /// Remove a peer
    pub fn remove_peer(&mut self, node_id: &str) {
        self.config.peers.retain(|p| p.node_id != node_id);
    }

    /// Generate sync manifest from local vault
    pub async fn generate_manifest(
        &self,
        vault_models: Vec<(String, Vec<crate::version::ModelVersion>)>,
    ) -> SyncManifest {
        let state = self.state.read().await;

        let models = vault_models
            .into_iter()
            .map(
                |(name, versions): (String, Vec<crate::version::ModelVersion>)| {
                    let model_clock = state
                        .models
                        .get(&name)
                        .map(|s| s.clock.clone())
                        .unwrap_or_default();

                    let version_entries = versions
                        .into_iter()
                        .map(|v| VersionManifestEntry {
                            version: v.version,
                            // The federation identity, not the local one: a
                            // model received from a peer keeps the id it was
                            // created with, so both nodes agree on what they
                            // already have and sync converges instead of
                            // re-transferring on every run.
                            checkpoint_id: crate::federation_transport::federation_checkpoint_id(
                                &v,
                            ),
                            created_at: v.timestamp,
                            checksum: v.checksum_sha256,
                            size_bytes: v.size_bytes,
                            parent_id: v.parent_version.map(|p| p.to_string()),
                            origin_node: self.config.node_id.clone(),
                        })
                        .collect();

                    ModelManifestEntry {
                        name,
                        versions: version_entries,
                        clock: model_clock,
                    }
                },
            )
            .collect();

        SyncManifest {
            source_node: self.config.node_id.clone(),
            timestamp: Utc::now(),
            models,
            clock: state.clock.clone(),
        }
    }

    /// Compute sync delta between local and remote manifests
    pub fn compute_delta(&self, local: &SyncManifest, remote: &SyncManifest) -> SyncDelta {
        let mut to_upload = Vec::new();
        let mut to_download = Vec::new();
        let mut conflicts = Vec::new();

        // Build lookup for remote models
        let remote_models: HashMap<&str, &ModelManifestEntry> =
            remote.models.iter().map(|m| (m.name.as_str(), m)).collect();

        // Check each local model
        for local_model in &local.models {
            if let Some(remote_model) = remote_models.get(local_model.name.as_str()) {
                // Model exists on both sides - compare versions
                let local_checkpoints: HashSet<&str> = local_model
                    .versions
                    .iter()
                    .map(|v| v.checkpoint_id.as_str())
                    .collect();
                let remote_checkpoints: HashSet<&str> = remote_model
                    .versions
                    .iter()
                    .map(|v| v.checkpoint_id.as_str())
                    .collect();

                // Versions we have but remote doesn't
                for version in &local_model.versions {
                    if !remote_checkpoints.contains(version.checkpoint_id.as_str()) {
                        to_upload.push(SyncItem {
                            model: local_model.name.clone(),
                            checkpoint_id: version.checkpoint_id.clone(),
                            size_bytes: version.size_bytes,
                        });
                    }
                }

                // Versions remote has but we don't
                for version in &remote_model.versions {
                    if !local_checkpoints.contains(version.checkpoint_id.as_str()) {
                        to_download.push(SyncItem {
                            model: remote_model.name.clone(),
                            checkpoint_id: version.checkpoint_id.clone(),
                            size_bytes: version.size_bytes,
                        });
                    }
                }

                // Check for conflicts (same version number, different checkpoints)
                let local_by_version: HashMap<u32, &VersionManifestEntry> = local_model
                    .versions
                    .iter()
                    .map(|v| (v.version, v))
                    .collect();
                let remote_by_version: HashMap<u32, &VersionManifestEntry> = remote_model
                    .versions
                    .iter()
                    .map(|v| (v.version, v))
                    .collect();

                for (version, local_v) in &local_by_version {
                    if let Some(remote_v) = remote_by_version.get(version) {
                        if local_v.checkpoint_id != remote_v.checkpoint_id {
                            conflicts.push(SyncConflict {
                                model: local_model.name.clone(),
                                local_version: local_v.checkpoint_id.clone(),
                                remote_version: remote_v.checkpoint_id.clone(),
                                remote_node: remote.source_node.clone(),
                                resolution: None,
                            });
                        }
                    }
                }
            } else {
                // Model only exists locally - upload all versions
                for version in &local_model.versions {
                    to_upload.push(SyncItem {
                        model: local_model.name.clone(),
                        checkpoint_id: version.checkpoint_id.clone(),
                        size_bytes: version.size_bytes,
                    });
                }
            }
        }

        // Check for models that only exist on remote
        let local_model_names: HashSet<&str> =
            local.models.iter().map(|m| m.name.as_str()).collect();
        for remote_model in &remote.models {
            if !local_model_names.contains(remote_model.name.as_str()) {
                for version in &remote_model.versions {
                    to_download.push(SyncItem {
                        model: remote_model.name.clone(),
                        checkpoint_id: version.checkpoint_id.clone(),
                        size_bytes: version.size_bytes,
                    });
                }
            }
        }

        SyncDelta {
            to_upload,
            to_download,
            conflicts,
        }
    }

    /// Sync with a specific peer
    pub async fn sync_with_peer(
        &self,
        peer: &PeerConfig,
        local_manifest: &SyncManifest,
        download_fn: impl Fn(&str, &str) -> Result<Vec<u8>>,
        upload_fn: impl Fn(&str, &str, &[u8]) -> Result<()>,
    ) -> Result<SyncResult> {
        let start = std::time::Instant::now();
        let mut errors = Vec::new();

        // Fetch remote manifest
        let remote_manifest = self.fetch_manifest(peer).await?;

        // Compute delta
        let delta = self.compute_delta(local_manifest, &remote_manifest);

        // Handle conflicts
        let mut resolved_conflicts = Vec::new();
        for conflict in delta.conflicts {
            let resolution = if self.config.auto_resolve_conflicts {
                // Last-writer-wins based on timestamp
                // In production, use vector clock comparison
                Some(ConflictResolution::KeepLocal)
            } else {
                Some(ConflictResolution::Manual)
            };
            resolved_conflicts.push(SyncConflict {
                resolution,
                ..conflict
            });
        }

        // Download missing versions
        let mut downloaded = 0;
        for item in &delta.to_download {
            match self
                .download_version(peer, &item.model, &item.checkpoint_id)
                .await
            {
                Ok(data) => {
                    if let Err(e) = upload_fn(&item.model, &item.checkpoint_id, &data) {
                        errors.push(format!(
                            "Failed to store {}/{}: {e}",
                            item.model, item.checkpoint_id
                        ));
                    } else {
                        downloaded += 1;
                    }
                }
                Err(e) => {
                    errors.push(format!(
                        "Failed to download {}/{}: {e}",
                        item.model, item.checkpoint_id
                    ));
                }
            }
        }

        // Upload missing versions
        let mut uploaded = 0;
        for item in &delta.to_upload {
            match download_fn(&item.model, &item.checkpoint_id) {
                Ok(data) => {
                    if let Err(e) = self
                        .upload_version(peer, &item.model, &item.checkpoint_id, &data)
                        .await
                    {
                        errors.push(format!(
                            "Failed to upload {}/{}: {e}",
                            item.model, item.checkpoint_id
                        ));
                    } else {
                        uploaded += 1;
                    }
                }
                Err(e) => {
                    errors.push(format!(
                        "Failed to read {}/{}: {e}",
                        item.model, item.checkpoint_id
                    ));
                }
            }
        }

        // Update state
        {
            let mut state = self.state.write().await;
            state.clock.increment(&self.config.node_id);
            state.clock.merge(&remote_manifest.clock);
        }

        let result = SyncResult {
            peer_id: peer.node_id.clone(),
            timestamp: Utc::now(),
            duration_ms: start.elapsed().as_millis() as u64,
            models_synced: (delta.to_upload.len() + delta.to_download.len()) as u32,
            versions_uploaded: uploaded,
            versions_downloaded: downloaded,
            conflicts: resolved_conflicts,
            errors,
        };

        // Record in history
        {
            let mut state = self.state.write().await;
            state.history.push(result.clone());
            if state.history.len() > 1000 {
                state.history.remove(0);
            }
            let _ = self.save_state(&state);
        }

        Ok(result)
    }

    /// Fetch manifest from peer
    /// Fetch a peer's manifest without syncing.
    ///
    /// Public so `aim federation plan` can show what a sync would move before
    /// anything moves.
    pub async fn fetch_peer_manifest(&self, peer: &PeerConfig) -> Result<SyncManifest> {
        self.fetch_manifest(peer).await
    }

    async fn fetch_manifest(&self, peer: &PeerConfig) -> Result<SyncManifest> {
        let url = format!("{}/api/v1/federation/manifest", peer.endpoint);
        let mut req = self.http_client.get(&url);

        if let Some(api_key) = &peer.api_key {
            req = req.header("X-API-Key", api_key);
        }

        let response = req
            .send()
            .await
            .map_err(|e| VaultError::IoError(std::io::Error::other(format!("HTTP error: {e}"))))?;

        if !response.status().is_success() {
            return Err(VaultError::IoError(std::io::Error::other(format!(
                "Peer returned error: {}",
                response.status()
            ))));
        }

        let manifest = response
            .json::<SyncManifest>()
            .await
            .map_err(|e| VaultError::IoError(std::io::Error::other(format!("JSON error: {e}"))))?;

        Ok(manifest)
    }

    /// Download a version from peer
    async fn download_version(
        &self,
        peer: &PeerConfig,
        model: &str,
        checkpoint_id: &str,
    ) -> Result<Vec<u8>> {
        let url = format!(
            "{}/api/v1/federation/models/{}/versions/{}",
            peer.endpoint, model, checkpoint_id
        );
        let mut req = self.http_client.get(&url);

        if let Some(api_key) = &peer.api_key {
            req = req.header("X-API-Key", api_key);
        }

        let response = req
            .send()
            .await
            .map_err(|e| VaultError::IoError(std::io::Error::other(format!("HTTP error: {e}"))))?;

        if !response.status().is_success() {
            return Err(VaultError::IoError(std::io::Error::other(format!(
                "Download failed: {}",
                response.status()
            ))));
        }

        let data = response
            .bytes()
            .await
            .map_err(|e| VaultError::IoError(std::io::Error::other(format!("Read error: {e}"))))?;

        Ok(data.to_vec())
    }

    /// Upload a version to peer
    async fn upload_version(
        &self,
        peer: &PeerConfig,
        model: &str,
        checkpoint_id: &str,
        data: &[u8],
    ) -> Result<()> {
        let url = format!(
            "{}/api/v1/federation/models/{}/versions/{}",
            peer.endpoint, model, checkpoint_id
        );
        let mut req = self.http_client.put(&url).body(data.to_vec());

        if let Some(api_key) = &peer.api_key {
            req = req.header("X-API-Key", api_key);
        }

        let response = req
            .send()
            .await
            .map_err(|e| VaultError::IoError(std::io::Error::other(format!("HTTP error: {e}"))))?;

        if !response.status().is_success() {
            return Err(VaultError::IoError(std::io::Error::other(format!(
                "Upload failed: {}",
                response.status()
            ))));
        }

        Ok(())
    }

    /// Save state to disk
    fn save_state(&self, state: &FederationState) -> Result<()> {
        let saved = SavedFederationState {
            models: state.models.clone(),
            clock: state.clock.clone(),
            history: state.history.clone(),
        };

        let json = serde_json::to_string_pretty(&saved)?;

        // Write with restrictive permissions
        {
            use std::io::Write;
            let mut opts = std::fs::OpenOptions::new();
            opts.write(true).create(true).truncate(true);
            crate::permissions::set_create_mode(&mut opts);
            let mut f = opts.open(&state.state_file)?;
            f.write_all(json.as_bytes())?;
        }
        crate::permissions::restrict_file(&state.state_file)?;

        Ok(())
    }

    /// Get sync history
    pub async fn get_history(&self, limit: Option<usize>) -> Vec<SyncResult> {
        let state = self.state.read().await;
        let history = &state.history;
        if let Some(n) = limit {
            history.iter().rev().take(n).cloned().collect()
        } else {
            history.clone()
        }
    }

    /// Get federation status
    pub async fn status(&self) -> FederationStatus {
        let state = self.state.read().await;
        FederationStatus {
            node_id: self.config.node_id.clone(),
            node_name: self.config.node_name.clone(),
            peer_count: self.config.peers.len(),
            model_count: state.models.len(),
            last_sync: state.history.last().map(|r| r.timestamp),
            clock: state.clock.clone(),
        }
    }
}

/// Sync delta
#[derive(Debug, Clone)]
pub struct SyncDelta {
    /// Items to upload to remote
    pub to_upload: Vec<SyncItem>,
    /// Items to download from remote
    pub to_download: Vec<SyncItem>,
    /// Conflicts detected
    pub conflicts: Vec<SyncConflict>,
}

/// Single item to sync
#[derive(Debug, Clone)]
pub struct SyncItem {
    /// Model name
    pub model: String,
    /// Checkpoint ID
    pub checkpoint_id: String,
    /// Size in bytes
    pub size_bytes: u64,
}

/// Saved federation state (for persistence)
#[derive(Debug, Clone, Serialize, Deserialize)]
struct SavedFederationState {
    models: HashMap<String, ModelSyncState>,
    clock: VectorClock,
    history: Vec<SyncResult>,
}

/// Federation status summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FederationStatus {
    /// This node's ID
    pub node_id: NodeId,
    /// This node's name
    pub node_name: String,
    /// Number of configured peers
    pub peer_count: usize,
    /// Number of synced models
    pub model_count: usize,
    /// Last sync time
    pub last_sync: Option<DateTime<Utc>>,
    /// Current vector clock
    pub clock: VectorClock,
}

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

    #[test]
    fn test_vector_clock_basic() {
        let mut clock1 = VectorClock::new();
        let mut clock2 = VectorClock::new();

        clock1.increment("node1");
        assert_eq!(clock1.timestamps.get("node1"), Some(&1));

        clock2.increment("node2");
        clock1.merge(&clock2);

        assert_eq!(clock1.timestamps.get("node1"), Some(&1));
        assert_eq!(clock1.timestamps.get("node2"), Some(&1));
    }

    #[test]
    fn test_vector_clock_comparison() {
        let mut clock1 = VectorClock::new();
        let mut clock2 = VectorClock::new();

        // Initially equal (both empty)
        assert_eq!(clock1.compare(&clock2), ClockComparison::Equal);

        // clock1 is now after clock2
        clock1.increment("node1");
        assert_eq!(clock1.compare(&clock2), ClockComparison::After);
        assert_eq!(clock2.compare(&clock1), ClockComparison::Before);

        // Now concurrent
        clock2.increment("node2");
        assert_eq!(clock1.compare(&clock2), ClockComparison::Concurrent);
    }

    #[test]
    fn test_vector_clock_multiple_increments() {
        let mut clock = VectorClock::new();
        clock.increment("a");
        clock.increment("a");
        clock.increment("a");
        assert_eq!(clock.timestamps.get("a"), Some(&3));
    }

    #[test]
    fn test_vector_clock_merge_takes_max() {
        let mut c1 = VectorClock::new();
        let mut c2 = VectorClock::new();

        c1.increment("a");
        c1.increment("a"); // a=2
        c2.increment("a"); // a=1
        c2.increment("b"); // b=1

        c1.merge(&c2);
        assert_eq!(c1.timestamps.get("a"), Some(&2)); // max(2,1)
        assert_eq!(c1.timestamps.get("b"), Some(&1)); // new entry
    }

    #[test]
    fn test_vector_clock_equal_after_merge() {
        let mut c1 = VectorClock::new();
        c1.increment("a");
        let c2 = c1.clone();
        assert_eq!(c1.compare(&c2), ClockComparison::Equal);
    }

    #[test]
    fn test_vector_clock_serialization() {
        let mut clock = VectorClock::new();
        clock.increment("node1");
        let json = serde_json::to_string(&clock).unwrap();
        let deserialized: VectorClock = serde_json::from_str(&json).unwrap();
        assert_eq!(clock, deserialized);
    }

    #[test]
    fn test_clock_comparison_debug() {
        assert_eq!(format!("{:?}", ClockComparison::Equal), "Equal");
        assert_eq!(format!("{:?}", ClockComparison::Before), "Before");
        assert_eq!(format!("{:?}", ClockComparison::After), "After");
        assert_eq!(format!("{:?}", ClockComparison::Concurrent), "Concurrent");
    }

    #[test]
    fn test_federation_config_default() {
        let config = FederationConfig::default();
        assert!(!config.node_id.is_empty());
        assert_eq!(config.sync_interval_secs, 300);
        assert!(config.auto_resolve_conflicts);
        assert_eq!(config.max_concurrent_syncs, 4);
        assert!(config.peers.is_empty());
        assert!(!config.node_name.is_empty());
    }

    #[test]
    fn test_federation_config_serialization() {
        let config = FederationConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        let deserialized: FederationConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.sync_interval_secs, 300);
        assert!(deserialized.auto_resolve_conflicts);
    }

    #[test]
    fn test_peer_config_zeroize_on_drop() {
        let peer = PeerConfig {
            node_id: "peer1".to_string(),
            name: "Test Peer".to_string(),
            endpoint: "https://example.com".to_string(),
            api_key: Some("secret-key-12345".to_string()),
            enabled: true,
        };
        // Drop should zeroize the api_key
        drop(peer);
    }

    #[test]
    fn test_peer_config_without_api_key() {
        let peer = PeerConfig {
            node_id: "peer1".to_string(),
            name: "Test Peer".to_string(),
            endpoint: "https://example.com".to_string(),
            api_key: None,
            enabled: false,
        };
        assert!(!peer.enabled);
        drop(peer);
    }

    #[test]
    fn test_peer_config_serialization() {
        let peer = PeerConfig {
            node_id: "p1".to_string(),
            name: "Peer 1".to_string(),
            endpoint: "https://peer1.example.com".to_string(),
            api_key: Some("key".to_string()),
            enabled: true,
        };
        let json = serde_json::to_string(&peer).unwrap();
        let deserialized: PeerConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.node_id, "p1");
        assert_eq!(deserialized.api_key.as_deref(), Some("key"));
    }

    #[test]
    fn test_model_sync_state_default_fields() {
        let state = ModelSyncState {
            name: "test-model".to_string(),
            clock: VectorClock::new(),
            last_sync: HashMap::new(),
            known_versions: HashSet::new(),
            pending_upload: HashSet::new(),
            pending_download: HashSet::new(),
        };
        assert_eq!(state.name, "test-model");
        assert!(state.known_versions.is_empty());
    }

    #[test]
    fn test_sync_manifest_serialization() {
        let manifest = SyncManifest {
            source_node: "node-a".to_string(),
            timestamp: Utc::now(),
            models: vec![],
            clock: VectorClock::new(),
        };
        let json = serde_json::to_string(&manifest).unwrap();
        let deserialized: SyncManifest = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.source_node, "node-a");
    }

    #[test]
    fn test_sync_result_serialization() {
        let result = SyncResult {
            peer_id: "peer-1".to_string(),
            timestamp: Utc::now(),
            duration_ms: 1500,
            models_synced: 3,
            versions_uploaded: 2,
            versions_downloaded: 1,
            conflicts: vec![],
            errors: vec![],
        };
        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("peer-1"));
    }

    #[test]
    fn test_sync_conflict_serialization() {
        let conflict = SyncConflict {
            model: "llama".to_string(),
            local_version: "abc".to_string(),
            remote_version: "def".to_string(),
            remote_node: "node-b".to_string(),
            resolution: Some(ConflictResolution::KeepLocal),
        };
        let json = serde_json::to_string(&conflict).unwrap();
        assert!(json.contains("KeepLocal"));
    }

    #[test]
    fn test_conflict_resolution_variants() {
        let keep = ConflictResolution::KeepLocal;
        let remote = ConflictResolution::UseRemote;
        let branch = ConflictResolution::Branch {
            local_name: "local-v2".to_string(),
            remote_name: "remote-v2".to_string(),
        };
        let manual = ConflictResolution::Manual;

        // Serialization round-trip
        for r in [keep, remote, manual] {
            let json = serde_json::to_string(&r).unwrap();
            let _: ConflictResolution = serde_json::from_str(&json).unwrap();
        }
        let json = serde_json::to_string(&branch).unwrap();
        assert!(json.contains("local-v2"));
    }

    #[test]
    fn test_version_manifest_entry() {
        let entry = VersionManifestEntry {
            version: 3,
            checkpoint_id: "cp-xyz".to_string(),
            created_at: Utc::now(),
            checksum: "sha256-abc".to_string(),
            size_bytes: 1024,
            parent_id: Some("cp-prev".to_string()),
            origin_node: "node-a".to_string(),
        };
        let json = serde_json::to_string(&entry).unwrap();
        let deserialized: VersionManifestEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.version, 3);
        assert_eq!(deserialized.checkpoint_id, "cp-xyz");
    }

    #[test]
    fn test_federation_status_serialization() {
        let status = FederationStatus {
            node_id: "n1".to_string(),
            node_name: "Node One".to_string(),
            peer_count: 2,
            model_count: 5,
            last_sync: Some(Utc::now()),
            clock: VectorClock::new(),
        };
        let json = serde_json::to_string(&status).unwrap();
        let d: FederationStatus = serde_json::from_str(&json).unwrap();
        assert_eq!(d.peer_count, 2);
    }

    #[tokio::test]
    async fn test_federation_manager_new() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();
        assert!(!mgr.node_id().is_empty());
        assert!(mgr.peers().is_empty());
    }

    #[tokio::test]
    async fn test_federation_manager_add_remove_peer() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mut mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let peer = PeerConfig {
            node_id: "peer-1".to_string(),
            name: "Peer 1".to_string(),
            endpoint: "https://peer1.example.com".to_string(),
            api_key: None,
            enabled: true,
        };
        mgr.add_peer(peer);
        assert_eq!(mgr.peers().len(), 1);

        mgr.remove_peer("peer-1");
        assert!(mgr.peers().is_empty());
    }

    #[tokio::test]
    async fn test_federation_manager_status() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig {
            node_name: "test-node".to_string(),
            ..Default::default()
        };
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let status = mgr.status().await;
        assert_eq!(status.node_name, "test-node");
        assert_eq!(status.peer_count, 0);
        assert_eq!(status.model_count, 0);
        assert!(status.last_sync.is_none());
    }

    #[tokio::test]
    async fn test_federation_manager_get_history_empty() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let history = mgr.get_history(None).await;
        assert!(history.is_empty());

        let limited = mgr.get_history(Some(5)).await;
        assert!(limited.is_empty());
    }

    #[tokio::test]
    async fn test_federation_manager_generate_manifest_empty() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let manifest = mgr.generate_manifest(vec![]).await;
        assert!(manifest.models.is_empty());
        assert_eq!(manifest.source_node, mgr.node_id());
    }

    #[tokio::test]
    async fn test_federation_manager_generate_manifest_with_models() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let versions = vec![crate::version::ModelVersion {
            version: 1,
            checkpoint_id: "cp-1".to_string(),
            timestamp: Utc::now(),
            checksum_sha256: "abc123".to_string(),
            size_bytes: 1024,
            compressed_size_bytes: 800,
            parent_version: None,
            format: "safetensors".to_string(),
            metadata: HashMap::new(),
            file_path: "models/cp-1.enc".to_string(),
        }];

        let manifest = mgr
            .generate_manifest(vec![("my-model".to_string(), versions)])
            .await;
        assert_eq!(manifest.models.len(), 1);
        assert_eq!(manifest.models[0].name, "my-model");
        assert_eq!(manifest.models[0].versions.len(), 1);
    }

    #[test]
    fn test_compute_delta_empty_manifests() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let local = SyncManifest {
            source_node: "a".to_string(),
            timestamp: Utc::now(),
            models: vec![],
            clock: VectorClock::new(),
        };
        let remote = SyncManifest {
            source_node: "b".to_string(),
            timestamp: Utc::now(),
            models: vec![],
            clock: VectorClock::new(),
        };

        let delta = mgr.compute_delta(&local, &remote);
        assert!(delta.to_upload.is_empty());
        assert!(delta.to_download.is_empty());
        assert!(delta.conflicts.is_empty());
    }

    #[test]
    fn test_compute_delta_local_only_model() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let local = SyncManifest {
            source_node: "a".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "model-x".to_string(),
                versions: vec![VersionManifestEntry {
                    version: 1,
                    checkpoint_id: "cp-1".to_string(),
                    created_at: Utc::now(),
                    checksum: "sha".to_string(),
                    size_bytes: 100,
                    parent_id: None,
                    origin_node: "a".to_string(),
                }],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };
        let remote = SyncManifest {
            source_node: "b".to_string(),
            timestamp: Utc::now(),
            models: vec![],
            clock: VectorClock::new(),
        };

        let delta = mgr.compute_delta(&local, &remote);
        assert_eq!(delta.to_upload.len(), 1);
        assert_eq!(delta.to_upload[0].model, "model-x");
        assert!(delta.to_download.is_empty());
    }

    #[test]
    fn test_compute_delta_remote_only_model() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let local = SyncManifest {
            source_node: "a".to_string(),
            timestamp: Utc::now(),
            models: vec![],
            clock: VectorClock::new(),
        };
        let remote = SyncManifest {
            source_node: "b".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "model-y".to_string(),
                versions: vec![VersionManifestEntry {
                    version: 1,
                    checkpoint_id: "cp-2".to_string(),
                    created_at: Utc::now(),
                    checksum: "sha2".to_string(),
                    size_bytes: 200,
                    parent_id: None,
                    origin_node: "b".to_string(),
                }],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };

        let delta = mgr.compute_delta(&local, &remote);
        assert!(delta.to_upload.is_empty());
        assert_eq!(delta.to_download.len(), 1);
        assert_eq!(delta.to_download[0].model, "model-y");
    }

    #[test]
    fn test_compute_delta_conflict() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let local = SyncManifest {
            source_node: "a".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "model-z".to_string(),
                versions: vec![VersionManifestEntry {
                    version: 1,
                    checkpoint_id: "cp-local".to_string(),
                    created_at: Utc::now(),
                    checksum: "sha-l".to_string(),
                    size_bytes: 100,
                    parent_id: None,
                    origin_node: "a".to_string(),
                }],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };
        let remote = SyncManifest {
            source_node: "b".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "model-z".to_string(),
                versions: vec![VersionManifestEntry {
                    version: 1,
                    checkpoint_id: "cp-remote".to_string(),
                    created_at: Utc::now(),
                    checksum: "sha-r".to_string(),
                    size_bytes: 100,
                    parent_id: None,
                    origin_node: "b".to_string(),
                }],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };

        let delta = mgr.compute_delta(&local, &remote);
        assert_eq!(delta.conflicts.len(), 1);
        assert_eq!(delta.conflicts[0].model, "model-z");
    }

    #[test]
    fn test_compute_delta_shared_versions() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        // Both nodes have the same version — no sync needed
        let entry = VersionManifestEntry {
            version: 1,
            checkpoint_id: "cp-same".to_string(),
            created_at: Utc::now(),
            checksum: "sha".to_string(),
            size_bytes: 100,
            parent_id: None,
            origin_node: "a".to_string(),
        };
        let local = SyncManifest {
            source_node: "a".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "m".to_string(),
                versions: vec![entry.clone()],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };
        let remote = SyncManifest {
            source_node: "b".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "m".to_string(),
                versions: vec![entry],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };

        let delta = mgr.compute_delta(&local, &remote);
        assert!(delta.to_upload.is_empty());
        assert!(delta.to_download.is_empty());
        assert!(delta.conflicts.is_empty());
    }

    #[tokio::test]
    async fn test_federation_manager_load_existing_state() {
        let dir = tempfile::tempdir().unwrap();
        let state_file = dir.path().join("federation_state.json");
        let saved = SavedFederationState {
            models: HashMap::new(),
            clock: {
                let mut c = VectorClock::new();
                c.increment("existing");
                c
            },
            history: vec![],
        };
        std::fs::write(&state_file, serde_json::to_string(&saved).unwrap()).unwrap();

        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let status = mgr.status().await;
        assert_eq!(status.clock.timestamps.get("existing"), Some(&1));
    }

    #[test]
    fn test_sync_item_debug() {
        let item = SyncItem {
            model: "m".to_string(),
            checkpoint_id: "cp".to_string(),
            size_bytes: 42,
        };
        let dbg = format!("{:?}", item);
        assert!(dbg.contains("42"));
    }

    #[test]
    fn test_model_manifest_entry_serialization() {
        let entry = ModelManifestEntry {
            name: "llm".to_string(),
            versions: vec![],
            clock: VectorClock::new(),
        };
        let json = serde_json::to_string(&entry).unwrap();
        let d: ModelManifestEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(d.name, "llm");
    }

    #[tokio::test]
    async fn test_federation_manager_save_and_reload_state() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let node_id = config.node_id.clone();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        // Modify internal state via generate_manifest (increments clock indirectly)
        // Then save state by accessing internals
        {
            let mut state = mgr.state.write().await;
            state.clock.increment(&node_id);
            state.models.insert(
                "saved-model".to_string(),
                ModelSyncState {
                    name: "saved-model".to_string(),
                    clock: VectorClock::new(),
                    last_sync: HashMap::new(),
                    known_versions: HashSet::new(),
                    pending_upload: HashSet::new(),
                    pending_download: HashSet::new(),
                },
            );
            mgr.save_state(&state).unwrap();
        }

        // Reload from same directory
        let config2 = FederationConfig::default();
        let mgr2 = FederationManager::new(config2, dir.path().to_path_buf()).unwrap();
        let status2 = mgr2.status().await;
        assert_eq!(status2.model_count, 1);
        assert_eq!(status2.clock.timestamps.get(&node_id), Some(&1));
    }

    #[test]
    fn test_compute_delta_mixed_versions() {
        // Both nodes have the model but with different extra versions
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let shared = VersionManifestEntry {
            version: 1,
            checkpoint_id: "cp-shared".to_string(),
            created_at: Utc::now(),
            checksum: "sha".to_string(),
            size_bytes: 100,
            parent_id: None,
            origin_node: "a".to_string(),
        };
        let local_only = VersionManifestEntry {
            version: 2,
            checkpoint_id: "cp-local-v2".to_string(),
            created_at: Utc::now(),
            checksum: "sha2".to_string(),
            size_bytes: 200,
            parent_id: Some("cp-shared".to_string()),
            origin_node: "a".to_string(),
        };
        let remote_only = VersionManifestEntry {
            version: 3,
            checkpoint_id: "cp-remote-v3".to_string(),
            created_at: Utc::now(),
            checksum: "sha3".to_string(),
            size_bytes: 300,
            parent_id: Some("cp-shared".to_string()),
            origin_node: "b".to_string(),
        };

        let local = SyncManifest {
            source_node: "a".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "m".to_string(),
                versions: vec![shared.clone(), local_only],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };
        let remote = SyncManifest {
            source_node: "b".to_string(),
            timestamp: Utc::now(),
            models: vec![ModelManifestEntry {
                name: "m".to_string(),
                versions: vec![shared, remote_only],
                clock: VectorClock::new(),
            }],
            clock: VectorClock::new(),
        };

        let delta = mgr.compute_delta(&local, &remote);
        assert_eq!(delta.to_upload.len(), 1);
        assert_eq!(delta.to_upload[0].checkpoint_id, "cp-local-v2");
        assert_eq!(delta.to_download.len(), 1);
        assert_eq!(delta.to_download[0].checkpoint_id, "cp-remote-v3");
        assert!(delta.conflicts.is_empty()); // Different version numbers, no conflict
    }

    #[tokio::test]
    async fn test_federation_manager_generate_manifest_multi_version() {
        let dir = tempfile::tempdir().unwrap();
        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let versions = vec![
            crate::version::ModelVersion {
                version: 1,
                checkpoint_id: "cp-1".to_string(),
                timestamp: Utc::now(),
                checksum_sha256: "abc".to_string(),
                size_bytes: 512,
                compressed_size_bytes: 400,
                parent_version: None,
                format: "safetensors".to_string(),
                metadata: HashMap::new(),
                file_path: "models/cp-1.enc".to_string(),
            },
            crate::version::ModelVersion {
                version: 2,
                checkpoint_id: "cp-2".to_string(),
                timestamp: Utc::now(),
                checksum_sha256: "def".to_string(),
                size_bytes: 1024,
                compressed_size_bytes: 900,
                parent_version: Some(1),
                format: "gguf".to_string(),
                metadata: HashMap::new(),
                file_path: "models/cp-2.enc".to_string(),
            },
        ];

        let manifest = mgr
            .generate_manifest(vec![("multi-v".to_string(), versions)])
            .await;
        assert_eq!(manifest.models.len(), 1);
        assert_eq!(manifest.models[0].versions.len(), 2);
        assert_eq!(manifest.models[0].versions[0].version, 1);
        assert_eq!(manifest.models[0].versions[1].version, 2);
        assert_eq!(
            manifest.models[0].versions[1].parent_id.as_deref(),
            Some("1")
        );
    }

    #[test]
    fn test_saved_federation_state_roundtrip() {
        let mut clock = VectorClock::new();
        clock.increment("n1");
        clock.increment("n2");

        let saved = SavedFederationState {
            models: {
                let mut m = HashMap::new();
                m.insert(
                    "model-a".to_string(),
                    ModelSyncState {
                        name: "model-a".to_string(),
                        clock: clock.clone(),
                        last_sync: HashMap::new(),
                        known_versions: {
                            let mut s = HashSet::new();
                            s.insert("cp-1".to_string());
                            s
                        },
                        pending_upload: HashSet::new(),
                        pending_download: HashSet::new(),
                    },
                );
                m
            },
            clock,
            history: vec![SyncResult {
                peer_id: "p1".to_string(),
                timestamp: Utc::now(),
                duration_ms: 500,
                models_synced: 1,
                versions_uploaded: 0,
                versions_downloaded: 1,
                conflicts: vec![],
                errors: vec![],
            }],
        };

        let json = serde_json::to_string(&saved).unwrap();
        let loaded: SavedFederationState = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.models.len(), 1);
        assert!(loaded.models.contains_key("model-a"));
        assert_eq!(loaded.history.len(), 1);
        assert_eq!(loaded.clock.timestamps.get("n1"), Some(&1));
    }

    #[test]
    fn test_sync_delta_debug() {
        let delta = SyncDelta {
            to_upload: vec![],
            to_download: vec![],
            conflicts: vec![],
        };
        let dbg = format!("{:?}", delta);
        assert!(dbg.contains("SyncDelta"));
    }

    #[test]
    fn test_model_sync_state_with_pending() {
        let mut state = ModelSyncState {
            name: "pending-model".to_string(),
            clock: VectorClock::new(),
            last_sync: HashMap::new(),
            known_versions: HashSet::new(),
            pending_upload: HashSet::new(),
            pending_download: HashSet::new(),
        };
        state.pending_upload.insert("cp-up-1".to_string());
        state
            .pending_download
            .insert(("node-b".to_string(), "cp-down-1".to_string()));
        state.known_versions.insert("cp-1".to_string());
        state.last_sync.insert("node-b".to_string(), Utc::now());

        let json = serde_json::to_string(&state).unwrap();
        let d: ModelSyncState = serde_json::from_str(&json).unwrap();
        assert_eq!(d.pending_upload.len(), 1);
        assert_eq!(d.pending_download.len(), 1);
        assert_eq!(d.known_versions.len(), 1);
        assert_eq!(d.last_sync.len(), 1);
    }

    #[tokio::test]
    async fn test_federation_manager_history_with_limit() {
        let dir = tempfile::tempdir().unwrap();
        let state_file = dir.path().join("federation_state.json");

        // Pre-populate with history
        let history: Vec<SyncResult> = (0..5)
            .map(|i| SyncResult {
                peer_id: format!("peer-{}", i),
                timestamp: Utc::now(),
                duration_ms: 100 * (i as u64 + 1),
                models_synced: 1,
                versions_uploaded: 0,
                versions_downloaded: 1,
                conflicts: vec![],
                errors: vec![],
            })
            .collect();

        let saved = SavedFederationState {
            models: HashMap::new(),
            clock: VectorClock::new(),
            history,
        };
        std::fs::write(&state_file, serde_json::to_string(&saved).unwrap()).unwrap();

        let config = FederationConfig::default();
        let mgr = FederationManager::new(config, dir.path().to_path_buf()).unwrap();

        let all = mgr.get_history(None).await;
        assert_eq!(all.len(), 5);

        let limited = mgr.get_history(Some(3)).await;
        assert_eq!(limited.len(), 3);
        // get_history returns reversed (most recent first)
        assert_eq!(limited[0].peer_id, "peer-4");
    }

    #[test]
    fn test_conflict_resolution_branch_serialization() {
        let res = ConflictResolution::Branch {
            local_name: "main-v2".to_string(),
            remote_name: "fork-v2".to_string(),
        };
        let json = serde_json::to_string(&res).unwrap();
        let d: ConflictResolution = serde_json::from_str(&json).unwrap();
        match d {
            ConflictResolution::Branch {
                local_name,
                remote_name,
            } => {
                assert_eq!(local_name, "main-v2");
                assert_eq!(remote_name, "fork-v2");
            }
            _ => panic!("Expected Branch variant"),
        }
    }

    #[test]
    fn test_federation_status_without_last_sync() {
        let status = FederationStatus {
            node_id: "n1".to_string(),
            node_name: "node1".to_string(),
            peer_count: 0,
            model_count: 0,
            last_sync: None,
            clock: VectorClock::new(),
        };
        let json = serde_json::to_string(&status).unwrap();
        let d: FederationStatus = serde_json::from_str(&json).unwrap();
        assert!(d.last_sync.is_none());
    }
}