aletheiadb 0.1.0

A high-performance bi-temporal graph database for LLM integration
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
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
//! Shard coordinator for managing distributed operations.
//!
//! The `ShardCoordinator` is the central component responsible for orchestrating
//! distributed transactions across multiple shards in AletheiaDB. It ensures
//! ACID properties (Atomicity, Consistency, Isolation, Durability) are maintained
//! even when data is partitioned across different nodes.
//!
//! # Key Responsibilities
//!
//! 1.  **Transaction Coordination**: Manages the lifecycle of distributed transactions using
//!     the Two-Phase Commit (2PC) protocol.
//! 2.  **Query Routing**: Uses the `ShardRouter` to direct queries to the appropriate shards.
//! 3.  **Time Synchronization**: Maintains a Hybrid Logical Clock (HLC) to assign consistent
//!     timestamps for cross-shard operations.
//! 4.  **Failure Recovery**: Detects failed transactions and orchestrates recovery or abortion.
//!
//! # Two-Phase Commit (2PC) Flow
//!
//! 1.  **Begin**: A transaction ID is generated.
//! 2.  **Prepare**: The coordinator asks all participating shards to prepare.
//!     *   If all vote YES, the transaction proceeds to commit.
//!     *   If any vote NO or timeout, the transaction is aborted.
//! 3.  **Commit**:
//!     *   The decision is logged to the `TwoPhaseCommitLog` (WAL).
//!     *   The coordinator tells all participants to commit.
//!     *   Once all acknowledge, the decision log is cleared.
//!
//! # ⚠️ Performance Warning: Sequential 2PC
//!
//! Currently, the 2PC protocol phases (Prepare and Commit) are executed **sequentially**
//! across participants.
//!
//! - **Latency**: The commit latency is the *sum* of participant latencies, not the max.
//! - **Lock Duration**: Locks are held on early participants while waiting for later ones.
//! - **Scalability**: Transaction throughput may degrade linearly with the number of participants.
//!
//! This design prioritizes simplicity and determinism over concurrency in the current
//! version. Future versions will introduce parallel 2PC execution.
//!
//! # Recovery
//!
//! If the coordinator crashes during the Commit phase, the `recover_pending_transactions`
//! method replays the pending decisions from the log, ensuring that all participants
//! eventually reach a consistent state.

use super::config::{RebalanceConfig, ShardConfig};
use super::persistent_commit_log::{CommitLogConfig, PersistentCommitLog};
use super::router::{ShardRouter, TraversalPlan};
use super::transaction::{DistributedTransaction, DistributedTxError, TransactionPhase};
use super::types::{ShardId, ShardMetrics, ShardState, ShardStatus};
use crate::core::hlc::{
    HybridTimestamp, MAX_FORWARD_JUMP_US, SendWithSelfHealError, evaluate_clock_skew,
    is_clock_skew_self_heal_enabled, send_with_overflow_self_heal,
};
use crate::core::id::{IdGenerator, TxId};
use crate::core::temporal::time;
use std::collections::HashMap;
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};

#[cfg(test)]
use crate::core::hlc::MAX_BACKWARD_DRIFT_US;

/// Result of recovery operation.
#[derive(Debug, Clone)]
pub struct RecoveryResult {
    /// Transactions that were successfully recovered.
    pub recovered: Vec<TxId>,
    /// Transactions that failed recovery and were dead-lettered.
    pub dead_lettered: Vec<DeadLetteredTransaction>,
}

impl RecoveryResult {
    /// Check if recovery was fully successful (no dead letters).
    pub fn is_complete(&self) -> bool {
        self.dead_lettered.is_empty()
    }

    /// Get the number of transactions that required manual intervention.
    ///
    /// # The Spark
    /// Similar to `is_complete`, this allows observability tools to report the exact size of the
    /// backlog that operations teams need to manually investigate and resolve.
    ///
    /// # Examples
    /// ```
    /// use aletheiadb::storage::sharding::coordinator::{RecoveryResult, DeadLetteredTransaction};
    /// use aletheiadb::core::id::TxId;
    /// use std::time::Instant;
    ///
    /// let result = RecoveryResult {
    ///     recovered: vec![],
    ///     dead_lettered: vec![DeadLetteredTransaction {
    ///         tx_id: TxId::new(2),
    ///         reason: "Shard unrecoverable".to_string(),
    ///         last_attempt: Instant::now(),
    ///         attempt_count: 1,
    ///     }],
    /// };
    /// assert_eq!(result.dead_letter_count(), 1);
    /// ```
    pub fn dead_letter_count(&self) -> usize {
        self.dead_lettered.len()
    }
}

/// A transaction that failed recovery and requires manual intervention.
#[derive(Debug, Clone)]
pub struct DeadLetteredTransaction {
    /// The transaction ID.
    pub tx_id: TxId,
    /// Reason for dead-lettering.
    pub reason: String,
    /// Time of last recovery attempt.
    pub last_attempt: Instant,
    /// Number of recovery attempts made.
    pub attempt_count: u32,
}

/// Connection to a shard (placeholder for actual network implementation).
#[derive(Debug)]
pub struct ShardConnection {
    /// The shard ID this connection is for.
    pub shard_id: ShardId,
    /// Endpoint address.
    pub endpoint: String,
    /// Whether the connection is healthy.
    pub healthy: bool,
    /// Remote HLC frontier observed from this shard.
    hlc_frontier: Mutex<HybridTimestamp>,
    /// Last successful ping time.
    pub last_ping: Option<Instant>,
}

impl ShardConnection {
    /// Create a new shard connection.
    pub fn new(shard_id: ShardId, endpoint: String) -> Self {
        Self {
            shard_id,
            endpoint,
            healthy: true,
            hlc_frontier: Mutex::new(time::now()),
            last_ping: None,
        }
    }

    /// Simulate a prepare call to the shard.
    pub fn prepare(
        &self,
        _tx_id: TxId,
        timestamp: Option<HybridTimestamp>,
    ) -> Result<(), DistributedTxError> {
        self.apply_remote_timestamp(timestamp);
        if !self.healthy {
            return Err(DistributedTxError::ParticipantUnavailable {
                shard_id: self.shard_id,
            });
        }
        // In a real implementation, this would make an RPC call
        Ok(())
    }

    /// Simulate a commit call to the shard.
    pub fn commit(
        &self,
        _tx_id: TxId,
        commit_timestamp: Option<HybridTimestamp>,
    ) -> Result<(), DistributedTxError> {
        self.apply_remote_timestamp(commit_timestamp);
        if !self.healthy {
            return Err(DistributedTxError::ParticipantUnavailable {
                shard_id: self.shard_id,
            });
        }
        // In a real implementation, this would make an RPC call
        Ok(())
    }

    /// Simulate an abort call to the shard.
    pub fn abort(&self, _tx_id: TxId) -> Result<(), DistributedTxError> {
        if !self.healthy {
            return Err(DistributedTxError::ParticipantUnavailable {
                shard_id: self.shard_id,
            });
        }
        // In a real implementation, this would make an RPC call
        Ok(())
    }

    fn apply_remote_timestamp(&self, timestamp: Option<HybridTimestamp>) {
        if let Some(remote_ts) = timestamp
            && let Ok(mut frontier) = self.hlc_frontier.lock()
            && let Ok(updated) = frontier.receive(remote_ts, time::now().wallclock())
        {
            *frontier = updated;
        }
    }

    /// Perform a health check.
    pub fn health_check(&mut self) -> bool {
        // In a real implementation, this would ping the shard
        self.last_ping = Some(Instant::now());
        self.healthy
    }

    /// Mark the connection as unhealthy.
    pub fn mark_unhealthy(&mut self) {
        self.healthy = false;
    }

    /// Mark the connection as healthy.
    pub fn mark_healthy(&mut self) {
        self.healthy = true;
        self.last_ping = Some(Instant::now());
    }
}

/// Coordinator for managing shards and distributed operations.
///
/// This struct holds the state required to coordinate distributed transactions,
/// including connections to shards, the transaction ID generator, and the commit log.
///
/// # Example
///
/// ```no_run
/// use aletheiadb::storage::sharding::{ShardCoordinator, ShardConfig};
///
/// // Configure shards
/// let config = ShardConfig::default(); // ... configure
/// let coordinator = ShardCoordinator::new(config);
///
/// // Route a query
/// let shard_id = coordinator.route_node("Person");
/// ```
pub struct ShardCoordinator {
    /// Router for query routing decisions.
    router: ShardRouter,
    /// Connections to each shard.
    connections: RwLock<HashMap<ShardId, ShardConnection>>,
    /// State of each shard.
    shard_states: RwLock<HashMap<ShardId, ShardState>>,
    /// Transaction ID generator.
    tx_id_generator: IdGenerator,
    /// Active distributed transactions.
    active_transactions: RwLock<HashMap<TxId, DistributedTransaction>>,
    /// Commit log for recovery.
    commit_log: RwLock<PersistentCommitLog>,
    /// Coordinating HLC frontier used to assign cross-shard commit timestamps.
    commit_clock: Mutex<HybridTimestamp>,
    /// Monotonic observation time for commit clock drift checks.
    commit_clock_observed_at: Mutex<Instant>,
    /// Metrics per shard.
    metrics: RwLock<HashMap<ShardId, Arc<ShardMetrics>>>,
    /// Rebalance configuration.
    rebalance_config: RebalanceConfig,
    /// Timeout for transaction operations.
    transaction_timeout: Duration,
    /// Dead letter queue for transactions that failed recovery.
    dead_letter_queue: RwLock<HashMap<TxId, DeadLetteredTransaction>>,
}

impl ShardCoordinator {
    /// Create a new shard coordinator.
    ///
    /// Initializes connections to all defined shards, sets up the router, and
    /// prepares the transaction ID generator.
    pub fn new(config: ShardConfig) -> Self {
        let mut connections = HashMap::new();
        let mut shard_states = HashMap::new();
        let mut metrics = HashMap::new();

        for shard_def in &config.shards {
            connections.insert(
                shard_def.id,
                ShardConnection::new(shard_def.id, shard_def.endpoint.clone()),
            );
            shard_states.insert(shard_def.id, ShardState::new(shard_def.id));
            metrics.insert(shard_def.id, Arc::new(ShardMetrics::new()));
        }

        let transaction_timeout = config.request_timeout;

        let commit_log = if let Some(path) = &config.wal_path {
            PersistentCommitLog::new(path, CommitLogConfig::default())
                .expect("Failed to open persistent commit log")
        } else {
            PersistentCommitLog::in_memory()
        };

        let max_tx_id = commit_log.max_seen_tx_id();
        let router = ShardRouter::new(config);

        let coordinator = Self {
            router,
            connections: RwLock::new(connections),
            shard_states: RwLock::new(shard_states),
            tx_id_generator: IdGenerator::with_start(max_tx_id + 1),
            active_transactions: RwLock::new(HashMap::new()),
            commit_log: RwLock::new(commit_log),
            commit_clock: Mutex::new(time::now()),
            commit_clock_observed_at: Mutex::new(Instant::now()),
            metrics: RwLock::new(metrics),
            rebalance_config: RebalanceConfig::default(),
            transaction_timeout,
            dead_letter_queue: RwLock::new(HashMap::new()),
        };

        // Recover pending transactions on startup
        coordinator.startup_recovery();

        coordinator
    }

    /// Create a coordinator with custom rebalance config.
    pub fn with_rebalance_config(mut self, config: RebalanceConfig) -> Self {
        self.rebalance_config = config;
        self
    }

    fn startup_recovery(&self) {
        // Recover pending transactions on startup
        // This will panic if the commit log lock is poisoned (unlikely on startup)
        let result = self.recover_pending_transactions();

        // Ensure result is used to avoid dead code elimination/coverage gaps
        if !result.is_complete() {
            #[cfg(feature = "observability")]
            tracing::error!(
                "Recovered partial state on startup: {} recovered, {} dead lettered",
                result.recovered.len(),
                result.dead_letter_count()
            );

            // Fail fast on startup if we cannot guarantee consistency
            panic!(
                "Failed to recover all pending transactions on startup. {} transactions are dead-lettered. Manual intervention required.",
                result.dead_letter_count()
            );
        }
    }

    fn reinsert_transaction(&self, tx_id: TxId, transaction: DistributedTransaction) {
        if let Ok(mut txns) = self.active_transactions.write() {
            txns.insert(tx_id, transaction);
        }
    }

    fn adaptive_forward_jump_limit_us(
        &self,
        observed_at: Instant,
    ) -> Result<i64, DistributedTxError> {
        let mut previous_observed_at =
            self.commit_clock_observed_at
                .lock()
                .map_err(|_| DistributedTxError::Aborted {
                    reason: "Clock observation lock poisoned".to_string(),
                })?;
        let elapsed = observed_at.duration_since(*previous_observed_at);
        *previous_observed_at = observed_at;

        let elapsed_us = i64::try_from(elapsed.as_micros()).unwrap_or(i64::MAX);
        Ok(MAX_FORWARD_JUMP_US.saturating_add(elapsed_us))
    }

    fn next_commit_timestamp(&self) -> Result<HybridTimestamp, DistributedTxError> {
        self.next_commit_timestamp_internal(Instant::now())
    }

    fn next_commit_timestamp_internal(
        &self,
        observed_at: Instant,
    ) -> Result<HybridTimestamp, DistributedTxError> {
        let mut frontier = self
            .commit_clock
            .lock()
            .map_err(|_| DistributedTxError::Aborted {
                reason: "Clock frontier lock poisoned".to_string(),
            })?;

        let current_wallclock = time::now();
        let self_heal_clock_skew = is_clock_skew_self_heal_enabled();
        let adaptive_forward_limit_us = self.adaptive_forward_jump_limit_us(observed_at)?;
        let skew_decision = evaluate_clock_skew(
            current_wallclock.wallclock(),
            frontier.wallclock(),
            Some(adaptive_forward_limit_us),
            self_heal_clock_skew,
        )
        .map_err(|violation| DistributedTxError::Aborted {
            reason: format!(
                "Clock skew detected: {} drift {}us exceeds max {}us",
                violation.direction.as_str(),
                violation.drift_us,
                violation.max_allowed
            ),
        })?;

        if self_heal_clock_skew && let Some(_direction) = skew_decision.healed_direction {
            #[cfg(feature = "observability")]
            tracing::warn!(
                wallclock_ts = %current_wallclock,
                prev_ts = %frontier,
                drift_us = skew_decision.drift_us,
                reason = _direction.as_str(),
                "Self-healing clock skew by clamping to local HLC frontier"
            );
        }

        let next = send_with_overflow_self_heal(
            &frontier,
            skew_decision.effective_wallclock,
            self_heal_clock_skew,
            |error| match error {
                SendWithSelfHealError::InitialSend(error) => DistributedTxError::Aborted {
                    reason: format!("Failed to advance HLC frontier: {}", error),
                },
                SendWithSelfHealError::FallbackWallclockOverflow {
                    wallclock,
                    current_logical: _,
                } => DistributedTxError::Aborted {
                    reason: format!(
                        "HLC logical counter overflow while self-healing at wallclock={}",
                        wallclock
                    ),
                },
                SendWithSelfHealError::FallbackSend(fallback_error) => {
                    DistributedTxError::Aborted {
                        reason: format!(
                            "HLC timestamp generation failed while self-healing: {}",
                            fallback_error
                        ),
                    }
                }
            },
        )?;

        *frontier = next;
        Ok(next)
    }

    /// Get the router.
    pub fn router(&self) -> &ShardRouter {
        &self.router
    }

    /// Route a node query.
    pub fn route_node(&self, label: &str) -> ShardId {
        self.router.route_node(label)
    }

    /// Route a traversal query.
    pub fn route_traversal(&self, start_label: &str, target_labels: &[&str]) -> TraversalPlan {
        self.router.route_traversal(start_label, target_labels)
    }

    /// Get the state of a shard.
    pub fn get_shard_state(&self, shard_id: ShardId) -> Option<ShardState> {
        self.shard_states.read().ok()?.get(&shard_id).cloned()
    }

    /// Get all shard states.
    pub fn get_all_shard_states(&self) -> Vec<ShardState> {
        self.shard_states
            .read()
            .map(|states| states.values().cloned().collect())
            .unwrap_or_default()
    }

    /// Update shard state.
    pub fn update_shard_state(&self, shard_id: ShardId, state: ShardState) {
        if let Ok(mut states) = self.shard_states.write() {
            states.insert(shard_id, state);
        }
    }

    /// Get metrics for a shard.
    pub fn get_metrics(&self, shard_id: ShardId) -> Option<Arc<ShardMetrics>> {
        self.metrics.read().ok()?.get(&shard_id).cloned()
    }

    /// Start a new distributed transaction.
    ///
    /// Generates a new unique `TxId` and registers the transaction as active.
    /// The transaction starts in the `Pending` phase.
    pub fn begin_distributed_transaction(
        &self,
        participants: Vec<ShardId>,
    ) -> Result<TxId, DistributedTxError> {
        let tx_id =
            TxId::new(
                self.tx_id_generator
                    .next()
                    .map_err(|_| DistributedTxError::Aborted {
                        reason: "Transaction ID exhausted".to_string(),
                    })?,
            );

        let transaction =
            DistributedTransaction::new(tx_id, participants, self.transaction_timeout);

        if let Ok(mut txns) = self.active_transactions.write() {
            txns.insert(tx_id, transaction);
        }

        Ok(tx_id)
    }

    /// Execute the prepare phase of 2PC.
    ///
    /// 1.  Assigns a commit timestamp (if not already assigned).
    /// 2.  Sends `Prepare` requests to all participant shards.
    /// 3.  If all shards respond with success, the transaction moves to `Prepared` state.
    /// 4.  If any shard fails or times out, the transaction is aborted.
    pub fn prepare_distributed_transaction(&self, tx_id: TxId) -> Result<(), DistributedTxError> {
        // Get the transaction
        let mut transaction = {
            let mut txns =
                self.active_transactions
                    .write()
                    .map_err(|_| DistributedTxError::Aborted {
                        reason: "Lock poisoned".to_string(),
                    })?;
            txns.remove(&tx_id)
                .ok_or_else(|| DistributedTxError::Aborted {
                    reason: "Transaction not found".to_string(),
                })?
        };

        // Begin prepare phase
        if let Err(error) = transaction.begin_prepare() {
            self.reinsert_transaction(tx_id, transaction);
            return Err(error);
        }

        // Allocate a single commit timestamp for prepare + commit.
        // This keeps participant RPCs causally ordered even if wallclock
        // shifts during the two-phase commit sequence.
        if transaction.commit_timestamp.is_none() {
            match self.next_commit_timestamp() {
                Ok(timestamp) => transaction.commit_timestamp = Some(timestamp),
                Err(error) => {
                    // No participant has seen this transaction yet, so reset phase to pending.
                    transaction.phase = TransactionPhase::Pending;
                    self.reinsert_transaction(tx_id, transaction);
                    return Err(error);
                }
            }
        }
        let prepare_timestamp = transaction.commit_timestamp;

        // Send prepare to all participants
        let connections = match self.connections.read() {
            Ok(connections) => connections,
            Err(_) => {
                transaction.phase = TransactionPhase::Pending;
                self.reinsert_transaction(tx_id, transaction);
                return Err(DistributedTxError::Aborted {
                    reason: "Lock poisoned".to_string(),
                });
            }
        };

        let mut unavailable_shards = Vec::new();

        for shard_id in transaction.participant_shards() {
            if let Some(conn) = connections.get(&shard_id) {
                match conn.prepare(tx_id, prepare_timestamp) {
                    Ok(()) => transaction.record_prepare_success(shard_id),
                    Err(DistributedTxError::ParticipantUnavailable { .. }) => {
                        transaction.record_unreachable(shard_id);
                        unavailable_shards.push(shard_id);
                    }
                    Err(_) => {
                        transaction.record_prepare_failure(shard_id);
                    }
                }
            } else {
                transaction.record_unreachable(shard_id);
                unavailable_shards.push(shard_id);
            }
        }

        // Check if all prepared
        if transaction.any_aborted() || transaction.any_unreachable() {
            // Abort the transaction
            let failed: Vec<ShardId> = transaction
                .participants
                .iter()
                .filter(|(_, state)| **state != super::transaction::ParticipantState::Prepared)
                .map(|(id, _)| *id)
                .collect();

            // Send abort to prepared participants
            for shard_id in transaction.participant_shards() {
                if let Some(conn) = connections.get(&shard_id) {
                    let _ = conn.abort(tx_id);
                }
            }

            transaction.abort("Prepare phase failed");
            drop(connections); // Prevent deadlock with active_transactions.write()

            for shard_id in unavailable_shards {
                self.mark_shard_unavailable(shard_id);
            }

            // Re-insert the aborted transaction for tracking
            if let Ok(mut txns) = self.active_transactions.write() {
                txns.insert(tx_id, transaction);
            }

            return Err(DistributedTxError::PrepareFailed {
                failed_participants: failed,
            });
        }

        // Mark as prepared
        drop(connections); // Prevent deadlock with active_transactions.write()
        if let Err(e) = transaction.mark_prepared() {
            if let Ok(mut txns) = self.active_transactions.write() {
                txns.insert(tx_id, transaction);
            }
            return Err(e);
        }

        // Re-insert the transaction
        if let Ok(mut txns) = self.active_transactions.write() {
            txns.insert(tx_id, transaction);
        }

        Ok(())
    }

    /// Execute the commit phase of 2PC.
    ///
    /// This method follows the critical 2PC protocol to ensure atomicity:
    ///
    /// 1.  **Log Decision**: The decision to commit is persisted to the `TwoPhaseCommitLog`.
    ///     This is the "Point of No Return". Once logged, the transaction *must* complete.
    /// 2.  **Send Commit**: `Commit` requests are sent to all participant shards.
    ///     This step includes retry logic with exponential backoff to handle transient failures.
    /// 3.  **Clear Log**: Once all participants acknowledge the commit, the decision is
    ///     removed from the log.
    ///
    /// If the coordinator crashes after step 1 but before step 3, recovery will
    /// replay this method to ensure completion.
    pub fn commit_distributed_transaction(&self, tx_id: TxId) -> Result<(), DistributedTxError> {
        // Get the transaction
        let mut transaction = {
            let mut txns =
                self.active_transactions
                    .write()
                    .map_err(|_| DistributedTxError::Aborted {
                        reason: "Lock poisoned".to_string(),
                    })?;
            txns.remove(&tx_id)
                .ok_or_else(|| DistributedTxError::Aborted {
                    reason: "Transaction not found".to_string(),
                })?
        };

        let commit_timestamp = if let Some(commit_timestamp) = transaction.commit_timestamp {
            Some(commit_timestamp)
        } else {
            match self.next_commit_timestamp() {
                Ok(timestamp) => {
                    transaction.commit_timestamp = Some(timestamp);
                    Some(timestamp)
                }
                Err(error) => {
                    self.reinsert_transaction(tx_id, transaction);
                    return Err(error);
                }
            }
        };

        // CRITICAL: Log the commit decision BEFORE sending commits
        // This ensures we can recover if the coordinator crashes
        {
            let log = match self.commit_log.write() {
                Ok(log) => log,
                Err(_) => {
                    self.reinsert_transaction(tx_id, transaction);
                    return Err(DistributedTxError::Aborted {
                        reason: "Lock poisoned".to_string(),
                    });
                }
            };

            let should_log = match log.get_decision(tx_id) {
                Some(existing) => {
                    // Check if existing decision matches what we want to log
                    // PersistentCommitLog entries are specific types (Commit/Abort)
                    // If we found an entry, check if it's a Commit and has same timestamp
                    use super::persistent_commit_log::EntryType;
                    !transaction.commit_decision_logged
                        || existing.entry_type != EntryType::Commit
                        || existing.commit_timestamp != commit_timestamp
                }
                None => true,
            };

            if should_log {
                match log.log_commit(tx_id, transaction.participant_shards(), commit_timestamp) {
                    Ok(_) => transaction.commit_decision_logged = true,
                    Err(e) => {
                        self.reinsert_transaction(tx_id, transaction);
                        return Err(DistributedTxError::Aborted {
                            reason: format!("Failed to log commit decision: {}", e),
                        });
                    }
                }
            }
        }

        // Begin commit phase
        match transaction.phase {
            TransactionPhase::Committing => {}
            TransactionPhase::Failed | TransactionPhase::Prepared => {
                transaction.phase = TransactionPhase::Committing;
            }
            _ => {
                if let Err(error) = transaction.begin_commit() {
                    self.reinsert_transaction(tx_id, transaction);
                    return Err(error);
                }
            }
        }

        // Send commit to all participants with retry
        let connections = match self.connections.read() {
            Ok(connections) => connections,
            Err(_) => {
                self.reinsert_transaction(tx_id, transaction);
                return Err(DistributedTxError::Aborted {
                    reason: "Lock poisoned".to_string(),
                });
            }
        };

        let mut unavailable_shards = Vec::new();

        for shard_id in transaction.participant_shards() {
            if let Some(conn) = connections.get(&shard_id) {
                // Retry logic for commit with exponential backoff
                let max_retries = 3;
                let mut retry_count = 0;

                loop {
                    match conn.commit(tx_id, commit_timestamp) {
                        Ok(()) => {
                            transaction.record_commit_success(shard_id);
                            break;
                        }
                        Err(_) if retry_count < max_retries => {
                            // Exponential backoff: 100ms, 200ms, 400ms
                            let backoff_ms = 100 * (1 << retry_count);
                            std::thread::sleep(Duration::from_millis(backoff_ms));
                            retry_count += 1;
                            continue;
                        }
                        Err(DistributedTxError::ParticipantUnavailable { .. }) => {
                            transaction.record_unreachable(shard_id);
                            unavailable_shards.push(shard_id);
                            // Participant unavailable during commit is bad, but we must retry later
                            // For now, break and let the transaction remain uncommitted
                            break;
                        }
                        Err(_) => {
                            // Exhausted retries - commit decision is logged, recovery will retry
                            break;
                        }
                    }
                }
            } else {
                transaction.record_unreachable(shard_id);
                unavailable_shards.push(shard_id);
            }
        }

        // Check if all committed
        if !transaction.all_committed() {
            // Some participants didn't acknowledge, but commit decision is logged
            // Recovery process will retry
            let uncommitted = transaction.uncommitted_participants();
            transaction.mark_failed();
            drop(connections); // Prevent deadlock with active_transactions.write()

            for shard_id in unavailable_shards {
                self.mark_shard_unavailable(shard_id);
            }

            // Re-insert for recovery tracking
            if let Ok(mut txns) = self.active_transactions.write() {
                txns.insert(tx_id, transaction);
            }

            return Err(DistributedTxError::CommitFailed {
                tx_id,
                failed_participants: uncommitted,
            });
        }

        drop(connections);

        // All committed successfully - log completion (clears pending state)
        {
            let log = self
                .commit_log
                .read() // log_complete takes &self (interior mutability for writer)
                .map_err(|_| DistributedTxError::Aborted {
                    reason: "Lock poisoned".to_string(),
                })?;
            // Note: log_complete is durable, so we should probably handle errors?
            // If we fail to log complete, the transaction remains "pending" in the log.
            // On recovery, it will be replayed. Since commit is idempotent (if participants handle it),
            // this is safe but wasteful.
            // However, we should try to log it.
            let _ = log.log_complete(tx_id);
        }

        // Mark transaction as committed
        transaction.mark_committed()?;

        // Record metrics
        if let Ok(metrics_map) = self.metrics.read() {
            for shard_id in transaction.participant_shards() {
                if let Some(metrics) = metrics_map.get(&shard_id) {
                    metrics.record_write(true); // Distributed write
                }
            }
        }

        Ok(())
    }

    /// Abort a distributed transaction.
    pub fn abort_distributed_transaction(
        &self,
        tx_id: TxId,
        reason: &str,
    ) -> Result<(), DistributedTxError> {
        // Get the transaction
        let mut transaction = {
            let mut txns =
                self.active_transactions
                    .write()
                    .map_err(|_| DistributedTxError::Aborted {
                        reason: "Lock poisoned".to_string(),
                    })?;
            txns.remove(&tx_id)
                .ok_or_else(|| DistributedTxError::Aborted {
                    reason: "Transaction not found".to_string(),
                })?
        };

        // Log the abort decision
        {
            let log = self
                .commit_log
                .read()
                .map_err(|_| DistributedTxError::Aborted {
                    reason: "Lock poisoned".to_string(),
                })?;
            if let Err(e) = log.log_abort(tx_id, transaction.participant_shards()) {
                // If logging fails, we should still try to abort locally and remotely,
                // but we might want to log this error.
                // Reinserting the transaction isn't strictly necessary since we are aborting,
                // but if we fail to log, recovery might be confused.
                // However, the transaction is already doomed.
                // We'll proceed with abort, but note the failure.
                #[cfg(feature = "observability")]
                tracing::warn!("Failed to log abort decision: {}", e);
                #[cfg(not(feature = "observability"))]
                let _ = e;
            }
        }

        // Send abort to all participants
        let connections = self
            .connections
            .read()
            .map_err(|_| DistributedTxError::Aborted {
                reason: "Lock poisoned".to_string(),
            })?;

        for shard_id in transaction.participant_shards() {
            if let Some(conn) = connections.get(&shard_id) {
                // Best effort abort - don't fail if participant unreachable
                let _ = conn.abort(tx_id);
            }
        }

        drop(connections);
        transaction.abort(reason);

        // Clear the decision log (log completion)
        {
            let log = self
                .commit_log
                .read()
                .map_err(|_| DistributedTxError::Aborted {
                    reason: "Lock poisoned".to_string(),
                })?;
            let _ = log.log_complete(tx_id);
        }

        Ok(())
    }

    /// Get an active transaction by ID.
    pub fn get_transaction(&self, tx_id: TxId) -> Option<DistributedTransaction> {
        self.active_transactions.read().ok()?.get(&tx_id).map(|tx| {
            // Create a copy of the transaction state
            DistributedTransaction {
                tx_id: tx.tx_id,
                phase: tx.phase,
                participants: tx.participants.clone(),
                start_time: tx.start_time,
                timeout: tx.timeout,
                retries_remaining: tx.retries_remaining,
                commit_decision_logged: tx.commit_decision_logged,
                commit_timestamp: tx.commit_timestamp,
            }
        })
    }

    /// Recovery: replay pending commit decisions.
    ///
    /// This should be called on coordinator startup to handle transactions that were
    /// in the middle of the commit phase when the coordinator crashed (or restarted).
    ///
    /// It reads the `PersistentCommitLog` and retries the commit for any pending decisions.
    ///
    /// # Returns
    ///
    /// A `RecoveryResult` containing:
    /// *   `recovered`: List of `TxId`s that were successfully completed.
    /// *   `dead_lettered`: List of transactions that failed after max retries and require manual intervention.
    pub fn recover_pending_transactions(&self) -> RecoveryResult {
        let decisions = {
            let log = self.commit_log.read().expect("Commit log lock poisoned");
            log.pending_commits()
        };

        let mut recovered = Vec::new();
        let mut dead_lettered = Vec::new();
        let max_recovery_attempts = 5;

        for d in decisions {
            let (tx_id, participants, commit_timestamp) =
                (d.tx_id, d.participants, d.commit_timestamp);
            // Create a transaction in committing state for recovery
            let mut tx = DistributedTransaction::new(tx_id, participants, self.transaction_timeout);
            tx.begin_prepare().ok();
            for shard_id in tx.participant_shards() {
                tx.record_prepare_success(shard_id);
            }
            tx.mark_prepared().ok();
            tx.begin_commit().ok();
            tx.commit_timestamp = commit_timestamp;
            tx.commit_decision_logged = true;

            // Insert into active transactions
            if let Ok(mut txns) = self.active_transactions.write() {
                txns.insert(tx_id, tx);
            }

            // Try to complete the commit with exponential backoff
            let mut attempts = 0;
            let mut success = false;

            while attempts < max_recovery_attempts && !success {
                match self.commit_distributed_transaction(tx_id) {
                    Ok(()) => {
                        recovered.push(tx_id);
                        success = true;
                    }
                    Err(e) => {
                        attempts += 1;
                        if attempts < max_recovery_attempts {
                            // Exponential backoff: 1s, 2s, 4s, 8s, 16s
                            let backoff_secs = 1 << attempts;
                            #[cfg(feature = "observability")]
                            tracing::warn!(
                                tx_id = %tx_id,
                                attempt = attempts,
                                backoff_secs = backoff_secs,
                                error = %e,
                                "Recovery attempt failed, retrying"
                            );
                            std::thread::sleep(Duration::from_secs(backoff_secs));
                        } else {
                            // Max attempts exceeded - dead letter this transaction
                            #[cfg(feature = "observability")]
                            tracing::error!(
                                tx_id = %tx_id,
                                max_attempts = max_recovery_attempts,
                                "Transaction exceeded max recovery attempts, moving to dead letter queue"
                            );
                            dead_lettered.push(DeadLetteredTransaction {
                                tx_id,
                                reason: format!("Exceeded max recovery attempts: {}", e),
                                last_attempt: Instant::now(),
                                attempt_count: attempts,
                            });

                            // Remove from active transactions to prevent unbounded growth
                            if let Ok(mut txns) = self.active_transactions.write() {
                                txns.remove(&tx_id);
                            }
                        }
                    }
                }
            }
        }

        // Store dead-lettered transactions
        if let Ok(mut dlq) = self.dead_letter_queue.write() {
            for tx in &dead_lettered {
                dlq.insert(tx.tx_id, tx.clone());
            }
        }

        RecoveryResult {
            recovered,
            dead_lettered,
        }
    }

    /// Get transactions in the dead letter queue.
    ///
    /// These are transactions that failed recovery and require manual intervention.
    pub fn get_dead_lettered_transactions(&self) -> Vec<DeadLetteredTransaction> {
        self.dead_letter_queue
            .read()
            .map(|dlq| dlq.values().cloned().collect())
            .unwrap_or_default()
    }

    /// Manually retry a dead-lettered transaction.
    ///
    /// This removes the transaction from the dead letter queue and attempts
    /// recovery again.
    pub fn retry_dead_lettered_transaction(&self, tx_id: TxId) -> Result<(), DistributedTxError> {
        // Remove from dead letter queue
        let dlq_entry = {
            let mut dlq =
                self.dead_letter_queue
                    .write()
                    .map_err(|_| DistributedTxError::Aborted {
                        reason: "Lock poisoned".to_string(),
                    })?;
            dlq.remove(&tx_id)
        };

        if dlq_entry.is_none() {
            return Err(DistributedTxError::Aborted {
                reason: format!("Transaction {} not found in dead letter queue", tx_id),
            });
        }

        // Re-attempt recovery using single-transaction recovery
        let decision = {
            let log = self
                .commit_log
                .read()
                .map_err(|_| DistributedTxError::Aborted {
                    reason: "Lock poisoned".to_string(),
                })?;
            log.pending_commits().into_iter().find(|d| d.tx_id == tx_id)
        };

        if let Some(d) = decision {
            let (found_tx_id, participants, commit_timestamp) =
                (d.tx_id, d.participants, d.commit_timestamp);
            let mut tx =
                DistributedTransaction::new(found_tx_id, participants, self.transaction_timeout);
            tx.begin_prepare().ok();
            for shard_id in tx.participant_shards() {
                tx.record_prepare_success(shard_id);
            }
            tx.mark_prepared().ok();
            tx.begin_commit().ok();
            tx.commit_timestamp = commit_timestamp;
            tx.commit_decision_logged = true;

            if let Ok(mut txns) = self.active_transactions.write() {
                txns.insert(found_tx_id, tx);
            }

            self.commit_distributed_transaction(found_tx_id)
        } else {
            Err(DistributedTxError::Aborted {
                reason: format!("No commit decision found for transaction {}", tx_id),
            })
        }
    }

    /// Clear all dead-lettered transactions (after manual resolution).
    pub fn clear_dead_letter_queue(&self) {
        if let Ok(mut dlq) = self.dead_letter_queue.write() {
            dlq.clear();
        }
    }

    /// Perform health checks on all shards.
    pub fn health_check_all(&self) {
        if let Ok(mut connections) = self.connections.write() {
            for conn in connections.values_mut() {
                conn.health_check();
            }
        }
    }

    /// Mark a shard as unavailable.
    pub fn mark_shard_unavailable(&self, shard_id: ShardId) {
        if let Ok(mut connections) = self.connections.write()
            && let Some(conn) = connections.get_mut(&shard_id)
        {
            conn.mark_unhealthy();
        }

        if let Ok(mut states) = self.shard_states.write()
            && let Some(state) = states.get_mut(&shard_id)
        {
            state.status = ShardStatus::Unavailable;
        }
    }

    /// Mark a shard as available.
    pub fn mark_shard_available(&self, shard_id: ShardId) {
        if let Ok(mut connections) = self.connections.write()
            && let Some(conn) = connections.get_mut(&shard_id)
        {
            conn.mark_healthy();
        }

        if let Ok(mut states) = self.shard_states.write()
            && let Some(state) = states.get_mut(&shard_id)
        {
            state.status = ShardStatus::Healthy;
        }
    }

    /// Calculate the imbalance ratio across shards.
    ///
    /// Returns the coefficient of variation (std dev / mean) of node counts.
    pub fn calculate_imbalance(&self) -> f64 {
        let states: Vec<u64> = self
            .shard_states
            .read()
            .map(|s| s.values().map(|state| state.node_count).collect())
            .unwrap_or_default();

        if states.is_empty() || states.len() == 1 {
            return 0.0;
        }

        let mean = states.iter().sum::<u64>() as f64 / states.len() as f64;
        if mean == 0.0 {
            return 0.0;
        }

        let variance = states
            .iter()
            .map(|&x| {
                let diff = x as f64 - mean;
                diff * diff
            })
            .sum::<f64>()
            / states.len() as f64;

        variance.sqrt() / mean
    }

    /// Check if rebalancing is needed based on current imbalance.
    pub fn needs_rebalancing(&self) -> bool {
        let imbalance = self.calculate_imbalance();
        self.rebalance_config.should_rebalance(imbalance)
    }

    /// Get the number of active distributed transactions.
    pub fn active_transaction_count(&self) -> usize {
        self.active_transactions
            .read()
            .map(|txns| txns.len())
            .unwrap_or(0)
    }
}

impl std::fmt::Debug for ShardCoordinator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ShardCoordinator")
            .field("num_shards", &self.router.config().num_shards())
            .field("active_transactions", &self.active_transaction_count())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::sharding::config::ShardDefinition;

    fn test_config() -> ShardConfig {
        ShardConfig::new(vec![
            ShardDefinition::new(0, "shard0:9000", vec!["Person"]),
            ShardDefinition::new(1, "shard1:9000", vec!["Place"]),
        ])
    }

    fn run_distributed_tx(
        coordinator: &ShardCoordinator,
        shards: &[ShardId],
    ) -> Result<HybridTimestamp, DistributedTxError> {
        let tx_id = coordinator.begin_distributed_transaction(shards.to_vec())?;
        coordinator.prepare_distributed_transaction(tx_id)?;
        let commit_timestamp = coordinator
            .get_transaction(tx_id)
            .and_then(|tx| tx.commit_timestamp)
            .ok_or_else(|| DistributedTxError::Aborted {
                reason: "Missing commit timestamp after prepare".to_string(),
            })?;
        coordinator.commit_distributed_transaction(tx_id)?;
        Ok(commit_timestamp)
    }

    #[test]
    fn test_coordinator_creation() {
        let config = test_config();
        let coordinator = ShardCoordinator::new(config);

        assert_eq!(coordinator.router().config().num_shards(), 2);
    }

    #[test]
    fn test_coordinator_routing() {
        let coordinator = ShardCoordinator::new(test_config());

        assert_eq!(coordinator.route_node("Person").as_u16(), 0);
        assert_eq!(coordinator.route_node("Place").as_u16(), 1);
    }

    #[test]
    fn test_coordinator_shard_state() {
        let coordinator = ShardCoordinator::new(test_config());
        let shard_id = ShardId::new(0).unwrap();

        let state = coordinator.get_shard_state(shard_id);
        assert!(state.is_some());
        assert_eq!(state.unwrap().status, ShardStatus::Healthy);
    }

    #[test]
    fn test_coordinator_mark_unavailable() {
        let coordinator = ShardCoordinator::new(test_config());
        let shard_id = ShardId::new(0).unwrap();

        coordinator.mark_shard_unavailable(shard_id);

        let state = coordinator.get_shard_state(shard_id);
        assert_eq!(state.unwrap().status, ShardStatus::Unavailable);
    }

    #[test]
    fn test_coordinator_mark_available() {
        let coordinator = ShardCoordinator::new(test_config());
        let shard_id = ShardId::new(0).unwrap();

        coordinator.mark_shard_unavailable(shard_id);
        coordinator.mark_shard_available(shard_id);

        let state = coordinator.get_shard_state(shard_id);
        assert_eq!(state.unwrap().status, ShardStatus::Healthy);
    }

    #[test]
    fn test_coordinator_begin_distributed_transaction() {
        let coordinator = ShardCoordinator::new(test_config());
        let shards = vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()];

        let tx_id = coordinator.begin_distributed_transaction(shards).unwrap();
        assert_eq!(coordinator.active_transaction_count(), 1);

        let tx = coordinator.get_transaction(tx_id);
        assert!(tx.is_some());
        assert_eq!(tx.unwrap().participants.len(), 2);
    }

    #[test]
    fn test_coordinator_prepare_commit_flow() {
        let coordinator = ShardCoordinator::new(test_config());
        let shards = vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()];

        // Begin transaction
        let tx_id = coordinator.begin_distributed_transaction(shards).unwrap();

        // Prepare
        let result = coordinator.prepare_distributed_transaction(tx_id);
        assert!(result.is_ok());

        // Commit
        let result = coordinator.commit_distributed_transaction(tx_id);
        assert!(result.is_ok());
    }

    #[test]
    fn test_coordinator_prepare_sets_commit_timestamp() {
        let coordinator = ShardCoordinator::new(test_config());
        let shards = vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()];

        let tx_id = coordinator.begin_distributed_transaction(shards).unwrap();
        assert!(coordinator.prepare_distributed_transaction(tx_id).is_ok());

        let tx = coordinator.get_transaction(tx_id).unwrap();
        assert!(tx.commit_timestamp.is_some());

        let result = coordinator.commit_distributed_transaction(tx_id);
        assert!(result.is_ok());
    }

    #[test]
    fn test_coordinator_prepare_with_unavailable_shard() {
        let coordinator = ShardCoordinator::new(test_config());
        let shard0 = ShardId::new(0).unwrap();
        let shard1 = ShardId::new(1).unwrap();

        // Mark one shard as unavailable
        coordinator.mark_shard_unavailable(shard1);

        // Begin transaction
        let tx_id = coordinator
            .begin_distributed_transaction(vec![shard0, shard1])
            .unwrap();

        // Prepare should fail
        let result = coordinator.prepare_distributed_transaction(tx_id);
        assert!(result.is_err());
    }

    #[test]
    fn test_coordinator_abort_transaction() {
        let coordinator = ShardCoordinator::new(test_config());
        let shards = vec![ShardId::new(0).unwrap()];

        let tx_id = coordinator.begin_distributed_transaction(shards).unwrap();
        let result = coordinator.abort_distributed_transaction(tx_id, "test abort");
        assert!(result.is_ok());
    }

    #[test]
    fn test_coordinator_calculate_imbalance() {
        let coordinator = ShardCoordinator::new(test_config());

        // Initially all shards are empty, so no imbalance
        assert_eq!(coordinator.calculate_imbalance(), 0.0);

        // Update one shard to have more nodes
        let mut state = coordinator
            .get_shard_state(ShardId::new(0).unwrap())
            .unwrap();
        state.node_count = 1000;
        coordinator.update_shard_state(ShardId::new(0).unwrap(), state);

        // Now there should be imbalance
        let imbalance = coordinator.calculate_imbalance();
        assert!(imbalance > 0.0);
    }

    #[test]
    fn test_coordinator_needs_rebalancing() {
        let coordinator = ShardCoordinator::new(test_config());

        // Initially no rebalancing needed
        assert!(!coordinator.needs_rebalancing());

        // Create significant imbalance
        let mut state0 = coordinator
            .get_shard_state(ShardId::new(0).unwrap())
            .unwrap();
        state0.node_count = 1000;
        coordinator.update_shard_state(ShardId::new(0).unwrap(), state0);

        let mut state1 = coordinator
            .get_shard_state(ShardId::new(1).unwrap())
            .unwrap();
        state1.node_count = 100;
        coordinator.update_shard_state(ShardId::new(1).unwrap(), state1);

        // Now rebalancing should be needed (>30% imbalance)
        assert!(coordinator.needs_rebalancing());
    }

    #[test]
    fn test_shard_connection() {
        let shard_id = ShardId::new(0).unwrap();
        let mut conn = ShardConnection::new(shard_id, "localhost:9000".to_string());

        assert!(conn.healthy);
        assert!(conn.prepare(TxId::new(1), None).is_ok());
        assert!(conn.commit(TxId::new(1), None).is_ok());
        assert!(conn.abort(TxId::new(1)).is_ok());

        conn.mark_unhealthy();
        assert!(!conn.healthy);
        assert!(conn.prepare(TxId::new(2), None).is_err());

        conn.mark_healthy();
        assert!(conn.healthy);
    }

    #[test]
    fn test_coordinator_debug() {
        let coordinator = ShardCoordinator::new(test_config());
        let debug = format!("{:?}", coordinator);
        assert!(debug.contains("ShardCoordinator"));
        assert!(debug.contains("num_shards"));
    }

    #[test]
    fn test_coordinator_get_all_shard_states() {
        let coordinator = ShardCoordinator::new(test_config());
        let states = coordinator.get_all_shard_states();
        assert_eq!(states.len(), 2);
    }

    #[test]
    fn test_coordinator_get_metrics() {
        let coordinator = ShardCoordinator::new(test_config());
        let shard_id = ShardId::new(0).unwrap();

        let metrics = coordinator.get_metrics(shard_id);
        assert!(metrics.is_some());
    }

    // ==================== RecoveryResult Tests ====================

    #[test]
    fn test_recovery_result_is_complete() {
        let result = RecoveryResult {
            recovered: vec![TxId::new(1), TxId::new(2)],
            dead_lettered: vec![],
        };
        assert!(result.is_complete());
        assert_eq!(result.dead_letter_count(), 0);

        let result_with_dead = RecoveryResult {
            recovered: vec![TxId::new(1)],
            dead_lettered: vec![DeadLetteredTransaction {
                tx_id: TxId::new(2),
                reason: "Test failure".to_string(),
                last_attempt: Instant::now(),
                attempt_count: 3,
            }],
        };
        assert!(!result_with_dead.is_complete());
        assert_eq!(result_with_dead.dead_letter_count(), 1);
    }

    // ==================== ShardConnection Tests ====================

    #[test]
    fn test_shard_connection_health_check() {
        let shard_id = ShardId::new(0).unwrap();
        let mut conn = ShardConnection::new(shard_id, "localhost:9000".to_string());

        assert!(conn.last_ping.is_none());

        let result = conn.health_check();
        assert!(result);
        assert!(conn.last_ping.is_some());
    }

    #[test]
    fn test_shard_connection_unhealthy_operations() {
        let shard_id = ShardId::new(0).unwrap();
        let mut conn = ShardConnection::new(shard_id, "localhost:9000".to_string());

        conn.mark_unhealthy();

        // All operations should fail when unhealthy
        assert!(conn.prepare(TxId::new(1), None).is_err());
        assert!(conn.commit(TxId::new(1), None).is_err());
        assert!(conn.abort(TxId::new(1)).is_err());
    }

    // ==================== Extended Coordinator Tests ====================

    #[test]
    fn test_coordinator_with_rebalance_config() {
        let config = test_config();
        let rebalance_config = RebalanceConfig {
            imbalance_threshold: 0.5,
            batch_size: 500,
            max_concurrent_migrations: 2,
            ..Default::default()
        };

        let coordinator = ShardCoordinator::new(config).with_rebalance_config(rebalance_config);
        assert_eq!(coordinator.router().config().num_shards(), 2);
    }

    #[test]
    fn test_coordinator_route_traversal() {
        let coordinator = ShardCoordinator::new(test_config());

        let plan = coordinator.route_traversal("Person", &["Place"]);
        assert!(!plan.involved_shards.is_empty());
    }

    #[test]
    fn test_coordinator_active_transaction_count() {
        let coordinator = ShardCoordinator::new(test_config());

        assert_eq!(coordinator.active_transaction_count(), 0);

        let shards = vec![ShardId::new(0).unwrap()];
        coordinator.begin_distributed_transaction(shards).unwrap();

        assert_eq!(coordinator.active_transaction_count(), 1);
    }

    #[test]
    fn test_coordinator_get_nonexistent_transaction() {
        let coordinator = ShardCoordinator::new(test_config());

        let tx = coordinator.get_transaction(TxId::new(99999));
        assert!(tx.is_none());
    }

    #[test]
    fn test_coordinator_prepare_nonexistent_transaction() {
        let coordinator = ShardCoordinator::new(test_config());

        let result = coordinator.prepare_distributed_transaction(TxId::new(99999));
        assert!(result.is_err());
    }

    #[test]
    fn test_coordinator_commit_nonexistent_transaction() {
        let coordinator = ShardCoordinator::new(test_config());

        let result = coordinator.commit_distributed_transaction(TxId::new(99999));
        assert!(result.is_err());
    }

    #[test]
    fn test_coordinator_abort_nonexistent_transaction() {
        let coordinator = ShardCoordinator::new(test_config());

        let result = coordinator.abort_distributed_transaction(TxId::new(99999), "test");
        assert!(result.is_err());
    }

    #[test]
    fn test_coordinator_get_shard_state_nonexistent() {
        let coordinator = ShardCoordinator::new(test_config());

        let state = coordinator.get_shard_state(ShardId::new(99).unwrap());
        assert!(state.is_none());
    }

    #[test]
    fn test_coordinator_get_metrics_nonexistent() {
        let coordinator = ShardCoordinator::new(test_config());

        let metrics = coordinator.get_metrics(ShardId::new(99).unwrap());
        assert!(metrics.is_none());
    }

    #[test]
    fn test_coordinator_dead_letter_queue() {
        let coordinator = ShardCoordinator::new(test_config());

        // Initially empty
        let dead = coordinator.get_dead_lettered_transactions();
        assert!(dead.is_empty());
    }

    #[test]
    fn test_coordinator_retry_existing_dead_letter() {
        let coordinator = ShardCoordinator::new(test_config());
        let tx_id = TxId::new(42);

        // Add to commit log
        {
            let log = coordinator.commit_log.read().unwrap();
            let _ = log.log_commit(tx_id, vec![ShardId::new(1).unwrap()], None);
        }

        // Add to dead letter queue
        {
            let mut dlq = coordinator.dead_letter_queue.write().unwrap();
            dlq.insert(
                tx_id,
                DeadLetteredTransaction {
                    tx_id,
                    reason: "Test".to_string(),
                    last_attempt: std::time::Instant::now(),
                    attempt_count: 1,
                },
            );
        }

        let result = coordinator.retry_dead_lettered_transaction(tx_id);
        // It succeeds in preparing and marking as committing in this mocked case
        assert!(result.is_ok());
    }

    #[test]
    fn test_coordinator_retry_nonexistent_dead_letter() {
        let coordinator = ShardCoordinator::new(test_config());

        let result = coordinator.retry_dead_lettered_transaction(TxId::new(99999));
        assert!(result.is_err());
    }

    #[test]
    fn test_dead_lettered_transaction_debug() {
        let tx = DeadLetteredTransaction {
            tx_id: TxId::new(1),
            reason: "Test failure".to_string(),
            last_attempt: Instant::now(),
            attempt_count: 3,
        };

        let debug = format!("{:?}", tx);
        assert!(debug.contains("tx_id"));
        assert!(debug.contains("reason"));
        assert!(debug.contains("attempt_count"));
    }

    #[test]
    fn test_recovery_result_debug() {
        let result = RecoveryResult {
            recovered: vec![TxId::new(1)],
            dead_lettered: vec![],
        };

        let debug = format!("{:?}", result);
        assert!(debug.contains("recovered"));
        assert!(debug.contains("dead_lettered"));
    }

    #[test]
    fn test_next_commit_timestamp_allows_idle_forward_drift() {
        let coordinator = ShardCoordinator::new(test_config());
        let idle_gap_us = MAX_FORWARD_JUMP_US + 2_000_000;
        let old_wallclock = time::now().wallclock() - idle_gap_us;

        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(old_wallclock, 0).unwrap();
        }

        let now = Instant::now();
        {
            let mut observed_at = coordinator
                .commit_clock_observed_at
                .lock()
                .expect("commit_clock_observed_at lock should be available");
            *observed_at = now;
        }

        let result = coordinator
            .next_commit_timestamp_internal(now + Duration::from_micros(idle_gap_us as u64));
        assert!(
            result.is_ok(),
            "normal idle time should not be treated as forward clock skew"
        );
    }

    #[test]
    fn test_prepare_reinserts_transaction_on_timestamp_failure() {
        let coordinator = ShardCoordinator::new(test_config());

        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(
                crate::core::temporal::MAX_VALID_TIMESTAMP,
                u32::MAX,
            )
            .unwrap();
        }

        let tx_id = coordinator
            .begin_distributed_transaction(vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()])
            .unwrap();

        let result = coordinator.prepare_distributed_transaction(tx_id);
        assert!(result.is_err());

        let transaction = coordinator
            .get_transaction(tx_id)
            .expect("transaction should be reinserted after prepare timestamp failure");
        assert_eq!(transaction.phase, TransactionPhase::Pending);
        assert!(transaction.commit_timestamp.is_none());
    }

    #[test]
    fn test_commit_reinserts_transaction_on_timestamp_failure() {
        let coordinator = ShardCoordinator::new(test_config());

        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(
                crate::core::temporal::MAX_VALID_TIMESTAMP,
                u32::MAX,
            )
            .unwrap();
        }

        let tx_id = coordinator
            .begin_distributed_transaction(vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()])
            .unwrap();

        let result = coordinator.commit_distributed_transaction(tx_id);
        assert!(result.is_err());

        let transaction = coordinator
            .get_transaction(tx_id)
            .expect("transaction should be reinserted after commit timestamp failure");
        assert_eq!(transaction.phase, TransactionPhase::Pending);
        assert!(transaction.commit_timestamp.is_none());
    }

    #[test]
    fn test_next_commit_timestamp_backward_skew() {
        let coordinator = ShardCoordinator::new(test_config());
        let now = time::now().wallclock();
        let self_heal = is_clock_skew_self_heal_enabled();
        let skewed_frontier = now + (MAX_BACKWARD_DRIFT_US * 2);

        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(skewed_frontier, 0).unwrap();
        }

        let result = coordinator.next_commit_timestamp();

        if self_heal {
            assert!(result.is_ok());
            let committed = result.unwrap();
            assert_eq!(committed.wallclock(), skewed_frontier);
            assert_eq!(committed.logical(), 1);
        } else {
            let error =
                result.expect_err("expected backward skew to abort when self-heal is disabled");
            let reason = match error {
                DistributedTxError::Aborted { reason } => reason,
                _ => panic!("unexpected error variant: {error:?}"),
            };
            assert!(reason.contains("backward"));
        }
    }

    #[test]
    fn test_next_commit_timestamp_forward_skew() {
        let coordinator = ShardCoordinator::new(test_config());
        let now = time::now().wallclock();
        let self_heal = is_clock_skew_self_heal_enabled();
        let skewed_frontier = now - (MAX_FORWARD_JUMP_US * 2);

        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(skewed_frontier, 0).unwrap();
        }

        let result = coordinator.next_commit_timestamp();

        if self_heal {
            assert!(result.is_ok());
            let committed = result.unwrap();
            assert_eq!(committed.wallclock(), skewed_frontier);
            assert_eq!(committed.logical(), 1);
        } else {
            let error =
                result.expect_err("expected forward skew to abort when self-heal is disabled");
            let reason = match error {
                DistributedTxError::Aborted { reason } => reason,
                _ => panic!("unexpected error variant: {error:?}"),
            };
            assert!(reason.contains("forward"));
        }
    }

    #[test]
    fn test_prepare_with_backward_skew_distributed_tx() {
        let coordinator = ShardCoordinator::new(test_config());
        let now = time::now().wallclock();
        let skewed_frontier = now + (MAX_BACKWARD_DRIFT_US * 2);

        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(skewed_frontier, 0).unwrap();
        }

        let tx_id = coordinator
            .begin_distributed_transaction(vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()])
            .unwrap();

        let self_heal = is_clock_skew_self_heal_enabled();
        let result = coordinator.prepare_distributed_transaction(tx_id);

        if self_heal {
            assert!(result.is_ok());
            let tx = coordinator.get_transaction(tx_id).unwrap();
            assert!(tx.commit_timestamp.is_some());
            assert!(coordinator.commit_distributed_transaction(tx_id).is_ok());
        } else {
            assert!(result.is_err());
            let error =
                result.expect_err("expected backward skew to abort when self-heal is disabled");
            let reason = match error {
                DistributedTxError::Aborted { reason } => reason,
                _ => panic!("unexpected error variant: {error:?}"),
            };
            assert!(reason.contains("backward"));
        }
    }

    #[test]
    fn test_prepare_with_forward_skew_distributed_tx() {
        let coordinator = ShardCoordinator::new(test_config());
        let now = time::now().wallclock();
        let skewed_frontier = now - (MAX_FORWARD_JUMP_US * 2);

        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(skewed_frontier, 0).unwrap();
        }

        let tx_id = coordinator
            .begin_distributed_transaction(vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()])
            .unwrap();

        let self_heal = is_clock_skew_self_heal_enabled();
        let result = coordinator.prepare_distributed_transaction(tx_id);

        if self_heal {
            assert!(result.is_ok());
            let tx = coordinator.get_transaction(tx_id).unwrap();
            assert!(tx.commit_timestamp.is_some());
            assert!(coordinator.commit_distributed_transaction(tx_id).is_ok());
        } else {
            assert!(result.is_err());
            let error =
                result.expect_err("expected forward skew to abort when self-heal is disabled");
            let reason = match error {
                DistributedTxError::Aborted { reason } => reason,
                _ => panic!("unexpected error variant: {error:?}"),
            };
            assert!(reason.contains("forward"));
        }
    }

    #[test]
    fn test_repeated_backward_skew_prepare_commit_flow() {
        let coordinator = ShardCoordinator::new(test_config());
        let shards = vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()];
        let self_heal = is_clock_skew_self_heal_enabled();

        // First prepare/commit starts with a heavily backward-skewed frontier.
        let first_frontier = time::now().wallclock() + (MAX_BACKWARD_DRIFT_US * 2);
        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(first_frontier, 0).unwrap();
        }

        let first = run_distributed_tx(&coordinator, &shards);

        if !self_heal {
            assert!(first.is_err());
            let error = first.expect_err("expected backward skew abort when self-heal is disabled");
            let reason = match error {
                DistributedTxError::Aborted { reason } => reason,
                _ => panic!("unexpected error variant: {error:?}"),
            };
            assert!(reason.contains("backward"));
            return;
        }

        let first = first.unwrap();

        // Second prepare/commit hits backward skew again with a slightly newer frontier.
        let second_frontier = first.wallclock() + 1;
        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(second_frontier, 0).unwrap();
        }

        let second = run_distributed_tx(&coordinator, &shards).unwrap();
        assert!(second > first);
    }

    #[test]
    fn test_repeated_forward_skew_prepare_commit_flow() {
        let coordinator = ShardCoordinator::new(test_config());
        let shards = vec![ShardId::new(0).unwrap(), ShardId::new(1).unwrap()];
        let self_heal = is_clock_skew_self_heal_enabled();

        // First prepare/commit with an aggressively forward-skewed frontier.
        let first_frontier = time::now().wallclock() - (MAX_FORWARD_JUMP_US * 2);
        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(first_frontier, 0).unwrap();
        }

        let first = run_distributed_tx(&coordinator, &shards);
        if !self_heal {
            assert!(first.is_err());
            let error =
                first.expect_err("expected forward skew to abort when self-heal is disabled");
            let reason = match error {
                DistributedTxError::Aborted { reason } => reason,
                _ => panic!("unexpected error variant: {error:?}"),
            };
            assert!(reason.contains("forward"));
            return;
        }

        let first = first.unwrap();

        // Second prepare/commit again under forward-skew pressure.
        let second_frontier = first.wallclock() + (MAX_FORWARD_JUMP_US * 2);
        {
            let mut frontier = coordinator
                .commit_clock
                .lock()
                .expect("commit_clock lock should be available");
            *frontier = crate::core::hlc::HybridTimestamp::new(second_frontier, 0).unwrap();
        }

        let second = run_distributed_tx(&coordinator, &shards).unwrap();
        assert!(second > first);
    }

    #[test]
    fn test_havoc_deadlock() {
        use std::sync::{Arc, RwLock};
        use std::thread;
        use std::time::Duration;

        let active_transactions = Arc::new(RwLock::new(()));
        let connections = Arc::new(RwLock::new(()));

        let tx1 = active_transactions.clone();
        let conn1 = connections.clone();
        let t1 = thread::spawn(move || {
            let _c = conn1.read().unwrap();
            thread::sleep(Duration::from_millis(50));
            // This simulates the missing drop(connections) before active_transactions.write()
            let _t = tx1.write().unwrap();
        });

        let tx2 = active_transactions.clone();
        let conn2 = connections.clone();
        let t2 = thread::spawn(move || {
            let _t = tx2.write().unwrap();
            thread::sleep(Duration::from_millis(50));
            // This simulates an operation that holds active_transactions and requests connections
            let _c = conn2.read().unwrap();
        });

        // Use a timeout to detect deadlock
        let (tx, rx) = std::sync::mpsc::channel();
        thread::spawn(move || {
            t1.join().unwrap();
            t2.join().unwrap();
            tx.send(()).unwrap();
        });

        assert!(
            rx.recv_timeout(Duration::from_secs(2)).is_ok(),
            "Deadlock detected!"
        );
    }
}