asupersync 0.3.6

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
//! Shutdown coordination for HTTP server lifecycle.
//!
//! This module provides [`ShutdownSignal`] for coordinating graceful server shutdown
//! with drain timeouts and phase tracking. It builds on the lower-level
//! [`ShutdownController`] by adding drain-phase
//! awareness and timeout semantics.

use crate::cancel::{DrainPhase, ProgressCertificate};
use crate::cx::Cx;
use crate::signal::{ShutdownController, ShutdownReceiver};
use crate::sync::Notify;
use crate::time::{Sleep, TimerDriverHandle, sleep_until, wall_now};
use crate::types::Time;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering};
use std::time::Duration;

#[cfg(test)]
thread_local! {
    static TRIGGER_IMMEDIATE_PRE_PHASE_HOOK:
        std::cell::RefCell<Option<Box<dyn FnMut()>>> = const { std::cell::RefCell::new(None) };
}

#[cfg(test)]
fn run_trigger_immediate_pre_phase_hook() {
    TRIGGER_IMMEDIATE_PRE_PHASE_HOOK.with(|hook| {
        if let Some(mut hook) = hook.borrow_mut().take() {
            hook();
        }
    });
}

#[cfg(not(test))]
fn run_trigger_immediate_pre_phase_hook() {}

#[derive(Clone)]
enum ShutdownTimeSource {
    WallClock,
    TimerDriver(TimerDriverHandle),
    Custom(fn() -> Time),
}

impl ShutdownTimeSource {
    fn capture_from_current() -> Self {
        Cx::current()
            .and_then(|cx| cx.timer_driver())
            .map_or(Self::WallClock, Self::TimerDriver)
    }

    fn now(&self) -> Time {
        match self {
            Self::WallClock => wall_now(),
            Self::TimerDriver(driver) => driver.now(),
            Self::Custom(time_getter) => time_getter(),
        }
    }
}

/// Phases of a graceful server shutdown.
///
/// Shutdown proceeds through these phases in order:
/// 1. [`Running`](ShutdownPhase::Running) — normal operation
/// 2. [`Draining`](ShutdownPhase::Draining) — stopped accepting, waiting for in-flight
/// 3. [`ForceClosing`](ShutdownPhase::ForceClosing) — drain timeout exceeded, force-closing
/// 4. [`Stopped`](ShutdownPhase::Stopped) — all connections closed
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ShutdownPhase {
    /// Normal operation — accepting connections and processing requests.
    Running = 0,
    /// Stopped accepting new connections; waiting for in-flight requests to complete.
    Draining = 1,
    /// Drain timeout exceeded; force-closing remaining connections.
    ForceClosing = 2,
    /// All connections closed; server fully stopped.
    Stopped = 3,
}

impl ShutdownPhase {
    fn from_u8(v: u8) -> Self {
        match v {
            0 => Self::Running,
            1 => Self::Draining,
            2 => Self::ForceClosing,
            _ => Self::Stopped,
        }
    }
}

impl std::fmt::Display for ShutdownPhase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Running => write!(f, "Running"),
            Self::Draining => write!(f, "Draining"),
            Self::ForceClosing => write!(f, "ForceClosing"),
            Self::Stopped => write!(f, "Stopped"),
        }
    }
}

/// Statistics collected during shutdown.
#[derive(Debug, Clone)]
pub struct ShutdownStats {
    /// Number of connections that completed gracefully during drain.
    pub drained: usize,
    /// Number of connections force-closed after the drain timeout.
    pub force_closed: usize,
    /// Total shutdown duration.
    pub duration: Duration,
    /// Request-aware drain report when the shutdown was supervised by a
    /// [`GracefulDrainSupervisor`]
    /// (br-asupersync-server-stack-hardening-eeexl1.2, D2.2b).
    ///
    /// `None` for connection-level-only drains (no request supervision was
    /// attached). The report carries the in-flight request trajectory and the
    /// [`ProgressCertificate`](crate::cancel::progress_certificate::ProgressCertificate)
    /// verdict for the drain.
    pub drain_report: Option<GracefulDrainReport>,
}

// ============================================================================
// Request-aware drain certificate (br-asupersync-server-stack-hardening-eeexl1.2, D2.1)
// ============================================================================

/// Structured outcome of a request-aware graceful drain.
///
/// Unlike [`ShutdownStats`], which counts *connections*, this report tracks
/// in-flight *requests* (region children — see `RegionRecord::child_count`)
/// and carries the [`ProgressCertificate`] verdict that turns "is the drain
/// converging?" into a measurable claim. It is the operator-visible surface
/// for the drain: a structured log line plus a programmatic value that tests
/// and dashboards can assert against.
///
/// The [`Display`](std::fmt::Display) rendering is deterministic (fixed float
/// precision, duration in whole milliseconds) so it can be golden-tested.
#[derive(Debug, Clone, PartialEq)]
pub struct GracefulDrainReport {
    /// In-flight request count observed when the drain began.
    pub requests_at_drain_start: usize,
    /// Requests that completed during the drain window
    /// (`requests_at_drain_start` minus those still live at the end).
    pub requests_completed: usize,
    /// Requests still in flight when the drain ended (force-cancelled at the
    /// hard deadline). Zero on a clean drain to quiescence.
    pub requests_stranded: usize,
    /// In-flight request count at the moment the soft budget elapsed and the
    /// drain escalated stragglers, or `None` if escalation never fired.
    ///
    /// `requests_completed` counts the in-flight counter reaching zero and
    /// cannot distinguish naturally-completed requests from ones interrupted
    /// by post-escalation force-close; this field preserves that boundary
    /// (br-asupersync-server-stack-hardening-eeexl1.2, D2.4).
    pub requests_at_escalation: Option<usize>,
    /// Number of in-flight-count observations fed to the certificate.
    pub observations: usize,
    /// Final drain-phase classification from the progress certificate.
    pub final_phase: DrainPhase,
    /// Whether the certificate judged the drain to be converging.
    pub converging: bool,
    /// Lower bound on P(quiescence within the estimated remaining steps).
    pub confidence_bound: f64,
    /// Estimated remaining steps to quiescence (`None` if undetermined).
    pub estimated_remaining_steps: Option<f64>,
    /// Whether the certificate detected a stall (no progress) during drain.
    pub stall_detected: bool,
    /// Whether the drain reached quiescence (zero in-flight requests).
    pub reached_quiescence: bool,
    /// Whether the drain ended by hitting the hard deadline rather than
    /// reaching quiescence.
    pub hard_deadline_hit: bool,
    /// Wall/virtual time elapsed from drain start to drain end.
    pub drain_duration: Duration,
}

impl std::fmt::Display for GracefulDrainReport {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Graceful Drain Report")?;
        writeln!(f, "=====================")?;
        writeln!(f, "Requests at start:  {}", self.requests_at_drain_start)?;
        writeln!(f, "Requests completed: {}", self.requests_completed)?;
        writeln!(f, "Requests stranded:  {}", self.requests_stranded)?;
        match self.requests_at_escalation {
            Some(count) => writeln!(f, "At escalation:      {count}")?,
            None => writeln!(f, "At escalation:      never")?,
        }
        writeln!(f, "Observations:       {}", self.observations)?;
        writeln!(f, "Final drain phase:  {}", self.final_phase)?;
        writeln!(f, "Converging:         {}", self.converging)?;
        writeln!(f, "Confidence bound:   {:.6}", self.confidence_bound)?;
        match self.estimated_remaining_steps {
            Some(est) => writeln!(f, "Est. remaining:     {est:.1} steps")?,
            None => writeln!(f, "Est. remaining:     N/A")?,
        }
        writeln!(f, "Stall detected:     {}", self.stall_detected)?;
        writeln!(f, "Reached quiescence: {}", self.reached_quiescence)?;
        writeln!(f, "Hard deadline hit:  {}", self.hard_deadline_hit)?;
        writeln!(
            f,
            "Drain duration:     {}ms",
            self.drain_duration.as_millis()
        )?;
        Ok(())
    }
}

/// Drives a [`ProgressCertificate`] over the live in-flight request count for
/// the duration of a graceful drain, then produces a [`GracefulDrainReport`].
///
/// The certificate models the remaining in-flight request count as a Lyapunov
/// potential that should descend to zero (quiescence). Feed it one observation
/// per drain tick via [`observe`](Self::observe); the resulting
/// [`drain_phase`](Self::phase) distinguishes a normal `slow_tail` from a true
/// `stalled` drain, which is exactly the signal an operator needs to decide
/// whether to keep waiting or escalate to the hard deadline.
///
/// This type is intentionally transport-agnostic: the HTTP/server layers
/// (D2.2+) supply the in-flight count (e.g. `RegionRecord::child_count`) and
/// the clock; the certificate math lives entirely in
/// [`ProgressCertificate`](crate::cancel::ProgressCertificate).
#[derive(Debug)]
pub struct GracefulDrainTracker {
    certificate: ProgressCertificate,
    requests_at_drain_start: usize,
    last_remaining: usize,
    requests_at_escalation: Option<usize>,
    observations: usize,
    start_time: Time,
}

impl GracefulDrainTracker {
    /// Begin tracking a drain that starts with `initial_in_flight` requests
    /// at logical time `now`.
    ///
    /// The initial count is recorded as the certificate's first observation so
    /// the report's `requests_at_drain_start` and the certificate's initial
    /// potential agree.
    #[must_use]
    pub fn new(initial_in_flight: usize, now: Time) -> Self {
        let mut certificate = ProgressCertificate::with_defaults();
        certificate.observe(usize_to_potential(initial_in_flight));
        Self {
            certificate,
            requests_at_drain_start: initial_in_flight,
            last_remaining: initial_in_flight,
            requests_at_escalation: None,
            observations: 1,
            start_time: now,
        }
    }

    /// Record that the drain escalated stragglers with `remaining_in_flight`
    /// requests still live.
    ///
    /// Call at most once, at the soft-deadline escalation transition; the
    /// first recorded value wins. The report's `requests_at_escalation`
    /// preserves the completed-naturally vs interrupted-by-escalation
    /// boundary that the final counter value alone cannot express.
    pub fn record_escalation(&mut self, remaining_in_flight: usize) {
        if self.requests_at_escalation.is_none() {
            self.requests_at_escalation = Some(remaining_in_flight);
        }
    }

    /// Record the current in-flight request count.
    ///
    /// Call once per drain tick. Monotonic descent is expected but not
    /// required; the certificate clamps and tracks non-decreasing runs as
    /// stalls.
    pub fn observe(&mut self, remaining_in_flight: usize) {
        self.certificate
            .observe(usize_to_potential(remaining_in_flight));
        self.last_remaining = remaining_in_flight;
        self.observations += 1;
    }

    /// The certificate's current drain-phase classification.
    #[must_use]
    pub fn phase(&self) -> DrainPhase {
        self.certificate.drain_phase()
    }

    /// The most recently observed in-flight request count.
    #[must_use]
    pub fn remaining(&self) -> usize {
        self.last_remaining
    }

    /// Finish the drain at logical time `now` and produce the report.
    ///
    /// `hard_deadline_hit` records whether the drain ended because the hard
    /// deadline elapsed (rather than reaching quiescence). Any requests still
    /// in flight at that point are reported as `requests_stranded`.
    #[must_use]
    pub fn finish(&self, now: Time, hard_deadline_hit: bool) -> GracefulDrainReport {
        let verdict = self.certificate.verdict();
        let stranded = self.last_remaining;
        let completed = self.requests_at_drain_start.saturating_sub(stranded);
        let drain_duration = if now > self.start_time {
            Duration::from_nanos(now.duration_since(self.start_time))
        } else {
            Duration::ZERO
        };
        GracefulDrainReport {
            requests_at_drain_start: self.requests_at_drain_start,
            requests_completed: completed,
            requests_stranded: stranded,
            requests_at_escalation: self.requests_at_escalation,
            observations: self.observations,
            final_phase: verdict.drain_phase,
            converging: verdict.converging,
            confidence_bound: verdict.confidence_bound,
            estimated_remaining_steps: verdict.estimated_remaining_steps,
            stall_detected: verdict.stall_detected,
            reached_quiescence: stranded == 0,
            hard_deadline_hit,
            drain_duration,
        }
    }
}

/// Maps an in-flight request count to a certificate potential.
///
/// `usize` request counts are far below `f64`'s exact-integer range
/// (`2^53`), so the conversion is lossless for any realistic in-flight count.
#[inline]
#[allow(clippy::cast_precision_loss)]
fn usize_to_potential(count: usize) -> f64 {
    // Precision-safe for counts up to 2^53; above that, saturate rather than
    // silently aliasing (a count near 2^53 is itself a runtime pathology).
    const MAX_EXACT_F64: f64 = 9_007_199_254_740_992.0;
    let count_as_f64 = count as f64;
    count_as_f64.min(MAX_EXACT_F64)
}

/// The action a graceful-drain driver should take after one in-flight-count
/// observation.
///
/// This is the decision output of [`GracefulDrainSupervisor::observe`]. The
/// driver (the HTTP accept/shutdown supervisor, wired in a later slice) maps
/// each variant to a concrete action; the supervisor itself performs no I/O.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DrainStep {
    /// Requests are still draining within the soft budget; keep waiting.
    Continue,
    /// The soft drain budget elapsed with requests still in flight: the driver
    /// should escalate the stragglers through the cancellation protocol. Emitted
    /// at most once (the transition into the escalation window).
    Escalate,
    /// All in-flight requests completed; the drain reached quiescence and the
    /// driver should finish and emit the report.
    Quiescent,
    /// The hard deadline elapsed with requests still in flight: the driver
    /// should force-close the stragglers and emit the report.
    HardDeadline,
}

/// Decision state machine for a request-aware graceful drain.
///
/// Wraps a [`GracefulDrainTracker`] (which carries the progress certificate)
/// and adds the soft-budget / hard-deadline sequencing the operator-facing
/// drain needs: feed it the current in-flight request count once per tick via
/// [`observe`](Self::observe) and it returns the [`DrainStep`] the driver
/// should act on. It is deliberately synchronous and I/O-free so the drain
/// policy can be unit-tested deterministically; the async driving loop, the
/// in-flight counter, and the actual straggler cancellation are wired by the
/// HTTP-layer integration slice.
///
/// Two budgets, both measured from drain start:
/// * `drain_budget` — soft: in-flight requests are allowed to finish on their
///   own until it elapses, after which the driver escalates ([`DrainStep::Escalate`]).
/// * `hard_budget` — hard: after it elapses the driver force-closes any
///   stragglers ([`DrainStep::HardDeadline`]). Must be `>= drain_budget`.
#[derive(Debug)]
pub struct GracefulDrainSupervisor {
    tracker: GracefulDrainTracker,
    drain_deadline: Time,
    hard_deadline: Time,
    escalated: bool,
}

impl GracefulDrainSupervisor {
    /// Begin supervising a drain that starts at `now` with
    /// `initial_in_flight` requests.
    ///
    /// `drain_budget` is the soft window; `hard_budget` is the hard window.
    /// `hard_budget` is clamped up to at least `drain_budget` so the hard
    /// deadline can never precede the soft one.
    #[must_use]
    pub fn new(
        initial_in_flight: usize,
        now: Time,
        drain_budget: Duration,
        hard_budget: Duration,
    ) -> Self {
        let effective_hard = hard_budget.max(drain_budget);
        let to_nanos = |d: Duration| -> u64 { d.as_nanos().min(u128::from(u64::MAX)) as u64 };
        Self {
            tracker: GracefulDrainTracker::new(initial_in_flight, now),
            drain_deadline: now.saturating_add_nanos(to_nanos(drain_budget)),
            hard_deadline: now.saturating_add_nanos(to_nanos(effective_hard)),
            escalated: false,
        }
    }

    /// Record the current in-flight request count at logical time `now` and
    /// return the action the driver should take.
    ///
    /// Quiescence (zero in-flight) wins over every deadline; the hard deadline
    /// wins over the returned action for the soft escalation window. The
    /// escalation boundary is still recorded before a hard-deadline return so
    /// the final report preserves the last pre-cancellation in-flight count.
    /// [`DrainStep::Escalate`] is emitted at most once — on the first
    /// observation at or after the soft deadline while requests remain and
    /// before the hard deadline — so the driver escalates exactly once.
    pub fn observe(&mut self, remaining_in_flight: usize, now: Time) -> DrainStep {
        self.tracker.observe(remaining_in_flight);
        if remaining_in_flight == 0 {
            return DrainStep::Quiescent;
        }
        if now >= self.drain_deadline && !self.escalated {
            self.escalated = true;
            self.tracker.record_escalation(remaining_in_flight);
            if now < self.hard_deadline {
                return DrainStep::Escalate;
            }
        }
        if now >= self.hard_deadline {
            return DrainStep::HardDeadline;
        }
        DrainStep::Continue
    }

    /// Record that another driver path already escalated to force-close at or
    /// after the soft deadline.
    ///
    /// The HTTP listener runs the request-aware supervisor concurrently with
    /// the connection-manager drain. The connection manager may win the race at
    /// the soft deadline, force-close handlers, and drop the in-flight counter
    /// to zero before the supervisor's next tick. In that case the supervisor
    /// must preserve the last observed non-zero request count as the
    /// escalation boundary even though its next observed action is
    /// [`DrainStep::Quiescent`].
    #[must_use]
    pub fn record_external_escalation(&mut self) -> bool {
        if self.escalated {
            return false;
        }
        let remaining = self.tracker.remaining();
        if remaining == 0 {
            return false;
        }
        self.escalated = true;
        self.tracker.record_escalation(remaining);
        true
    }

    /// The current drain-phase classification from the progress certificate.
    #[must_use]
    pub fn phase(&self) -> DrainPhase {
        self.tracker.phase()
    }

    /// The most recently observed in-flight request count.
    #[must_use]
    pub fn remaining(&self) -> usize {
        self.tracker.remaining()
    }

    /// The soft drain deadline (escalation point).
    #[must_use]
    pub fn drain_deadline(&self) -> Time {
        self.drain_deadline
    }

    /// The hard drain deadline (force-close point).
    #[must_use]
    pub fn hard_deadline(&self) -> Time {
        self.hard_deadline
    }

    /// Whether the soft budget has elapsed and escalation has been signalled.
    #[must_use]
    pub fn escalated(&self) -> bool {
        self.escalated
    }

    /// Produce the final report at logical time `now`.
    ///
    /// `hard_deadline_hit` should reflect whether the drain ended on the hard
    /// deadline (the driver typically passes `true` when it last saw
    /// [`DrainStep::HardDeadline`]). Any still-in-flight requests are reported
    /// as stranded.
    #[must_use]
    pub fn finish(&self, now: Time, hard_deadline_hit: bool) -> GracefulDrainReport {
        self.tracker.finish(now, hard_deadline_hit)
    }
}

/// Internal state shared between the signal and its subscribers.
struct SignalState {
    phase: AtomicU8,
    controller: ShutdownController,
    phase_notify: Notify,
    force_close_notify: Notify,
    stopped_notify: Notify,
    time_source: ShutdownTimeSource,
    has_drain_deadline: AtomicBool,
    drain_deadline: AtomicU64,
    has_drain_start: AtomicBool,
    drain_start: AtomicU64,
}

/// Broadcast signal for server shutdown coordination.
///
/// `ShutdownSignal` wraps the lower-level [`ShutdownController`] with
/// shutdown-phase tracking and drain timeout awareness. Handlers can check
/// whether the server is draining to add `Connection: close` headers or
/// reject new work.
///
/// # Example
///
/// ```ignore
/// use asupersync::server::ShutdownSignal;
/// use std::time::Duration;
///
/// let signal = ShutdownSignal::new();
///
/// // In the accept loop:
/// if signal.is_draining() {
///     break; // stop accepting
/// }
///
/// // Initiate shutdown with a 30-second drain period:
/// signal.begin_drain(Duration::from_secs(30));
/// ```
#[derive(Clone)]
pub struct ShutdownSignal {
    state: Arc<SignalState>,
}

impl ShutdownSignal {
    fn duration_to_nanos(duration: Duration) -> u64 {
        duration.as_nanos().min(u128::from(u64::MAX)) as u64
    }

    /// Creates a new shutdown signal in the [`Running`](ShutdownPhase::Running) phase.
    #[must_use]
    pub fn new() -> Self {
        Self::with_time_source(ShutdownTimeSource::capture_from_current())
    }

    /// Creates a new shutdown signal with a custom time source.
    #[must_use]
    pub fn with_time_getter(time_getter: fn() -> Time) -> Self {
        Self::with_time_source(ShutdownTimeSource::Custom(time_getter))
    }

    fn with_time_source(time_source: ShutdownTimeSource) -> Self {
        Self {
            state: Arc::new(SignalState {
                phase: AtomicU8::new(ShutdownPhase::Running as u8),
                controller: ShutdownController::new(),
                phase_notify: Notify::new(),
                force_close_notify: Notify::new(),
                stopped_notify: Notify::new(),
                time_source,
                has_drain_deadline: AtomicBool::new(false),
                drain_deadline: AtomicU64::new(0),
                has_drain_start: AtomicBool::new(false),
                drain_start: AtomicU64::new(0),
            }),
        }
    }

    pub(crate) fn current_time(&self) -> Time {
        self.state.time_source.now()
    }

    pub(crate) async fn wait_until(&self, deadline: Time) {
        match &self.state.time_source {
            ShutdownTimeSource::TimerDriver(driver) => {
                Sleep::with_timer_driver(deadline, driver.clone()).await;
            }
            ShutdownTimeSource::Custom(time_getter) => {
                Sleep::with_time_getter(deadline, *time_getter).await;
            }
            ShutdownTimeSource::WallClock => {
                sleep_until(deadline).await;
            }
        }
    }

    /// Returns the current shutdown phase.
    #[must_use]
    pub fn phase(&self) -> ShutdownPhase {
        ShutdownPhase::from_u8(self.state.phase.load(Ordering::Acquire))
    }

    /// Returns `true` if the server is in the draining phase.
    #[must_use]
    pub fn is_draining(&self) -> bool {
        self.phase() == ShutdownPhase::Draining
    }

    /// Returns `true` if shutdown has been initiated (draining or later).
    #[must_use]
    pub fn is_shutting_down(&self) -> bool {
        self.phase() != ShutdownPhase::Running
    }

    /// Returns `true` if the server has fully stopped.
    #[must_use]
    pub fn is_stopped(&self) -> bool {
        self.phase() == ShutdownPhase::Stopped
    }

    /// Returns the drain deadline, if one has been set.
    #[must_use]
    pub fn drain_deadline(&self) -> Option<Time> {
        self.state
            .has_drain_deadline
            .load(Ordering::Acquire)
            .then(|| Time::from_nanos(self.state.drain_deadline.load(Ordering::Acquire)))
    }

    /// Subscribes to the underlying shutdown controller for async waiting.
    #[must_use]
    pub fn subscribe(&self) -> ShutdownReceiver {
        self.state.controller.subscribe()
    }

    /// Begins the drain phase with the given timeout.
    ///
    /// Transitions from `Running` to `Draining` and sets a drain deadline.
    /// The caller should stop accepting new connections after this call.
    ///
    /// Returns `false` if shutdown was already initiated.
    #[must_use]
    pub fn begin_drain(&self, timeout: Duration) -> bool {
        let result = self.state.phase.compare_exchange(
            ShutdownPhase::Running as u8,
            ShutdownPhase::Draining as u8,
            Ordering::AcqRel,
            Ordering::Acquire,
        );
        if result.is_ok() {
            let now = self.current_time();
            let deadline = now.saturating_add_nanos(Self::duration_to_nanos(timeout));
            self.state
                .drain_deadline
                .store(deadline.as_nanos(), Ordering::Release);
            self.state.has_drain_deadline.store(true, Ordering::Release);
            self.state
                .drain_start
                .store(now.as_nanos(), Ordering::Release);
            self.state.has_drain_start.store(true, Ordering::Release);
            self.state.controller.shutdown();
            self.state.phase_notify.notify_waiters();
            true
        } else {
            false
        }
    }

    /// Transitions to the force-closing phase.
    ///
    /// Called when the drain timeout has expired and remaining connections
    /// must be terminated. Returns `false` if not currently draining.
    #[must_use]
    pub fn begin_force_close(&self) -> bool {
        let result = self.state.phase.compare_exchange(
            ShutdownPhase::Draining as u8,
            ShutdownPhase::ForceClosing as u8,
            Ordering::AcqRel,
            Ordering::Acquire,
        );
        if result.is_ok() {
            self.state.force_close_notify.notify_waiters();
            self.state.phase_notify.notify_waiters();
            true
        } else {
            false
        }
    }

    /// Marks the server as fully stopped.
    ///
    /// Called when all connections have been closed.
    ///
    /// Also trips the underlying shutdown controller so subscribers blocked on
    /// [`ShutdownReceiver::wait`] wake even if the server reaches `Stopped`
    /// without first entering the drain phase.
    pub fn mark_stopped(&self) {
        self.state
            .phase
            .store(ShutdownPhase::Stopped as u8, Ordering::Release);
        self.state.controller.shutdown();
        self.state.stopped_notify.notify_waiters();
        self.state.force_close_notify.notify_waiters();
        self.state.phase_notify.notify_waiters();
    }

    /// Waits until the shutdown phase reaches or passes the target phase.
    ///
    /// This method is race-free: it guarantees that it will not miss a phase
    /// transition that occurs concurrently.
    pub async fn wait_for_phase(&self, target: ShutdownPhase) {
        let state = Arc::clone(&self.state);
        loop {
            if ShutdownPhase::from_u8(state.phase.load(Ordering::Acquire)) as u8 >= target as u8 {
                return;
            }

            let notify = match target {
                ShutdownPhase::ForceClosing => &state.force_close_notify,
                ShutdownPhase::Stopped => &state.stopped_notify,
                ShutdownPhase::Running | ShutdownPhase::Draining => &state.phase_notify,
            };
            let mut notified = std::pin::pin!(notify.notified());
            std::future::poll_fn(|cx| {
                if std::future::Future::poll(notified.as_mut(), cx).is_ready()
                    || ShutdownPhase::from_u8(state.phase.load(Ordering::Acquire)) as u8
                        >= target as u8
                {
                    return std::task::Poll::Ready(());
                }
                std::task::Poll::Pending
            })
            .await;
        }
    }

    /// Returns the time when drain began, if applicable.
    #[must_use]
    pub fn drain_start(&self) -> Option<Time> {
        self.state
            .has_drain_start
            .load(Ordering::Acquire)
            .then(|| Time::from_nanos(self.state.drain_start.load(Ordering::Acquire)))
    }

    /// Collects shutdown statistics.
    ///
    /// Call after `mark_stopped()` to get the final stats. The `drained` count
    /// is the number of connections that completed gracefully, and `force_closed`
    /// is the number that were force-closed after the drain timeout.
    ///
    /// # Arguments
    ///
    /// * `drained` — Number of connections that completed during drain phase.
    /// * `force_closed` — Number of connections force-closed after timeout.
    #[must_use]
    pub fn collect_stats(&self, drained: usize, force_closed: usize) -> ShutdownStats {
        let duration = self.drain_start().map_or(Duration::ZERO, |start| {
            let now = self.current_time();
            Duration::from_nanos(now.duration_since(start))
        });
        ShutdownStats {
            drained,
            force_closed,
            duration,
            drain_report: None,
        }
    }

    /// Triggers an immediate stop (skips drain phase).
    ///
    /// This transitions directly to [`ShutdownPhase::ForceClosing`], records a
    /// zero-length drain deadline at the current time, and wakes drain waiters.
    /// Call [`mark_stopped`](Self::mark_stopped) once all connections have
    /// actually closed to reach the terminal [`ShutdownPhase::Stopped`] phase.
    ///
    /// Useful for hard shutdowns or test scenarios.
    pub fn trigger_immediate(&self) {
        if self.phase() == ShutdownPhase::Stopped {
            self.state.controller.shutdown();
            self.state.stopped_notify.notify_waiters();
            self.state.force_close_notify.notify_waiters();
            self.state.phase_notify.notify_waiters();
            return;
        }
        let now = self.current_time();
        run_trigger_immediate_pre_phase_hook();
        self.state
            .phase
            .fetch_max(ShutdownPhase::ForceClosing as u8, Ordering::AcqRel);
        if self.phase() == ShutdownPhase::Stopped {
            self.state.controller.shutdown();
            self.state.stopped_notify.notify_waiters();
            self.state.force_close_notify.notify_waiters();
            self.state.phase_notify.notify_waiters();
            return;
        }
        self.state
            .drain_deadline
            .store(now.as_nanos(), Ordering::Release);
        self.state.has_drain_deadline.store(true, Ordering::Release);
        if !self.state.has_drain_start.load(Ordering::Acquire) {
            self.state
                .drain_start
                .store(now.as_nanos(), Ordering::Release);
            self.state.has_drain_start.store(true, Ordering::Release);
        }
        self.state.controller.shutdown();
        self.state.force_close_notify.notify_waiters();
        self.state.phase_notify.notify_waiters();
    }
}

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

impl std::fmt::Debug for ShutdownSignal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ShutdownSignal")
            .field("phase", &self.phase())
            .finish()
    }
}

#[cfg(test)]
mod tests {
    #![allow(
        clippy::pedantic,
        clippy::nursery,
        clippy::expect_fun_call,
        clippy::map_unwrap_or,
        clippy::cast_possible_wrap,
        clippy::future_not_send
    )]
    use super::*;
    use crate::cx::Cx;
    use crate::test_utils::init_test_logging;
    use crate::time::{TimerDriverHandle, VirtualClock};
    use crate::types::{Budget, RegionId, TaskId};
    use std::sync::Arc;
    use std::sync::atomic::{AtomicU64, Ordering};

    static TEST_NOW: AtomicU64 = AtomicU64::new(0);

    fn init_test(name: &str) {
        init_test_logging();
        crate::test_phase!(name);
    }

    fn set_test_time(nanos: u64) {
        TEST_NOW.store(nanos, Ordering::SeqCst);
    }

    fn test_time() -> Time {
        Time::from_nanos(TEST_NOW.load(Ordering::SeqCst))
    }

    fn set_trigger_immediate_pre_phase_hook(hook: Option<Box<dyn FnMut()>>) {
        TRIGGER_IMMEDIATE_PRE_PHASE_HOOK.with(|slot| {
            *slot.borrow_mut() = hook;
        });
    }

    struct FlagWaker(Arc<AtomicBool>);

    impl std::task::Wake for FlagWaker {
        fn wake(self: Arc<Self>) {
            self.0.store(true, Ordering::SeqCst);
        }

        fn wake_by_ref(self: &Arc<Self>) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    #[test]
    fn initial_state_is_running() {
        init_test("initial_state_is_running");
        let signal = ShutdownSignal::new();
        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Running,
            "phase",
            ShutdownPhase::Running,
            signal.phase()
        );
        crate::assert_with_log!(
            !signal.is_draining(),
            "not draining",
            false,
            signal.is_draining()
        );
        crate::assert_with_log!(
            !signal.is_shutting_down(),
            "not shutting down",
            false,
            signal.is_shutting_down()
        );
        crate::assert_with_log!(
            !signal.is_stopped(),
            "not stopped",
            false,
            signal.is_stopped()
        );
        crate::test_complete!("initial_state_is_running");
    }

    #[test]
    fn begin_drain_transitions_to_draining() {
        init_test("begin_drain_transitions_to_draining");
        let signal = ShutdownSignal::new();
        let initiated = signal.begin_drain(Duration::from_secs(30));
        crate::assert_with_log!(initiated, "initiated", true, initiated);
        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Draining,
            "phase",
            ShutdownPhase::Draining,
            signal.phase()
        );
        crate::assert_with_log!(
            signal.is_draining(),
            "is draining",
            true,
            signal.is_draining()
        );
        crate::assert_with_log!(
            signal.is_shutting_down(),
            "is shutting down",
            true,
            signal.is_shutting_down()
        );
        let has_deadline = signal.drain_deadline().is_some();
        crate::assert_with_log!(has_deadline, "has deadline", true, has_deadline);
        crate::test_complete!("begin_drain_transitions_to_draining");
    }

    #[test]
    fn begin_drain_idempotent() {
        init_test("begin_drain_idempotent");
        let signal = ShutdownSignal::new();
        let first = signal.begin_drain(Duration::from_secs(30));
        crate::assert_with_log!(first, "first drain", true, first);

        let second = signal.begin_drain(Duration::from_secs(60));
        crate::assert_with_log!(!second, "second drain rejected", false, second);

        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Draining,
            "still draining",
            ShutdownPhase::Draining,
            signal.phase()
        );
        crate::test_complete!("begin_drain_idempotent");
    }

    #[test]
    fn with_time_getter_controls_deadline_and_duration() {
        init_test("with_time_getter_controls_deadline_and_duration");
        set_test_time(0);
        let signal = ShutdownSignal::with_time_getter(test_time);

        let initiated = signal.begin_drain(Duration::from_nanos(25));
        crate::assert_with_log!(initiated, "initiated", true, initiated);
        crate::assert_with_log!(
            signal.drain_start() == Some(Time::from_nanos(0)),
            "drain start uses injected clock",
            Some(Time::from_nanos(0)),
            signal.drain_start()
        );
        crate::assert_with_log!(
            signal.drain_deadline() == Some(Time::from_nanos(25)),
            "deadline uses injected clock",
            Some(Time::from_nanos(25)),
            signal.drain_deadline()
        );

        set_test_time(80);
        let stats = signal.collect_stats(2, 1);
        crate::assert_with_log!(
            stats.duration == Duration::from_nanos(80),
            "duration uses injected clock",
            Duration::from_nanos(80),
            stats.duration
        );
        crate::test_complete!("with_time_getter_controls_deadline_and_duration");
    }

    #[test]
    fn new_captures_timer_driver_from_current_context() {
        init_test("new_captures_timer_driver_from_current_context");
        let virtual_clock = Arc::new(VirtualClock::starting_at(Time::from_secs(42)));
        let timer_driver = TimerDriverHandle::with_virtual_clock(Arc::clone(&virtual_clock));
        let cx = Cx::new_with_drivers(
            RegionId::new_for_test(7, 0),
            TaskId::new_for_test(9, 0),
            Budget::INFINITE,
            None,
            None,
            None,
            Some(timer_driver),
            None,
        );

        let signal = {
            let _guard = Cx::set_current(Some(cx));
            ShutdownSignal::new()
        };

        let initiated = signal.begin_drain(Duration::from_secs(3));
        crate::assert_with_log!(initiated, "initiated", true, initiated);
        crate::assert_with_log!(
            signal.drain_start() == Some(Time::from_secs(42)),
            "captured driver sets drain start",
            Some(Time::from_secs(42)),
            signal.drain_start()
        );
        crate::assert_with_log!(
            signal.drain_deadline() == Some(Time::from_secs(45)),
            "captured driver sets drain deadline",
            Some(Time::from_secs(45)),
            signal.drain_deadline()
        );

        virtual_clock.advance(7_000_000_000);
        let stats = signal.collect_stats(1, 0);
        crate::assert_with_log!(
            stats.duration == Duration::from_secs(7),
            "captured driver sets stats duration",
            Duration::from_secs(7),
            stats.duration
        );
        crate::test_complete!("new_captures_timer_driver_from_current_context");
    }

    #[test]
    fn wait_until_uses_captured_timer_driver_without_ambient_context() {
        init_test("wait_until_uses_captured_timer_driver_without_ambient_context");
        let virtual_clock = Arc::new(VirtualClock::starting_at(Time::from_secs(10)));
        let timer_driver = TimerDriverHandle::with_virtual_clock(Arc::clone(&virtual_clock));
        let cx = Cx::new_with_drivers(
            RegionId::new_for_test(7, 1),
            TaskId::new_for_test(9, 1),
            Budget::INFINITE,
            None,
            None,
            None,
            Some(timer_driver.clone()),
            None,
        );

        let signal = {
            let _guard = Cx::set_current(Some(cx));
            ShutdownSignal::new()
        };
        let _no_cx = Cx::set_current(None);

        let waker = std::task::Waker::noop();
        let mut task_cx = std::task::Context::from_waker(waker);
        let deadline = Time::from_secs(12);
        let mut wait = std::pin::pin!(signal.wait_until(deadline));

        let first_poll = std::future::Future::poll(wait.as_mut(), &mut task_cx);
        crate::assert_with_log!(
            first_poll.is_pending(),
            "wait is pending before deadline",
            true,
            first_poll.is_pending()
        );
        crate::assert_with_log!(
            timer_driver.pending_count() == 1,
            "captured timer driver owns registration",
            1,
            timer_driver.pending_count()
        );

        virtual_clock.advance(1_000_000_000);
        let fired_before_deadline = timer_driver.process_timers();
        crate::assert_with_log!(
            fired_before_deadline == 0,
            "timer does not fire before deadline",
            0,
            fired_before_deadline
        );

        virtual_clock.advance(1_000_000_000);
        let fired_at_deadline = timer_driver.process_timers();
        crate::assert_with_log!(
            fired_at_deadline == 1,
            "captured timer driver fires at deadline",
            1,
            fired_at_deadline
        );

        let second_poll = std::future::Future::poll(wait.as_mut(), &mut task_cx);
        crate::assert_with_log!(
            second_poll.is_ready(),
            "wait becomes ready after captured timer fires",
            true,
            second_poll.is_ready()
        );
        crate::assert_with_log!(
            timer_driver.pending_count() == 0,
            "registration clears after completion",
            0,
            timer_driver.pending_count()
        );
        crate::test_complete!("wait_until_uses_captured_timer_driver_without_ambient_context");
    }

    #[test]
    fn wait_until_with_time_getter_wakes_after_logical_clock_advance() {
        init_test("wait_until_with_time_getter_wakes_after_logical_clock_advance");
        set_test_time(0);
        let signal = ShutdownSignal::with_time_getter(test_time);

        let woke = Arc::new(AtomicBool::new(false));
        let waker = std::task::Waker::from(Arc::new(FlagWaker(Arc::clone(&woke))));
        let mut task_cx = std::task::Context::from_waker(&waker);
        let deadline = Time::from_secs(10);
        let mut wait = std::pin::pin!(signal.wait_until(deadline));

        let first_poll = std::future::Future::poll(wait.as_mut(), &mut task_cx);
        crate::assert_with_log!(
            first_poll.is_pending(),
            "wait is pending before deadline",
            true,
            first_poll.is_pending()
        );

        set_test_time(deadline.as_nanos());

        let second_poll = std::future::Future::poll(wait.as_mut(), &mut task_cx);
        crate::assert_with_log!(
            second_poll.is_ready(),
            "wait becomes ready after logical deadline",
            true,
            second_poll.is_ready()
        );
        crate::test_complete!("wait_until_with_time_getter_wakes_after_logical_clock_advance");
    }

    #[test]
    fn force_close_from_draining() {
        init_test("force_close_from_draining");
        let signal = ShutdownSignal::new();
        let began = signal.begin_drain(Duration::from_secs(1));
        crate::assert_with_log!(began, "begin drain", true, began);

        let forced = signal.begin_force_close();
        crate::assert_with_log!(forced, "force close", true, forced);
        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::ForceClosing,
            "phase",
            ShutdownPhase::ForceClosing,
            signal.phase()
        );
        crate::test_complete!("force_close_from_draining");
    }

    #[test]
    fn force_close_waiter_is_not_consumed_by_drain_transition() {
        init_test("force_close_waiter_is_not_consumed_by_drain_transition");
        let signal = ShutdownSignal::new();
        let woke = Arc::new(AtomicBool::new(false));
        let waker = std::task::Waker::from(Arc::new(FlagWaker(Arc::clone(&woke))));
        let mut task_cx = std::task::Context::from_waker(&waker);
        let mut wait = Box::pin(signal.wait_for_phase(ShutdownPhase::ForceClosing));

        let first_poll = std::future::Future::poll(wait.as_mut(), &mut task_cx);
        crate::assert_with_log!(
            first_poll.is_pending(),
            "force-close waiter starts pending",
            true,
            first_poll.is_pending()
        );

        let began = signal.begin_drain(Duration::from_secs(1));
        crate::assert_with_log!(began, "begin drain", true, began);
        crate::assert_with_log!(
            !woke.load(Ordering::SeqCst),
            "drain transition does not consume force-close waiter",
            false,
            woke.load(Ordering::SeqCst)
        );

        let forced = signal.begin_force_close();
        crate::assert_with_log!(forced, "force close", true, forced);
        crate::assert_with_log!(
            woke.load(Ordering::SeqCst),
            "force-close transition wakes force-close waiter",
            true,
            woke.load(Ordering::SeqCst)
        );

        let second_poll = std::future::Future::poll(wait.as_mut(), &mut task_cx);
        crate::assert_with_log!(
            second_poll.is_ready(),
            "force-close waiter completes after target transition",
            true,
            second_poll.is_ready()
        );
        crate::test_complete!("force_close_waiter_is_not_consumed_by_drain_transition");
    }

    #[test]
    fn force_close_only_from_draining() {
        init_test("force_close_only_from_draining");
        let signal = ShutdownSignal::new();

        // Can't force close from Running
        let forced = signal.begin_force_close();
        crate::assert_with_log!(!forced, "can't force from running", false, forced);
        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Running,
            "still running",
            ShutdownPhase::Running,
            signal.phase()
        );
        crate::test_complete!("force_close_only_from_draining");
    }

    #[test]
    fn mark_stopped() {
        init_test("mark_stopped");
        let signal = ShutdownSignal::new();
        let began = signal.begin_drain(Duration::from_secs(1));
        crate::assert_with_log!(began, "begin drain", true, began);
        let forced = signal.begin_force_close();
        crate::assert_with_log!(forced, "force close", true, forced);
        signal.mark_stopped();

        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Stopped,
            "stopped",
            ShutdownPhase::Stopped,
            signal.phase()
        );
        crate::assert_with_log!(signal.is_stopped(), "is stopped", true, signal.is_stopped());
        crate::test_complete!("mark_stopped");
    }

    #[test]
    fn trigger_immediate_skips_drain() {
        init_test("trigger_immediate_skips_drain");
        let signal = ShutdownSignal::new();
        signal.trigger_immediate();

        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::ForceClosing,
            "force-closing immediately",
            ShutdownPhase::ForceClosing,
            signal.phase()
        );
        crate::assert_with_log!(
            !signal.is_stopped(),
            "not stopped until mark_stopped",
            false,
            signal.is_stopped()
        );
        crate::test_complete!("trigger_immediate_skips_drain");
    }

    #[test]
    fn trigger_immediate_records_force_close_metadata_without_prior_drain() {
        init_test("trigger_immediate_records_force_close_metadata_without_prior_drain");
        set_test_time(123);
        let signal = ShutdownSignal::with_time_getter(test_time);

        signal.trigger_immediate();

        crate::assert_with_log!(
            signal.drain_start() == Some(Time::from_nanos(123)),
            "immediate start uses injected clock",
            Some(Time::from_nanos(123)),
            signal.drain_start()
        );
        crate::assert_with_log!(
            signal.drain_deadline() == Some(Time::from_nanos(123)),
            "immediate deadline is current time",
            Some(Time::from_nanos(123)),
            signal.drain_deadline()
        );
        crate::test_complete!("trigger_immediate_records_force_close_metadata_without_prior_drain");
    }

    #[test]
    fn trigger_immediate_does_not_regress_stopped_phase() {
        init_test("trigger_immediate_does_not_regress_stopped_phase");
        let signal = ShutdownSignal::new();
        signal.mark_stopped();

        signal.trigger_immediate();

        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Stopped,
            "stopped phase preserved",
            ShutdownPhase::Stopped,
            signal.phase()
        );
        crate::test_complete!("trigger_immediate_does_not_regress_stopped_phase");
    }

    #[test]
    fn trigger_immediate_preserves_stopped_phase_under_interleaved_mark_stopped() {
        init_test("trigger_immediate_preserves_stopped_phase_under_interleaved_mark_stopped");
        let signal = ShutdownSignal::new();
        let hook_signal = signal.clone();
        set_trigger_immediate_pre_phase_hook(Some(Box::new(move || {
            hook_signal.mark_stopped();
        })));

        signal.trigger_immediate();

        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Stopped,
            "interleaved mark_stopped keeps terminal phase",
            ShutdownPhase::Stopped,
            signal.phase()
        );
        crate::assert_with_log!(
            signal.is_stopped(),
            "signal remains stopped after interleaving",
            true,
            signal.is_stopped()
        );
        crate::test_complete!(
            "trigger_immediate_preserves_stopped_phase_under_interleaved_mark_stopped"
        );
    }

    #[test]
    fn trigger_immediate_overrides_interleaved_begin_drain_metadata() {
        init_test("trigger_immediate_overrides_interleaved_begin_drain_metadata");
        set_test_time(123);
        let signal = ShutdownSignal::with_time_getter(test_time);
        let hook_signal = signal.clone();
        set_trigger_immediate_pre_phase_hook(Some(Box::new(move || {
            let began = hook_signal.begin_drain(Duration::from_secs(30));
            assert!(began, "hook begin_drain should succeed");
        })));

        signal.trigger_immediate();

        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::ForceClosing,
            "interleaved begin_drain still reaches force-closing",
            ShutdownPhase::ForceClosing,
            signal.phase()
        );
        crate::assert_with_log!(
            signal.drain_start() == Some(Time::from_nanos(123)),
            "original drain start is retained",
            Some(Time::from_nanos(123)),
            signal.drain_start()
        );
        crate::assert_with_log!(
            signal.drain_deadline() == Some(Time::from_nanos(123)),
            "immediate trigger overwrites graceful-drain deadline",
            Some(Time::from_nanos(123)),
            signal.drain_deadline()
        );
        crate::test_complete!("trigger_immediate_overrides_interleaved_begin_drain_metadata");
    }

    #[test]
    fn subscribe_receives_shutdown() {
        init_test("subscribe_receives_shutdown");
        let signal = ShutdownSignal::new();
        let receiver = signal.subscribe();

        let not_shutting = receiver.is_shutting_down();
        crate::assert_with_log!(!not_shutting, "not shutting", false, not_shutting);

        let began = signal.begin_drain(Duration::from_secs(30));
        crate::assert_with_log!(began, "begin drain", true, began);

        let shutting = receiver.is_shutting_down();
        crate::assert_with_log!(shutting, "shutting down", true, shutting);
        crate::test_complete!("subscribe_receives_shutdown");
    }

    #[test]
    fn display_formatting() {
        init_test("display_formatting");
        let cases = [
            (ShutdownPhase::Running, "Running"),
            (ShutdownPhase::Draining, "Draining"),
            (ShutdownPhase::ForceClosing, "ForceClosing"),
            (ShutdownPhase::Stopped, "Stopped"),
        ];
        for (phase, expected) in cases {
            let actual = format!("{phase}");
            crate::assert_with_log!(actual == expected, "phase display", expected, actual);
        }
        crate::test_complete!("display_formatting");
    }

    #[test]
    fn clone_shares_state() {
        init_test("clone_shares_state");
        let signal = ShutdownSignal::new();
        let cloned = signal.clone();

        let began = signal.begin_drain(Duration::from_secs(30));
        crate::assert_with_log!(began, "begin drain", true, began);

        crate::assert_with_log!(
            cloned.is_draining(),
            "clone sees drain",
            true,
            cloned.is_draining()
        );
        crate::test_complete!("clone_shares_state");
    }

    // ====================================================================
    // Async integration tests
    // ====================================================================

    #[test]
    fn phase_changed_fires_on_drain() {
        init_test("phase_changed_fires_on_drain");
        crate::test_utils::run_test(|| async {
            let signal = ShutdownSignal::new();
            let signal2 = signal.clone();

            // Spawn a thread that will begin drain after a short delay
            let handle = std::thread::spawn(move || {
                std::thread::sleep(Duration::from_millis(20));
                let began = signal2.begin_drain(Duration::from_secs(30));
                assert!(began, "begin drain should succeed");
            });

            // Wait for the phase change
            signal.wait_for_phase(ShutdownPhase::Draining).await;
            let new_phase = signal.phase();
            crate::assert_with_log!(
                new_phase == ShutdownPhase::Draining,
                "phase after drain",
                ShutdownPhase::Draining,
                new_phase
            );

            handle.join().expect("thread panicked");
        });
        crate::test_complete!("phase_changed_fires_on_drain");
    }

    #[test]
    fn phase_changed_fires_on_force_close() {
        init_test("phase_changed_fires_on_force_close");
        crate::test_utils::run_test(|| async {
            let signal = ShutdownSignal::new();
            let began = signal.begin_drain(Duration::from_secs(30));
            crate::assert_with_log!(began, "begin drain", true, began);

            let signal2 = signal.clone();
            let handle = std::thread::spawn(move || {
                std::thread::sleep(Duration::from_millis(20));
                let forced = signal2.begin_force_close();
                assert!(forced, "force close should succeed");
            });

            signal.wait_for_phase(ShutdownPhase::ForceClosing).await;
            let new_phase = signal.phase();
            crate::assert_with_log!(
                new_phase == ShutdownPhase::ForceClosing,
                "phase after force close",
                ShutdownPhase::ForceClosing,
                new_phase
            );

            handle.join().expect("thread panicked");
        });
        crate::test_complete!("phase_changed_fires_on_force_close");
    }

    #[test]
    fn phase_changed_fires_on_mark_stopped() {
        init_test("phase_changed_fires_on_mark_stopped");
        crate::test_utils::run_test(|| async {
            let signal = ShutdownSignal::new();
            let began = signal.begin_drain(Duration::from_secs(30));
            crate::assert_with_log!(began, "begin drain", true, began);
            let forced = signal.begin_force_close();
            crate::assert_with_log!(forced, "force close", true, forced);

            let signal2 = signal.clone();
            let handle = std::thread::spawn(move || {
                std::thread::sleep(Duration::from_millis(20));
                signal2.mark_stopped();
            });

            signal.wait_for_phase(ShutdownPhase::Stopped).await;
            let new_phase = signal.phase();
            crate::assert_with_log!(
                new_phase == ShutdownPhase::Stopped,
                "phase after stopped",
                ShutdownPhase::Stopped,
                new_phase
            );

            handle.join().expect("thread panicked");
        });
        crate::test_complete!("phase_changed_fires_on_mark_stopped");
    }

    #[test]
    fn phase_changed_fires_on_immediate() {
        init_test("phase_changed_fires_on_immediate");
        crate::test_utils::run_test(|| async {
            let signal = ShutdownSignal::new();
            let signal2 = signal.clone();

            let handle = std::thread::spawn(move || {
                std::thread::sleep(Duration::from_millis(20));
                signal2.trigger_immediate();
            });

            signal.wait_for_phase(ShutdownPhase::ForceClosing).await;
            let new_phase = signal.phase();
            crate::assert_with_log!(
                new_phase == ShutdownPhase::ForceClosing,
                "phase after immediate",
                ShutdownPhase::ForceClosing,
                new_phase
            );

            handle.join().expect("thread panicked");
        });
        crate::test_complete!("phase_changed_fires_on_immediate");
    }

    #[test]
    fn full_lifecycle_phase_transitions() {
        init_test("full_lifecycle_phase_transitions");
        crate::test_utils::run_test(|| async {
            let signal = ShutdownSignal::new();

            // Phase 1: Running
            crate::assert_with_log!(
                signal.phase() == ShutdownPhase::Running,
                "starts running",
                ShutdownPhase::Running,
                signal.phase()
            );

            // Phase 2: Draining (triggered from another thread)
            {
                let sig = signal.clone();
                let h = std::thread::spawn(move || {
                    std::thread::sleep(Duration::from_millis(10));
                    let began = sig.begin_drain(Duration::from_secs(1));
                    assert!(began, "begin drain should succeed");
                });
                signal.wait_for_phase(ShutdownPhase::Draining).await;
                let p = signal.phase();
                crate::assert_with_log!(
                    p == ShutdownPhase::Draining,
                    "draining",
                    ShutdownPhase::Draining,
                    p
                );
                h.join().expect("thread panicked");
            }

            // Phase 3: ForceClosing
            {
                let sig = signal.clone();
                let h = std::thread::spawn(move || {
                    std::thread::sleep(Duration::from_millis(10));
                    let forced = sig.begin_force_close();
                    assert!(forced, "force close should succeed");
                });
                signal.wait_for_phase(ShutdownPhase::ForceClosing).await;
                let p = signal.phase();
                crate::assert_with_log!(
                    p == ShutdownPhase::ForceClosing,
                    "force closing",
                    ShutdownPhase::ForceClosing,
                    p
                );
                h.join().expect("thread panicked");
            }

            // Phase 4: Stopped
            {
                let sig = signal.clone();
                let h = std::thread::spawn(move || {
                    std::thread::sleep(Duration::from_millis(10));
                    sig.mark_stopped();
                });
                signal.wait_for_phase(ShutdownPhase::Stopped).await;
                let p = signal.phase();
                crate::assert_with_log!(
                    p == ShutdownPhase::Stopped,
                    "stopped",
                    ShutdownPhase::Stopped,
                    p
                );
                h.join().expect("thread panicked");
            }
        });
        crate::test_complete!("full_lifecycle_phase_transitions");
    }

    #[test]
    fn subscriber_receives_drain_signal() {
        init_test("subscriber_receives_drain_signal");
        crate::test_utils::run_test(|| async {
            let signal = ShutdownSignal::new();
            let mut receiver = signal.subscribe();

            let not_shutting = receiver.is_shutting_down();
            crate::assert_with_log!(!not_shutting, "not shutting down", false, not_shutting);

            // Trigger drain from thread
            let sig = signal.clone();
            let h = std::thread::spawn(move || {
                std::thread::sleep(Duration::from_millis(10));
                let began = sig.begin_drain(Duration::from_secs(30));
                assert!(began, "begin drain should succeed");
            });

            // Wait for the underlying signal
            receiver.wait().await;

            let shutting = receiver.is_shutting_down();
            crate::assert_with_log!(shutting, "is shutting down", true, shutting);

            h.join().expect("thread panicked");
        });
        crate::test_complete!("subscriber_receives_drain_signal");
    }

    #[test]
    fn subscriber_receives_mark_stopped_without_prior_drain() {
        init_test("subscriber_receives_mark_stopped_without_prior_drain");
        crate::test_utils::run_test(|| async {
            let signal = ShutdownSignal::new();
            let mut receiver = signal.subscribe();
            let signal2 = signal.clone();

            let handle = std::thread::spawn(move || {
                std::thread::sleep(Duration::from_millis(10));
                signal2.mark_stopped();
            });

            receiver.wait().await;

            let shutting = receiver.is_shutting_down();
            crate::assert_with_log!(shutting, "receiver sees shutdown", true, shutting);
            crate::assert_with_log!(
                signal.phase() == ShutdownPhase::Stopped,
                "phase after mark_stopped",
                ShutdownPhase::Stopped,
                signal.phase()
            );

            handle.join().expect("thread panicked");
        });
        crate::test_complete!("subscriber_receives_mark_stopped_without_prior_drain");
    }

    // ====================================================================
    // Stats collection tests
    // ====================================================================

    #[test]
    fn collect_stats_before_drain() {
        init_test("collect_stats_before_drain");
        let signal = ShutdownSignal::new();

        let stats = signal.collect_stats(0, 0);
        crate::assert_with_log!(stats.drained == 0, "drained", 0, stats.drained);
        crate::assert_with_log!(
            stats.force_closed == 0,
            "force_closed",
            0,
            stats.force_closed
        );
        // Duration should be zero since drain hasn't started
        crate::assert_with_log!(
            stats.duration == Duration::ZERO,
            "duration zero",
            Duration::ZERO,
            stats.duration
        );
        crate::test_complete!("collect_stats_before_drain");
    }

    #[test]
    fn collect_stats_after_drain() {
        init_test("collect_stats_after_drain");
        let signal = ShutdownSignal::new();

        let began = signal.begin_drain(Duration::from_secs(30));
        crate::assert_with_log!(began, "drain started", true, began);

        // Small sleep to ensure measurable duration
        std::thread::sleep(Duration::from_millis(5));

        let stats = signal.collect_stats(10, 3);
        crate::assert_with_log!(stats.drained == 10, "drained", 10, stats.drained);
        crate::assert_with_log!(
            stats.force_closed == 3,
            "force_closed",
            3,
            stats.force_closed
        );

        let nonzero = stats.duration > Duration::ZERO;
        crate::assert_with_log!(nonzero, "nonzero duration", true, nonzero);
        crate::test_complete!("collect_stats_after_drain");
    }

    #[test]
    fn drain_start_tracking() {
        init_test("drain_start_tracking");
        let signal = ShutdownSignal::new();

        let before = signal.drain_start();
        crate::assert_with_log!(
            before.is_none(),
            "no start before drain",
            true,
            before.is_none()
        );

        let began = signal.begin_drain(Duration::from_secs(30));
        crate::assert_with_log!(began, "drain started", true, began);

        let after = signal.drain_start();
        crate::assert_with_log!(after.is_some(), "start after drain", true, after.is_some());
        crate::test_complete!("drain_start_tracking");
    }

    #[test]
    fn concurrent_begin_drain_only_one_wins() {
        init_test("concurrent_begin_drain_only_one_wins");
        let signal = ShutdownSignal::new();
        let barrier = std::sync::Arc::new(std::sync::Barrier::new(3));
        let winners = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));

        let mut handles = Vec::new();
        for _ in 0..2 {
            let sig = signal.clone();
            let b = std::sync::Arc::clone(&barrier);
            let w = std::sync::Arc::clone(&winners);
            handles.push(std::thread::spawn(move || {
                b.wait();
                if sig.begin_drain(Duration::from_secs(30)) {
                    w.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                }
            }));
        }

        barrier.wait();
        for h in handles {
            h.join().expect("thread panicked");
        }

        let winner_count = winners.load(std::sync::atomic::Ordering::Relaxed);
        crate::assert_with_log!(winner_count == 1, "exactly one winner", 1, winner_count);
        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Draining,
            "phase is draining",
            ShutdownPhase::Draining,
            signal.phase()
        );
        crate::test_complete!("concurrent_begin_drain_only_one_wins");
    }

    #[test]
    fn mark_stopped_from_draining_skips_force_close() {
        init_test("mark_stopped_from_draining_skips_force_close");
        let signal = ShutdownSignal::new();
        let began = signal.begin_drain(Duration::from_secs(30));
        crate::assert_with_log!(began, "begin drain", true, began);

        // Directly mark stopped without going through ForceClosing
        signal.mark_stopped();
        crate::assert_with_log!(
            signal.phase() == ShutdownPhase::Stopped,
            "stopped from draining",
            ShutdownPhase::Stopped,
            signal.phase()
        );
        crate::test_complete!("mark_stopped_from_draining_skips_force_close");
    }

    #[test]
    fn shutdown_phase_debug_clone_copy_eq() {
        let p = ShutdownPhase::Draining;
        let dbg = format!("{p:?}");
        assert!(dbg.contains("Draining"), "{dbg}");
        let copied: ShutdownPhase = p;
        let cloned = p;
        assert_eq!(copied, cloned);
        assert_ne!(p, ShutdownPhase::Running);
    }

    #[test]
    fn shutdown_stats_debug_clone() {
        let s = ShutdownStats {
            drained: 5,
            force_closed: 1,
            duration: Duration::from_secs(3),
            drain_report: None,
        };
        let dbg = format!("{s:?}");
        assert!(dbg.contains("ShutdownStats"), "{dbg}");
        let cloned = s;
        assert_eq!(format!("{cloned:?}"), dbg);
    }

    // ------------------------------------------------------------------
    // Request-aware drain certificate (D2.1, br-...-eeexl1.2)
    // ------------------------------------------------------------------

    fn t(nanos: u64) -> Time {
        Time::from_nanos(nanos)
    }

    /// A clean drain from N in-flight requests down to zero reaches
    /// quiescence with nothing stranded and a converging certificate.
    #[test]
    fn drain_tracker_clean_drain_reaches_quiescence() {
        init_test("drain_tracker_clean_drain_reaches_quiescence");
        let mut tracker = GracefulDrainTracker::new(50, t(0));
        // Steady descent: 50 -> 0 in 10 even steps.
        for step in 1..=10 {
            let remaining = 50 - step * 5;
            tracker.observe(remaining);
        }
        assert_eq!(tracker.remaining(), 0);
        let report = tracker.finish(t(1_000_000), false);

        assert_eq!(report.requests_at_drain_start, 50);
        assert_eq!(report.requests_completed, 50);
        assert_eq!(report.requests_stranded, 0);
        assert!(report.reached_quiescence);
        assert!(!report.hard_deadline_hit);
        assert!(
            report.converging,
            "a monotone descent to zero must read as converging: {report:?}"
        );
        assert_eq!(report.drain_duration, Duration::from_millis(1));
        crate::test_complete!("drain_tracker_clean_drain_reaches_quiescence");
    }

    /// A drain that plateaus (no progress) is classified as stalled and
    /// strands the remaining requests when the hard deadline is hit.
    #[test]
    fn drain_tracker_stalled_drain_strands_requests() {
        init_test("drain_tracker_stalled_drain_strands_requests");
        let mut tracker = GracefulDrainTracker::new(8, t(0));
        // Stuck at 5 in-flight for many ticks: no progress.
        for _ in 0..40 {
            tracker.observe(5);
        }
        let report = tracker.finish(t(5_000_000), true);

        assert_eq!(report.requests_at_drain_start, 8);
        assert_eq!(report.requests_stranded, 5);
        assert_eq!(report.requests_completed, 3);
        assert!(!report.reached_quiescence);
        assert!(report.hard_deadline_hit);
        assert!(
            matches!(report.final_phase, DrainPhase::Stalled),
            "a long plateau must classify as stalled, got {}",
            report.final_phase
        );
        assert!(report.stall_detected, "stall must be detected: {report:?}");
        crate::test_complete!("drain_tracker_stalled_drain_strands_requests");
    }

    /// A partial drain that hits the hard deadline mid-descent strands only
    /// the still-in-flight requests and counts the rest as completed.
    #[test]
    fn drain_tracker_partial_drain_accounts_completed_and_stranded() {
        init_test("drain_tracker_partial_drain_accounts_completed_and_stranded");
        let mut tracker = GracefulDrainTracker::new(20, t(0));
        for remaining in [16, 12, 9, 7] {
            tracker.observe(remaining);
        }
        let report = tracker.finish(t(2_000_000), true);

        assert_eq!(report.requests_completed, 13);
        assert_eq!(report.requests_stranded, 7);
        assert_eq!(report.observations, 5); // initial + 4 observations
        assert!(report.hard_deadline_hit);
        assert!(!report.reached_quiescence);
        crate::test_complete!("drain_tracker_partial_drain_accounts_completed_and_stranded");
    }

    /// The report rendering is deterministic and golden-stable. The float
    /// fields are produced by the certificate from the fixed observation
    /// series, so the whole block is reproducible.
    #[test]
    fn drain_report_display_is_golden_stable() {
        init_test("drain_report_display_is_golden_stable");
        // A directly-constructed report keeps the golden independent of the
        // certificate's internal float evolution (which has its own tests);
        // this pins the operator-facing rendering contract.
        let report = GracefulDrainReport {
            requests_at_drain_start: 50,
            requests_completed: 50,
            requests_stranded: 0,
            requests_at_escalation: None,
            observations: 11,
            final_phase: DrainPhase::Quiescent,
            converging: true,
            confidence_bound: 0.987_654,
            estimated_remaining_steps: Some(0.0),
            stall_detected: false,
            reached_quiescence: true,
            hard_deadline_hit: false,
            drain_duration: Duration::from_millis(42),
        };
        let expected = "\
Graceful Drain Report
=====================
Requests at start:  50
Requests completed: 50
Requests stranded:  0
At escalation:      never
Observations:       11
Final drain phase:  quiescent
Converging:         true
Confidence bound:   0.987654
Est. remaining:     0.0 steps
Stall detected:     false
Reached quiescence: true
Hard deadline hit:  false
Drain duration:     42ms
";
        assert_eq!(report.to_string(), expected);
        crate::test_complete!("drain_report_display_is_golden_stable");
    }

    /// The estimated-remaining `None` branch renders as `N/A`.
    #[test]
    fn drain_report_display_handles_unknown_remaining() {
        init_test("drain_report_display_handles_unknown_remaining");
        let report = GracefulDrainReport {
            requests_at_drain_start: 3,
            requests_completed: 0,
            requests_stranded: 3,
            requests_at_escalation: Some(3),
            observations: 1,
            final_phase: DrainPhase::Warmup,
            converging: false,
            confidence_bound: 0.0,
            estimated_remaining_steps: None,
            stall_detected: false,
            reached_quiescence: false,
            hard_deadline_hit: true,
            drain_duration: Duration::ZERO,
        };
        let rendered = report.to_string();
        assert!(rendered.contains("Est. remaining:     N/A"), "{rendered}");
        assert!(
            rendered.contains("Final drain phase:  warmup"),
            "{rendered}"
        );
        assert!(rendered.contains("At escalation:      3"), "{rendered}");
        assert!(rendered.contains("Drain duration:     0ms"), "{rendered}");
        crate::test_complete!("drain_report_display_handles_unknown_remaining");
    }

    /// The supervisor records the in-flight count at the escalation
    /// transition; the report keeps the completed-vs-interrupted boundary
    /// (br-asupersync-server-stack-hardening-eeexl1.2, D2.4).
    #[test]
    fn supervisor_records_requests_at_escalation() {
        init_test("supervisor_records_requests_at_escalation");
        let mut supervisor =
            GracefulDrainSupervisor::new(5, t(0), Duration::from_secs(1), Duration::from_secs(10));

        // Within the soft budget: no escalation recorded.
        assert_eq!(supervisor.observe(5, t(500_000_000)), DrainStep::Continue);

        // Soft deadline elapses with 3 still in flight: escalation fires once
        // and captures the boundary.
        assert_eq!(supervisor.observe(3, t(1_500_000_000)), DrainStep::Escalate);
        assert_eq!(supervisor.observe(1, t(2_000_000_000)), DrainStep::Continue);
        assert_eq!(
            supervisor.observe(0, t(2_500_000_000)),
            DrainStep::Quiescent
        );

        let report = supervisor.finish(t(2_500_000_000), false);
        assert_eq!(report.requests_at_escalation, Some(3));
        assert_eq!(report.requests_completed, 5);
        assert!(report.reached_quiescence);
        crate::test_complete!("supervisor_records_requests_at_escalation");
    }

    /// A drain that reaches quiescence inside the soft budget never
    /// escalates, and the report says so.
    #[test]
    fn supervisor_reports_never_escalated_on_clean_drain() {
        init_test("supervisor_reports_never_escalated_on_clean_drain");
        let mut supervisor =
            GracefulDrainSupervisor::new(2, t(0), Duration::from_secs(1), Duration::from_secs(10));
        assert_eq!(supervisor.observe(1, t(200_000_000)), DrainStep::Continue);
        assert_eq!(supervisor.observe(0, t(400_000_000)), DrainStep::Quiescent);

        let report = supervisor.finish(t(400_000_000), false);
        assert_eq!(report.requests_at_escalation, None);
        assert!(report.to_string().contains("At escalation:      never"));
        crate::test_complete!("supervisor_reports_never_escalated_on_clean_drain");
    }

    /// If another driver path force-closes at the soft deadline before the
    /// supervisor's next tick, the report still preserves the last observed
    /// in-flight count as the escalation boundary.
    #[test]
    fn supervisor_records_external_escalation_before_quiescent_tick() {
        init_test("supervisor_records_external_escalation_before_quiescent_tick");
        let mut supervisor =
            GracefulDrainSupervisor::new(5, t(0), Duration::from_secs(1), Duration::from_secs(10));
        assert_eq!(supervisor.observe(5, t(500_000_000)), DrainStep::Continue);
        assert!(supervisor.record_external_escalation());
        assert_eq!(
            supervisor.observe(0, t(1_100_000_000)),
            DrainStep::Quiescent
        );

        let report = supervisor.finish(t(1_100_000_000), false);
        assert_eq!(report.requests_at_escalation, Some(5));
        assert!(report.reached_quiescence);
        crate::test_complete!("supervisor_records_external_escalation_before_quiescent_tick");
    }

    /// `finish` clamps a backward clock to a zero duration rather than
    /// underflowing.
    #[test]
    fn drain_tracker_backward_clock_is_zero_duration() {
        init_test("drain_tracker_backward_clock_is_zero_duration");
        let mut tracker = GracefulDrainTracker::new(2, t(1_000));
        tracker.observe(0);
        let report = tracker.finish(t(500), false);
        assert_eq!(report.drain_duration, Duration::ZERO);
        assert!(report.reached_quiescence);
        crate::test_complete!("drain_tracker_backward_clock_is_zero_duration");
    }

    // ------------------------------------------------------------------
    // Drain supervisor decision state machine (D2.2a)
    // ------------------------------------------------------------------

    /// A drain that empties before the soft budget reports Continue then
    /// Quiescent, never escalating.
    #[test]
    fn drain_supervisor_clean_drain_never_escalates() {
        init_test("drain_supervisor_clean_drain_never_escalates");
        let mut sup = GracefulDrainSupervisor::new(
            10,
            t(0),
            Duration::from_secs(30),
            Duration::from_secs(60),
        );
        assert_eq!(sup.observe(6, t(1_000_000_000)), DrainStep::Continue);
        assert_eq!(sup.observe(2, t(2_000_000_000)), DrainStep::Continue);
        assert_eq!(sup.observe(0, t(3_000_000_000)), DrainStep::Quiescent);
        assert!(!sup.escalated());
        let report = sup.finish(t(3_000_000_000), false);
        assert!(report.reached_quiescence);
        assert_eq!(report.requests_stranded, 0);
        crate::test_complete!("drain_supervisor_clean_drain_never_escalates");
    }

    /// Crossing the soft budget with requests still in flight escalates
    /// exactly once, then keeps reporting Continue until the hard deadline.
    #[test]
    fn drain_supervisor_escalates_once_at_soft_budget() {
        init_test("drain_supervisor_escalates_once_at_soft_budget");
        let mut sup =
            GracefulDrainSupervisor::new(5, t(0), Duration::from_secs(10), Duration::from_secs(30));
        // Before the soft deadline: keep waiting.
        assert_eq!(sup.observe(5, t(5_000_000_000)), DrainStep::Continue);
        // First observation at/after the soft deadline escalates.
        assert_eq!(sup.observe(4, t(10_000_000_000)), DrainStep::Escalate);
        assert!(sup.escalated());
        // Subsequent observations in the escalation window do not re-escalate.
        assert_eq!(sup.observe(3, t(12_000_000_000)), DrainStep::Continue);
        assert_eq!(sup.observe(2, t(20_000_000_000)), DrainStep::Continue);
        crate::test_complete!("drain_supervisor_escalates_once_at_soft_budget");
    }

    /// Reaching the hard deadline with requests still in flight reports
    /// HardDeadline; the report records the stranded requests.
    #[test]
    fn drain_supervisor_hard_deadline_strands_remaining() {
        init_test("drain_supervisor_hard_deadline_strands_remaining");
        let mut sup =
            GracefulDrainSupervisor::new(8, t(0), Duration::from_secs(10), Duration::from_secs(20));
        assert_eq!(sup.observe(6, t(10_000_000_000)), DrainStep::Escalate);
        assert_eq!(sup.observe(4, t(20_000_000_000)), DrainStep::HardDeadline);
        let report = sup.finish(t(20_000_000_000), true);
        assert!(report.hard_deadline_hit);
        assert_eq!(report.requests_stranded, 4);
        assert_eq!(report.requests_completed, 4);
        assert!(!report.reached_quiescence);
        crate::test_complete!("drain_supervisor_hard_deadline_strands_remaining");
    }

    /// Quiescence wins over a simultaneously-elapsed hard deadline: a tick
    /// that observes zero in-flight reports Quiescent even past the deadline.
    #[test]
    fn drain_supervisor_quiescence_beats_hard_deadline() {
        init_test("drain_supervisor_quiescence_beats_hard_deadline");
        let mut sup =
            GracefulDrainSupervisor::new(3, t(0), Duration::from_secs(10), Duration::from_secs(20));
        // Past the hard deadline, but the count hit zero this tick.
        assert_eq!(sup.observe(0, t(25_000_000_000)), DrainStep::Quiescent);
        let report = sup.finish(t(25_000_000_000), false);
        assert!(report.reached_quiescence);
        assert_eq!(report.requests_stranded, 0);
        crate::test_complete!("drain_supervisor_quiescence_beats_hard_deadline");
    }

    /// `hard_budget` is clamped up to at least `drain_budget` so the hard
    /// deadline can never precede the soft escalation point.
    #[test]
    fn drain_supervisor_clamps_hard_budget_below_soft() {
        init_test("drain_supervisor_clamps_hard_budget_below_soft");
        let sup = GracefulDrainSupervisor::new(
            1,
            t(0),
            Duration::from_secs(30),
            Duration::from_secs(5), // smaller than the soft budget
        );
        assert!(
            sup.hard_deadline() >= sup.drain_deadline(),
            "hard deadline must not precede the soft deadline"
        );
        crate::test_complete!("drain_supervisor_clamps_hard_budget_below_soft");
    }
}