asupersync 0.3.0

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
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
//! Cancellation reason and kind types.
//!
//! Cancellation in Asupersync is a first-class protocol, not a silent drop.
//! This module defines the types that describe why and how cancellation occurred.
//!
//! # Cleanup Budget Policy
//!
//! Every cancellation carries a **cleanup budget** that bounds the resources
//! available for cleanup code (drop glue, finalizers, obligation discharge).
//! The policy is **severity-scaled**: more urgent cancellations get tighter
//! budgets but higher scheduling priority.
//!
//! | CancelKind | Poll Quota | Priority | Rationale |
//! |------------|-----------|----------|-----------|
//! | User | 1000 | 200 | Graceful; user expects orderly shutdown |
//! | Timeout/Deadline | 500 | 210 | Time-driven; moderate cleanup window |
//! | PollQuota/CostBudget | 300 | 215 | Budget violation; tight but fair |
//! | FailFast/RaceLost/Parent/Resource | 200 | 220 | Cascading; fast teardown needed |
//! | Shutdown | 50 | 255 | Urgent; minimal cleanup, max priority |
//!
//! ## Min-Plus Rules (Budget Interaction)
//!
//! When a task receives multiple cancellation requests, the cleanup budgets
//! are **combined** via the meet (∧) operation from the budget algebra
//! (see `Budget::combine`). This means:
//!
//! - Poll quotas: min (tighter quota wins)
//! - Priority: max (higher urgency wins)
//!
//! This ensures that strengthening a cancellation never *increases* the
//! cleanup budget — the monotone-narrowing property from the budget
//! semilattice is preserved.
//!
//! ## Bounded Completion Guarantee
//!
//! Because cleanup budgets have finite poll quotas:
//!
//! 1. Each `consume_poll()` call strictly decreases the remaining quota
//!    (Lemma 7 in `types::budget::tests`)
//! 2. After exactly `poll_quota` polls, the budget is exhausted
//!    (Lemma 10 in `types::budget::tests`)
//! 3. An exhausted budget causes the scheduler to stop polling the task
//!
//! Therefore cleanup terminates within `poll_quota` poll cycles, which
//! is the **sufficient-budget termination** property.
//!
//! ## Calibration Guidelines
//!
//! - **Production**: Use default quotas. If cleanup routines need more
//!   polls (e.g., flushing large buffers), increase the User/Timeout
//!   quotas proportionally but keep Shutdown ≤ 100.
//! - **Lab/Testing**: Use `Budget::MINIMAL` (100 polls) for fast
//!   deterministic runs. Set `ASUPERSYNC_CLEANUP_BUDGET_OVERRIDE` env
//!   var to override all cleanup quotas for calibration experiments.
//! - **Backpressure**: Priority elevation (200–255) ensures cancelled
//!   tasks get scheduled promptly even under load, preventing
//!   indefinite queueing of cleanup work.
//!
//! # Attribution
//!
//! Each cancellation reason includes full attribution information:
//! - **Origin**: The region and optionally task that initiated the cancellation
//! - **Timestamp**: When the cancellation was requested
//! - **Cause chain**: Optional chain of parent causes for debugging
//!
//! This enables debugging and diagnostics to trace cancellation back to its source.
//!
//! # Memory Management
//!
//! Cause chains can grow unboundedly deep in complex cancellation scenarios.
//! To prevent unbounded memory growth, use [`CancelAttributionConfig`] to set limits:
//!
//! - `max_chain_depth`: Maximum depth of cause chain to preserve (default: 16)
//! - `max_chain_memory`: Maximum total memory for cause chain (default: 4KB)
//!
//! When chains exceed limits, they are truncated with a clear marker.
//!
//! ## Memory Cost Analysis
//!
//! Memory cost per `CancelReason`:
//! - Base: ~80 bytes (ids, timestamp, kind, flags)
//! - Message: variable (static &str pointer = 16 bytes)
//! - Cause: recursive (Box pointer = 8 bytes + child cost)
//!
//! Total cost for chain of depth D with no messages:
//! ```text
//! cost = 80 * D + 8 * (D-1)  // ~88 bytes per level
//! ```
//!
//! Default limits (depth=16, memory=4KB) allow chains up to ~45 levels
//! but prefer depth limiting for predictability.

use super::{Budget, RegionId, TaskId, Time};
use core::fmt;
use serde::{Deserialize, Serialize};

/// Configuration for cancel attribution chain limits.
///
/// Controls memory usage by limiting cause chain depth and total memory.
/// Use this to prevent unbounded memory growth in complex cancellation scenarios.
///
/// # Example
///
/// ```rust,ignore
/// use asupersync::types::CancelAttributionConfig;
///
/// let config = CancelAttributionConfig::default();
/// assert_eq!(config.max_chain_depth, 16);
/// assert_eq!(config.max_chain_memory, 4096);
///
/// let custom = CancelAttributionConfig::new(8, 2048);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CancelAttributionConfig {
    /// Maximum depth of cause chain to preserve.
    /// Deeper chains are truncated with a 'truncated' marker.
    /// Default: 16
    pub max_chain_depth: usize,

    /// Maximum total memory (in bytes) for cause chain.
    /// When exceeded, chain is truncated.
    /// Default: 4096 (4KB)
    pub max_chain_memory: usize,
}

impl CancelAttributionConfig {
    /// Default maximum chain depth.
    pub const DEFAULT_MAX_DEPTH: usize = 16;

    /// Default maximum chain memory (4KB).
    pub const DEFAULT_MAX_MEMORY: usize = 4096;

    /// Creates a new configuration with custom limits.
    #[inline]
    #[must_use]
    pub const fn new(max_chain_depth: usize, max_chain_memory: usize) -> Self {
        Self {
            max_chain_depth,
            max_chain_memory,
        }
    }

    /// Creates a configuration with no limits (for testing or special cases).
    #[inline]
    #[must_use]
    pub const fn unlimited() -> Self {
        Self {
            max_chain_depth: usize::MAX,
            max_chain_memory: usize::MAX,
        }
    }

    /// Returns the estimated memory cost of a single `CancelReason` (without cause chain).
    ///
    /// This is approximately:
    /// - 8 bytes: kind (enum)
    /// - 8 bytes: origin_region (RegionId)
    /// - 16 bytes: origin_task (`Option<TaskId>`)
    /// - 8 bytes: timestamp (Time)
    /// - 16 bytes: message (`Option<&'static str>`)
    /// - 8 bytes: cause (`Option<Box<...>>` pointer, not content)
    /// - 1 byte: truncated flag
    /// - 8 bytes: truncated_at_depth (`Option<usize>`)
    /// - Total: ~80 bytes (rounded up for alignment)
    #[inline]
    #[must_use]
    pub const fn single_reason_cost() -> usize {
        80
    }

    /// Estimates memory cost for a chain of given depth.
    #[inline]
    #[must_use]
    pub const fn estimated_chain_cost(depth: usize) -> usize {
        if depth == 0 {
            return 0;
        }
        // Each level: ~80 bytes base + 8 bytes Box overhead for parent
        Self::single_reason_cost() * depth + 8 * depth.saturating_sub(1)
    }
}

impl Default for CancelAttributionConfig {
    fn default() -> Self {
        Self {
            max_chain_depth: Self::DEFAULT_MAX_DEPTH,
            max_chain_memory: Self::DEFAULT_MAX_MEMORY,
        }
    }
}

/// The kind of cancellation request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub enum CancelKind {
    /// Explicit cancellation requested by user code.
    User,
    /// Cancellation due to timeout/deadline.
    Timeout,
    /// Cancellation due to deadline budget exhaustion (§3.2.1).
    Deadline,
    /// Cancellation due to poll quota exhaustion (§3.2.2).
    PollQuota,
    /// Cancellation due to cost budget exhaustion (§3.2.3).
    CostBudget,
    /// Cancellation due to fail-fast policy (sibling failed).
    FailFast,
    /// Cancellation due to losing a race (another branch completed first).
    RaceLost,
    /// Cancellation due to parent region being cancelled/closing.
    ParentCancelled,
    /// Cancellation due to resource unavailability (e.g., file descriptors, memory).
    ResourceUnavailable,
    /// Cancellation due to runtime shutdown.
    Shutdown,
    /// Cancellation due to a linked task's abnormal exit (Spork link propagation).
    LinkedExit,
}

// ========================================================================
// Cancellation Witnesses
// ========================================================================

/// The cancellation phase witnessed by the runtime.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum CancelPhase {
    /// Cancellation has been requested but not yet acknowledged.
    Requested,
    /// Task has acknowledged cancellation and is draining cleanup.
    Cancelling,
    /// Task is running finalizers.
    Finalizing,
    /// Task completed with a cancelled outcome.
    Completed,
}

impl CancelPhase {
    #[inline]
    fn rank(self) -> u8 {
        match self {
            Self::Requested => 0,
            Self::Cancelling => 1,
            Self::Finalizing => 2,
            Self::Completed => 3,
        }
    }
}

/// A proof-of-completion token for cancellation.
///
/// This witness is emitted by the cancellation protocol to make completion
/// verifiable and to detect inconsistent or out-of-order transitions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CancelWitness {
    /// The task associated with this cancellation.
    pub task_id: TaskId,
    /// The owning region.
    pub region_id: RegionId,
    /// Cancellation epoch (increments on first request).
    pub epoch: u64,
    /// The phase observed.
    pub phase: CancelPhase,
    /// The cancellation reason.
    pub reason: CancelReason,
}

impl CancelWitness {
    /// Creates a new cancellation witness.
    #[inline]
    #[must_use]
    pub fn new(
        task_id: TaskId,
        region_id: RegionId,
        epoch: u64,
        phase: CancelPhase,
        reason: CancelReason,
    ) -> Self {
        Self {
            task_id,
            region_id,
            epoch,
            phase,
            reason,
        }
    }

    /// Validates a transition between two witnesses.
    ///
    /// Invariants:
    /// - Same task, region, and epoch
    /// - Phase must be monotone (no regression)
    /// - Cancellation severity must not weaken
    pub fn validate_transition(prev: Option<&Self>, next: &Self) -> Result<(), CancelWitnessError> {
        let Some(prev) = prev else {
            return Ok(());
        };

        if prev.task_id != next.task_id {
            return Err(CancelWitnessError::TaskMismatch);
        }
        if prev.region_id != next.region_id {
            return Err(CancelWitnessError::RegionMismatch);
        }
        if prev.epoch != next.epoch {
            return Err(CancelWitnessError::EpochMismatch);
        }
        if next.phase.rank() < prev.phase.rank() {
            return Err(CancelWitnessError::PhaseRegression {
                from: prev.phase,
                to: next.phase,
            });
        }
        if next.reason.severity() < prev.reason.severity() {
            return Err(CancelWitnessError::ReasonWeakened {
                from: prev.reason.kind(),
                to: next.reason.kind(),
            });
        }
        Ok(())
    }
}

/// Errors when validating cancellation witnesses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CancelWitnessError {
    /// Task identifiers do not match.
    TaskMismatch,
    /// Region identifiers do not match.
    RegionMismatch,
    /// Cancellation epoch differs.
    EpochMismatch,
    /// Phase regression detected.
    PhaseRegression {
        /// Previous phase observed.
        from: CancelPhase,
        /// New phase observed.
        to: CancelPhase,
    },
    /// Cancellation severity weakened.
    ReasonWeakened {
        /// Previous cancellation kind.
        from: CancelKind,
        /// New cancellation kind.
        to: CancelKind,
    },
}

impl CancelKind {
    /// Returns the variant name as a static string (matches `Debug` output).
    #[inline]
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::User => "User",
            Self::Timeout => "Timeout",
            Self::Deadline => "Deadline",
            Self::PollQuota => "PollQuota",
            Self::CostBudget => "CostBudget",
            Self::FailFast => "FailFast",
            Self::RaceLost => "RaceLost",
            Self::ParentCancelled => "ParentCancelled",
            Self::ResourceUnavailable => "ResourceUnavailable",
            Self::Shutdown => "Shutdown",
            Self::LinkedExit => "LinkedExit",
        }
    }

    /// Returns the severity of this cancellation kind.
    ///
    /// Higher severity cancellations take precedence when strengthening.
    /// Severity groups (low to high):
    /// - 0: User (explicit, gentle)
    /// - 1: Timeout, Deadline (time-based constraints)
    /// - 2: PollQuota, CostBudget (resource budgets)
    /// - 3: FailFast, RaceLost (sibling/peer outcomes)
    /// - 4: ParentCancelled, ResourceUnavailable (structural/resource)
    /// - 5: Shutdown (system-level)
    #[inline]
    #[must_use]
    pub const fn severity(self) -> u8 {
        match self {
            Self::User => 0,
            Self::Timeout | Self::Deadline => 1,
            Self::PollQuota | Self::CostBudget => 2,
            Self::FailFast | Self::RaceLost | Self::LinkedExit => 3,
            Self::ParentCancelled | Self::ResourceUnavailable => 4,
            Self::Shutdown => 5,
        }
    }
}

impl fmt::Display for CancelKind {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::User => write!(f, "user"),
            Self::Timeout => write!(f, "timeout"),
            Self::Deadline => write!(f, "deadline"),
            Self::PollQuota => write!(f, "poll quota"),
            Self::CostBudget => write!(f, "cost budget"),
            Self::FailFast => write!(f, "fail-fast"),
            Self::RaceLost => write!(f, "race lost"),
            Self::ParentCancelled => write!(f, "parent cancelled"),
            Self::ResourceUnavailable => write!(f, "resource unavailable"),
            Self::Shutdown => write!(f, "shutdown"),
            Self::LinkedExit => write!(f, "linked exit"),
        }
    }
}

/// The reason for a cancellation, including kind, attribution, and optional context.
///
/// # Attribution
///
/// Every cancellation includes full attribution:
/// - `origin_region`: The region that initiated the cancellation
/// - `origin_task`: Optionally, the specific task that initiated it
/// - `timestamp`: When the cancellation was requested
/// - `cause`: Optional parent cause for building diagnostic chains
///
/// # Cause Chains
///
/// Cancellations can form chains when one cancellation causes another.
/// For example, a timeout might trigger a parent cancellation, which then
/// cascades to children. Use [`root_cause()`][CancelReason::root_cause] to
/// find the original cause, or iterate with [`chain()`][CancelReason::chain].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CancelReason {
    /// The kind of cancellation.
    pub kind: CancelKind,
    /// The region that initiated this cancellation.
    pub origin_region: RegionId,
    /// The task that initiated this cancellation (if any).
    pub origin_task: Option<TaskId>,
    /// When the cancellation was requested.
    pub timestamp: Time,
    /// Optional human-readable message (static for determinism).
    pub message: Option<String>,
    /// The parent cause of this cancellation (for building chains).
    pub cause: Option<Box<Self>>,
    /// True if the cause chain was truncated due to limits.
    pub truncated: bool,
    /// Depth at which truncation occurred (if truncated).
    pub truncated_at_depth: Option<usize>,
}

impl CancelReason {
    // ========================================================================
    // Constructors
    // ========================================================================

    /// Creates a new cancellation reason with the given kind and origin.
    ///
    /// This is the primary constructor that requires full attribution.
    #[inline]
    #[must_use]
    pub const fn with_origin(kind: CancelKind, origin_region: RegionId, timestamp: Time) -> Self {
        Self {
            kind,
            origin_region,
            origin_task: None,
            timestamp,
            message: None,
            cause: None,
            truncated: false,
            truncated_at_depth: None,
        }
    }

    /// Creates a new cancellation reason with minimal attribution (for testing/defaults).
    ///
    /// Uses `RegionId::testing_default()` and `Time::ZERO` for attribution.
    /// Prefer `with_origin` in production code.
    #[inline]
    #[must_use]
    pub const fn new(kind: CancelKind) -> Self {
        Self {
            kind,
            origin_region: RegionId::testing_default(),
            origin_task: None,
            timestamp: Time::ZERO,
            message: None,
            cause: None,
            truncated: false,
            truncated_at_depth: None,
        }
    }

    /// Creates a user cancellation reason with a message.
    #[inline]
    #[must_use]
    pub fn user(message: &'static str) -> Self {
        Self {
            kind: CancelKind::User,
            origin_region: RegionId::testing_default(),
            origin_task: None,
            timestamp: Time::ZERO,
            message: Some(message.to_string()),
            cause: None,
            truncated: false,
            truncated_at_depth: None,
        }
    }

    /// Creates a timeout cancellation reason.
    #[inline]
    #[must_use]
    pub const fn timeout() -> Self {
        Self::new(CancelKind::Timeout)
    }

    /// Creates a deadline cancellation reason (budget deadline exceeded).
    #[inline]
    #[must_use]
    pub const fn deadline() -> Self {
        Self::new(CancelKind::Deadline)
    }

    /// Creates a poll quota cancellation reason (budget poll quota exceeded).
    #[inline]
    #[must_use]
    pub const fn poll_quota() -> Self {
        Self::new(CancelKind::PollQuota)
    }

    /// Creates a cost budget cancellation reason (budget cost quota exceeded).
    #[inline]
    #[must_use]
    pub const fn cost_budget() -> Self {
        Self::new(CancelKind::CostBudget)
    }

    /// Creates a fail-fast cancellation reason (sibling failed).
    #[inline]
    #[must_use]
    pub const fn sibling_failed() -> Self {
        Self::new(CancelKind::FailFast)
    }

    /// Creates a fail-fast cancellation reason (alias for sibling_failed).
    ///
    /// Used when a task is cancelled because a sibling failed in a fail-fast region.
    #[inline]
    #[must_use]
    pub const fn fail_fast() -> Self {
        Self::new(CancelKind::FailFast)
    }

    /// Creates a race loser cancellation reason.
    ///
    /// Used when a task is cancelled because another task in a race completed first.
    #[inline]
    #[must_use]
    pub const fn race_loser() -> Self {
        Self::new(CancelKind::RaceLost)
    }

    /// Creates a race lost cancellation reason (alias for race_loser).
    ///
    /// Used when a task is cancelled because another task in a race completed first.
    #[inline]
    #[must_use]
    pub const fn race_lost() -> Self {
        Self::new(CancelKind::RaceLost)
    }

    /// Creates a parent-cancelled cancellation reason.
    #[inline]
    #[must_use]
    pub const fn parent_cancelled() -> Self {
        Self::new(CancelKind::ParentCancelled)
    }

    /// Creates a resource unavailable cancellation reason.
    #[inline]
    #[must_use]
    pub const fn resource_unavailable() -> Self {
        Self::new(CancelKind::ResourceUnavailable)
    }

    /// Creates a shutdown cancellation reason.
    #[inline]
    #[must_use]
    pub const fn shutdown() -> Self {
        Self::new(CancelKind::Shutdown)
    }

    /// Creates a linked-exit cancellation reason (Spork link propagation).
    #[inline]
    #[must_use]
    pub const fn linked_exit() -> Self {
        Self::new(CancelKind::LinkedExit)
    }

    // ========================================================================
    // Builder Methods
    // ========================================================================

    /// Sets the origin task for this cancellation reason.
    #[inline]
    #[must_use]
    pub const fn with_task(mut self, task: TaskId) -> Self {
        self.origin_task = Some(task);
        self
    }

    /// Sets a message for this cancellation reason.
    #[inline]
    #[must_use]
    pub fn with_message(mut self, message: &'static str) -> Self {
        self.message = Some(message.to_string());
        self
    }

    /// Sets the cause chain for this cancellation reason.
    ///
    /// This does not apply any limits to the chain depth.
    /// For production use with limits, prefer [`with_cause_limited`][Self::with_cause_limited].
    #[inline]
    #[must_use]
    pub fn with_cause(mut self, cause: Self) -> Self {
        self.cause = Some(Box::new(cause));
        self
    }

    /// Sets the cause chain with depth and memory limits.
    ///
    /// If the chain would exceed the configured limits, it is truncated
    /// and the `truncated` flag is set.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let config = CancelAttributionConfig::new(4, 1024);
    /// let reason = CancelReason::shutdown()
    ///     .with_cause_limited(deep_cause_chain, &config);
    ///
    /// if reason.truncated {
    ///     println!("Chain truncated at depth {}", reason.truncated_at_depth.unwrap());
    /// }
    /// ```
    #[must_use]
    pub fn with_cause_limited(mut self, cause: Self, config: &CancelAttributionConfig) -> Self {
        // Check if adding this cause would exceed limits.
        // Use 1 for self (not chain_depth) because self.cause will be replaced.
        let current_depth = 1_usize;
        let cause_depth = cause.chain_depth();
        let total_depth = current_depth + cause_depth;

        if total_depth > config.max_chain_depth {
            // Truncate the cause chain to fit within limits
            let allowed_cause_depth = config.max_chain_depth.saturating_sub(current_depth);
            if allowed_cause_depth == 0 {
                // No room for any cause - mark as truncated
                self.truncated = true;
                self.truncated_at_depth = Some(current_depth);
                return self;
            }
            // Truncate the cause chain
            let truncated_cause = Self::truncate_chain(cause, allowed_cause_depth);
            self.cause = Some(Box::new(truncated_cause));
            self.truncated = true;
            self.truncated_at_depth = Some(current_depth + allowed_cause_depth);
            return self;
        }

        // Check memory limit
        let estimated_memory = CancelAttributionConfig::estimated_chain_cost(total_depth);
        if estimated_memory > config.max_chain_memory {
            // Calculate how deep we can go within memory budget
            let mut allowed_depth = 0;
            while CancelAttributionConfig::estimated_chain_cost(current_depth + allowed_depth + 1)
                <= config.max_chain_memory
                && allowed_depth < cause_depth
            {
                allowed_depth += 1;
            }

            if allowed_depth == 0 {
                self.truncated = true;
                self.truncated_at_depth = Some(current_depth);
                return self;
            }

            let truncated_cause = Self::truncate_chain(cause, allowed_depth);
            self.cause = Some(Box::new(truncated_cause));
            self.truncated = true;
            self.truncated_at_depth = Some(current_depth + allowed_depth);
            return self;
        }

        // Within limits - add full cause chain
        self.cause = Some(Box::new(cause));
        self
    }

    /// Truncates a cause chain to the specified maximum depth.
    ///
    /// Returns a new `CancelReason` with at most `max_depth` levels,
    /// with the `truncated` flag set on the deepest retained level.
    fn truncate_chain(reason: Self, max_depth: usize) -> Self {
        if max_depth == 0 {
            return Self {
                cause: None,
                truncated: true,
                truncated_at_depth: Some(0),
                ..reason
            };
        }

        if max_depth == 1 || reason.cause.is_none() {
            // Keep only this level
            return Self {
                cause: None,
                truncated: reason.cause.is_some(), // Mark truncated if we removed a cause
                truncated_at_depth: if reason.cause.is_some() {
                    Some(1)
                } else {
                    reason.truncated_at_depth
                },
                ..reason
            };
        }

        // Recursively truncate the cause chain
        let truncated_cause = reason
            .cause
            .map(|boxed_cause| Box::new(Self::truncate_chain(*boxed_cause, max_depth - 1)));

        Self {
            cause: truncated_cause,
            truncated: reason.truncated,
            truncated_at_depth: reason.truncated_at_depth,
            ..reason
        }
    }

    /// Sets the timestamp for this cancellation reason.
    #[inline]
    #[must_use]
    pub const fn with_timestamp(mut self, timestamp: Time) -> Self {
        self.timestamp = timestamp;
        self
    }

    /// Sets the origin region for this cancellation reason.
    #[inline]
    #[must_use]
    pub const fn with_region(mut self, region: RegionId) -> Self {
        self.origin_region = region;
        self
    }

    // ========================================================================
    // Cause Chain Traversal
    // ========================================================================

    /// Returns an iterator over the cause chain, starting with this reason.
    ///
    /// # Example
    ///
    /// ```ignore
    /// for reason in cancel_reason.chain() {
    ///     println!("Cause: {:?}", reason.kind);
    /// }
    /// ```
    #[inline]
    #[must_use]
    pub fn chain(&self) -> CancelReasonChain<'_> {
        CancelReasonChain {
            current: Some(self),
        }
    }

    /// Returns the root cause of this cancellation (the end of the chain).
    ///
    /// If there is no cause chain, returns `self`.
    #[must_use]
    pub fn root_cause(&self) -> &Self {
        let mut current = self;
        while let Some(ref cause) = current.cause {
            current = cause;
        }
        current
    }

    /// Returns the depth of the cause chain (1 = no parent, 2 = one parent, etc.).
    #[inline]
    #[must_use]
    pub fn chain_depth(&self) -> usize {
        self.chain().count()
    }

    /// Returns true if this reason or any cause in the chain matches the given kind.
    #[must_use]
    pub fn any_cause_is(&self, kind: CancelKind) -> bool {
        self.chain().any(|r| r.kind == kind)
    }

    /// Returns true if this reason was directly or transitively caused by the given cause.
    ///
    /// Checks if `cause` appears anywhere in this reason's cause chain.
    #[must_use]
    pub fn caused_by(&self, cause: &Self) -> bool {
        self.chain().skip(1).any(|r| r == cause)
    }

    // ========================================================================
    // Kind Checks and Severity
    // ========================================================================

    /// Returns the severity level of this cancellation reason.
    ///
    /// Severity determines cancellation priority. Higher values are more severe
    /// and should override lower-severity cancellations.
    ///
    /// Severity levels:
    /// - 0: User (graceful, allows full cleanup)
    /// - 1: Timeout/Deadline (time pressure)
    /// - 2: PollQuota/CostBudget (resource exhaustion)
    /// - 3: FailFast/RaceLost (sibling events)
    /// - 4: ParentCancelled/ResourceUnavailable (external pressure)
    /// - 5: Shutdown (highest priority, minimal cleanup)
    #[inline]
    #[must_use]
    pub const fn severity(&self) -> u8 {
        self.kind.severity()
    }

    /// Returns true if this reason's kind matches the given kind.
    #[inline]
    #[must_use]
    pub const fn is_kind(&self, kind: CancelKind) -> bool {
        matches!(
            (self.kind, kind),
            (CancelKind::User, CancelKind::User)
                | (CancelKind::Timeout, CancelKind::Timeout)
                | (CancelKind::Deadline, CancelKind::Deadline)
                | (CancelKind::PollQuota, CancelKind::PollQuota)
                | (CancelKind::CostBudget, CancelKind::CostBudget)
                | (CancelKind::FailFast, CancelKind::FailFast)
                | (CancelKind::RaceLost, CancelKind::RaceLost)
                | (CancelKind::ParentCancelled, CancelKind::ParentCancelled)
                | (
                    CancelKind::ResourceUnavailable,
                    CancelKind::ResourceUnavailable
                )
                | (CancelKind::LinkedExit, CancelKind::LinkedExit)
                | (CancelKind::Shutdown, CancelKind::Shutdown)
        )
    }

    /// Returns true if this reason indicates shutdown.
    #[inline]
    #[must_use]
    pub const fn is_shutdown(&self) -> bool {
        matches!(self.kind, CancelKind::Shutdown)
    }

    /// Returns true if this is a budget-related cancellation (Deadline, PollQuota, CostBudget).
    #[inline]
    #[must_use]
    pub const fn is_budget_exceeded(&self) -> bool {
        matches!(
            self.kind,
            CancelKind::Deadline | CancelKind::PollQuota | CancelKind::CostBudget
        )
    }

    /// Returns true if this is a timeout or deadline cancellation.
    #[inline]
    #[must_use]
    pub const fn is_time_exceeded(&self) -> bool {
        matches!(self.kind, CancelKind::Timeout | CancelKind::Deadline)
    }

    // ========================================================================
    // Strengthen Operation
    // ========================================================================

    /// Strengthens this reason with another, keeping the more severe one.
    ///
    /// Implements `inv.cancel.idempotence` (#5, SEM-INV-003):
    /// `strengthen(a, b) = max_severity(a, b)`.
    ///
    /// When strengthening:
    /// - The more severe kind wins
    /// - On equal severity, the earlier timestamp wins
    /// - Messages are preserved from the winning reason
    /// - Cause chains are not merged (the winning reason's chain is kept)
    ///
    /// Returns `true` if the reason was changed.
    pub fn strengthen(&mut self, other: &Self) -> bool {
        if other.kind.severity() > self.kind.severity() {
            self.kind = other.kind;
            self.origin_region = other.origin_region;
            self.origin_task = other.origin_task;
            self.timestamp = other.timestamp;
            self.message.clone_from(&other.message);
            self.cause.clone_from(&other.cause);
            self.truncated = other.truncated;
            self.truncated_at_depth = other.truncated_at_depth;
            return true;
        }

        if other.kind.severity() < self.kind.severity() {
            return false;
        }

        // Same severity: use deterministic tie-breaking
        // Prefer earlier timestamp, then lexicographically smaller message
        if other.timestamp < self.timestamp {
            self.kind = other.kind;
            self.origin_region = other.origin_region;
            self.origin_task = other.origin_task;
            self.timestamp = other.timestamp;
            self.message.clone_from(&other.message);
            self.cause.clone_from(&other.cause);
            self.truncated = other.truncated;
            self.truncated_at_depth = other.truncated_at_depth;
            return true;
        }

        if other.timestamp > self.timestamp {
            return false;
        }

        // Same timestamp: fallback to message comparison
        let should_replace = match (&self.message, &other.message) {
            (None, Some(_)) => true,
            (Some(current), Some(candidate)) if candidate < current => true,
            _ => false,
        };
        if should_replace {
            self.kind = other.kind;
            self.origin_region = other.origin_region;
            self.origin_task = other.origin_task;
            self.timestamp = other.timestamp;
            self.message.clone_from(&other.message);
            self.cause.clone_from(&other.cause);
            self.truncated = other.truncated;
            self.truncated_at_depth = other.truncated_at_depth;
        }
        should_replace
    }

    // ========================================================================
    // Cleanup Budget
    // ========================================================================

    /// Returns the appropriate cleanup budget for this cancellation reason.
    ///
    /// Different cancellation kinds get different cleanup budgets:
    /// - **User**: Generous budget (1000 polls) for user-initiated cancellation
    /// - **Timeout/Deadline**: Moderate budget (500 polls) for time-driven cleanup
    /// - **PollQuota/CostBudget**: Tight budget (300 polls) for budget violations
    /// - **FailFast/RaceLost**: Tight budget (200 polls) for sibling failure cleanup
    /// - **ParentCancelled/ResourceUnavailable**: Tight budget (200 polls) for cascading cleanup
    /// - **Shutdown**: Minimal budget (50 polls) for urgent shutdown
    ///
    /// These budgets ensure the cancellation completeness theorem holds:
    /// tasks will reach terminal state within bounded resources.
    #[must_use]
    pub fn cleanup_budget(&self) -> Budget {
        match self.kind {
            CancelKind::User => Budget::new().with_poll_quota(1000).with_priority(200),
            CancelKind::Timeout | CancelKind::Deadline => {
                Budget::new().with_poll_quota(500).with_priority(210)
            }
            CancelKind::PollQuota | CancelKind::CostBudget => {
                Budget::new().with_poll_quota(300).with_priority(215)
            }
            CancelKind::FailFast
            | CancelKind::RaceLost
            | CancelKind::ParentCancelled
            | CancelKind::ResourceUnavailable
            | CancelKind::LinkedExit => Budget::new().with_poll_quota(200).with_priority(220),
            CancelKind::Shutdown => Budget::new().with_poll_quota(50).with_priority(255),
        }
    }

    // ========================================================================
    // Accessors
    // ========================================================================

    /// Returns the kind of this cancellation reason.
    #[inline]
    #[must_use]
    pub const fn kind(&self) -> CancelKind {
        self.kind
    }

    /// Returns the origin region of this cancellation.
    #[inline]
    #[must_use]
    pub const fn origin_region(&self) -> RegionId {
        self.origin_region
    }

    /// Returns the origin task of this cancellation (if any).
    #[inline]
    #[must_use]
    pub const fn origin_task(&self) -> Option<TaskId> {
        self.origin_task
    }

    /// Returns the timestamp when this cancellation was requested.
    #[inline]
    #[must_use]
    pub const fn timestamp(&self) -> Time {
        self.timestamp
    }

    /// Returns the message associated with this cancellation (if any).
    #[inline]
    #[must_use]
    pub fn message(&self) -> Option<&str> {
        self.message.as_deref()
    }

    /// Returns a reference to the parent cause (if any).
    #[inline]
    #[must_use]
    pub fn cause(&self) -> Option<&Self> {
        self.cause.as_deref()
    }

    /// Returns true if this reason's cause chain was truncated due to limits.
    #[inline]
    #[must_use]
    pub const fn is_truncated(&self) -> bool {
        self.truncated
    }

    /// Returns the depth at which truncation occurred (if any).
    #[inline]
    #[must_use]
    pub const fn truncated_at_depth(&self) -> Option<usize> {
        self.truncated_at_depth
    }

    /// Returns true if this reason or any cause in the chain was truncated.
    #[must_use]
    pub fn any_truncated(&self) -> bool {
        self.chain().any(|r| r.truncated)
    }

    /// Estimates the memory cost of this entire cause chain.
    #[must_use]
    pub fn estimated_memory_cost(&self) -> usize {
        CancelAttributionConfig::estimated_chain_cost(self.chain_depth())
    }
}

/// Iterator over a cancellation reason's cause chain.
pub struct CancelReasonChain<'a> {
    current: Option<&'a CancelReason>,
}

impl<'a> Iterator for CancelReasonChain<'a> {
    type Item = &'a CancelReason;

    fn next(&mut self) -> Option<Self::Item> {
        let current = self.current?;
        self.current = current.cause.as_deref();
        Some(current)
    }
}

impl Default for CancelReason {
    fn default() -> Self {
        Self::new(CancelKind::User)
    }
}

impl fmt::Display for CancelReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.kind)?;
        if let Some(msg) = &self.message {
            write!(f, ": {msg}")?;
        }
        // Include origin attribution in alternate mode
        if f.alternate() {
            write!(f, " (from {} at {})", self.origin_region, self.timestamp)?;
            if let Some(ref task) = self.origin_task {
                write!(f, " task {task}")?;
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::init_test_logging;
    use serde_json::{Value, json};

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

    fn combine(mut a: CancelReason, b: &CancelReason) -> CancelReason {
        a.strengthen(b);
        a
    }

    fn scrub_cancel_snapshot(mut value: Value) -> Value {
        fn scrub_in_place(value: &mut Value) {
            match value {
                Value::Object(map) => {
                    for (key, entry) in map.iter_mut() {
                        match key.as_str() {
                            "task_id" | "origin_task" if !entry.is_null() => {
                                *entry = Value::String("[TASK_ID]".into());
                            }
                            "region_id" | "origin_region" => {
                                *entry = Value::String("[REGION_ID]".into());
                            }
                            "timestamp" => {
                                *entry = Value::String("[TIME]".into());
                            }
                            _ => scrub_in_place(entry),
                        }
                    }
                }
                Value::Array(items) => {
                    for item in items {
                        scrub_in_place(item);
                    }
                }
                _ => {}
            }
        }

        scrub_in_place(&mut value);
        value
    }

    #[test]
    fn severity_ordering() {
        init_test("severity_ordering");
        // Test severity levels are ordered correctly
        crate::assert_with_log!(
            CancelKind::User.severity() < CancelKind::Timeout.severity(),
            "User should be below Timeout",
            true,
            CancelKind::User.severity() < CancelKind::Timeout.severity()
        );
        crate::assert_with_log!(
            CancelKind::Timeout.severity() == CancelKind::Deadline.severity(),
            "Timeout and Deadline should have same severity",
            true,
            CancelKind::Timeout.severity() == CancelKind::Deadline.severity()
        );
        crate::assert_with_log!(
            CancelKind::Deadline.severity() < CancelKind::PollQuota.severity(),
            "Deadline should be below PollQuota",
            true,
            CancelKind::Deadline.severity() < CancelKind::PollQuota.severity()
        );
        crate::assert_with_log!(
            CancelKind::PollQuota.severity() == CancelKind::CostBudget.severity(),
            "PollQuota and CostBudget should have same severity",
            true,
            CancelKind::PollQuota.severity() == CancelKind::CostBudget.severity()
        );
        crate::assert_with_log!(
            CancelKind::CostBudget.severity() < CancelKind::FailFast.severity(),
            "CostBudget should be below FailFast",
            true,
            CancelKind::CostBudget.severity() < CancelKind::FailFast.severity()
        );
        crate::assert_with_log!(
            CancelKind::FailFast.severity() == CancelKind::RaceLost.severity(),
            "FailFast and RaceLost should have same severity",
            true,
            CancelKind::FailFast.severity() == CancelKind::RaceLost.severity()
        );
        crate::assert_with_log!(
            CancelKind::RaceLost.severity() < CancelKind::ParentCancelled.severity(),
            "RaceLost should be below ParentCancelled",
            true,
            CancelKind::RaceLost.severity() < CancelKind::ParentCancelled.severity()
        );
        crate::assert_with_log!(
            CancelKind::ParentCancelled.severity() == CancelKind::ResourceUnavailable.severity(),
            "ParentCancelled and ResourceUnavailable should have same severity",
            true,
            CancelKind::ParentCancelled.severity() == CancelKind::ResourceUnavailable.severity()
        );
        crate::assert_with_log!(
            CancelKind::ParentCancelled.severity() < CancelKind::Shutdown.severity(),
            "ParentCancelled should be below Shutdown",
            true,
            CancelKind::ParentCancelled.severity() < CancelKind::Shutdown.severity()
        );
        crate::test_complete!("severity_ordering");
    }

    #[test]
    fn strengthen_takes_more_severe() {
        init_test("strengthen_takes_more_severe");
        let mut reason = CancelReason::new(CancelKind::User);
        let strengthened = reason.strengthen(&CancelReason::timeout());
        crate::assert_with_log!(
            strengthened,
            "should strengthen to Timeout",
            true,
            strengthened
        );
        crate::assert_with_log!(
            reason.kind == CancelKind::Timeout,
            "kind should be Timeout",
            CancelKind::Timeout,
            reason.kind
        );

        let strengthened_shutdown = reason.strengthen(&CancelReason::shutdown());
        crate::assert_with_log!(
            strengthened_shutdown,
            "should strengthen to Shutdown",
            true,
            strengthened_shutdown
        );
        crate::assert_with_log!(
            reason.kind == CancelKind::Shutdown,
            "kind should be Shutdown",
            CancelKind::Shutdown,
            reason.kind
        );

        // Less severe should not change.
        let unchanged = !reason.strengthen(&CancelReason::timeout());
        crate::assert_with_log!(unchanged, "less severe should not change", true, unchanged);
        crate::assert_with_log!(
            reason.kind == CancelKind::Shutdown,
            "kind should remain Shutdown",
            CancelKind::Shutdown,
            reason.kind
        );
        crate::test_complete!("strengthen_takes_more_severe");
    }

    #[test]
    fn strengthen_adopts_truncation_metadata_from_winner() {
        init_test("strengthen_adopts_truncation_metadata_from_winner");
        // Build a truncated reason (self) and a non-truncated stronger reason (other).
        let config = CancelAttributionConfig::new(2, usize::MAX);
        let deep_cause = CancelReason::timeout().with_cause(CancelReason::user("root"));
        let mut truncated_reason =
            CancelReason::user("weak").with_cause_limited(deep_cause, &config);
        crate::assert_with_log!(
            truncated_reason.truncated || truncated_reason.any_truncated(),
            "pre-strengthen reason should be truncated",
            true,
            truncated_reason.truncated || truncated_reason.any_truncated()
        );

        let non_truncated = CancelReason::shutdown();
        crate::assert_with_log!(
            !non_truncated.truncated,
            "stronger reason should not be truncated",
            false,
            non_truncated.truncated
        );

        let changed = truncated_reason.strengthen(&non_truncated);
        crate::assert_with_log!(changed, "should strengthen to Shutdown", true, changed);
        crate::assert_with_log!(
            !truncated_reason.truncated,
            "truncated flag should adopt winner's value (false)",
            false,
            truncated_reason.truncated
        );
        crate::assert_with_log!(
            truncated_reason.truncated_at_depth.is_none(),
            "truncated_at_depth should adopt winner's value (None)",
            true,
            truncated_reason.truncated_at_depth.is_none()
        );
        crate::test_complete!("strengthen_adopts_truncation_metadata_from_winner");
    }

    #[test]
    fn strengthen_is_idempotent() {
        init_test("strengthen_is_idempotent");
        let mut reason = CancelReason::timeout();
        let unchanged = !reason.strengthen(&CancelReason::timeout());
        crate::assert_with_log!(
            unchanged,
            "strengthen should be idempotent",
            true,
            unchanged
        );
        crate::assert_with_log!(
            reason.kind == CancelKind::Timeout,
            "kind should remain Timeout",
            CancelKind::Timeout,
            reason.kind
        );
        crate::test_complete!("strengthen_is_idempotent");
    }

    #[test]
    fn strengthen_is_associative() {
        init_test("strengthen_is_associative");
        let a = CancelReason::user("a");
        let b = CancelReason::timeout();
        let c = CancelReason::shutdown();

        let left = combine(combine(a.clone(), &b), &c);
        let right = {
            let bc = combine(b, &c);
            combine(a, &bc)
        };

        crate::assert_with_log!(
            left == right,
            "strengthen should be associative",
            left,
            right
        );
        crate::test_complete!("strengthen_is_associative");
    }

    #[test]
    fn strengthen_same_kind_picks_deterministic_message() {
        init_test("strengthen_same_kind_picks_deterministic_message");
        let mut reason = CancelReason::user("b");
        let changed = reason.strengthen(&CancelReason::user("a"));
        crate::assert_with_log!(
            changed,
            "same-kind strengthen should change message",
            true,
            changed
        );
        crate::assert_with_log!(
            reason.kind == CancelKind::User,
            "kind should remain User",
            CancelKind::User,
            reason.kind
        );
        crate::assert_with_log!(
            reason.message == Some("a".to_string()),
            "message should be deterministic",
            Some("a"),
            reason.message
        );
        crate::test_complete!("strengthen_same_kind_picks_deterministic_message");
    }

    #[test]
    fn strengthen_resets_message_when_kind_increases() {
        init_test("strengthen_resets_message_when_kind_increases");
        let mut reason = CancelReason::user("please stop");
        let changed = reason.strengthen(&CancelReason::shutdown());
        crate::assert_with_log!(changed, "kind increase should change reason", true, changed);
        crate::assert_with_log!(
            reason.kind == CancelKind::Shutdown,
            "kind should be Shutdown",
            CancelKind::Shutdown,
            reason.kind
        );
        crate::assert_with_log!(
            reason.message.is_none(),
            "message should reset on kind increase",
            true,
            reason.message.is_none()
        );
        crate::test_complete!("strengthen_resets_message_when_kind_increases");
    }

    #[test]
    fn cleanup_budget_scales_with_severity() {
        init_test("cleanup_budget_scales_with_severity");
        // User cancellation gets the most generous budget
        let user_budget = CancelReason::user("stop").cleanup_budget();
        crate::assert_with_log!(
            user_budget.poll_quota == 1000,
            "user budget poll_quota should be 1000",
            1000,
            user_budget.poll_quota
        );

        // Timeout gets moderate budget
        let timeout_budget = CancelReason::timeout().cleanup_budget();
        crate::assert_with_log!(
            timeout_budget.poll_quota == 500,
            "timeout budget poll_quota should be 500",
            500,
            timeout_budget.poll_quota
        );

        // Budget exhaustion (PollQuota/CostBudget) gets tight budget
        let poll_quota_budget = CancelReason::poll_quota().cleanup_budget();
        crate::assert_with_log!(
            poll_quota_budget.poll_quota == 300,
            "poll_quota budget poll_quota should be 300",
            300,
            poll_quota_budget.poll_quota
        );

        // FailFast gets tight budget
        let fail_fast_budget = CancelReason::sibling_failed().cleanup_budget();
        crate::assert_with_log!(
            fail_fast_budget.poll_quota == 200,
            "fail_fast budget poll_quota should be 200",
            200,
            fail_fast_budget.poll_quota
        );

        // Shutdown gets minimal budget with highest priority
        let shutdown_budget = CancelReason::shutdown().cleanup_budget();
        crate::assert_with_log!(
            shutdown_budget.poll_quota == 50,
            "shutdown budget poll_quota should be 50",
            50,
            shutdown_budget.poll_quota
        );
        crate::assert_with_log!(
            shutdown_budget.priority == 255,
            "shutdown budget priority should be 255",
            255,
            shutdown_budget.priority
        );

        // Priority increases with severity (cancel lane needs higher priority)
        crate::assert_with_log!(
            user_budget.priority < timeout_budget.priority,
            "user priority should be below timeout",
            true,
            user_budget.priority < timeout_budget.priority
        );
        crate::assert_with_log!(
            timeout_budget.priority < poll_quota_budget.priority,
            "timeout priority should be below poll_quota",
            true,
            timeout_budget.priority < poll_quota_budget.priority
        );
        crate::assert_with_log!(
            poll_quota_budget.priority < fail_fast_budget.priority,
            "poll_quota priority should be below fail_fast",
            true,
            poll_quota_budget.priority < fail_fast_budget.priority
        );
        crate::assert_with_log!(
            fail_fast_budget.priority < shutdown_budget.priority,
            "fail_fast priority should be below shutdown",
            true,
            fail_fast_budget.priority < shutdown_budget.priority
        );
        crate::test_complete!("cleanup_budget_scales_with_severity");
    }

    // ========================================================================
    // Bounded Cleanup Completion Tests (bd-3cq88)
    //
    // These tests verify the sufficient-budget termination property:
    // cleanup budgets have finite poll quotas, so cleanup always
    // terminates within a bounded number of polls.
    // ========================================================================

    /// Verifies that every CancelKind produces a cleanup budget with
    /// finite, positive poll quota — the precondition for bounded completion.
    #[test]
    fn cleanup_budget_always_finite_and_positive() {
        init_test("cleanup_budget_always_finite_and_positive");

        let kinds = [
            CancelKind::User,
            CancelKind::Timeout,
            CancelKind::Deadline,
            CancelKind::PollQuota,
            CancelKind::CostBudget,
            CancelKind::FailFast,
            CancelKind::RaceLost,
            CancelKind::ParentCancelled,
            CancelKind::ResourceUnavailable,
            CancelKind::Shutdown,
        ];

        for kind in kinds {
            let reason =
                CancelReason::with_origin(kind, RegionId::new_for_test(1, 0), Time::from_secs(0));
            let budget = reason.cleanup_budget();
            crate::assert_with_log!(
                budget.poll_quota > 0 && budget.poll_quota < u32::MAX,
                "cleanup budget must be finite and positive",
                true,
                budget.poll_quota
            );
        }

        crate::test_complete!("cleanup_budget_always_finite_and_positive");
    }

    /// Verifies that consuming exactly poll_quota polls exhausts the cleanup
    /// budget — the termination bound.
    #[test]
    fn cleanup_budget_terminates_after_quota_polls() {
        init_test("cleanup_budget_terminates_after_quota_polls");

        let reason = CancelReason::timeout();
        let mut budget = reason.cleanup_budget();
        let quota = budget.poll_quota;

        // Consume exactly quota polls
        for i in 0..quota {
            let result = budget.consume_poll();
            crate::assert_with_log!(
                result.is_some(),
                "poll should succeed within budget",
                true,
                i
            );
        }

        // Now exhausted
        crate::assert_with_log!(
            budget.is_exhausted(),
            "budget exhausted after quota polls",
            true,
            budget.poll_quota
        );

        // No further progress possible
        let result = budget.consume_poll();
        crate::assert_with_log!(
            result.is_none(),
            "poll fails after exhaustion",
            true,
            result.is_none()
        );

        crate::test_complete!("cleanup_budget_terminates_after_quota_polls");
    }

    /// Verifies that combining (strengthening) cleanup budgets never
    /// increases the poll quota — monotone narrowing.
    #[test]
    fn cleanup_budget_combine_never_widens() {
        init_test("cleanup_budget_combine_never_widens");

        let user_budget = CancelReason::user("stop").cleanup_budget();
        let timeout_budget = CancelReason::timeout().cleanup_budget();
        let shutdown_budget = CancelReason::shutdown().cleanup_budget();

        // Combining user + timeout takes the tighter quota
        let combined = user_budget.combine(timeout_budget);
        crate::assert_with_log!(
            combined.poll_quota <= user_budget.poll_quota,
            "combined ≤ user",
            user_budget.poll_quota,
            combined.poll_quota
        );
        crate::assert_with_log!(
            combined.poll_quota <= timeout_budget.poll_quota,
            "combined ≤ timeout",
            timeout_budget.poll_quota,
            combined.poll_quota
        );

        // Priority is max because Budget::combine always takes the maximum (most urgent) priority
        crate::assert_with_log!(
            combined.priority >= user_budget.priority,
            "combined priority >= user",
            user_budget.priority,
            combined.priority
        );

        // Combining with shutdown (tightest) always tightens
        let with_shutdown = combined.combine(shutdown_budget);
        crate::assert_with_log!(
            with_shutdown.poll_quota <= combined.poll_quota,
            "shutdown tightens further",
            combined.poll_quota,
            with_shutdown.poll_quota
        );
        crate::assert_with_log!(
            with_shutdown.priority >= combined.priority,
            "shutdown priority max",
            combined.priority,
            with_shutdown.priority
        );

        crate::test_complete!("cleanup_budget_combine_never_widens");
    }

    /// Verifies severity ordering: more severe → fewer polls, higher priority.
    #[test]
    fn cleanup_budget_severity_monotone() {
        init_test("cleanup_budget_severity_monotone");

        let user = CancelReason::user("stop");
        let timeout = CancelReason::timeout();
        let quota = CancelReason::poll_quota();
        let fail_fast = CancelReason::sibling_failed();
        let shutdown = CancelReason::shutdown();

        let budgets = [
            user.cleanup_budget(),
            timeout.cleanup_budget(),
            quota.cleanup_budget(),
            fail_fast.cleanup_budget(),
            shutdown.cleanup_budget(),
        ];

        // Poll quotas should be non-increasing (more severe → tighter)
        for i in 1..budgets.len() {
            crate::assert_with_log!(
                budgets[i].poll_quota <= budgets[i - 1].poll_quota,
                "poll quota non-increasing with severity",
                budgets[i - 1].poll_quota,
                budgets[i].poll_quota
            );
        }

        // Priorities should be non-decreasing (more severe → higher priority)
        for i in 1..budgets.len() {
            crate::assert_with_log!(
                budgets[i].priority >= budgets[i - 1].priority,
                "priority non-decreasing with severity",
                budgets[i - 1].priority,
                budgets[i].priority
            );
        }

        crate::test_complete!("cleanup_budget_severity_monotone");
    }

    /// Verifies that cleanup budgets have no deadline — cleanup should not
    /// be time-bounded, only poll-bounded, so the scheduler can always
    /// make progress regardless of clock skew.
    #[test]
    fn cleanup_budget_has_no_deadline() {
        init_test("cleanup_budget_has_no_deadline");

        let kinds = [CancelKind::User, CancelKind::Timeout, CancelKind::Shutdown];

        for kind in kinds {
            let reason =
                CancelReason::with_origin(kind, RegionId::new_for_test(1, 0), Time::from_secs(0));
            let budget = reason.cleanup_budget();
            crate::assert_with_log!(
                budget.deadline.is_none(),
                "cleanup budget should have no deadline",
                true,
                budget.deadline.is_none()
            );
        }

        crate::test_complete!("cleanup_budget_has_no_deadline");
    }

    // ========================================================================
    // Attribution Tests
    // ========================================================================

    #[test]
    fn cancel_reason_with_full_attribution() {
        init_test("cancel_reason_with_full_attribution");
        let region = RegionId::new_for_test(1, 0);
        let task = TaskId::new_for_test(2, 0);
        let timestamp = Time::from_millis(1000);

        let reason = CancelReason::with_origin(CancelKind::Timeout, region, timestamp)
            .with_task(task)
            .with_message("test timeout");

        crate::assert_with_log!(
            reason.kind == CancelKind::Timeout,
            "kind should be Timeout",
            CancelKind::Timeout,
            reason.kind
        );
        crate::assert_with_log!(
            reason.origin_region == region,
            "origin_region should match",
            region,
            reason.origin_region
        );
        crate::assert_with_log!(
            reason.origin_task == Some(task),
            "origin_task should match",
            Some(task),
            reason.origin_task
        );
        crate::assert_with_log!(
            reason.timestamp == timestamp,
            "timestamp should match",
            timestamp,
            reason.timestamp
        );
        crate::assert_with_log!(
            reason.message == Some("test timeout".to_string()),
            "message should match",
            Some("test timeout"),
            reason.message
        );
        crate::test_complete!("cancel_reason_with_full_attribution");
    }

    #[test]
    fn cancel_witness_json_snapshot_scrubbed() {
        init_test("cancel_witness_json_snapshot_scrubbed");
        let task = TaskId::new_for_test(8, 2);
        let region = RegionId::new_for_test(7, 1);
        let cause = CancelReason::with_origin(
            CancelKind::Timeout,
            RegionId::new_for_test(3, 0),
            Time::from_millis(220),
        )
        .with_task(TaskId::new_for_test(4, 0))
        .with_message("deadline budget expired");
        let reason = CancelReason::with_origin(
            CancelKind::ParentCancelled,
            RegionId::new_for_test(9, 1),
            Time::from_millis(550),
        )
        .with_task(TaskId::new_for_test(10, 0))
        .with_message("closing subtree")
        .with_cause(cause);
        let witness = CancelWitness::new(task, region, 3, CancelPhase::Finalizing, reason);

        insta::assert_json_snapshot!(
            "cancel_witness_json_scrubbed",
            scrub_cancel_snapshot(json!({
                "phase_label": format!("{:?}", witness.phase),
                "witness": witness,
            }))
        );
    }

    // ========================================================================
    // Cause Chain Tests
    // ========================================================================

    #[test]
    fn cause_chain_single() {
        init_test("cause_chain_single");
        let reason = CancelReason::timeout();

        crate::assert_with_log!(
            reason.chain_depth() == 1,
            "single reason should have depth 1",
            1,
            reason.chain_depth()
        );

        let root = reason.root_cause();
        crate::assert_with_log!(
            root == &reason,
            "root_cause of single reason should be itself",
            true,
            root == &reason
        );
        crate::test_complete!("cause_chain_single");
    }

    #[test]
    fn cause_chain_multiple() {
        init_test("cause_chain_multiple");
        let root = CancelReason::timeout().with_message("original timeout");
        let middle = CancelReason::parent_cancelled()
            .with_message("parent cancelled")
            .with_cause(root);
        let leaf = CancelReason::shutdown()
            .with_message("shutdown")
            .with_cause(middle);

        crate::assert_with_log!(
            leaf.chain_depth() == 3,
            "three-level chain should have depth 3",
            3,
            leaf.chain_depth()
        );

        let found_root = leaf.root_cause();
        crate::assert_with_log!(
            found_root.kind == CancelKind::Timeout,
            "root_cause should be Timeout",
            CancelKind::Timeout,
            found_root.kind
        );
        crate::assert_with_log!(
            found_root.message == Some("original timeout".to_string()),
            "root_cause message should match",
            Some("original timeout"),
            found_root.message
        );
        crate::test_complete!("cause_chain_multiple");
    }

    #[test]
    fn any_cause_is_works() {
        init_test("any_cause_is_works");
        let root = CancelReason::timeout();
        let leaf = CancelReason::shutdown().with_cause(root);

        crate::assert_with_log!(
            leaf.any_cause_is(CancelKind::Shutdown),
            "should find Shutdown in chain",
            true,
            leaf.any_cause_is(CancelKind::Shutdown)
        );
        crate::assert_with_log!(
            leaf.any_cause_is(CancelKind::Timeout),
            "should find Timeout in chain",
            true,
            leaf.any_cause_is(CancelKind::Timeout)
        );
        crate::assert_with_log!(
            !leaf.any_cause_is(CancelKind::User),
            "should not find User in chain",
            false,
            leaf.any_cause_is(CancelKind::User)
        );
        crate::test_complete!("any_cause_is_works");
    }

    #[test]
    fn caused_by_works() {
        init_test("caused_by_works");
        let root = CancelReason::timeout().with_message("root");
        let leaf = CancelReason::shutdown().with_cause(root.clone());

        crate::assert_with_log!(
            leaf.caused_by(&root),
            "leaf should be caused_by root",
            true,
            leaf.caused_by(&root)
        );
        crate::assert_with_log!(
            !root.caused_by(&leaf),
            "root should not be caused_by leaf",
            false,
            root.caused_by(&leaf)
        );
        crate::assert_with_log!(
            !leaf.caused_by(&leaf),
            "leaf should not be caused_by itself",
            false,
            leaf.caused_by(&leaf)
        );
        crate::test_complete!("caused_by_works");
    }

    // ========================================================================
    // Kind Check Tests
    // ========================================================================

    #[test]
    fn is_kind_works() {
        init_test("is_kind_works");
        let reason = CancelReason::poll_quota();
        crate::assert_with_log!(
            reason.is_kind(CancelKind::PollQuota),
            "is_kind should return true for matching kind",
            true,
            reason.is_kind(CancelKind::PollQuota)
        );
        crate::assert_with_log!(
            !reason.is_kind(CancelKind::Timeout),
            "is_kind should return false for non-matching kind",
            false,
            reason.is_kind(CancelKind::Timeout)
        );
        crate::test_complete!("is_kind_works");
    }

    #[test]
    fn is_budget_exceeded_works() {
        init_test("is_budget_exceeded_works");
        crate::assert_with_log!(
            CancelReason::deadline().is_budget_exceeded(),
            "Deadline should be budget_exceeded",
            true,
            CancelReason::deadline().is_budget_exceeded()
        );
        crate::assert_with_log!(
            CancelReason::poll_quota().is_budget_exceeded(),
            "PollQuota should be budget_exceeded",
            true,
            CancelReason::poll_quota().is_budget_exceeded()
        );
        crate::assert_with_log!(
            CancelReason::cost_budget().is_budget_exceeded(),
            "CostBudget should be budget_exceeded",
            true,
            CancelReason::cost_budget().is_budget_exceeded()
        );
        crate::assert_with_log!(
            !CancelReason::timeout().is_budget_exceeded(),
            "Timeout should not be budget_exceeded",
            false,
            CancelReason::timeout().is_budget_exceeded()
        );
        crate::test_complete!("is_budget_exceeded_works");
    }

    #[test]
    fn is_time_exceeded_works() {
        init_test("is_time_exceeded_works");
        crate::assert_with_log!(
            CancelReason::timeout().is_time_exceeded(),
            "Timeout should be time_exceeded",
            true,
            CancelReason::timeout().is_time_exceeded()
        );
        crate::assert_with_log!(
            CancelReason::deadline().is_time_exceeded(),
            "Deadline should be time_exceeded",
            true,
            CancelReason::deadline().is_time_exceeded()
        );
        crate::assert_with_log!(
            !CancelReason::poll_quota().is_time_exceeded(),
            "PollQuota should not be time_exceeded",
            false,
            CancelReason::poll_quota().is_time_exceeded()
        );
        crate::test_complete!("is_time_exceeded_works");
    }

    // ========================================================================
    // New Variant Tests
    // ========================================================================

    #[test]
    fn new_variants_constructors() {
        init_test("new_variants_constructors");

        let deadline = CancelReason::deadline();
        crate::assert_with_log!(
            deadline.kind == CancelKind::Deadline,
            "deadline() should create Deadline kind",
            CancelKind::Deadline,
            deadline.kind
        );

        let poll_quota = CancelReason::poll_quota();
        crate::assert_with_log!(
            poll_quota.kind == CancelKind::PollQuota,
            "poll_quota() should create PollQuota kind",
            CancelKind::PollQuota,
            poll_quota.kind
        );

        let cost_budget = CancelReason::cost_budget();
        crate::assert_with_log!(
            cost_budget.kind == CancelKind::CostBudget,
            "cost_budget() should create CostBudget kind",
            CancelKind::CostBudget,
            cost_budget.kind
        );

        let resource = CancelReason::resource_unavailable();
        crate::assert_with_log!(
            resource.kind == CancelKind::ResourceUnavailable,
            "resource_unavailable() should create ResourceUnavailable kind",
            CancelKind::ResourceUnavailable,
            resource.kind
        );

        crate::test_complete!("new_variants_constructors");
    }

    #[test]
    fn new_variants_display() {
        init_test("new_variants_display");

        crate::assert_with_log!(
            format!("{}", CancelKind::Deadline) == "deadline",
            "Deadline display should be 'deadline'",
            "deadline",
            format!("{}", CancelKind::Deadline)
        );
        crate::assert_with_log!(
            format!("{}", CancelKind::PollQuota) == "poll quota",
            "PollQuota display should be 'poll quota'",
            "poll quota",
            format!("{}", CancelKind::PollQuota)
        );
        crate::assert_with_log!(
            format!("{}", CancelKind::CostBudget) == "cost budget",
            "CostBudget display should be 'cost budget'",
            "cost budget",
            format!("{}", CancelKind::CostBudget)
        );
        crate::assert_with_log!(
            format!("{}", CancelKind::ResourceUnavailable) == "resource unavailable",
            "ResourceUnavailable display should be 'resource unavailable'",
            "resource unavailable",
            format!("{}", CancelKind::ResourceUnavailable)
        );

        crate::test_complete!("new_variants_display");
    }

    // ========================================================================
    // Chain Limit and Truncation Tests
    // ========================================================================

    #[test]
    fn cancel_attribution_config_defaults() {
        init_test("cancel_attribution_config_defaults");
        let config = CancelAttributionConfig::default();
        crate::assert_with_log!(
            config.max_chain_depth == 16,
            "default max_chain_depth should be 16",
            16,
            config.max_chain_depth
        );
        crate::assert_with_log!(
            config.max_chain_memory == 4096,
            "default max_chain_memory should be 4096",
            4096,
            config.max_chain_memory
        );
        crate::test_complete!("cancel_attribution_config_defaults");
    }

    #[test]
    fn cancel_attribution_config_custom() {
        init_test("cancel_attribution_config_custom");
        let config = CancelAttributionConfig::new(8, 2048);
        crate::assert_with_log!(
            config.max_chain_depth == 8,
            "custom max_chain_depth should be 8",
            8,
            config.max_chain_depth
        );
        crate::assert_with_log!(
            config.max_chain_memory == 2048,
            "custom max_chain_memory should be 2048",
            2048,
            config.max_chain_memory
        );
        crate::test_complete!("cancel_attribution_config_custom");
    }

    #[test]
    fn cancel_attribution_config_unlimited() {
        init_test("cancel_attribution_config_unlimited");
        let config = CancelAttributionConfig::unlimited();
        crate::assert_with_log!(
            config.max_chain_depth == usize::MAX,
            "unlimited max_chain_depth should be usize::MAX",
            usize::MAX,
            config.max_chain_depth
        );
        crate::test_complete!("cancel_attribution_config_unlimited");
    }

    #[test]
    fn chain_at_exact_limit() {
        init_test("chain_at_exact_limit");
        let config = CancelAttributionConfig::new(3, usize::MAX);

        // Build a chain of exactly 3 levels
        let level1 = CancelReason::timeout();
        let level2 = CancelReason::parent_cancelled().with_cause(level1);
        let level3 = CancelReason::shutdown().with_cause_limited(level2, &config);

        crate::assert_with_log!(
            level3.chain_depth() == 3,
            "chain at limit should have depth 3",
            3,
            level3.chain_depth()
        );
        crate::assert_with_log!(
            !level3.truncated,
            "chain at limit should not be truncated",
            false,
            level3.truncated
        );
        crate::test_complete!("chain_at_exact_limit");
    }

    #[test]
    fn chain_beyond_limit_truncates() {
        init_test("chain_beyond_limit_truncates");
        let config = CancelAttributionConfig::new(2, usize::MAX);

        // Build a chain of 3 levels, which exceeds limit of 2
        let level1 = CancelReason::timeout();
        let level2 = CancelReason::parent_cancelled().with_cause(level1);

        // This should truncate because we'd have 3 levels total
        let level3 = CancelReason::shutdown().with_cause_limited(level2, &config);

        crate::assert_with_log!(
            level3.chain_depth() <= 2,
            "chain beyond limit should be truncated to 2",
            2,
            level3.chain_depth()
        );
        crate::assert_with_log!(
            level3.truncated || level3.any_truncated(),
            "truncated chain should have truncated flag",
            true,
            level3.truncated || level3.any_truncated()
        );
        crate::test_complete!("chain_beyond_limit_truncates");
    }

    #[test]
    fn truncated_reason_new_fields() {
        init_test("truncated_reason_new_fields");
        let reason = CancelReason::timeout();

        crate::assert_with_log!(
            !reason.truncated,
            "new reason should not be truncated",
            false,
            reason.truncated
        );
        crate::assert_with_log!(
            reason.truncated_at_depth.is_none(),
            "new reason should have no truncated_at_depth",
            true,
            reason.truncated_at_depth.is_none()
        );
        crate::assert_with_log!(
            !reason.is_truncated(),
            "is_truncated() should be false",
            false,
            reason.is_truncated()
        );
        crate::test_complete!("truncated_reason_new_fields");
    }

    #[test]
    fn estimated_memory_cost() {
        init_test("estimated_memory_cost");
        let single = CancelReason::timeout();
        let cost1 = single.estimated_memory_cost();
        crate::assert_with_log!(
            cost1 > 0,
            "single reason should have positive memory cost",
            true,
            cost1 > 0
        );

        // Chain of 2 should cost more
        let chain2 = CancelReason::shutdown().with_cause(CancelReason::timeout());
        let cost2 = chain2.estimated_memory_cost();
        crate::assert_with_log!(
            cost2 > cost1,
            "chain of 2 should cost more than single",
            true,
            cost2 > cost1
        );

        crate::test_complete!("estimated_memory_cost");
    }

    #[test]
    fn memory_limit_triggers_truncation() {
        init_test("memory_limit_triggers_truncation");
        // Set a very tight memory limit that should trigger truncation
        let config = CancelAttributionConfig::new(usize::MAX, 100);

        let level1 = CancelReason::timeout();
        let level2 = CancelReason::parent_cancelled().with_cause(level1);
        let level3 = CancelReason::shutdown().with_cause_limited(level2, &config);

        // With only 100 bytes, we can't fit even 2 full levels
        // So truncation should occur
        let truncated = level3.truncated || level3.any_truncated();
        crate::assert_with_log!(
            truncated,
            "tight memory limit should trigger truncation",
            true,
            truncated
        );
        crate::test_complete!("memory_limit_triggers_truncation");
    }

    #[test]
    fn any_truncated_finds_nested_truncation() {
        init_test("any_truncated_finds_nested_truncation");
        // Manually create a chain where an inner level is truncated
        let inner = CancelReason {
            truncated: true,
            truncated_at_depth: Some(1),
            ..CancelReason::timeout()
        };
        let outer = CancelReason::shutdown().with_cause(inner);

        crate::assert_with_log!(
            !outer.truncated,
            "outer itself is not truncated",
            false,
            outer.truncated
        );
        crate::assert_with_log!(
            outer.any_truncated(),
            "any_truncated should find inner truncation",
            true,
            outer.any_truncated()
        );
        crate::test_complete!("any_truncated_finds_nested_truncation");
    }

    #[test]
    fn stress_deep_chain_bounded_by_config() {
        init_test("stress_deep_chain_bounded_by_config");
        let config = CancelAttributionConfig::new(16, 4096);
        let mut current = CancelReason::timeout();
        for _ in 1..100 {
            current = CancelReason::parent_cancelled().with_cause_limited(current, &config);
        }
        crate::assert_with_log!(
            current.chain_depth() <= 16,
            "deep chain must be bounded by max_chain_depth",
            true,
            current.chain_depth() <= 16
        );
        crate::assert_with_log!(
            current.any_truncated(),
            "deep chain must report truncation",
            true,
            current.any_truncated()
        );
        crate::test_complete!("stress_deep_chain_bounded_by_config");
    }

    #[test]
    fn stress_wide_fanout_bounded() {
        init_test("stress_wide_fanout_bounded");
        let config = CancelAttributionConfig::new(4, 4096);
        let root = CancelReason::shutdown();
        for _i in 0..200 {
            let child = CancelReason::parent_cancelled().with_cause_limited(root.clone(), &config);
            crate::assert_with_log!(
                child.chain_depth() <= 4,
                "fanout child must respect depth limit",
                true,
                child.chain_depth() <= 4
            );
        }
        crate::test_complete!("stress_wide_fanout_bounded");
    }

    #[test]
    fn zero_depth_config_drops_all_causes() {
        init_test("zero_depth_config_drops_all_causes");
        let config = CancelAttributionConfig::new(0, usize::MAX);
        let cause = CancelReason::timeout();
        let result = CancelReason::shutdown().with_cause_limited(cause, &config);
        crate::assert_with_log!(
            result.cause.is_none(),
            "zero depth config should prevent cause attachment",
            true,
            result.cause.is_none()
        );
        crate::assert_with_log!(
            result.truncated,
            "should be marked truncated when cause is dropped",
            true,
            result.truncated
        );
        crate::test_complete!("zero_depth_config_drops_all_causes");
    }

    #[test]
    fn depth_one_config_keeps_only_self() {
        init_test("depth_one_config_keeps_only_self");
        let config = CancelAttributionConfig::new(1, usize::MAX);
        let deep = CancelReason::timeout().with_cause(CancelReason::parent_cancelled());
        let result = CancelReason::shutdown().with_cause_limited(deep, &config);
        crate::assert_with_log!(
            result.chain_depth() <= 1,
            "depth-1 config should keep only the outermost level",
            true,
            result.chain_depth() <= 1
        );
        crate::test_complete!("depth_one_config_keeps_only_self");
    }

    #[test]
    fn zero_memory_config_drops_all_causes() {
        init_test("zero_memory_config_drops_all_causes");
        let config = CancelAttributionConfig::new(usize::MAX, 0);
        let cause = CancelReason::timeout();
        let result = CancelReason::shutdown().with_cause_limited(cause, &config);
        crate::assert_with_log!(
            result.truncated || result.any_truncated(),
            "zero memory should trigger truncation",
            true,
            result.truncated || result.any_truncated()
        );
        crate::test_complete!("zero_memory_config_drops_all_causes");
    }

    #[test]
    fn stress_incremental_chain_growth() {
        init_test("stress_incremental_chain_growth");
        let config = CancelAttributionConfig::default();
        let root_reason = CancelReason::shutdown();
        let mut parent_reason = root_reason;
        for _i in 0..50 {
            let child_reason =
                CancelReason::parent_cancelled().with_cause_limited(parent_reason.clone(), &config);
            crate::assert_with_log!(
                child_reason.chain_depth() <= config.max_chain_depth,
                "incremental chain must stay within configured depth",
                true,
                child_reason.chain_depth() <= config.max_chain_depth
            );
            parent_reason = child_reason;
        }
        crate::assert_with_log!(
            parent_reason.any_truncated(),
            "deeply nested region chain must report truncation",
            true,
            parent_reason.any_truncated()
        );
        crate::test_complete!("stress_incremental_chain_growth");
    }

    // ========================================================================
    // Pure data-type trait coverage (wave 25)
    // ========================================================================

    #[test]
    fn cancel_kind_debug_clone_copy() {
        let k = CancelKind::Timeout;
        let k2 = k; // Copy
        let k3 = k; // Copy again
        assert_eq!(k2, k3);
        let dbg = format!("{k:?}");
        assert!(dbg.contains("Timeout"));
    }

    #[test]
    fn cancel_kind_hash_consistency() {
        use std::collections::HashSet;
        let mut set = HashSet::new();
        set.insert(CancelKind::User);
        set.insert(CancelKind::Shutdown);
        set.insert(CancelKind::User); // dup
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn cancel_kind_as_str_all_variants() {
        assert_eq!(CancelKind::User.as_str(), "User");
        assert_eq!(CancelKind::Timeout.as_str(), "Timeout");
        assert_eq!(CancelKind::Deadline.as_str(), "Deadline");
        assert_eq!(CancelKind::PollQuota.as_str(), "PollQuota");
        assert_eq!(CancelKind::CostBudget.as_str(), "CostBudget");
        assert_eq!(CancelKind::FailFast.as_str(), "FailFast");
        assert_eq!(CancelKind::RaceLost.as_str(), "RaceLost");
        assert_eq!(CancelKind::ParentCancelled.as_str(), "ParentCancelled");
        assert_eq!(
            CancelKind::ResourceUnavailable.as_str(),
            "ResourceUnavailable"
        );
        assert_eq!(CancelKind::Shutdown.as_str(), "Shutdown");
        assert_eq!(CancelKind::LinkedExit.as_str(), "LinkedExit");
    }

    #[test]
    fn cancel_kind_display_all_variants() {
        assert_eq!(format!("{}", CancelKind::User), "user");
        assert_eq!(format!("{}", CancelKind::Timeout), "timeout");
        assert_eq!(format!("{}", CancelKind::Deadline), "deadline");
        assert_eq!(format!("{}", CancelKind::PollQuota), "poll quota");
        assert_eq!(format!("{}", CancelKind::CostBudget), "cost budget");
        assert_eq!(format!("{}", CancelKind::FailFast), "fail-fast");
        assert_eq!(format!("{}", CancelKind::RaceLost), "race lost");
        assert_eq!(
            format!("{}", CancelKind::ParentCancelled),
            "parent cancelled"
        );
        assert_eq!(
            format!("{}", CancelKind::ResourceUnavailable),
            "resource unavailable"
        );
        assert_eq!(format!("{}", CancelKind::Shutdown), "shutdown");
        assert_eq!(format!("{}", CancelKind::LinkedExit), "linked exit");
    }

    #[test]
    fn cancel_kind_ord() {
        // Ord should be consistent (derive order matches declaration order)
        assert!(CancelKind::User < CancelKind::Timeout);
        assert!(CancelKind::Shutdown > CancelKind::User);
    }

    #[test]
    fn cancel_phase_debug_clone_copy_eq() {
        let p = CancelPhase::Requested;
        let p2 = p; // Copy
        assert_eq!(p, p2);
        assert!(format!("{p:?}").contains("Requested"));
    }

    #[test]
    fn cancel_phase_ord() {
        assert!(CancelPhase::Requested < CancelPhase::Cancelling);
        assert!(CancelPhase::Cancelling < CancelPhase::Finalizing);
        assert!(CancelPhase::Finalizing < CancelPhase::Completed);
    }

    #[test]
    fn cancel_witness_error_debug_clone_copy_eq() {
        let e = CancelWitnessError::TaskMismatch;
        let e2 = e; // Copy
        assert_eq!(e, e2);
        assert!(format!("{e:?}").contains("TaskMismatch"));

        let e3 = CancelWitnessError::RegionMismatch;
        assert_ne!(e, e3);

        let e4 = CancelWitnessError::EpochMismatch;
        assert!(format!("{e4:?}").contains("EpochMismatch"));

        let e5 = CancelWitnessError::PhaseRegression {
            from: CancelPhase::Cancelling,
            to: CancelPhase::Requested,
        };
        assert!(format!("{e5:?}").contains("PhaseRegression"));

        let e6 = CancelWitnessError::ReasonWeakened {
            from: CancelKind::Shutdown,
            to: CancelKind::User,
        };
        assert!(format!("{e6:?}").contains("ReasonWeakened"));
    }

    #[test]
    fn cancel_reason_debug_clone_eq() {
        let r = CancelReason::timeout();
        let dbg = format!("{r:?}");
        assert!(dbg.contains("CancelReason"));
        let r2 = r.clone();
        assert_eq!(r, r2);
    }

    #[test]
    fn cancel_reason_default() {
        let r = CancelReason::default();
        assert_eq!(r.kind, CancelKind::User);
        assert!(r.cause.is_none());
        assert!(!r.truncated);
    }

    #[test]
    fn cancel_reason_display_normal() {
        let r = CancelReason::timeout();
        assert_eq!(format!("{r}"), "timeout");

        let r2 = CancelReason::user("custom msg");
        assert_eq!(format!("{r2}"), "user: custom msg");
    }

    #[test]
    fn cancel_reason_display_alternate() {
        let r = CancelReason::shutdown();
        let alt = format!("{r:#}");
        assert!(alt.contains("shutdown"));
        assert!(alt.contains("from"));
    }

    #[test]
    fn cancel_reason_root_cause_no_chain() {
        let r = CancelReason::timeout();
        assert_eq!(r.root_cause().kind, CancelKind::Timeout);
    }

    #[test]
    fn cancel_reason_root_cause_with_chain() {
        let root = CancelReason::shutdown();
        let child = CancelReason::parent_cancelled().with_cause(root);
        assert_eq!(child.root_cause().kind, CancelKind::Shutdown);
    }

    #[test]
    fn cancel_reason_chain_depth() {
        let r1 = CancelReason::user("a");
        assert_eq!(r1.chain_depth(), 1);

        let r2 = CancelReason::timeout().with_cause(r1);
        assert_eq!(r2.chain_depth(), 2);

        let r3 = CancelReason::shutdown().with_cause(r2);
        assert_eq!(r3.chain_depth(), 3);
    }

    #[test]
    fn cancel_reason_estimated_memory_cost() {
        let r = CancelReason::user("x");
        let cost = r.estimated_memory_cost();
        assert_eq!(cost, CancelAttributionConfig::estimated_chain_cost(1));
    }

    #[test]
    fn cancel_attribution_config_estimated_chain_cost() {
        assert_eq!(CancelAttributionConfig::estimated_chain_cost(0), 0);
        assert_eq!(
            CancelAttributionConfig::estimated_chain_cost(1),
            CancelAttributionConfig::single_reason_cost()
        );
        // depth 2: 80*2 + 8*1 = 168
        assert_eq!(CancelAttributionConfig::estimated_chain_cost(2), 168);
    }

    #[test]
    fn cancel_witness_validate_transition_ok() {
        let w1 = CancelWitness::new(
            TaskId::testing_default(),
            RegionId::testing_default(),
            1,
            CancelPhase::Requested,
            CancelReason::timeout(),
        );
        let w2 = CancelWitness::new(
            TaskId::testing_default(),
            RegionId::testing_default(),
            1,
            CancelPhase::Cancelling,
            CancelReason::timeout(),
        );
        assert!(CancelWitness::validate_transition(Some(&w1), &w2).is_ok());
    }

    #[test]
    fn cancel_witness_validate_transition_none_prev() {
        let w = CancelWitness::new(
            TaskId::testing_default(),
            RegionId::testing_default(),
            1,
            CancelPhase::Requested,
            CancelReason::timeout(),
        );
        assert!(CancelWitness::validate_transition(None, &w).is_ok());
    }

    #[test]
    fn cancel_witness_validate_phase_regression() {
        let w1 = CancelWitness::new(
            TaskId::testing_default(),
            RegionId::testing_default(),
            1,
            CancelPhase::Cancelling,
            CancelReason::timeout(),
        );
        let w2 = CancelWitness::new(
            TaskId::testing_default(),
            RegionId::testing_default(),
            1,
            CancelPhase::Requested,
            CancelReason::timeout(),
        );
        let err = CancelWitness::validate_transition(Some(&w1), &w2).unwrap_err();
        assert!(matches!(err, CancelWitnessError::PhaseRegression { .. }));
    }

    /// SEM-08.5 TEST-GAP #7: `def.cancel.reason_kinds` — canonical-5 mapping.
    ///
    /// Verifies that:
    /// 1. All 11 CancelKind variants map to severity levels in {0,1,2,3,4,5}.
    /// 2. The 5 canonical kinds (User, ParentCancelled, Timeout, Panicked, Shutdown)
    ///    are present and map to the contract-specified severity levels.
    /// 3. Extension kinds (Deadline, PollQuota, CostBudget, FailFast, RaceLost,
    ///    ResourceUnavailable, LinkedExit) each map to a valid severity level.
    #[test]
    fn canonical_5_mapping_and_extension_policy() {
        init_test("canonical_5_mapping_and_extension_policy");

        // All variants must map to {0,1,2,3,4,5}
        let all_kinds = [
            CancelKind::User,
            CancelKind::Timeout,
            CancelKind::Deadline,
            CancelKind::PollQuota,
            CancelKind::CostBudget,
            CancelKind::FailFast,
            CancelKind::RaceLost,
            CancelKind::ParentCancelled,
            CancelKind::ResourceUnavailable,
            CancelKind::Shutdown,
            CancelKind::LinkedExit,
        ];
        for kind in &all_kinds {
            let sev = kind.severity();
            assert!(
                sev <= 5,
                "CancelKind::{kind:?} has severity {sev} > 5, violating extension policy"
            );
        }

        // Canonical 5 kinds and their contract-specified severity levels
        // Per SEM-04.2 §5.1: User=0, ParentCancelled=1, Timeout=2, Panicked=4, Shutdown=5
        // Note: RT severity mapping differs from contract (RT groups by operational category).
        // The contract requires each extension maps to {0..5}; the canonical 5 anchor the scale.
        assert_eq!(CancelKind::User.severity(), 0, "User must be severity 0");
        assert_eq!(
            CancelKind::Shutdown.severity(),
            5,
            "Shutdown must be severity 5"
        );

        // Verify no duplicate severity holes — every level 0..=5 has at least one kind
        let mut covered = [false; 6];
        for kind in &all_kinds {
            covered[kind.severity() as usize] = true;
        }
        for (level, &has_kind) in covered.iter().enumerate() {
            assert!(has_kind, "Severity level {level} has no CancelKind mapping");
        }

        // Verify strengthen monotonicity: strengthening always produces >= severity
        for &a in &all_kinds {
            for &b in &all_kinds {
                let mut reason_a = CancelReason::new(a);
                let reason_b = CancelReason::new(b);
                let original_sev = reason_a.kind.severity();
                reason_a.strengthen(&reason_b);
                assert!(
                    reason_a.kind.severity() >= original_sev,
                    "strengthen({a:?}, {b:?}) decreased severity from {original_sev} to {}",
                    reason_a.kind.severity()
                );
            }
        }
    }
}