tributaries 0.1.0

Cross-platform filesystem notification: overlapping watch subscriptions are subsumed into disjoint roots, and every event is attributed back to each subscription covering it.
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
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
use std::{
  collections::{BTreeMap, HashMap, VecDeque},
  ffi::OsString,
  io,
  marker::PhantomData,
  num::NonZeroU64,
  path::{Path, PathBuf},
  time::Duration,
};

use agnostic_lite::tokio::TokioRuntime;
use tributary_proto::{ChangeId, Epoch, Location};

use super::{Owner, epoch::EpochLedger};
use crate::{
  coalesce::Coalescer,
  error::{FaultKind, SourceFault, UnwatchError, WatchError},
  event::{Event, EventKind},
  filter::Filter,
  interest::Interest,
  options::{Debounce, DebounceConfig, TributariesOptions, WatchOptions},
  source::{Armed, Source, SourceEvent, SyncToken},
  subscription::Subscription,
  subsume::Subsumer,
};

/// A path's `OsString` components — the key form the fs subsumer uses.
fn key(path: &str) -> Vec<OsString> {
  components(Path::new(path))
}

/// A path's `OsString` components.
fn components(path: &Path) -> Vec<OsString> {
  path
    .components()
    .map(|c| c.as_os_str().to_os_string())
    .collect()
}

/// One recorded call against the fake source, in the order it happened.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Call {
  Arm(PathBuf),
  Disarm(u32),
  /// An in-place root RETARGET (the gapless widen): the preserved handle and the
  /// wider key it now covers. Distinguishable from the release-and-rearm dance
  /// (a `Disarm` followed by an `Arm`) precisely because it is neither.
  Replace(u32, PathBuf),
  /// An in-place coverage PRUNE request: the root handle and the retained cover (the survivor
  /// antichain) the driver forwarded (the set-cover design v3, the shrink-in-place seam).
  SetCover(u32, Vec<Vec<OsString>>),
  /// An AWAITED in-place coverage GROW: the root handle and the fresh cover (including the newcomer)
  /// the driver awaited (the set-cover design v3). Applied-before-return, mirroring the fs source's acked
  /// `Watcher::set_cover`.
  Grow(u32, Vec<Vec<OsString>>),
}

/// One step of a scripted [`FakeSource::begin_sync`] poll: consumed one-per-poll of the
/// scripted future so a cell can force finding 1's inter-arm race deterministically under
/// manual noop-waker polling.
enum ScriptStep {
  /// The poll returns `Pending` — the write is still in flight.
  Pending,
  /// The poll SIDE-EFFECT-DELIVERS the cookie key (models the fs worker's `reply.send(Ok)`
  /// landing) yet STILL returns `Pending`: the delivered-but-unread cookie is now physical,
  /// but this `select` pass will move on to poll a later arm — the exact "the fs sent Ok
  /// between select arm 1 and arm 3 of one pass" interleave.
  PendingThenComplete,
  /// The poll resolves `Ready` with the cookie key — the ordinary completion, so a cell can
  /// pin the write-first bias winning a tie against a simultaneously-ready cancellation.
  Ready,
}

/// The hand-rolled future a scripted [`FakeSource::begin_sync`] awaits: each poll consumes one
/// [`ScriptStep`], so polling the enclosing `on_sync` future advances the write one arm-race pass
/// at a time. Borrows the source's script and its delivery sink (disjoint fields) for the life of
/// the `begin_sync` call.
struct ScriptedBegin<'a> {
  script: &'a mut VecDeque<ScriptStep>,
  delivered: &'a mut Vec<Vec<OsString>>,
  cookie_key: Vec<OsString>,
}

impl std::future::Future for ScriptedBegin<'_> {
  type Output = Vec<OsString>;

  fn poll(
    self: std::pin::Pin<&mut Self>,
    _cx: &mut std::task::Context<'_>,
  ) -> std::task::Poll<Vec<OsString>> {
    // All fields are `Unpin`, so the pin projects trivially.
    let this = self.get_mut();
    match this.script.pop_front() {
      Some(ScriptStep::Ready) => std::task::Poll::Ready(this.cookie_key.clone()),
      Some(ScriptStep::PendingThenComplete) => {
        // The write's `reply.send(Ok)` succeeds as a side effect, yet the arm still parks:
        // the physical cookie exists but this pass will poll a later, ready arm.
        this.delivered.push(this.cookie_key.clone());
        std::task::Poll::Pending
      }
      Some(ScriptStep::Pending) | None => std::task::Poll::Pending,
    }
  }
}

/// A fake [`Source`] over `u32` handles: it records every arm/disarm in order (so a test
/// can assert the widen sequence), can be told to fail the *next* arm (so a test can drive
/// the arm-failure path), can be told to return the *next* arm **dead-on-arrival** (a
/// reported-armed handle the source has already forgotten — [`Source::root_key`] is `None` —
/// so a test can drive the driver's I2 arm-choke-point liveness check), and models the
/// source's canonical-key adoption (a `retarget` diverges the reported canonical key from the
/// requested one — the design §4 TOCTOU).
///
/// **It enforces the source's disjoint-root contract** (mirroring the `tributary-fs` watcher):
/// arming a key overlapping a currently-armed fake root returns a
/// [`FaultKind::Conflict`] fault, so the widen-ordering tests validate a *real-executable*
/// sequence — a naive arm-before-unwatch would be rejected here just as the kernel watcher
/// rejects it.
struct FakeSource {
  next_handle: u32,
  calls: Vec<Call>,
  /// Each currently-live handle's arm-path — the overlap check keys on this.
  live: HashMap<u32, PathBuf>,
  /// Each live handle's fs-authoritative canonical key ([`Source::root_key`] reports it,
  /// and [`Source::arm`] returns it). `None` once the root is disarmed or killed.
  canonical: HashMap<u32, Vec<OsString>>,
  /// Whether this source supports the gapless in-place widen ([`Source::replace`]).
  /// OFF by default, so every existing cell keeps exercising the release-and-rearm
  /// path; the adoption cells turn it on.
  supports_replace: bool,
  /// Whether this source offers the sync barrier ([`Source::begin_sync`]). OFF by default (the trait
  /// default is `Unsupported`), so the cells that push a [`super::PendingSync`] directly are
  /// unaffected; a cell driving the real `on_sync`/end-to-end install path turns it on, and
  /// `begin_sync` then returns a deterministic `<dir>/cookie-<seq>` key the test can deliver.
  supports_sync: bool,
  /// Cookie keys handed to `end_sync` — the reap ledger (F5).
  ended_syncs: Vec<Vec<OsString>>,
  /// Tokens handed to `cancel_sync` — the abandon-arm ledger. Records the
  /// token an `on_sync` abandon (a caller timeout or a close) hands the source
  /// so a cell can prove a delivered-but-unread cookie is freed by TOKEN, not
  /// by the path the owner never learned.
  cancelled_syncs: Vec<SyncToken>,
  /// The token the most recent `begin_sync` was minted with — the same token an
  /// abandon then cancels, recorded so a cell can assert the cancel names EXACTLY
  /// the sync that began (the nonce is owner-random and unreconstructable).
  begun_token: Option<SyncToken>,
  /// A hand-driven `begin_sync` poll schedule (empty = the ordinary immediate
  /// `Ok(cookie_key)`). Each step drives ONE poll of the scripted future so a
  /// cell can force finding 1's inter-arm race under manual noop-waker polling.
  sync_script: VecDeque<ScriptStep>,
  /// Cookie keys the scripted `begin_sync` SIDE-EFFECT-DELIVERED (a
  /// [`ScriptStep::PendingThenComplete`] poll): the fs worker's `reply.send(Ok)`
  /// landing modeled as a physical delivery that the `select` pass never reads.
  fs_delivered: Vec<Vec<OsString>>,
  /// How many of the next `arm` calls to fail, decremented on each failed arm.
  fail_arms: u32,
  /// How many of the next `grow` calls to fail with [`WatchError::CoverageIncomplete`],
  /// decremented on each failed grow — drives the grow-before-commit failure path (ratified R1:
  /// the watch fails, the record does not broaden). A failed grow applies NO coverage change
  /// (the conservative model: the missing subtree stays missing, survivors keep theirs).
  fail_grows: u32,
  /// How many of the next `arm` calls to return **dead-on-arrival**: the arm reports success
  /// (a handle + canonical key) but the root is NOT recorded live, so [`Source::root_key`]
  /// answers `None` for its handle — modelling a root torn down between the arm request and its
  /// completion. The driver's arm-choke-point liveness check (invariant I2) must reject it.
  dead_on_arrival_arms: u32,
  /// Planned path → the divergent canonical path `arm` should report for it (the §4
  /// canonicalization TOCTOU: the source commits a different coordinate than planned).
  retarget: HashMap<PathBuf, PathBuf>,
  /// Models [`Source::canonicalize_key`]: a caller key → its canonicalization result —
  /// `Some(canonical)` re-keys it (a symlink/`..` resolving to a different coordinate), `None`
  /// rejects it (the fs source's non-existent-path case). A key ABSENT here canonicalizes to
  /// itself (identity — the already-canonical common case).
  canonicalize: HashMap<Vec<OsString>, Option<Vec<OsString>>>,
  /// Forces the next SUCCESSFUL `arm` to return this handle value instead of a freshly-minted one —
  /// modelling a source that REUSES a still-recorded handle value, a **generation-unique**
  /// [`Source::Handle`] contract VIOLATION, so a test can drive the failed-widen restore's rebind
  /// debug_assert tripwire. One-shot: consumed by the next arm that gets past the fail/overlap
  /// checks.
  reuse_next_handle: Option<u32>,
  /// Each live handle's modelled ACTUAL kernel coverage as a retained antichain (set-cover): the fake applies every [`set_cover`](Source::set_cover) IMMEDIATELY (unlike `FsSource`,
  /// which queues) so a test can assert the source's true coverage after a shrink-then-grow. A
  /// handle ABSENT here is at FULL coverage (its whole armed root — the fresh-arm default and the
  /// cancel-equivalent); a `Some(cover)` covers exactly the union of the retained prefixes'
  /// subtrees. Queried by [`actual_covers`](Self::actual_covers).
  actual_cover: HashMap<u32, Vec<Vec<OsString>>>,
  /// The handle's raw event stream, drained by [`next`](Source::next). A successful
  /// [`replace`](Source::replace) ENQUEUES one epoch-bumped full-root `Rescan` here, mirroring the
  /// real `FsSource::replace` commit — the fidelity that surfaces a stale transient-root `Rescan`
  /// left on the preserved handle by a diverging-then-rolled-back widen.
  pending_events: VecDeque<SourceEvent<OsString, u32>>,
  /// The next raw epoch a `replace`-emitted `Rescan` carries — bumped per replace so each commit's
  /// full-root Rescan is "epoch-bumped" like the real source's.
  next_replace_epoch: u64,
}

impl FakeSource {
  fn new() -> Self {
    Self {
      next_handle: 0,
      calls: Vec::new(),
      live: HashMap::new(),
      canonical: HashMap::new(),
      supports_replace: false,
      supports_sync: false,
      ended_syncs: Vec::new(),
      cancelled_syncs: Vec::new(),
      begun_token: None,
      sync_script: VecDeque::new(),
      fs_delivered: Vec::new(),
      fail_arms: 0,
      fail_grows: 0,
      dead_on_arrival_arms: 0,
      retarget: HashMap::new(),
      canonicalize: HashMap::new(),
      reuse_next_handle: None,
      actual_cover: HashMap::new(),
      pending_events: VecDeque::new(),
      next_replace_epoch: 1,
    }
  }

  /// Whether the fake's modelled ACTUAL kernel coverage for `handle` includes `key`: a
  /// handle at FULL coverage (no recorded `set_cover`) covers everything under its armed root; a
  /// narrowed handle covers exactly the union of its retained prefixes' subtrees. The regression
  /// probe: after a shrink that pruned a subtree then a grow re-issue, the previously-pruned key is
  /// covered again.
  fn actual_covers(&self, handle: u32, key: &[OsString]) -> bool {
    match self.actual_cover.get(&handle) {
      Some(cover) => cover
        .iter()
        .any(|prefix| key.starts_with(prefix.as_slice())),
      None => self
        .canonical
        .get(&handle)
        .is_some_and(|root| key.starts_with(root.as_slice())),
    }
  }

  /// Reconcile the modelled ACTUAL coverage to exactly `retained` — the shared application both
  /// [`set_cover`](Source::set_cover) (prune) and [`grow`](Source::grow) perform. A cover including
  /// the root's own key is FULL coverage (drop the narrowing entry); else it narrows to exactly the
  /// retained antichain. Both a prune and a grow reconcile to `retained` (the driver only ever sends
  /// a narrower cover to `set_cover` and a wider one to `grow`), so the model application is identical
  /// — the tests distinguish them by the recorded [`Call`].
  fn apply_cover(&mut self, handle: u32, retained: &[Vec<OsString>]) {
    let root_is_covered = self.canonical.get(&handle).is_some_and(|root| {
      retained
        .iter()
        .any(|prefix| prefix.as_slice() == root.as_slice())
    });
    if root_is_covered {
      self.actual_cover.remove(&handle);
    } else {
      self.actual_cover.insert(handle, retained.to_vec());
    }
  }

  /// Model [`Source::canonicalize_key`] re-keying `from` onto the canonical coordinate `to`
  /// (a symlink / `..` path resolving elsewhere).
  fn canonicalizes_to(&mut self, from: &str, to: &str) {
    self.canonicalize.insert(key(from), Some(key(to)));
  }

  /// Model [`Source::canonicalize_key`] REJECTING `k` (the fs source's non-existent-path case):
  /// the driver must fail the watch rather than commit an eventless key.
  fn cannot_canonicalize(&mut self, k: &str) {
    self.canonicalize.insert(key(k), None);
  }

  /// Force the next successful `arm` to return `handle` (a REUSED handle value) — drives the
  /// failed-widen restore's rebind debug_assert tripwire for a contract-violating source.
  fn reuse_next_arm_handle(&mut self, handle: u32) {
    self.reuse_next_handle = Some(handle);
  }

  /// The next `arm` call fails.
  fn fail_next_arm(&mut self) {
    self.fail_arms = 1;
  }

  /// The next `grow` call fails with [`WatchError::CoverageIncomplete`], applying no coverage
  /// change — drives the grow-before-commit failure path (R1).
  fn fail_next_grow(&mut self) {
    self.fail_grows = 1;
  }

  /// The next `arm` call returns **dead-on-arrival**: a reported-armed handle the source has
  /// already forgotten ([`Source::root_key`] answers `None` for it), driving the driver's I2
  /// liveness check at the arm choke point.
  fn dead_on_arrival_next_arm(&mut self) {
    self.dead_on_arrival_arms = 1;
  }

  /// The next `n` `arm` calls fail (each decrements the counter) — drives the failed-widen
  /// restore where the wider arm AND some re-arms fail.
  fn fail_next_arms(&mut self, n: u32) {
    self.fail_arms = n;
  }

  /// Model the canonicalization TOCTOU: an `arm(planned)` reports `fs` as the handle's
  /// canonical key, diverging from what was planned.
  fn retarget(&mut self, planned: &str, fs: &str) {
    self
      .retarget
      .insert(PathBuf::from(planned), PathBuf::from(fs));
  }

  /// Model the root dying out of band (deleted / torn down): its handle stops naming a
  /// live root, so [`Source::root_key`] answers `None` — without recording a `Disarm`
  /// (the umbrella never released it; the source did).
  fn kill_root(&mut self, handle: u32) {
    self.canonical.remove(&handle);
    self.live.remove(&handle);
  }

  fn calls(&self) -> Vec<Call> {
    self.calls.clone()
  }

  fn arm_count(&self) -> usize {
    self
      .calls
      .iter()
      .filter(|c| matches!(c, Call::Arm(_)))
      .count()
  }
}

impl Source<OsString> for FakeSource {
  type Handle = u32;

  fn canonicalize_key(&self, k: &[OsString]) -> Result<Vec<OsString>, WatchError> {
    match self.canonicalize.get(k) {
      // Re-key onto the modelled canonical coordinate.
      Some(Some(canonical)) => Ok(canonical.clone()),
      // A key the source cannot canonicalize (non-existent path): reject, don't commit-eventless.
      Some(None) => Err(WatchError::canonicalize(
        k.iter().collect::<PathBuf>().display().to_string(),
        SourceFault::new(FaultKind::NotFound)
          .with_source(io::Error::other("injected non-canonicalizable key")),
      )),
      // Absent → already canonical (identity), the common case.
      None => Ok(k.to_vec()),
    }
  }

  async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
    let path: PathBuf = key.iter().collect();
    self.calls.push(Call::Arm(path.clone()));
    if self.fail_arms > 0 {
      self.fail_arms -= 1;
      return Err(WatchError::source(
        SourceFault::new(FaultKind::Other).with_source(io::Error::other("injected arm failure")),
      ));
    }
    // The disjoint-root contract (design §4): reject a key overlapping any live root,
    // exactly as the `tributary-fs` watcher does — this forces disarm-before-arm on a widen.
    if let Some(existing) = self
      .live
      .values()
      .find(|live| path.starts_with(live) || live.starts_with(&path))
      .cloned()
    {
      return Err(WatchError::source(
        SourceFault::new(FaultKind::Conflict).with_source(io::Error::other(format!(
          "fake root {} overlaps the already-armed {}",
          path.display(),
          existing.display()
        ))),
      ));
    }
    // Mint a fresh monotonic handle, UNLESS a test forced this arm to reuse a specific value (a
    // `Source::Handle` generation-unique contract violation) to drive the restore's debug_assert.
    let handle = match self.reuse_next_handle.take() {
      Some(forced) => forced,
      None => {
        self.next_handle += 1;
        self.next_handle
      }
    };
    // The canonical key: the retarget override, else the requested key. Overlap tracks the
    // arm-path (the coordinate planned against); the retarget models a separate fs-side
    // divergence the `fs_path_preserves_plan` guard catches, not this overlap check.
    let canonical_path = self
      .retarget
      .get(&path)
      .cloned()
      .unwrap_or_else(|| path.clone());
    let canonical_key = components(&canonical_path);
    // A dead-on-arrival arm reports success but the root was torn down before it completed: do
    // NOT record it live/canonical, so [`Source::root_key`] answers `None` and the driver's I2
    // liveness check at the arm choke point must reject it (best-effort disarming the stray
    // handle). Models the fs source's `root_path == None` after a nominally-successful watch.
    if self.dead_on_arrival_arms > 0 {
      self.dead_on_arrival_arms -= 1;
      return Ok(Armed::new(handle, canonical_key));
    }
    self.canonical.insert(handle, canonical_key.clone());
    self.live.insert(handle, path);
    Ok(Armed::new(handle, canonical_key))
  }

  fn is_sync_artifact(&self, key: &[OsString]) -> bool {
    // The fake's reserved namespace: a leaf starting with `cookie-`.
    key
      .last()
      .and_then(|leaf| leaf.to_str())
      .is_some_and(|leaf| leaf.starts_with("cookie-"))
  }

  fn end_sync(&mut self, _handle: u32, cookie_key: &[OsString]) {
    self.ended_syncs.push(cookie_key.to_vec());
  }

  fn cancel_sync(&mut self, _handle: u32, token: SyncToken) {
    // The abandon-arm ledger: an `on_sync` timeout or close hands the token here, and only
    // this call can free a cookie whose delivered-but-unread write the owner never got a path
    // for. Overrides the seam's defaulted no-op.
    self.cancelled_syncs.push(token);
  }

  async fn begin_sync(
    &mut self,
    _handle: u32,
    dir_key: &[OsString],
    token: SyncToken,
  ) -> Result<Vec<OsString>, crate::error::SyncError> {
    if !self.supports_sync {
      return Err(crate::error::SyncError::Unsupported);
    }
    // The token the abandon arm will later cancel — recorded so a cell can prove the cancel
    // names EXACTLY the sync that began (the nonce is owner-random, so the token cannot be
    // reconstructed from outside).
    self.begun_token = Some(token);
    // A deterministic cookie key under the sub's directory: `<dir>/cookie-<seq>`. The seq makes it
    // predictable so a test can deliver the matching artifact event; `is_sync_artifact` (a `cookie-`
    // leaf) both suppresses that event and resolves the barrier on it.
    let mut cookie_key = dir_key.to_vec();
    cookie_key.push(OsString::from(format!("cookie-{}", token.seq())));
    // With no script this is the ordinary immediate completion. A script drives the poll
    // schedule by hand so a cell can force finding 1's inter-arm race: a poll that
    // side-effect-delivers the cookie yet returns `Pending`, letting a later ready arm win the
    // same `select` pass.
    if self.sync_script.is_empty() {
      return Ok(cookie_key);
    }
    let delivered = ScriptedBegin {
      script: &mut self.sync_script,
      delivered: &mut self.fs_delivered,
      cookie_key,
    }
    .await;
    Ok(delivered)
  }

  async fn replace(
    &mut self,
    handle: u32,
    new_key: &[OsString],
  ) -> Result<Armed<OsString, u32>, WatchError> {
    if !self.supports_replace {
      return Err(WatchError::source(SourceFault::new(FaultKind::Unsupported)));
    }
    let path: PathBuf = new_key.iter().collect();
    self.calls.push(Call::Replace(handle, path.clone()));
    // The canonical key honors the retarget override (mirroring `arm`), so a
    // test can model an fs-side canonicalization race the `fs_path_preserves_plan`
    // guard must catch — the divergent-widen rollback path.
    let canonical_path = self
      .retarget
      .get(&path)
      .cloned()
      .unwrap_or_else(|| path.clone());
    let canonical: Vec<OsString> = components(&canonical_path);
    // Make-before-break, modeled: the SAME handle now covers the (canonical)
    // key, and the old coverage is never dropped in between.
    self.live.insert(handle, canonical_path);
    self.canonical.insert(handle, canonical.clone());
    // Model the real `FsSource::replace`: a successful make-before-break commit delivers ONE
    // epoch-bumped full-root `Rescan` on the PRESERVED handle, keyed at the committed canonical root.
    // A diverging widen enqueues `Rescan(divergent)`, then its rollback enqueues `Rescan(restored)`,
    // both riding this one preserved handle — the fidelity that surfaces the stale transient-root
    // Rescan the umbrella must clamp.
    let epoch = self.next_replace_epoch;
    self.next_replace_epoch += 1;
    self.pending_events.push_back(SourceEvent::new(
      handle,
      canonical.clone(),
      EventKind::Rescan,
      Location::new(),
      Epoch::new(epoch),
      Some(ChangeId::new(NonZeroU64::MIN)),
    ));
    Ok(Armed::new(handle, canonical))
  }

  fn disarm(&mut self, handle: u32) {
    // Synchronous release request (contract clauses 1 & 3): record it and mark the handle released
    // IMMEDIATELY — drop it from `canonical`/`live`, so `root_key` answers `None` at once and a
    // subsequent arm never sees it. Immediate application trivially satisfies the
    // release-before-subsequent-arm ordering.
    self.calls.push(Call::Disarm(handle));
    self.canonical.remove(&handle);
    self.live.remove(&handle);
  }

  fn set_cover(&mut self, handle: u32, retained: &[Vec<OsString>]) {
    // Synchronous, fire-and-forget in-place coverage PRUNE request (the set-cover design v3): record the root
    // handle and the retained cover the driver forwarded, so a test can assert exactly which prunes
    // fired and in what order. The fake keeps the root live — a prune reconciles coverage BELOW a
    // root, never releases it — so `root_key` still answers, unlike `disarm`. Unlike `FsSource` (which
    // QUEUES and drains opportunistically), the fake APPLIES immediately, so `actual_covers` reflects
    // the source's true coverage right away.
    self.calls.push(Call::SetCover(handle, retained.to_vec()));
    self.apply_cover(handle, retained);
  }

  async fn grow(&mut self, handle: u32, retained: &[Vec<OsString>]) -> Result<(), WatchError> {
    // The AWAITED, applied-before-`Ok` GROW (the set-cover design v3): record the fresh cover
    // (including the newcomer) and apply it to the modelled ACTUAL coverage IMMEDIATELY — so
    // `actual_covers` reflects the newcomer the instant this returns `Ok`, mirroring the fs source's
    // fenced `Watcher::set_cover` ack. This is what lets a test observe a pruned key regain coverage
    // the moment the covered-outside watch returns, with no bridging Rescan. An injected failure
    // (`fail_next_grow`) records the ATTEMPT but applies nothing — coverage may not include the
    // retained keys (the conservative model) — and reports the fs binding's degraded-fence error,
    // driving the grow-before-commit abort (R1).
    self.calls.push(Call::Grow(handle, retained.to_vec()));
    if self.fail_grows > 0 {
      self.fail_grows -= 1;
      return Err(WatchError::CoverageIncomplete);
    }
    self.apply_cover(handle, retained);
    Ok(())
  }

  async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
    // Drain the handle's raw event stream (a `replace`-emitted full-root `Rescan`), or `None` once
    // empty — the source-drained signal every existing cell already relied on.
    self.pending_events.pop_front()
  }

  fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
    self.canonical.get(&handle).cloned()
  }
}

/// Builds a `Rescan` [`SourceEvent`] for `handle` at `path` with raw fs `epoch` — the terminal /
/// overflow coverage-loss signal `retire_if_dead` classifies via [`Source::root_key`]. The epoch is
/// the source's raw stamp (rebased at fan-out); it is irrelevant on the retire path (which mints its
/// own `shed_rescan`) and load-bearing only when the fanned `Rescan` overflows and parks at its own
/// stamped epoch.
fn rescan_event(handle: u32, path: &str, epoch: u64) -> SourceEvent<OsString, u32> {
  SourceEvent::new(
    handle,
    key(path),
    EventKind::Rescan,
    Location::new(),
    Epoch::new(epoch),
    Some(ChangeId::new(NonZeroU64::MIN)),
  )
}

/// A synthetic `Modified` [`Event`] for `sub` at `path`, pre-stamped umbrella epoch
/// `epoch` — the ready-to-deliver event the backpressure tests feed straight into
/// [`Owner::try_emit`], the funnel that fills the bounded channel and sheds on overflow.
fn modified_event(sub: Subscription, path: &str, epoch: u64) -> Event<OsString, ()> {
  Event::synthetic(
    sub,
    key(path),
    Location::new(),
    EventKind::Modified,
    Epoch::new(epoch),
  )
}

/// A raw `Modified` [`SourceEvent`] for `handle` at `path` — the cancel-safety test's queued
/// changes, distinguished by key.
fn source_modified(handle: u32, path: &str, epoch: u64) -> SourceEvent<OsString, u32> {
  SourceEvent::new(
    handle,
    key(path),
    EventKind::Modified,
    Location::new(),
    Epoch::new(epoch),
    Some(ChangeId::new(NonZeroU64::MIN)),
  )
}

/// A raw `Removed` [`SourceEvent`] for `handle` at `path` — the user-visible NON-`Rescan`
/// terminal event the fs layer can surface for a watched-root deletion before its terminal
/// `Rescan`, which `retire_if_dead` must also retire a dead root on.
fn source_removed(handle: u32, path: &str, epoch: u64) -> SourceEvent<OsString, u32> {
  SourceEvent::new(
    handle,
    key(path),
    EventKind::Removed,
    Location::new(),
    Epoch::new(epoch),
    Some(ChangeId::new(NonZeroU64::MIN)),
  )
}

/// A raw `Created` [`SourceEvent`] for `handle` at `path`.
fn source_created(handle: u32, path: &str, epoch: u64) -> SourceEvent<OsString, u32> {
  SourceEvent::new(
    handle,
    key(path),
    EventKind::Created,
    Location::new(),
    Epoch::new(epoch),
    Some(ChangeId::new(NonZeroU64::MIN)),
  )
}

/// A raw whole-`Moved` [`SourceEvent`] on `handle` from `from` to `to` — fan-out
/// projects it per subscriber coverage (design §5), so one raw move drives both the
/// whole-move interest gate and the single-endpoint projections.
fn source_moved(handle: u32, from: &str, to: &str, epoch: u64) -> SourceEvent<OsString, u32> {
  SourceEvent::new(
    handle,
    key(to),
    EventKind::Moved { from: key(from) },
    Location::new(),
    Epoch::new(epoch),
    Some(ChangeId::new(NonZeroU64::MIN)),
  )
}

/// Drives the owner's reconcile primitives over a [`FakeSource`], with the owner's event
/// stream drained on demand — the sans-I/O reconcile logic exercised without a real
/// filesystem, runtime timers, or the select loop.
struct Harness {
  owner: Owner<OsString, (), TokioRuntime, FakeSource>,
  events: async_channel::Receiver<Event<OsString, ()>>,
  /// Kept alive so the owner's command receiver never observes a closed channel (the loop
  /// is not run here; reconcile is driven directly).
  _commands: async_channel::Sender<super::Command<OsString, ()>>,
  /// Kept alive so the owner's sync-admission receiver never observes a closed channel (the loop
  /// is not run here; the sync primitives are driven directly).
  _sync_commands: async_channel::Sender<super::SyncRequest>,
  /// The dedicated close signal's sender: kept alive so the owner's close receiver
  /// never observes a closed channel, and used by the close-under-teardown tests to inject a close
  /// exactly as `Tributaries::close` does — `try_send(reply)` on this channel, never a command.
  closes: async_channel::Sender<super::CloseReply>,
}

impl Harness {
  fn new() -> Self {
    Self::with_coalescer(None)
  }

  fn with_coalescer(coalescer: Option<Coalescer<OsString, ()>>) -> Self {
    Self::build(coalescer, None)
  }

  /// A harness whose owner→consumer event channel is **bounded** at `capacity` — for the
  /// backpressure tests, where a stalled consumer fills the channel and the owner sheds the
  /// affected subscription to a parked dominating `Rescan` (design backpressure doc).
  fn bounded(capacity: usize) -> Self {
    Self::build(None, Some(capacity))
  }

  fn build(coalescer: Option<Coalescer<OsString, ()>>, capacity: Option<usize>) -> Self {
    let (event_tx, event_rx) = match capacity {
      Some(cap) => async_channel::bounded(cap),
      None => async_channel::unbounded(),
    };
    let (command_tx, command_rx) = async_channel::unbounded();
    let (sync_command_tx, sync_command_rx) = async_channel::unbounded::<super::SyncRequest>();
    let (close_tx, close_rx) = async_channel::bounded(1);
    let (cleanup_tx, cleanup_rx) = async_channel::unbounded();
    let owner = Owner {
      source: FakeSource::new(),
      subsumer: Subsumer::new(),
      epochs: EpochLedger::new(),
      filters: HashMap::new(),
      needs_rescan: BTreeMap::new(),
      suppressed_rescan: BTreeMap::new(),
      unclaimed: std::collections::HashSet::new(),
      flush_cursor: None,
      #[cfg(test)]
      last_flush_visited: 0,
      #[cfg(test)]
      test_pre_cut_claims: Vec::new(),
      debounce: None,
      coalescer,
      pending_syncs: Vec::new(),
      sync_seq: 0,
      sync_nonce_seed: std::collections::hash_map::RandomState::new(),
      loss_serial: HashMap::new(),
      loss_gen: std::sync::Arc::new(core::sync::atomic::AtomicU64::new(0)),
      cleanup_tx,
      cleanup_rx,
      commands: command_rx,
      sync_commands: sync_command_rx,
      closes: close_rx,
      events: event_tx,
      #[cfg(debug_assertions)]
      observed_handles: std::collections::HashSet::new(),
      _rt: PhantomData::<TokioRuntime>,
    };
    Self {
      owner,
      events: event_rx,
      _commands: command_tx,
      _sync_commands: sync_command_tx,
      closes: close_tx,
    }
  }

  async fn watch(&mut self, path: &str, interest: Interest) -> Result<Subscription, WatchError> {
    self
      .watch_with(path, WatchOptions::new().with_interest(interest))
      .await
  }

  /// [`watch`](Self::watch) with the full per-watch options — for the tests exercising
  /// a custom filter or a per-subscription debounce posture.
  async fn watch_with(
    &mut self,
    path: &str,
    options: WatchOptions<OsString>,
  ) -> Result<Subscription, WatchError> {
    self
      .owner
      .reconcile_watch(&key(path), (), options)
      .await
      .map_err(|stop| match stop {
        super::ReconcileStop::Failed(err) => err,
        // These harness owners are driven directly — nothing ever sends on their close signal — so
        // the in-place widen's close race cannot fire here.
        super::ReconcileStop::CloseRequested(_) => {
          unreachable!("no close is sent to a directly-driven harness owner")
        }
      })
  }

  fn unwatch(&mut self, sub: Subscription) -> Result<(), UnwatchError> {
    self.owner.release_subscription(sub)
  }

  /// Every event the owner has pushed to its stream so far (Rescans, coalescer output).
  fn drain(&self) -> Vec<Event<OsString, ()>> {
    let mut out = Vec::new();
    while let Ok(event) = self.events.try_recv() {
      out.push(event);
    }
    out
  }
}

#[tokio::test]
async fn overlapping_watch_issues_one_arm() {
  let mut h = Harness::new();

  h.watch("/a", Interest::all()).await.expect("watch /a");
  let covered = h.watch("/a/b", Interest::all()).await;
  assert!(
    covered.is_ok(),
    "an overlapping watch never surfaces Overlaps"
  );

  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "two overlapping subscriptions collapse to exactly one arm"
  );
  // Only /a is ARMED. The covered /a/b lands INSIDE the covering root's full coverage (never
  // narrowed), so it grows nothing and arms nothing — so filter to the arms.
  let arms: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Arm(_)))
    .collect();
  assert_eq!(
    arms,
    vec![Call::Arm(PathBuf::from("/a"))],
    "only the covering root /a is armed"
  );
}

/// Regression (design §3, a handle is a per-watcher capability): every
/// [`Subscription`] is branded with its owning watcher's `InstanceId`, so a handle minted by one
/// watcher can never `unwatch` another's subscription — even when their `ScopeId`s collide (each
/// owner mints scope ids independently from 1). The brand is checked BEFORE any state is mutated.
///
/// Fail-on-old: without the instance brand the two watchers' first subscriptions are equal bare
/// `ScopeId(1)`s, so `b.unwatch(a_sub)` matches B's own live subscription by scope id and wrongly
/// retires it.
#[tokio::test]
async fn a_foreign_subscription_cannot_unwatch_a_local_one_with_a_colliding_scope_id() {
  let mut a = Harness::new();
  let mut b = Harness::new();

  // Each owner mints scope ids from 1, so these two handles share the SAME `ScopeId` but carry
  // DIFFERENT per-watcher instance brands.
  let a_sub = a.watch("/x", Interest::all()).await.expect("watch on A");
  let b_sub = b.watch("/x", Interest::all()).await.expect("watch on B");
  assert_eq!(
    a_sub.id(),
    b_sub.id(),
    "the two owners minted a colliding ScopeId"
  );
  assert_ne!(
    a_sub, b_sub,
    "…but the per-watcher instance brand makes them distinct handles"
  );

  // B rejects A's foreign handle BEFORE touching any state, even though its ScopeId collides with
  // B's live subscription.
  let err = b
    .unwatch(a_sub)
    .expect_err("a foreign subscription is rejected");
  assert!(
    err.is_unknown_subscription(),
    "a foreign handle is Unknown, not applied to B's colliding subscription"
  );

  // B's own subscription stayed live throughout: still watched, and still unwatchable itself.
  assert!(
    b.owner.subsumer.view().is_watched(&key("/x")),
    "B's subscription stays live after rejecting the foreign unwatch"
  );
  b.unwatch(b_sub)
    .expect("B's own subscription is still live and unwatchable");
}

/// The widen ordering (design §4), forced by the source's disjoint-root contract: the
/// wider root cannot be armed while a subsumed one is live, so the widen must **disarm the
/// subsumed roots BEFORE arming the wider root**. The brief coverage gap is closed by the
/// dominating `Rescan` each re-pointed subscription receives.
#[tokio::test]
async fn widen_prefers_the_gapless_in_place_replace_when_the_source_offers_it() {
  let mut h = Harness::new();
  h.owner.source.supports_replace = true;

  let s_narrow = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_wide = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");

  // NO disarm, NO second arm: the root was retargeted in place, so its coverage
  // was never dropped — the gap the release-and-rearm dance opens does not exist.
  assert_eq!(
    h.owner.source.calls(),
    vec![
      Call::Arm(PathBuf::from("/a/b")),
      Call::Replace(1, PathBuf::from("/a")),
    ],
    "a widen the source can do in place must never release-and-rearm"
  );

  // The root is now keyed at the WIDER path — under the SAME (preserved) handle.
  let roots: Vec<(PathBuf, u32)> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, handle)| (PathBuf::from_iter(k), handle))
    .collect();
  assert_eq!(
    roots,
    vec![(PathBuf::from("/a"), 1)],
    "one root, widened, on the preserved handle"
  );

  // The re-pointed subscriber is still rebased with its dominating Rescan: its
  // root changed, so its view must re-base even though no coverage was lost.
  let rescans: Vec<Subscription> = h
    .drain()
    .into_iter()
    .filter(|e| e.kind().is_rescan())
    .map(|e| e.subscription())
    .collect();
  assert!(
    rescans.contains(&s_narrow),
    "the re-pointed subscription is rebased onto the wider root: {rescans:?}"
  );
  let _ = s_wide;
}

/// A source that cannot widen in place (the default) keeps the old dance — the
/// adoption is a pure optimization, never a behavior change.
#[tokio::test]
async fn widen_falls_back_to_release_and_rearm_without_replace_support() {
  let mut h = Harness::new();
  // `supports_replace` is off by default.
  let _narrow = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  h.watch("/a", Interest::all()).await.expect("watch /a");

  assert_eq!(
    h.owner.source.calls(),
    vec![
      Call::Arm(PathBuf::from("/a/b")),
      Call::Disarm(1),
      Call::Arm(PathBuf::from("/a")),
    ],
    "without in-place support the widen still releases and re-arms"
  );
}

#[tokio::test]
async fn widen_disarms_subsumed_before_arming_the_wider_root() {
  let mut h = Harness::new();

  let s_narrow = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  h.watch("/a", Interest::all())
    .await
    .expect("watch /a widens (subsumed /a/b disarmed first, so the wider arm is legal)");

  assert_eq!(
    h.owner.source.calls(),
    vec![
      Call::Arm(PathBuf::from("/a/b")),
      Call::Disarm(1),
      Call::Arm(PathBuf::from("/a")),
    ],
    "disarm-subsumed precedes arm-wider on a widen (the only real-executable order)"
  );

  let roots: Vec<PathBuf> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, _)| PathBuf::from_iter(k))
    .collect();
  assert_eq!(
    roots,
    vec![PathBuf::from("/a")],
    "the widen collapses to /a"
  );

  let rescans = h.drain();
  assert_eq!(rescans.len(), 1, "one dominating Rescan per re-pointed sub");
  assert!(rescans[0].is_rescan(), "the re-point signal is a Rescan");
  assert_eq!(
    rescans[0].subscription(),
    s_narrow,
    "it is delivered to the re-pointed subscriber"
  );
  assert_eq!(
    rescans[0].path(),
    Path::new("/a"),
    "the Rescan names the widened root the consumer must re-enumerate"
  );
}

#[tokio::test]
async fn arm_failure_abandons_plan_no_pending_leak() {
  let mut h = Harness::new();

  h.owner.source.fail_next_arm();
  let result = h.watch("/a", Interest::all()).await;

  assert!(result.is_err(), "a failed arm surfaces the error");
  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "a failed arm abandons the plan — no pending reservation leaks"
  );
  h.watch("/a", Interest::all())
    .await
    .expect("watch /a after the earlier failure");
  assert_eq!(
    h.owner.source.arm_count(),
    2,
    "the retried watch arms again (the failed plan left no live root)"
  );
}

/// The STRUCTURAL close of the handle-liveness class at the ARM choke point: a fresh
/// `Disjoint` `watch` whose arm is **dead-on-arrival** — the source reports it armed but has
/// already forgotten the root ([`Source::root_key`] is `None`) — must FAIL the watch, not commit a
/// root no live source watch backs. The driver's I2 liveness validation at the single arm-and-key
/// choke point rejects it: it best-effort disarms the stray handle and surfaces
/// [`WatchError::DeadOnArrival`], leaving NO root recorded, NO `is_watched` published, and NO
/// subscription returned.
///
/// Fail-on-old: without the choke-point liveness check the dead handle is committed as a root —
/// `watch` returns `Ok`, `is_watched` is true, a root is recorded, and a subscription leaks — so
/// every assertion below flips.
#[tokio::test]
async fn disjoint_dead_on_arrival_arm_fails_no_root_committed() {
  let mut h = Harness::new();

  h.owner.source.dead_on_arrival_next_arm();
  let result = h.watch("/a", Interest::all()).await;

  let err = result.expect_err("a dead-on-arrival fresh arm fails the watch");
  assert!(
    err.is_dead_on_arrival(),
    "the failure is the dead-on-arrival arm error, got {err:?}"
  );
  // The arm happened once (minting handle 1); the choke point found it dead and best-effort
  // disarmed it — the stray handle is released, never committed.
  assert_eq!(
    h.owner.source.calls(),
    vec![Call::Arm(PathBuf::from("/a")), Call::Disarm(1)],
    "the dead-on-arrival handle is best-effort disarmed, not committed"
  );
  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "the failed arm abandons the plan — no pending reservation leaks"
  );
  assert!(
    !h.owner.subsumer.view().is_watched(&key("/a")),
    "NO root is committed for a dead-on-arrival arm (fail-on-old: is_watched true)"
  );
  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "no root is recorded (fail-on-old: the dead handle is committed as a root)"
  );
  // The view is clean, so a retry arms afresh — the dead-on-arrival left no live root behind.
  h.watch("/a", Interest::all())
    .await
    .expect("watch /a after the dead-on-arrival failure");
  assert_eq!(
    h.owner.source.arm_count(),
    2,
    "the retried watch arms again (the dead-on-arrival plan left no live root)"
  );
}

#[tokio::test]
async fn widen_emits_dominating_rescan_per_repointed_sub() {
  let mut h = Harness::new();

  let sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let sc = h.watch("/a/c", Interest::all()).await.expect("watch /a/c");
  h.owner.epochs.stamp(sb, Epoch::new(4));
  h.owner.epochs.stamp(sc, Epoch::new(2));

  h.watch("/a", Interest::all())
    .await
    .expect("watch /a widens");

  let rescans = h.drain();
  assert_eq!(rescans.len(), 2, "one Rescan per re-pointed subscription");
  for ev in &rescans {
    assert!(ev.is_rescan(), "the synthetic event is a Rescan");
    assert_eq!(ev.path(), Path::new("/a"), "it names the widened root");
  }
  let by_sub: HashMap<Subscription, Epoch> = rescans
    .iter()
    .map(|ev| (ev.subscription(), ev.epoch()))
    .collect();
  assert_eq!(
    by_sub.get(&sb).copied(),
    Some(Epoch::new(5)),
    "sb's Rescan strictly dominates its high-water of 4"
  );
  assert_eq!(
    by_sub.get(&sc).copied(),
    Some(Epoch::new(3)),
    "sc's Rescan strictly dominates its high-water of 2"
  );
}

/// set-cover shrink-in-place call-site (design §5): unwatching the widening subscription of a root that
/// widened over NESTED survivors leaves the armed root over-broad, so `release_subscription` forwards
/// EXACTLY ONE `source.set_cover` with the survivor antichain — and NO `disarm` (the root survives; shrink
/// reclaims coverage BELOW it, never releases it).
#[tokio::test]
async fn over_broad_unwatch_set_covers_root_in_place() {
  let mut h = Harness::new();

  // Nested survivors under a to-be-wide root: /a/b and its covered child /a/b/c.
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let _s_bc = h
    .watch("/a/b/c", Interest::all())
    .await
    .expect("watch /a/b/c covered");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");

  // Baseline: the widen disarmed the subsumed /a/b root and armed /a — no prune of the WIDE root
  // yet. (The covered /a/b/c landed INSIDE the /a/b root's full coverage, so it grew and pruned
  // nothing — v3 issues a cover call only when a newcomer falls OUTSIDE a narrowed cover.)
  assert!(
    !h.owner
      .source
      .calls()
      .iter()
      .any(|c| matches!(c, Call::SetCover(handle, _) if *handle == wide)),
    "no prune of the wide root before the over-broadening unwatch"
  );

  h.unwatch(s_a).expect("unwatch the widening /a");

  let shrinks: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::SetCover(handle, _) if *handle == wide))
    .collect();
  assert_eq!(
    shrinks,
    vec![Call::SetCover(wide, vec![key("/a/b")])],
    "exactly one shrink of the wide root to the nested-survivor antichain /a/b (not the raw \
     {{/a/b, /a/b/c}})"
  );
  // The over-broad root is reclaimed in place, never released: no disarm of the wide handle.
  assert!(
    !h.owner
      .source
      .calls()
      .iter()
      .any(|c| matches!(c, Call::Disarm(handle) if *handle == wide)),
    "an over-broad drop shrinks the surviving root, never disarms it"
  );
}

/// No shrink when the drop does NOT leave the root over-broad (design §5): unwatching a NARROWER
/// covered subscription leaves the root still pinned by its own equal-key subscriber, so
/// `release_subscription` forwards no shrink.
#[tokio::test]
async fn narrow_unwatch_does_not_set_cover() {
  let mut h = Harness::new();

  let _s_a = h.watch("/a", Interest::all()).await.expect("watch /a");
  let s_b = h
    .watch("/a/b", Interest::all())
    .await
    .expect("watch /a/b covered");

  // The covered /a/b landed INSIDE the /a root's full coverage (never narrowed), so it issued no
  // cover call; snapshot the prune calls so far, so we can assert the UNWATCH below adds none of its
  // own.
  let shrinks_before: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::SetCover(..)))
    .collect();

  // Drop the narrower /a/b: the root /a is still watched by its own /a subscriber — not over-broad.
  h.unwatch(s_b).expect("unwatch /a/b");

  let shrinks_after: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::SetCover(..)))
    .collect();
  assert_eq!(
    shrinks_before, shrinks_after,
    "a narrower drop widens no gap — the root stays pinned by its own /a subscriber, so the unwatch \
     fires no shrink"
  );
}

/// The orphan (`DropOrphan`) release path also shrinks (the set-cover design): a committed-but-unclaimed wide
/// watch whose caller wait was dropped funnels through the SAME `release_subscription` a caller unwatch
/// does, so an over-broad drop on THAT path forwards the shrink too. The synchronous fire-and-forget
/// shape is exactly what makes one call uniform across every release path (caller unwatch, orphan,
/// teardown) with no async-seam split.
#[tokio::test]
async fn over_broad_droporphan_also_set_covers() {
  let mut h = Harness::new();

  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");

  // Retire the wide /a subscription through the ORPHAN path, not a caller unwatch.
  h.owner.apply_cleanup(super::Cleanup::DropOrphan(s_a));

  let shrinks: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::SetCover(..)))
    .collect();
  assert_eq!(
    shrinks,
    vec![Call::SetCover(wide, vec![key("/a/b")])],
    "the orphan release shrinks the over-broad root too (uniform sync call-site)"
  );
}

/// set-cover : covered-outside commits GROW (never prune), and the cancel-equivalent grows back to FULL.
/// Wide /a over survivor /a/b: unwatching the widening /a PRUNES to {/a/b}; a later `watch /a/c`
/// (Covered-outside, arms nothing) GROWS to the FRESH {/a/b, /a/c}; and a final `watch /a` (Covered,
/// key == root, also outside the narrowed cover) grows back to FULL coverage — the cancel-equivalent —
/// and clears the record to None (a subscriber now pins the root at its own key). One prune then two
/// grows, each carrying the CURRENT fresh cover; and — every grow being AWAITED — the record stays
/// EXACT at every step (None → {/a/b} → {/a/b, /a/c} → None).
#[tokio::test]
async fn covered_outside_grows_then_repins_to_full() {
  let mut h = Harness::new();

  // Wide /a over a disjoint survivor /a/b (a widen, so /a/b is NOT a covered commit — no cover call
  // until the covered watches below).
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");

  // No cover call on the wide root yet (the widen issues none).
  assert!(
    !h.owner
      .source
      .calls()
      .iter()
      .any(|c| matches!(c, Call::SetCover(handle, _) | Call::Grow(handle, _) if *handle == wide)),
    "no cover call on the wide root before any over-broadening drop or covered-outside commit"
  );

  // (1) Unwatch the widening /a → over-broad → PRUNE to {/a/b}, record Some({/a/b}).
  h.unwatch(s_a).expect("unwatch the widening /a");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b")]),
    "the prune narrowed the record to {{/a/b}}"
  );
  // (2) watch /a/c, Covered-OUTSIDE {/a/b} → GROW to the fresh {/a/b, /a/c}, record Some({/a/b, /a/c}).
  let _s_c = h
    .watch("/a/c", Interest::all())
    .await
    .expect("watch /a/c covered-outside");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b"), key("/a/c")]),
    "the grow broadened the record EXACTLY to {{/a/b, /a/c}} (broaden-on-return)"
  );
  // (3) watch /a again, Covered with key == root (also outside the narrowed cover) → GROW to FULL
  //     coverage (the cancel-equivalent), record None (/a now pins the root at its own key).
  let _s_a2 = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a again covered");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    None,
    "re-pinning the root at its own key grows back to FULL and clears the record"
  );

  // The prune is a Call::SetCover; both broadens are Call::Grow carrying the CURRENT fresh cover.
  let covers: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::SetCover(handle, _) | Call::Grow(handle, _) if *handle == wide))
    .collect();
  assert_eq!(
    covers,
    vec![
      Call::SetCover(wide, vec![key("/a/b")]),
      Call::Grow(wide, vec![key("/a/b"), key("/a/c")]),
      Call::Grow(wide, vec![key("/a")]),
    ],
    "one PRUNE then two GROWs, each carrying the fresh cover: the {{/a/b}} survivor drop, then the \
     grow to {{/a/b, /a/c}}, then the cancel-equivalent grow to FULL {{/a}}"
  );
  // The full-coverage repin restored actual coverage everywhere under /a.
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/c")),
    "the grow covered /a/c"
  );
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/z")),
    "the cancel-equivalent grew back to FULL coverage under /a"
  );
}

/// set-cover Covered-OUTSIDE grow, end to end at the driver: after a PRUNE narrowed a
/// wide root's ACTUAL coverage below a key, a later watch of that pruned key is `Covered` (arms
/// nothing) yet the source no longer backs it. The driver AWAITS a `Source::grow` to a fresh cover
/// that INCLUDES the newcomer, applied BEFORE the watch returns, so: (i) NO bridging Rescan is parked
/// (coverage is live on return — a new watch is "changes from now on"); (ii) the commit issued a
/// Call::Grow with the fresh cover; (iii) the source's ACTUAL coverage includes the newcomer at
/// return while the survivor never lost coverage; and the record broadens EXACTLY to the grown cover.
///
/// Fail-on-old: a deferred fire-and-forget re-issue behind an already-flushed bridge could drop the
/// write between commit and apply — the exact commit-to-apply silent loss the awaited grow closes.
#[tokio::test]
async fn covered_outside_narrowed_root_grows_before_returning() {
  let mut h = Harness::new();

  // Wide /a over a disjoint survivor /a/b, then drop the widening /a → over-broad → PRUNE narrows the
  // wide root's ACTUAL coverage to {/a/b}; /a/c is now strictly outside it (pruned at the source).
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");
  h.unwatch(s_a).expect("unwatch the widening /a");

  // Precondition — the source's actual coverage was narrowed: /a/b covered, /a/c NOT; record {/a/b}.
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/b")),
    "the retained /a/b survivor stays covered after the prune"
  );
  assert!(
    !h.owner.source.actual_covers(wide, &key("/a/c")),
    "the pruned /a/c is NOT covered before the newcomer (the narrowed source state)"
  );
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b")]),
    "the record narrowed to {{/a/b}} on the prune issue"
  );

  let grows_before = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .count();

  // Watch /a/c: Covered under the still-armed wide /a, but OUTSIDE the narrowed cover {/a/b}.
  let s_c = h
    .watch("/a/c", Interest::all())
    .await
    .expect("watch /a/c covered-outside");

  // (i) NO bridging Rescan is parked — the grow was applied before the watch returned, so nothing is
  // owed under the newcomer's key (v3: coverage is live on return, so no bridge).
  assert!(
    !h.owner.needs_rescan.contains_key(&s_c),
    "no bridge Rescan is parked for the covered-outside newcomer — coverage is live on return (v3)"
  );

  // (ii) The commit AWAITED a Call::Grow whose FRESH cover INCLUDES the newcomer.
  let grows_after: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .collect();
  assert!(
    grows_after.len() > grows_before,
    "the covered-outside commit issued a Call::Grow (the grow trigger)"
  );
  assert_eq!(
    grows_after,
    vec![Call::Grow(wide, vec![key("/a/b"), key("/a/c")])],
    "the grow carried the fresh survivor+newcomer cover {{/a/b, /a/c}}"
  );

  // (iii) The source's ACTUAL coverage now includes /a/c (grown before return), /a/b never lost it,
  // and the record broadened EXACTLY to the grown cover.
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/c")),
    "the source grew its actual coverage to include /a/c before the watch returned"
  );
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/b")),
    "the retained /a/b never lost coverage across the grow (no gap, no re-crawl)"
  );
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b"), key("/a/c")]),
    "the record broadened EXACTLY on grow-return, matching the source's live coverage"
  );
}

/// set-cover : a second Covered newcomer landing INSIDE the record broadened by an earlier grow does NOT
/// re-grow and owes NO bridge. Because the first grow is AWAITED and applied before its
/// watch returned, the record broadened EXACTLY to the source's live coverage — so a newcomer now
/// under that coverage classifies INSIDE and the source already backs it. (Contrast the v2 pessimism:
/// there the grow was a fire-and-forget re-issue with an enqueue→apply window, so the record could not
/// broaden at issuance and a second newcomer had to park its own bridge; v3 has no such window.)
#[tokio::test]
async fn second_covered_inside_the_grown_cover_does_not_regrow() {
  let mut h = Harness::new();

  // Narrow the wide /a root to {/a/b}, then GROW it back to {/a/b, /a/c} via a covered-outside /a/c.
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");
  h.unwatch(s_a).expect("unwatch the widening /a");
  let _s_c = h
    .watch("/a/c", Interest::all())
    .await
    .expect("watch /a/c covered-outside grows");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b"), key("/a/c")]),
    "the first grow broadened the record EXACTLY to {{/a/b, /a/c}}"
  );
  let grows_before = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .count();

  // A SECOND Covered newcomer INSIDE the grown cover: /a/c/x is under the retained prefix /a/c.
  let s_cx = h
    .watch("/a/c/x", Interest::all())
    .await
    .expect("watch /a/c/x covered-INSIDE");

  // It classifies INSIDE the record, so the source already backs it: NO new grow, NO bridge parked.
  assert!(
    !h.owner.needs_rescan.contains_key(&s_cx),
    "an inside-cover newcomer parks no bridge — coverage already backs it"
  );
  let grows_after = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .count();
  assert_eq!(
    grows_after, grows_before,
    "an inside-cover newcomer issues NO grow (the exact broadened record classifies it INSIDE)"
  );
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b"), key("/a/c")]),
    "the record is unchanged by an inside-cover newcomer"
  );
}

/// set-cover F2, end to end at the driver: a non-root unwatch that shrinks an
/// already-narrowed cover RE-PRUNES. After a grow broadened the wide /a root's cover to {/a/b, /a/c},
/// unwatching the non-root /a/c survivor leaves the cover reclaimable to {/a/b}, so
/// `release_subscription` issues a sync `set_cover` PRUNE with the shrunken antichain and narrows the
/// record. Fail-on-old: the old detect_shrink only fired for a departing key EQUAL to the root key, so
/// a non-root departure left the grown /a/c coverage pinned forever (a budget leak).
#[tokio::test]
async fn grow_then_unwatch_non_root_reprunes() {
  let mut h = Harness::new();

  // Narrow /a to {/a/b}, then grow to {/a/b, /a/c} via a covered-outside /a/c.
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");
  h.unwatch(s_a).expect("unwatch the widening /a");
  let s_c = h
    .watch("/a/c", Interest::all())
    .await
    .expect("watch /a/c covered-outside grows");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b"), key("/a/c")]),
    "the grow broadened the record to {{/a/b, /a/c}}"
  );
  let prunes_before = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::SetCover(handle, _) if *handle == wide))
    .count();

  // Unwatch the NON-ROOT /a/c survivor: the cover can shrink further, to {/a/b} (F2).
  h.unwatch(s_c).expect("unwatch the non-root /a/c");

  let prunes_after: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::SetCover(handle, _) if *handle == wide))
    .collect();
  assert_eq!(
    prunes_after.len(),
    prunes_before + 1,
    "the non-root unwatch issued exactly one re-prune of the wide root"
  );
  assert_eq!(
    prunes_after.last(),
    Some(&Call::SetCover(wide, vec![key("/a/b")])),
    "the re-prune carries the shrunken {{/a/b}} antichain"
  );
  // The record narrowed back to {/a/b}, and the source reclaimed /a/c's coverage in place.
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b")]),
    "the record narrowed on the F2 re-prune issue"
  );
  assert!(
    !h.owner.source.actual_covers(wide, &key("/a/c")),
    "the /a/c coverage was reclaimed by the re-prune"
  );
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/b")),
    "the retained /a/b survivor keeps coverage"
  );
  assert!(
    !h.owner
      .source
      .calls()
      .iter()
      .any(|c| matches!(c, Call::Disarm(handle) if *handle == wide)),
    "the re-prune reclaims in place, never disarms the surviving root"
  );
}

/// A live-root source `Rescan` degrades the root's NARROWED retained-cover record to the
/// empty cover: the loss signal means the recorded claim may span a hole, so a newcomer
/// that would have classified Covered-INSIDE (committing with NO grow — silently unwatched
/// over the hole) instead classifies Covered-OUTSIDE and drives a coverage-re-proving grow.
/// Fail-on-old: without the degrade, the post-`Rescan` newcomer under the retained cover
/// commits with zero `Grow` calls and its subtree stays dead until an unrelated reconcile.
#[tokio::test]
async fn source_rescan_degrades_the_retained_cover_so_newcomers_regrow() {
  let mut h = Harness::new();

  // Narrow the wide /a root to {/a/b}: widen /a over /a/b, then drop the widening /a (PRUNE).
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");
  h.unwatch(s_a).expect("unwatch the widening /a");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b")]),
    "the prune narrowed the record to {{/a/b}}"
  );
  let _ = h.drain();

  // Baseline: a newcomer INSIDE the retained cover commits with no grow.
  let _s_inside = h
    .watch("/a/b/deep", Interest::all())
    .await
    .expect("a covered-inside newcomer commits");
  assert!(
    !h.owner
      .source
      .calls()
      .iter()
      .any(|c| matches!(c, Call::Grow(handle, _) if *handle == wide)),
    "inside the retained cover no grow is needed"
  );

  // The source signals coverage loss on the LIVE root (an overflow / failed re-arm
  // Rescan): mirror the loop's live-root sequence — retire (no-op, the root is live),
  // degrade, fan out.
  let loss = rescan_event(wide, "/a/b", 1);
  h.owner.consume_source_event(&loss);
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![]),
    "the narrowed record degraded to the empty cover — it claims nothing below the root"
  );
  let _ = h.drain();

  // A newcomer that WOULD have been covered-inside now re-proves coverage via grow, and
  // the record broadens exactly to the grown cover.
  let _s2 = h
    .watch("/a/b/deeper", Interest::all())
    .await
    .expect("the post-loss newcomer commits through a re-proving grow");
  let grows: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .collect();
  assert_eq!(
    grows.len(),
    1,
    "the post-loss newcomer drove exactly one grow"
  );
  let Call::Grow(_, grown) = &grows[0] else {
    unreachable!("filtered to grows");
  };
  assert!(
    grown
      .iter()
      .any(|k| key("/a/b/deeper").starts_with(k.as_slice())),
    "the grown antichain covers the newcomer, got {grown:?}"
  );
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/b/deeper")),
    "the newcomer's subtree is genuinely covered after the grow"
  );
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(grown.clone()),
    "the record broadened exactly to the grown cover"
  );
}

/// An unwatch after the degrade must NOT resurrect the coverage claim: the shrink detector
/// re-prunes only when the survivor antichain is CONTAINED by the recorded cover, and after
/// a degrade to the empty cover no survivor is — the survivors' keys are membership, not
/// proof of coverage, and the fire-and-forget prune never establishes any. Fail-on-old:
/// containment unchecked, the sibling unwatch "shrank" the empty record to the survivor
/// antichain and recorded it as authoritative, so the follow-up newcomer classified
/// Covered-INSIDE, skipped the grow, and committed over the unproven region.
#[tokio::test]
async fn post_degrade_unwatch_keeps_the_record_degraded_until_a_grow_re_proves() {
  let mut h = Harness::new();

  // A wide /a root narrowed to {/a/b}, then grown to {/a/b, /a/c} via a covered-outside watch.
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");
  h.unwatch(s_a).expect("unwatch the widening /a");
  let s_c = h
    .watch("/a/c", Interest::all())
    .await
    .expect("watch /a/c grows the cover");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b"), key("/a/c")]),
    "the grown record names both survivors"
  );
  let _ = h.drain();

  // The source signals coverage loss; the claim degrades to the empty cover.
  let loss = rescan_event(wide, "/a", 1);
  h.owner.consume_source_event(&loss);
  assert_eq!(h.owner.subsumer.retained_cover_of(wide), Some(vec![]));
  let calls_before = h.owner.source.calls().len();

  // A sibling unwatch must neither re-prune nor resurrect the claim.
  h.unwatch(s_c).expect("unwatch the /a/c sibling");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![]),
    "the record stays degraded across the unwatch — survivors are not proof of coverage"
  );
  assert!(
    !h.owner.source.calls()[calls_before..]
      .iter()
      .any(|c| matches!(c, Call::SetCover(handle, _) if *handle == wide)),
    "no reclaim prune is issued against a degraded record"
  );

  // The next newcomer under the surviving cover still re-proves coverage via grow.
  let _s2 = h
    .watch("/a/b/x", Interest::all())
    .await
    .expect("the post-unwatch newcomer commits through a re-proving grow");
  let grows: Vec<Call> = h.owner.source.calls()[calls_before..]
    .iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .cloned()
    .collect();
  assert_eq!(grows.len(), 1, "the newcomer drove the re-proving grow");
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/b/x")),
    "the newcomer's subtree is genuinely covered after the grow"
  );
}

/// Grow-before-commit (ratified R1): a Covered-outside newcomer whose awaited [`Source::grow`]
/// FAILS fails the `watch()` with the retryable `CoverageIncomplete` — never a committed
/// subscription whose subtree has no kernel backing and no retry owner. The record is NOT broadened
/// (record-exact), nothing leaks (the not-yet-committed plan unwinds through the same `abort_watch`
/// the dead-covering-root re-plan uses), and a subsequent identical `watch()` re-issues the grow
/// and succeeds (self-healing). Fail-on-old: the pre-R1 order committed BEFORE the grow, so a
/// failed grow returned `Ok` over a coverage hole with the record broadened to coverage that does
/// not exist — and the SECOND newcomer then classified inside-cover and silently received nothing.
#[tokio::test]
async fn covered_outside_grow_failure_fails_the_watch_and_broadens_nothing() {
  let mut h = Harness::new();

  // Narrow the wide /a root to {/a/b}: widen /a over /a/b, then drop the widening /a (PRUNE).
  let _s_b = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let s_a = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");
  let wide = h
    .owner
    .subsumer
    .roots()
    .find(|(k, _)| *k == key("/a").as_slice())
    .map(|(_, handle)| handle)
    .expect("the wide /a root is live");
  h.unwatch(s_a).expect("unwatch the widening /a");
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b")]),
    "the prune narrowed the record to {{/a/b}}"
  );
  // Clear the setup's widen Rescan so the post-failure stream assert below is exact.
  let _ = h.drain();
  let filters_before = h.owner.filters.len();

  // The next grow fails: the covered-outside watch of /a/c must FAIL, not commit (R1).
  h.owner.source.fail_next_grow();
  let err = h
    .watch("/a/c", Interest::all())
    .await
    .expect_err("a failed covered-outside grow fails the watch (grow-before-commit)");
  assert!(
    err.is_coverage_incomplete(),
    "the failure is the retryable CoverageIncomplete, got {err:?}"
  );

  // The grow was ATTEMPTED, carrying the fresh survivors+newcomer cover computed BEFORE commit
  // (the explicit-newcomer parameter)...
  let grows: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .collect();
  assert_eq!(
    grows,
    vec![Call::Grow(wide, vec![key("/a/b"), key("/a/c")])],
    "the failed grow carried the fresh {{/a/b, /a/c}} cover"
  );
  // ...but the record did NOT broaden — record-exact, so the next newcomer under the pruned
  // region still classifies outside-cover...
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b")]),
    "no broaden on a failed grow — the record still names the source's true coverage"
  );
  // ...the source's actual coverage is unchanged (the fake applies nothing on a failed grow)...
  assert!(
    !h.owner.source.actual_covers(wide, &key("/a/c")),
    "the newcomer's subtree is NOT covered after the failed grow"
  );
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/b")),
    "survivor coverage is untouched by the failed grow (never moves)"
  );
  // ...and nothing leaked: no pending reservation, no published subscription, no per-sub state,
  // no parked debt, nothing delivered.
  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "the aborted plan leaks no pending reservation"
  );
  assert!(
    !h.owner.subsumer.view().is_watched(&key("/a/c")),
    "the failed watch is never published as watched"
  );
  assert_eq!(
    h.owner.filters.len(),
    filters_before,
    "no filter was registered for the never-committed subscription"
  );
  assert!(
    h.owner.needs_rescan.is_empty() && h.owner.suppressed_rescan.is_empty(),
    "no parked Rescan debt for a never-committed subscription"
  );
  assert!(
    h.drain().is_empty(),
    "the failed watch delivered nothing (existing subscribers are covered by the source's own \
     in-band Rescan, per the grow error contract — not by the umbrella)"
  );

  // Self-heal: an identical retry re-issues the grow (the record still classifies /a/c
  // outside-cover) and commits with the broadened record.
  let _s_c = h
    .watch("/a/c", Interest::all())
    .await
    .expect("the identical retry succeeds");
  let grows_after: Vec<Call> = h
    .owner
    .source
    .calls()
    .into_iter()
    .filter(|c| matches!(c, Call::Grow(handle, _) if *handle == wide))
    .collect();
  assert_eq!(
    grows_after,
    vec![
      Call::Grow(wide, vec![key("/a/b"), key("/a/c")]),
      Call::Grow(wide, vec![key("/a/b"), key("/a/c")]),
    ],
    "the retry re-issued the SAME grow — the unbroadened record classified it outside-cover again"
  );
  assert_eq!(
    h.owner.subsumer.retained_cover_of(wide),
    Some(vec![key("/a/b"), key("/a/c")]),
    "the successful retry broadened the record exactly on grow-Ok"
  );
  assert!(
    h.owner.source.actual_covers(wide, &key("/a/c")),
    "the retry's grow applied — the newcomer's subtree is live"
  );
  assert!(
    h.owner.subsumer.view().is_watched(&key("/a/c")),
    "the retry committed and published"
  );
}

/// The widen arm failure RESTORE (design driver-golden doc, invariant I3): when the wider
/// arm FAILS after the subsumed roots were disarmed, the owner
/// must **not** leave those live subscriptions bound to disarmed handles (recorded-live yet
/// never delivering again). It re-arms each disarmed root through the choke point and mints
/// a dominating Rescan per subscriber — the subs are live-and-covered again, never
/// published-watched-but-disarmed. Regression: the old code signalled one Rescan and left
/// the roots disarmed, so future changes were silently lost.
#[tokio::test]
async fn widen_arm_failure_restores_disarmed_roots() {
  let mut h = Harness::new();

  let sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let sc = h.watch("/a/c", Interest::all()).await.expect("watch /a/c");
  h.owner.epochs.stamp(sb, Epoch::new(4));
  h.owner.epochs.stamp(sc, Epoch::new(2));

  // Only the wider arm fails; the two restore re-arms succeed.
  h.owner.source.fail_next_arm();
  let result = h.watch("/a", Interest::all()).await;
  assert!(result.is_err(), "the failed wider arm surfaces the error");

  // Disarm-subsumed, wider arm fails, THEN both subsumed roots are re-armed (the restore).
  assert_eq!(
    h.owner.source.calls(),
    vec![
      Call::Arm(PathBuf::from("/a/b")),
      Call::Arm(PathBuf::from("/a/c")),
      Call::Disarm(1),
      Call::Disarm(2),
      Call::Arm(PathBuf::from("/a")),
      Call::Arm(PathBuf::from("/a/b")),
      Call::Arm(PathBuf::from("/a/c")),
    ],
    "the failed widen re-arms the disarmed subsumed roots (restore, not strand)"
  );

  // No pending reservation leaked (the newcomer's plan was aborted).
  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "the aborted widen leaks no pending reservation"
  );

  // Both subsumed subscriptions are live-and-covered again on FRESH, live handles.
  let view = h.owner.subsumer.view();
  assert!(
    view.is_watched(&key("/a/b")) && view.is_watched(&key("/a/c")),
    "the restored subscriptions read watched again"
  );
  let roots: Vec<(PathBuf, u32)> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, handle)| (PathBuf::from_iter(k), handle))
    .collect();
  assert_eq!(
    roots.iter().map(|(p, _)| p.clone()).collect::<Vec<_>>(),
    vec![PathBuf::from("/a/b"), PathBuf::from("/a/c")],
    "the two subsumed roots are back (re-armed), not collapsed and not gone"
  );
  for (path, handle) in &roots {
    assert!(
      h.owner.source.root_key(*handle).is_some(),
      "the re-armed root {path:?} is on a LIVE handle — never published-watched-but-disarmed"
    );
  }

  // Each restored subscriber got a dominating Rescan (re-enumerate onto the re-armed root).
  let by_sub: HashMap<Subscription, Epoch> = h
    .drain()
    .iter()
    .map(|ev| {
      assert!(ev.is_rescan(), "every restore signal is a Rescan");
      (ev.subscription(), ev.epoch())
    })
    .collect();
  assert_eq!(
    by_sub.get(&sb).copied(),
    Some(Epoch::new(5)),
    "sb's restore Rescan strictly dominates its high-water of 4"
  );
  assert_eq!(
    by_sub.get(&sc).copied(),
    Some(Epoch::new(3)),
    "sc's restore Rescan strictly dominates its high-water of 2"
  );
}

/// The failed-widen restore when a subsumed root is genuinely DEAD (design driver-golden
/// doc, invariant I3/I4): the wider arm fails AND one disarmed root
/// cannot be re-armed. That root is RETIRED — a dominating terminal Rescan, its per-sub
/// state freed, and it leaves the view — while the re-armable one is restored. Never a
/// sub left recorded-live-but-disarmed.
#[tokio::test]
async fn widen_arm_failure_retires_root_that_cannot_rearm() {
  let mut h = Harness::new();

  let sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let sc = h.watch("/a/c", Interest::all()).await.expect("watch /a/c");
  h.owner.epochs.stamp(sb, Epoch::new(4));
  h.owner.epochs.stamp(sc, Epoch::new(2));

  // Fail the wider arm AND the first restore re-arm (/a/b): so /a/b cannot be re-armed
  // (retired) while /a/c re-arms (restored). Restore iterates in root-key order (/a/b, /a/c).
  h.owner.source.fail_next_arms(2);
  let result = h.watch("/a", Interest::all()).await;
  assert!(result.is_err(), "the failed wider arm surfaces the error");

  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "the aborted widen leaks no pending reservation"
  );

  let view = h.owner.subsumer.view();
  // /a/c re-armed and covered; /a/b retired (removed from the view — no longer watched).
  assert!(
    view.is_watched(&key("/a/c")),
    "the re-armable subsumed root is restored and watched"
  );
  assert!(
    !view.is_watched(&key("/a/b")),
    "the dead subsumed root is RETIRED — not left published-watched-but-disarmed"
  );
  let roots: Vec<PathBuf> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, _)| PathBuf::from_iter(k))
    .collect();
  assert_eq!(
    roots,
    vec![PathBuf::from("/a/c")],
    "only the re-armed root remains; the un-re-armable one is gone"
  );
  // The retired /a/b subscriber's per-sub state is freed (I4); the restored /a/c's is kept.
  assert!(
    !h.owner.filters.contains_key(&sb),
    "the retired root's subscriber filter is freed (I4)"
  );
  assert!(
    h.owner.filters.contains_key(&sc),
    "the restored subscriber's filter is kept"
  );

  // BOTH subscribers got a dominating Rescan (sb: terminal/retire; sc: restore re-point). The
  // retired sb's terminal Rescan is durably PARKED by the shared retire primitive (before its
  // subsumer state was freed), so flush it into the stream before draining.
  h.owner.flush_pending_rescans();
  let by_sub: HashMap<Subscription, Epoch> = h
    .drain()
    .iter()
    .map(|ev| {
      assert!(ev.is_rescan(), "every loss/restore signal is a Rescan");
      (ev.subscription(), ev.epoch())
    })
    .collect();
  assert_eq!(
    by_sub.get(&sb).copied(),
    Some(Epoch::new(5)),
    "the retired sb's terminal Rescan strictly dominates its high-water of 4"
  );
  assert_eq!(
    by_sub.get(&sc).copied(),
    Some(Epoch::new(3)),
    "the restored sc's Rescan strictly dominates its high-water of 2"
  );
}

/// Regression (the failed-widen restore under the **generation-unique**
/// [`Source::Handle`] contract): a source that re-mints a **still-recorded** sibling's handle value
/// for a re-arm violates the contract, and the arm choke point's observed-handle `debug_assert`
/// must catch it LOUDLY rather than let `rebind_root` silently corrupt the reverse
/// index. Here the widen of `/a` fails and the restore of `/a/b` (old handle 1) re-arms while the
/// source REUSES handle `2` — already observed when the sibling `/a/c` was armed. The re-arm trips
/// the observed-handle assert first (`rebind_root(1, 2)` would otherwise overwrite `by_handle[2]`
/// and strand `/a/c`).
///
/// The earlier defensive recovery (disarm the aliased handle + retire `old`) was
/// RETIRED: it was incomplete, and when the alias was an unrelated *live* root its `disarm`
/// released that root's real source watch while its record + coverage stayed live — silently missing
/// future changes. The strengthened contract makes the alias impossible for a conforming
/// source (a re-arm mints a fresh generation while `old` and its siblings are still recorded), so
/// the debug_assert is the debug/test-only tripwire for a violating one. Hence `#[should_panic]` —
/// and `ignore`d in release builds, where `debug_assert!` is compiled out and nothing panics.
#[tokio::test]
#[should_panic(expected = "already observed by this owner")]
#[cfg_attr(
  not(debug_assertions),
  ignore = "the debug_assert tripwire is compiled out in release builds"
)]
async fn failed_widen_restore_reusing_a_recorded_sibling_handle_trips_the_tripwire() {
  let mut h = Harness::new();

  let _sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b"); // handle 1
  let _sc = h.watch("/a/c", Interest::all()).await.expect("watch /a/c"); // handle 2

  // The wider /a arm fails, AND the first restore re-arm (/a/b) REUSES handle 2 — already observed
  // when the sibling /a/c was armed. That is a generation-unique `Source::Handle` VIOLATION, so the
  // arm choke point's observed-handle debug_assert panics at re-arm time, before the rebind.
  h.owner.source.fail_next_arm();
  h.owner.source.reuse_next_arm_handle(2);
  // Panics inside the restore, before the watch returns — the source violated the handle contract.
  let _ = h.watch("/a", Interest::all()).await;
}

/// Regression (generation-unique contract, the SAME-key case the original rebind
/// tripwire wrongly exempted with `|| new_handle == old`): the failed-widen restore re-arm must
/// mint a FRESH handle even for the same key — reusing `old` is a `Source::Handle` violation,
/// because a stale pre-disarm event still carrying `old` would then route through the re-armed root
/// and be stamped in the new generation past the restore Rescan (a handle-ABA sibling). The
/// exhaustive observed-handle tripwire has NO same-key exemption: `old` was observed
/// when `/a/b` was first armed, so re-arming with it trips the arm choke point's assert.
///
/// Fail-on-old: the retired rebind assert's `|| new_handle == old` exemption masked this
/// same-`old` reuse (no panic). The observed-handle set — which records `old` at its first arm and
/// never prunes — has no such exemption, so a same-`old` re-arm still trips.
#[tokio::test]
#[should_panic(expected = "already observed by this owner")]
#[cfg_attr(
  not(debug_assertions),
  ignore = "the debug_assert tripwire is compiled out in release builds"
)]
async fn failed_widen_restore_reusing_old_handle_trips_the_tripwire() {
  let mut h = Harness::new();

  let _sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b"); // handle 1
  let _sc = h.watch("/a/c", Interest::all()).await.expect("watch /a/c"); // handle 2

  // The wider /a arm fails; the first restore re-arm (/a/b) REUSES handle 1 — /a/b's OWN old handle
  // (same-key reuse), observed when /a/b was first armed. Generation-unique forbids reissuing `old`,
  // so the arm choke point's observed-handle debug_assert panics at re-arm time () rather
  // than let a stale `old` event route through the re-armed root.
  h.owner.source.fail_next_arm();
  h.owner.source.reuse_next_arm_handle(1);
  let _ = h.watch("/a", Interest::all()).await;
}

/// Regression (the exhaustive observed-handle tripwire — the POST-RETIREMENT reuse the
/// per-site live-index checks MISSED): a handle removed from the live index by an `unwatch` (or a
/// terminal retirement) that a later arm REUSES is still a generation-unique `Source::Handle`
/// violation — a stale event still carrying it would route through the re-armed root in its new
/// generation. The retired per-site checks only asserted `entry(handle).is_none()` against the
/// CURRENT index, so a reused post-retirement handle (absent from the index) passed them silently;
/// the owner-level observed-handle set catches it, because the handle was observed at its first arm
/// and the set is never pruned.
///
/// Fail-on-old: after the `unwatch`, handle 1 is gone from `by_handle` (`plan_unwatch` removes it on
/// `RootEmptied`), so the reused-handle re-watch takes the `Disjoint` commit path whose retired
/// live-index-only assert `entry(1).is_none()` would PASS — no panic, and `/b` would silently bind
/// to the retired handle 1. The observed-handle assert trips instead, at the arm choke point.
#[tokio::test]
#[should_panic(expected = "already observed by this owner")]
#[cfg_attr(
  not(debug_assertions),
  ignore = "the debug_assert tripwire is compiled out in release builds"
)]
async fn rearm_reusing_a_retired_handle_trips_the_tripwire() {
  let mut h = Harness::new();

  // Watch /a (handle 1), then unwatch it: the root is emptied, so `plan_unwatch` disarms handle 1
  // AND removes it from the live index (`by_handle`). Handle 1 is now retired — gone from every live
  // structure, but permanently recorded in the owner's observed-handle set.
  let sa = h.watch("/a", Interest::all()).await.expect("watch /a"); // handle 1
  h.unwatch(sa).expect("unwatch /a");

  // Re-watch a disjoint key and force the source to REUSE the retired handle 1 — a generation-unique
  // `Source::Handle` violation. The retired live-index check would pass (handle 1 is absent from the
  // index after the unwatch), but the arm choke point's observed-handle debug_assert panics.
  h.owner.source.reuse_next_arm_handle(1);
  let _ = h.watch("/b", Interest::all()).await;
}

/// The ARM-choke-point liveness close, widen path: a `Widen` whose **wider** arm is
/// dead-on-arrival — the source reports it armed but has already forgotten the wider root
/// ([`Source::root_key`] is `None`) — must run the same restore the injected arm-failure does, not
/// strand the subsumed roots it disarmed. The choke point rejects the dead wider handle
/// (best-effort disarming it) and surfaces [`WatchError::DeadOnArrival`]; the widen's failure path
/// then re-arms the disarmed subsumed roots (restore) and aborts the newcomer's plan. No subsumed
/// subscription is left recorded-live-but-disarmed.
///
/// Fail-on-old: without the choke-point check the dead wider handle is committed as the widened
/// root — the subsumed roots collapse onto a handle NO live watch backs and their subscribers
/// silently stop seeing events.
#[tokio::test]
async fn widen_dead_on_arrival_wider_arm_restores_disarmed_roots() {
  let mut h = Harness::new();

  let sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let sc = h.watch("/a/c", Interest::all()).await.expect("watch /a/c");
  h.owner.epochs.stamp(sb, Epoch::new(4));
  h.owner.epochs.stamp(sc, Epoch::new(2));

  // The wider /a arm is dead-on-arrival; the two restore re-arms succeed (live).
  h.owner.source.dead_on_arrival_next_arm();
  let result = h.watch("/a", Interest::all()).await;
  let err = result.expect_err("the dead-on-arrival wider arm fails the widen");
  assert!(
    err.is_dead_on_arrival(),
    "the widen fails with the dead-on-arrival arm error, got {err:?}"
  );

  // Disarm-subsumed, the wider arm reports armed-then-dead so the choke point disarms its stray
  // handle (3), THEN both subsumed roots are re-armed (the restore) — never stranded.
  assert_eq!(
    h.owner.source.calls(),
    vec![
      Call::Arm(PathBuf::from("/a/b")),
      Call::Arm(PathBuf::from("/a/c")),
      Call::Disarm(1),
      Call::Disarm(2),
      Call::Arm(PathBuf::from("/a")),
      Call::Disarm(3),
      Call::Arm(PathBuf::from("/a/b")),
      Call::Arm(PathBuf::from("/a/c")),
    ],
    "the dead-on-arrival wider handle is disarmed, then the subsumed roots restored (not stranded)"
  );

  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "the aborted widen leaks no pending reservation"
  );

  // Both subsumed subscriptions are live-and-covered again on FRESH, live handles — the dead wider
  // root was never committed.
  let view = h.owner.subsumer.view();
  assert!(
    view.is_watched(&key("/a/b")) && view.is_watched(&key("/a/c")),
    "the restored subscriptions read watched again"
  );
  assert!(
    !view.is_watched(&key("/a")),
    "the dead-on-arrival wider root was NEVER committed (fail-on-old: /a is watched)"
  );
  let roots: Vec<(PathBuf, u32)> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, handle)| (PathBuf::from_iter(k), handle))
    .collect();
  assert_eq!(
    roots.iter().map(|(p, _)| p.clone()).collect::<Vec<_>>(),
    vec![PathBuf::from("/a/b"), PathBuf::from("/a/c")],
    "the two subsumed roots are back (re-armed), not collapsed onto the dead wider handle"
  );
  for (path, handle) in &roots {
    assert!(
      h.owner.source.root_key(*handle).is_some(),
      "the re-armed root {path:?} is on a LIVE handle — never published-watched-but-disarmed"
    );
  }

  // Each restored subscriber got a dominating Rescan (re-enumerate onto the re-armed root).
  let by_sub: HashMap<Subscription, Epoch> = h
    .drain()
    .iter()
    .map(|ev| {
      assert!(ev.is_rescan(), "every restore signal is a Rescan");
      (ev.subscription(), ev.epoch())
    })
    .collect();
  assert_eq!(
    by_sub.get(&sb).copied(),
    Some(Epoch::new(5)),
    "sb's restore Rescan strictly dominates its high-water of 4"
  );
  assert_eq!(
    by_sub.get(&sc).copied(),
    Some(Epoch::new(3)),
    "sc's restore Rescan strictly dominates its high-water of 2"
  );
}

/// A `Covered` subscription may ask for a kind its covering root's original watcher never
/// requested, and is still served — every root is armed the source's widest interest
/// (design §4), so nothing is under-served. The requested interest is recorded as this
/// subscription's fan-out gate.
#[tokio::test]
async fn covered_sub_with_wider_interest_still_delivered() {
  let mut h = Harness::new();

  let created_only = Interest::none().with_created();
  h.watch("/a", created_only).await.expect("watch /a");

  let removed_only = Interest::none().with_removed();
  let sb = h
    .watch("/a/b", removed_only)
    .await
    .expect("watch /a/b covered");

  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "the covered watch issues no second arm"
  );
  assert_eq!(
    h.owner.subsumer.subscription_interest(sb),
    Some(removed_only),
    "the covered sub's own removed-only interest is its fan-out gate"
  );
  assert!(
    removed_only.admits(&EventKind::<OsString>::Removed),
    "a removal under /a/b is admitted by the covered sub's gate — not silently lost"
  );
  assert!(
    !created_only.admits(&EventKind::<OsString>::Removed),
    "…and the gate is genuinely narrowing (a created-only gate would drop it)"
  );
}

/// the public command mailbox is BOUNDED — a submission past
/// `command_capacity` awaits ADMISSION while the owner is parked inside a
/// caller-bounded reconcile, so poll-then-cancel callers can never grow the queue (a
/// cancel before admission leaves nothing queued). Race-free negative window: the
/// gated arm parks the owner, capacity 1 is filled by the second watch, and this test
/// controls both the gate and the only submitters.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn watch_admission_backpressures_when_the_mailbox_is_full() {
  struct GatedArmSource {
    inner: FakeSource,
    /// Each `arm` awaits one token before delegating — the owner parks mid-reconcile
    /// until the test feeds it.
    gate: async_channel::Receiver<()>,
  }

  impl Source<OsString> for GatedArmSource {
    type Handle = u32;

    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      self.inner.canonicalize_key(key)
    }

    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      let _ = self.gate.recv().await;
      self.inner.arm(key).await
    }

    fn disarm(&mut self, handle: u32) {
      self.inner.disarm(handle);
    }

    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      // Keep the source alive: the FakeSource's instant `None` would drain the owner
      // before the watches under test even arrive.
      core::future::pending().await
    }

    fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
      self.inner.root_key(handle)
    }
  }

  let (gate_tx, gate_rx) = async_channel::unbounded::<()>();
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> = super::Tributaries::with_source(
    GatedArmSource {
      inner: FakeSource::new(),
      gate: gate_rx,
    },
    TributariesOptions::new().with_command_capacity(std::num::NonZeroUsize::new(1).unwrap()),
  );

  // Watch 1 is consumed by the owner, which parks inside the gated arm. Watch 2 then
  // fills the capacity-1 mailbox. Watch 3's submission must AWAIT admission.
  let w1 = {
    let w = w.clone();
    tokio::spawn(async move { w.watch(key("/one"), (), WatchOptions::new()).await })
  };
  let w2 = {
    let w = w.clone();
    tokio::spawn(async move { w.watch(key("/two"), (), WatchOptions::new()).await })
  };
  // Deterministic fill order: give the owner and the two submitters time to reach
  // their steady state (owner parked in arm; mailbox holding exactly one command).
  tokio::time::sleep(std::time::Duration::from_millis(200)).await;
  let w3 = {
    let w = w.clone();
    tokio::spawn(async move { w.watch(key("/three"), (), WatchOptions::new()).await })
  };
  tokio::time::sleep(std::time::Duration::from_millis(300)).await;
  assert!(
    !w3.is_finished(),
    "the third watch awaits mailbox ADMISSION while the owner is parked and the \
     capacity-1 mailbox is full — it must not resolve, and nothing queues"
  );

  // Open the gate for all three arms: every submission admits, reconciles, resolves.
  for _ in 0..3 {
    gate_tx.send(()).await.expect("feed the arm gate");
  }
  for handle in [w1, w2, w3] {
    tokio::time::timeout(std::time::Duration::from_secs(20), handle)
      .await
      .expect("watch resolves once the owner unparks")
      .expect("task")
      .expect("watch succeeds");
  }
}

/// `parts()`: the caller owns the spawn — the same construction as `with_source`
/// minus the detach. Spawning the returned driver future manually drives the full
/// watch → unwatch → close lifecycle.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn parts_future_drives_the_watcher_when_caller_spawned() {
  struct AliveSource(FakeSource);
  impl Source<OsString> for AliveSource {
    type Handle = u32;
    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      self.0.canonicalize_key(key)
    }
    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      self.0.arm(key).await
    }
    fn disarm(&mut self, handle: u32) {
      self.0.disarm(handle);
    }
    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      // Keep the source alive (FakeSource's instant `None` would drain the owner).
      core::future::pending().await
    }
    fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
      self.0.root_key(handle)
    }
  }

  let (w, driver): (super::Tributaries<OsString, (), TokioRuntime, u32>, _) =
    super::Tributaries::parts(AliveSource(FakeSource::new()), TributariesOptions::new());
  let driver = tokio::spawn(driver);

  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch through the caller-spawned driver");
  assert!(w.view().is_watched(&key("/a")), "the read plane published");
  w.unwatch(sub).await.expect("unwatch");
  w.close()
    .await
    .expect("close resolves — the caller-spawned driver serviced it");
  tokio::time::timeout(Duration::from_secs(5), driver)
    .await
    .expect("the driver future completes after close")
    .expect("driver task");
}

/// `parts()` caveat two, pinned: DROPPING the un-spawned driver future is hard
/// teardown — the owner's drop publishes an empty read plane and closes every channel,
/// so calls surface Closed/Stopped rather than hanging.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dropping_the_parts_future_is_hard_teardown() {
  let (mut w, driver): (super::Tributaries<OsString, (), TokioRuntime, u32>, _) =
    super::Tributaries::parts(FakeSource::new(), TributariesOptions::new());
  drop(driver);

  let err = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect_err("watch against a dropped driver surfaces Closed");
  assert!(matches!(err, WatchError::Closed), "got {err:?}");
  assert!(
    !w.view().is_watched(&key("/a")),
    "the read plane is empty-and-honest after the hard teardown"
  );
  assert!(w.next().await.is_none(), "the event stream is ended");
}

/// dropping the `parts()` driver MID-ARM cancels the in-flight reconcile at
/// its await point — and the SOURCE drops with the owner, which is the contract's
/// reclamation boundary: a conforming source's `Drop` tears down whatever external
/// effect the cancelled arm had initiated (`grow` shares the same await surface). The
/// instrumented source proves both halves: the arm was genuinely in flight when the
/// driver dropped, and the source's Drop ran promptly to reclaim.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dropping_the_parts_future_mid_arm_drops_the_source_for_reclamation() {
  struct InstrumentedSource {
    inner: FakeSource,
    /// Closed when an arm is entered — the test's proof the reconcile was in flight.
    arm_entered: Option<futures_channel::oneshot::Sender<()>>,
    /// Never yields: holds the owner at the arm await point.
    gate: async_channel::Receiver<()>,
    /// Set by Drop — the reclamation boundary observed.
    dropped: std::sync::Arc<std::sync::atomic::AtomicBool>,
  }
  impl Drop for InstrumentedSource {
    fn drop(&mut self) {
      // A real transport would tear down its external watches here (the contract's
      // cancellation clause); the flag stands in for that teardown.
      self
        .dropped
        .store(true, std::sync::atomic::Ordering::Release);
    }
  }
  impl Source<OsString> for InstrumentedSource {
    type Handle = u32;
    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      self.inner.canonicalize_key(key)
    }
    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      if let Some(entered) = self.arm_entered.take() {
        let _ = entered.send(());
      }
      // Park forever: the cancellation arrives as this future being dropped.
      let _ = self.gate.recv().await;
      self.inner.arm(key).await
    }
    fn disarm(&mut self, handle: u32) {
      self.inner.disarm(handle);
    }
    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      core::future::pending().await
    }
    fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
      self.inner.root_key(handle)
    }
  }

  let dropped = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
  let (entered_tx, entered_rx) = futures_channel::oneshot::channel();
  let (_gate_tx, gate_rx) = async_channel::unbounded::<()>();
  let (w, driver): (super::Tributaries<OsString, (), TokioRuntime, u32>, _) =
    super::Tributaries::parts(
      InstrumentedSource {
        inner: FakeSource::new(),
        arm_entered: Some(entered_tx),
        gate: gate_rx,
        dropped: std::sync::Arc::clone(&dropped),
      },
      TributariesOptions::new(),
    );
  let driver = tokio::spawn(driver);

  // Submit a watch and wait until the owner is provably INSIDE the gated arm.
  let watching = {
    let w = w.clone();
    tokio::spawn(async move { w.watch(key("/a"), (), WatchOptions::new()).await })
  };
  entered_rx
    .await
    .expect("the arm was entered — reconcile in flight");

  // Cancel the driver mid-arm.
  driver.abort();
  let _ = driver.await;

  // The source dropped WITH the owner — the reclamation boundary fired...
  assert!(
    dropped.load(std::sync::atomic::Ordering::Acquire),
    "the source's Drop ran when the mid-arm driver was cancelled"
  );
  // ...and the caller's in-flight watch surfaces Closed rather than hanging.
  let err = tokio::time::timeout(Duration::from_secs(5), watching)
    .await
    .expect("the watch settles")
    .expect("task")
    .expect_err("a cancelled owner surfaces Closed to the in-flight watch");
  assert!(matches!(err, WatchError::Closed), "got {err:?}");
}

/// The mid-arm twin for `grow`: dropping the `parts()` driver MID-GROW cancels the in-flight
/// covered-outside reconcile at its await point — which, grow-before-commit (R1), is now
/// PRE-commit, so the unwind strands nothing: no subscription was committed, no grant minted to
/// orphan, and the pending plan drops with the owner. The SOURCE drops with the owner too — the
/// contract's reclamation boundary for whatever external re-arm the cancelled grow had initiated
/// — and the caller's in-flight watch surfaces Closed rather than hanging.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dropping_the_parts_future_mid_grow_drops_the_source_for_reclamation() {
  struct GrowGatedSource {
    inner: FakeSource,
    /// Closed when a grow is entered — the test's proof the covered-outside reconcile was
    /// parked at the PRE-commit grow await when the driver dropped.
    grow_entered: Option<futures_channel::oneshot::Sender<()>>,
    /// Never yields: holds the owner at the grow await point.
    gate: async_channel::Receiver<()>,
    /// Set by Drop — the reclamation boundary observed.
    dropped: std::sync::Arc<std::sync::atomic::AtomicBool>,
  }
  impl Drop for GrowGatedSource {
    fn drop(&mut self) {
      self
        .dropped
        .store(true, std::sync::atomic::Ordering::Release);
    }
  }
  impl Source<OsString> for GrowGatedSource {
    type Handle = u32;
    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      self.inner.canonicalize_key(key)
    }
    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      self.inner.arm(key).await
    }
    fn disarm(&mut self, handle: u32) {
      self.inner.disarm(handle);
    }
    fn set_cover(&mut self, handle: u32, retained: &[Vec<OsString>]) {
      self.inner.set_cover(handle, retained);
    }
    async fn grow(&mut self, handle: u32, retained: &[Vec<OsString>]) -> Result<(), WatchError> {
      if let Some(entered) = self.grow_entered.take() {
        let _ = entered.send(());
      }
      // Park forever: the cancellation arrives as this future being dropped.
      let _ = self.gate.recv().await;
      self.inner.grow(handle, retained).await
    }
    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      core::future::pending().await
    }
    fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
      self.inner.root_key(handle)
    }
  }

  let dropped = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
  let (entered_tx, entered_rx) = futures_channel::oneshot::channel();
  let (_gate_tx, gate_rx) = async_channel::unbounded::<()>();
  let (w, driver): (super::Tributaries<OsString, (), TokioRuntime, u32>, _) =
    super::Tributaries::parts(
      GrowGatedSource {
        inner: FakeSource::new(),
        grow_entered: Some(entered_tx),
        gate: gate_rx,
        dropped: std::sync::Arc::clone(&dropped),
      },
      TributariesOptions::new(),
    );
  let driver = tokio::spawn(driver);

  // Build the covered-outside state through the real loop: /a/b, widen /a, unwatch the widening
  // sub — the wide root's cover narrows to {/a/b} (prune + record), and no grow has run yet.
  // (The widen's dominating Rescan sits harmlessly in the event channel; nothing here drains it.)
  let _s_b = w
    .watch(key("/a/b"), (), WatchOptions::new())
    .await
    .expect("watch /a/b");
  let s_a = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a widens");
  w.unwatch(s_a).await.expect("unwatch the widening /a");

  // Watch /a/c: Covered-outside → the owner parks INSIDE the gated grow, before any commit.
  let watching = {
    let w = w.clone();
    tokio::spawn(async move { w.watch(key("/a/c"), (), WatchOptions::new()).await })
  };
  entered_rx
    .await
    .expect("the grow was entered — the covered-outside reconcile is parked pre-commit");

  // Cancel the driver mid-grow.
  driver.abort();
  let _ = driver.await;

  // The source dropped WITH the owner — the reclamation boundary fired...
  assert!(
    dropped.load(std::sync::atomic::Ordering::Acquire),
    "the source's Drop ran when the mid-grow driver was cancelled"
  );
  // ...and the caller's in-flight watch surfaces Closed rather than hanging (nothing was
  // committed, so there is no grant and no subscription to strand).
  let err = tokio::time::timeout(Duration::from_secs(5), watching)
    .await
    .expect("the watch settles")
    .expect("task")
    .expect_err("a cancelled owner surfaces Closed to the in-flight watch");
  assert!(matches!(err, WatchError::Closed), "got {err:?}");
  assert!(
    !w.view().is_watched(&key("/a/c")),
    "the cancelled covered-outside watch was never published"
  );
}

/// `parts_local()`: the `!Send` construction path end-to-end — a genuinely thread-local
/// source (`Rc` state, implementing [`LocalSource`](crate::source::LocalSource) directly,
/// so no [`Source`] impl could be written for it) drives the full watch → raw event →
/// attributed delivery → close lifecycle, with the driver future polled on the thread
/// that owns the source (a tokio `LocalSet` — the "poll it where the source lives"
/// caveat) while the handle plane is used exactly as under `parts()`.
#[tokio::test]
async fn parts_local_drives_a_thread_local_source_end_to_end() {
  use std::{
    cell::{Cell, RefCell},
    rc::Rc,
  };

  use crate::source::LocalSource;

  /// A thread-local source: handle minting and the live-root map ride `Rc`s, so `Self`
  /// is `!Send` and every `async fn` future (capturing `&mut Self`) is too.
  struct RcSource {
    next_handle: Rc<Cell<u32>>,
    live: Rc<RefCell<HashMap<u32, Vec<OsString>>>>,
    /// Raw changes the test feeds in; `recv` is cancel-safe, satisfying the `next`
    /// contract, and parks while the test holds the sender (the driver exits via
    /// `close`, not stream end).
    events: async_channel::Receiver<SourceEvent<OsString, u32>>,
  }

  impl LocalSource<OsString> for RcSource {
    type Handle = u32;

    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      Ok(key.to_vec())
    }

    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      let handle = self.next_handle.get() + 1;
      self.next_handle.set(handle);
      self.live.borrow_mut().insert(handle, key.to_vec());
      Ok(Armed::new(handle, key.to_vec()))
    }

    fn disarm(&mut self, handle: u32) {
      self.live.borrow_mut().remove(&handle);
    }

    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      self.events.recv().await.ok()
    }

    fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
      self.live.borrow().get(&handle).cloned()
    }
  }

  let (event_tx, event_rx) = async_channel::unbounded();
  let source = RcSource {
    next_handle: Rc::new(Cell::new(0)),
    live: Rc::new(RefCell::new(HashMap::new())),
    events: event_rx,
  };
  let (mut w, driver): (super::Tributaries<OsString, (), TokioRuntime, u32>, _) =
    super::Tributaries::parts_local(source, TributariesOptions::new());

  let local = tokio::task::LocalSet::new();
  local
    .run_until(async move {
      let driver = tokio::task::spawn_local(driver);

      let sub = w
        .watch(key("/a"), (), WatchOptions::new())
        .await
        .expect("watch through the locally-polled driver");
      assert!(w.view().is_watched(&key("/a")), "the read plane published");

      // Feed a raw change under the armed root (the fixture mints handle 1 first).
      event_tx
        .send(source_modified(1, "/a/file.txt", 1))
        .await
        .expect("feed the thread-local source");
      let event = tokio::time::timeout(Duration::from_secs(10), w.next())
        .await
        .expect("delivery is prompt")
        .expect("the change is delivered");
      assert_eq!(
        event.subscription(),
        sub,
        "attributed to the covering subscription"
      );
      assert!(event.kind().is_modified(), "the kind survives the fan-out");

      w.close().await.expect("close resolves on the local driver");
      tokio::time::timeout(Duration::from_secs(5), driver)
        .await
        .expect("the driver future completes after close")
        .expect("driver task");
    })
    .await;
}

/// the coalescer's buffered-entry cap engages the EXISTING loss-accounting
/// path — a high-cardinality burst past the cap sheds the subscription to a dominating
/// parked Rescan (`park_rescan`: shed epoch + `needs_rescan` merge + coalescer purge),
/// so debounce keeps the crate's bounded-memory guarantee with no silent loss.
#[tokio::test]
async fn coalescer_overflow_sheds_to_a_dominating_parked_rescan() {
  // Never-settling windows: nothing drains on its own, so the buffer would grow one
  // entry per distinct key without the cap.
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200))
    .with_max_buffered(2);
  let mut h = Harness::with_coalescer(Some(Coalescer::new(Some(cfg))));
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  h.owner.epochs.stamp(sub, Epoch::new(3));

  // Two distinct keys fill the cap; the third OVERFLOWS: the coalescer sheds the
  // subscription instead of growing.
  h.owner.fan_out_and_push(&source_modified(1, "/a/k0", 4));
  h.owner.fan_out_and_push(&source_modified(1, "/a/k1", 5));
  assert!(
    h.owner.needs_rescan.is_empty(),
    "under the cap nothing is shed"
  );
  h.owner.fan_out_and_push(&source_modified(1, "/a/k2", 6));

  // The shed landed in the parked-Rescan debt (dominating epoch) and the purge freed
  // the subscription's buffered entries — bounded memory, accounted loss.
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "the overflowed subscription is owed a dominating parked Rescan"
  );
  let parked_epoch = h.owner.needs_rescan.get(&sub).expect("parked").epoch;
  assert!(
    parked_epoch >= Epoch::new(6),
    "the shed Rescan dominates the purged buffered epochs (got {parked_epoch:?})"
  );

  // The flush delivers the dominating Rescan to the consumer — the loss is announced.
  h.owner.flush_pending_rescans();
  let delivered = h.drain();
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == sub && e.kind().is_rescan()),
    "the parked Rescan flushes to the consumer"
  );
}

/// Per-subscription debounce (design §6): two subscriptions over ONE root,
/// one inheriting the watcher-global settle policy, one watched with `Debounce::Off` —
/// one raw burst delivers every event to the raw subscription undelayed while the
/// settled sibling holds a single collapsed entry.
#[tokio::test]
async fn per_sub_off_delivers_raw_while_inherit_sibling_settles() {
  // Never-settling global windows: the settled side deterministically holds, no timers.
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let mut h = Harness::with_coalescer(Some(Coalescer::new(Some(cfg))));
  let settled = h.watch("/a", Interest::all()).await.expect("watch /a");
  let raw = h
    .watch_with("/a", WatchOptions::new().with_debounce(Debounce::Off))
    .await
    .expect("watch /a raw");

  // One three-write burst to a single path fans out to BOTH covering subscriptions.
  for epoch in 1..=3 {
    h.owner.fan_out_and_push(&source_modified(1, "/a/f", epoch));
  }

  let delivered = h.drain();
  let raw_events: Vec<_> = delivered
    .iter()
    .filter(|e| e.subscription() == raw)
    .collect();
  assert_eq!(
    raw_events.len(),
    3,
    "the Off subscription sees every raw event, undelayed and uncollapsed"
  );
  assert!(
    raw_events.windows(2).all(|w| w[0].epoch() < w[1].epoch()),
    "…in admission (= epoch) order"
  );
  assert!(
    !delivered.iter().any(|e| e.subscription() == settled),
    "the inheriting sibling's burst is still settling — nothing delivered yet"
  );

  // The settled sibling holds exactly its ONE collapsed entry: the teardown flush
  // releases a single event carrying the newest stamp.
  h.owner.drain_owed_once();
  let tail = h.drain();
  assert_eq!(
    tail.len(),
    1,
    "the settled sibling collapsed the burst to one"
  );
  assert_eq!(tail[0].subscription(), settled);
  assert_eq!(
    tail[0].epoch(),
    Epoch::new(3),
    "the newest observation's stamp"
  );
}

/// The lazy-instantiation matrix, `Off` half: `Debounce::Off` when the watcher-global
/// debounce is off too is a NO-OP — events already pass through untouched, so no
/// coalescer is instantiated (the zero-cost claim).
#[tokio::test]
async fn off_with_global_none_never_instantiates_the_coalescer() {
  let mut h = Harness::new();
  let sub = h
    .watch_with("/a", WatchOptions::new().with_debounce(Debounce::Off))
    .await
    .expect("watch /a");
  assert!(
    h.owner.coalescer.is_none(),
    "Off atop a disabled global debounce instantiates nothing"
  );

  h.owner.fan_out_and_push(&source_modified(1, "/a/f", 0));
  let delivered = h.drain();
  assert_eq!(delivered.len(), 1, "events pass straight through");
  assert_eq!(delivered[0].subscription(), sub);
}

/// The lazy-instantiation matrix, `Custom` half: a `Debounce::Custom` commit when the
/// watcher-global debounce is off instantiates the coalescer lazily with NO default —
/// the override settles while an inheriting sibling keeps passing through raw.
#[tokio::test]
async fn custom_with_global_none_lazily_instantiates_the_coalescer() {
  let mut h = Harness::new();
  assert!(h.owner.coalescer.is_none(), "nothing opted in yet");

  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let custom = h
    .watch_with(
      "/a",
      WatchOptions::new().with_debounce(Debounce::Custom(cfg)),
    )
    .await
    .expect("watch /a"); // root 1
  assert!(
    h.owner.coalescer.is_some(),
    "the first Custom commit lazily instantiated the coalescer"
  );
  let inherit = h.watch("/b", Interest::all()).await.expect("watch /b"); // root 2

  h.owner.fan_out_and_push(&source_modified(1, "/a/f", 0)); // custom → settles
  h.owner.fan_out_and_push(&source_modified(2, "/b/g", 0)); // inherit-of-nothing → raw

  let delivered = h.drain();
  assert_eq!(
    delivered.len(),
    1,
    "only the inheriting sibling's event passes through"
  );
  assert_eq!(delivered[0].subscription(), inherit);
  assert!(
    delivered[0].kind().is_modified(),
    "…as the raw delivery itself, not a Rescan"
  );
  let _ = custom;
  assert!(
    h.owner
      .coalescer
      .as_ref()
      .and_then(Coalescer::next_deadline)
      .is_some(),
    "the Custom subscription's delta is held settling"
  );
}

/// The forget-vs-drop regression at the driver seam (design §6, the subtle
/// cleanup split): a WIDEN re-points a live subscription — its buffered pre-widen deltas
/// are dropped, but its `Debounce::Custom` policy survives, so post-widen events on the
/// wider root still settle. An overflow park likewise keeps the policy; only release
/// (unwatch) and terminal retirement forget it.
#[tokio::test]
async fn widen_repoint_keeps_the_custom_policy_release_forgets_it() {
  let mut h = Harness::new();
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let sb = h
    .watch_with(
      "/a/b",
      WatchOptions::new().with_debounce(Debounce::Custom(cfg)),
    )
    .await
    .expect("watch /a/b"); // root 1, lazily instantiates the coalescer

  // A pre-widen delta buffers under the Custom policy.
  h.owner.fan_out_and_push(&source_modified(1, "/a/b/f", 0));
  assert!(h.drain().is_empty(), "the pre-widen delta is settling");

  // Widen to /a: sb re-points onto the wider root — the coalescer purge is
  // drop_subscription (buffers only), NOT forget (policy retained).
  let sa = h.watch("/a", Interest::all()).await.expect("widen to /a"); // root 2
  let delivered = h.drain();
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == sb && e.is_rescan()),
    "the re-pointed subscription received its dominating widen Rescan"
  );
  assert!(
    !delivered
      .iter()
      .any(|e| e.subscription() == sb && !e.is_rescan()),
    "the purged pre-widen delta never delivers (dominated by the Rescan)"
  );
  let coalescer = h.owner.coalescer.as_ref().expect("coalescer live");
  assert!(
    coalescer.has_policy(sb),
    "the widen re-point KEPT the Custom policy — the subscription is still live"
  );

  // THE regression assert: a post-widen event on the WIDER root fans out to BOTH
  // subscribers — the widener (inheriting the disabled default: raw pass-through) and
  // the re-pointed sb, whose retained Custom policy must still settle its copy.
  h.owner.fan_out_and_push(&source_modified(2, "/a/b/g", 0));
  let delivered = h.drain();
  assert!(
    !delivered.iter().any(|e| e.subscription() == sb),
    "sb's post-widen delta is buffered under the retained Custom policy, not raw"
  );
  assert!(
    delivered.iter().any(|e| e.subscription() == sa),
    "…while the inheriting widener's copy passes through raw (per-sub isolation)"
  );

  // The overflow park keeps it too…
  h.owner.park_rescan(sb);
  let coalescer = h.owner.coalescer.as_ref().expect("coalescer live");
  assert!(
    coalescer.has_policy(sb),
    "an overflow park purges buffers but keeps the policy"
  );

  // …while release (unwatch) FORGETS it with the subscription.
  h.unwatch(sb).expect("unwatch");
  let coalescer = h.owner.coalescer.as_ref().expect("coalescer live");
  assert!(
    !coalescer.has_policy(sb),
    "release_subscription forgets the retired subscription's policy"
  );
}

/// Terminal retirement (root death) FORGETS the dead subscription's policy — the other
/// half of the forget split, so a retired subscription leaks no policy entry.
#[tokio::test]
async fn terminal_retirement_forgets_the_policy() {
  let mut h = Harness::new();
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let sub = h
    .watch_with(
      "/a",
      WatchOptions::new().with_debounce(Debounce::Custom(cfg)),
    )
    .await
    .expect("watch /a"); // root 1
  assert!(
    h.owner
      .coalescer
      .as_ref()
      .expect("coalescer")
      .has_policy(sub),
    "the Custom policy is registered at commit"
  );

  h.owner.source.kill_root(1);
  h.owner.retire_root_with_terminal_rescan(1);
  assert!(
    !h.owner
      .coalescer
      .as_ref()
      .expect("coalescer")
      .has_policy(sub),
    "terminal retirement forgets the dead subscription's policy"
  );
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "…while its owed terminal Rescan is still parked (unaffected by the forget)"
  );
}

/// a retire-and-rewatch cycle against a FULL channel is structurally
/// bounded — terminal parked Rescans retained past their subscriptions' retirement
/// count as RETIRED debt, and watch admission is refused at the cap
/// ([`WatchError::RescanBacklog`]), breaking the only loop that grows it. Draining the
/// owed Rescans (the flush frees entries) restores admission. Capacity-1 channel, the
/// exact repro shape.
#[tokio::test]
async fn retire_rewatch_cycle_is_bounded_by_the_retired_debt_gate() {
  let mut h = Harness::bounded(1);

  // Fill the single event slot so every terminal Rescan parks instead of delivering.
  let plug = h.watch("/plug", Interest::all()).await.expect("watch plug");
  h.owner.epochs.stamp(plug, Epoch::new(1));
  h.owner.source.kill_root(1);
  h.owner.retire_root_with_terminal_rescan(1);
  h.owner.flush_pending_rescans(); // occupies the one slot with plug's terminal Rescan

  // Cycle: watch a fresh key, kill its root, retire — each round force-removes the sub
  // while RETAINING its parked terminal entry (the retired debt the gate counts).
  let mut cycles = 0usize;
  loop {
    match h.watch(&format!("/cycle{cycles}"), Interest::all()).await {
      Ok(_) => {
        // FakeSource handles are monotone: this watch armed handle cycles+2 (plug took 1).
        let handle = u32::try_from(cycles).expect("small") + 2;
        h.owner.source.kill_root(handle);
        h.owner.retire_root_with_terminal_rescan(handle);
        cycles += 1;
        assert!(
          cycles
            <= super::Owner::<OsString, (), TokioRuntime, FakeSource>::RETIRED_RESCAN_DEBT_LIMIT
              + 1,
          "the gate must refuse before the debt exceeds its cap"
        );
      }
      Err(WatchError::RescanBacklog) => break,
      Err(other) => panic!("unexpected watch error: {other:?}"),
    }
  }
  let retired = h
    .owner
    .needs_rescan
    .keys()
    .filter(|&&sub| h.owner.subsumer.subscription_key(sub).is_none())
    .count();
  assert!(
    retired <= super::Owner::<OsString, (), TokioRuntime, FakeSource>::RETIRED_RESCAN_DEBT_LIMIT,
    "retired debt is capped (got {retired})"
  );

  // Drain the owed Rescans: each flush frees entries, restoring admission.
  loop {
    h.owner.flush_pending_rescans();
    if h.drain().is_empty() {
      break;
    }
  }
  h.watch("/after-drain", Interest::all())
    .await
    .expect("admission restored once the retired debt drained");
}

/// a BATCH conversion — one root death retiring an entire covered cohort at
/// once — legitimately stands ABOVE the retired-debt threshold (bounded by the caller's
/// own peak concurrent subscriptions, state it was already paying for), and the gate
/// then refuses the replenishing watch, so the live-plus-retired total can never grow
/// past peak-live-plus-threshold. Draining restores admission.
#[tokio::test]
async fn batch_retirement_stands_above_the_threshold_but_cannot_replenish() {
  const COHORT: usize = 1200; // deliberately above RETIRED_RESCAN_DEBT_LIMIT (1024)
  let mut h = Harness::bounded(1);

  // A covered cohort: the first watch arms the root, the rest subsume onto it — all
  // admitted while retired debt is zero (the batch-conversion bypass shape).
  for _ in 0..COHORT {
    h.watch("/batch", Interest::all())
      .await
      .expect("cohort watch admitted under a zero retired count");
  }

  // One root death converts the whole cohort 1:1 into retired parked debt.
  h.owner.source.kill_root(1);
  h.owner.retire_root_with_terminal_rescan(1);
  let retired = h
    .owner
    .needs_rescan
    .keys()
    .filter(|&&sub| h.owner.subsumer.subscription_key(sub).is_none())
    .count();
  assert_eq!(
    retired, COHORT,
    "the batch stands above the threshold — bounded by the caller's own peak cohort"
  );

  // Replenishment is refused: the gate sees the retired debt at/above the threshold.
  let refused = h.watch("/fresh", Interest::all()).await;
  assert!(
    matches!(refused, Err(WatchError::RescanBacklog)),
    "admission refused while retired debt sits at/above the threshold (got {refused:?})"
  );

  // Drain the owed terminal Rescans (capacity-1: one flush offer per drained slot —
  // also exercising the cursor-resumed pass); admission then returns.
  loop {
    h.owner.flush_pending_rescans();
    if h.drain().is_empty() {
      break;
    }
  }
  h.watch("/fresh", Interest::all())
    .await
    .expect("admission restored once the retired debt drained");
}

/// each flush pass visits a ROOM-PROPORTIONAL number of candidates — never
/// the whole parked map. Capacity-1 drain over a 64-entry retired cohort: every pass
/// visits at most two keys (the one delivered offer plus the probe that found the
/// channel full or the map empty), pinned by the test-only visited counter.
#[tokio::test]
async fn flush_pass_work_is_room_proportional_not_map_proportional() {
  const COHORT: usize = 64;
  let mut h = Harness::bounded(1);
  for _ in 0..COHORT {
    h.watch("/cohort", Interest::all())
      .await
      .expect("cohort watch");
  }
  h.owner.source.kill_root(1);
  h.owner.retire_root_with_terminal_rescan(1);

  // Drain slot-by-slot: each pass must do O(room) work, not O(map).
  let mut delivered = 0usize;
  while delivered < COHORT {
    h.owner.flush_pending_rescans();
    assert!(
      h.owner.last_flush_visited <= 2,
      "a capacity-1 pass visits at most the delivered offer plus one probe \
       (visited {} with {} entries left)",
      h.owner.last_flush_visited,
      COHORT - delivered
    );
    let batch = h.drain();
    if batch.is_empty() {
      break;
    }
    delivered += batch.len();
  }
  assert_eq!(delivered, COHORT, "every owed terminal Rescan drained");
  assert!(h.owner.needs_rescan.is_empty(), "no residue");
}

/// an ALL-UNCLAIMED retired cohort costs the flush NOTHING — the debt lives
/// in the suppressed partition, the offerable map stays empty (so the 25 ms retry timer
/// never arms for it), and every flush pass visits zero candidates instead of probing
/// the whole cohort each tick. Claiming one grant moves exactly its entry into the
/// offerable map and it delivers.
#[tokio::test]
async fn unclaimed_retired_cohort_costs_the_flush_nothing() {
  const COHORT: usize = 64;
  let mut h = Harness::new();
  let mut subs = Vec::new();
  for _ in 0..COHORT {
    let sub = h.watch("/cohort", Interest::all()).await.expect("watch");
    h.owner.unclaimed.insert(sub); // model every grant as still in flight
    subs.push(sub);
  }
  h.owner.source.kill_root(1);
  h.owner.retire_root_with_terminal_rescan(1);

  assert!(
    h.owner.needs_rescan.is_empty(),
    "no offerable debt — the retry timer has nothing to arm for"
  );
  assert_eq!(
    h.owner.suppressed_rescan.len(),
    COHORT,
    "the whole cohort's terminal debt is suppressed apart"
  );
  h.owner.flush_pending_rescans();
  assert_eq!(
    h.owner.last_flush_visited, 0,
    "a flush pass over an all-unclaimed cohort visits ZERO candidates"
  );
  assert!(h.drain().is_empty(), "nothing delivered while unclaimed");

  // One claim lifts exactly one entry into the offerable map; it delivers.
  let claimed = subs[COHORT / 2];
  h.owner.apply_cleanup(super::Cleanup::Claim(claimed));
  assert_eq!(h.owner.needs_rescan.len(), 1, "the claim moved its entry");
  h.owner.flush_pending_rescans();
  let delivered = h.drain();
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == claimed && e.is_rescan()),
    "the claimed sub's terminal Rescan is delivered — suppression never became loss"
  );
  assert_eq!(
    h.owner.suppressed_rescan.len(),
    COHORT - 1,
    "the other suppressed entries remain, still costing nothing"
  );
}

/// a `Cleanup::Claim` that RACES the source-drain's atomic cut —
/// injected via the test-only window hook BETWEEN the emptiness observation and the
/// close, so the CUT BLOCK's own drain finds it — re-arms OFFERABLE debt that a full
/// event channel cannot take in the final best-effort pass. The drain must NOT return
/// then (the claimer holds a live Ok subscription; returning strands its terminal
/// Rescan forever): it takes the post-cut `continue`, the closed cleanup channel's
/// select arm disables on its first error instead of spinning, and the retry loop
/// delivers once the consumer drains the plug. Fail-on-old: the old cut block
/// returned unconditionally — the racer's Rescan was stranded and this test's second
/// recv would time out.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn raced_pre_cut_claim_is_delivered_not_stranded() {
  let mut h = Harness::bounded(1);
  // Plug the single event slot with an unrelated claimed sub's owed Rescan.
  let plug = h.watch("/plug", Interest::all()).await.expect("watch plug");
  h.owner.epochs.stamp(plug, Epoch::new(1));
  h.owner.source.kill_root(1);
  h.owner.retire_root_with_terminal_rescan(1);
  h.owner.flush_pending_rescans(); // the slot now holds plug's terminal Rescan

  // The racing sub: unclaimed grant, terminal-retired — its debt sits SUPPRESSED, so
  // the drain's emptiness predicate holds (offerable empty, cleanup empty)…
  let racer = h
    .watch("/racer", Interest::all())
    .await
    .expect("watch racer");
  h.owner.unclaimed.insert(racer);
  h.owner.source.kill_root(2);
  h.owner.retire_root_with_terminal_rescan(2);
  assert!(h.owner.needs_rescan.is_empty() && h.owner.suppressed_rescan.contains_key(&racer));

  // …and the claim lands IN THE WINDOW: the test injection point sends it
  // onto the still-open cleanup channel exactly between the drain's emptiness
  // observation and the atomic cut — so the CUT BLOCK ITSELF drains it, re-arms
  // offerable debt against the FULL channel, and must take the post-cut `continue`
  // rather than returning. (A claim merely pre-queued before the drain is drained at
  // the loop top and never reaches the cut — the path this regression exists to pin.)
  h.owner
    .test_pre_cut_claims
    .push(super::Cleanup::Claim(racer));

  let events = h.events.clone();
  let drain = tokio::spawn(async move {
    // Drive the drain to completion on its own task; it must not return while the
    // racer's offerable debt is undeliverable.
    h.owner.drain_owed_before_shutdown().await;
    h
  });
  tokio::time::sleep(Duration::from_millis(300)).await;
  assert!(
    !drain.is_finished(),
    "the drain stays in the retry loop while the raced claim's Rescan cannot be \
     delivered — returning here would strand a live Ok subscription"
  );

  // The consumer drains the plug; the retry tick then delivers the racer's Rescan and
  // the drain exits.
  let first = tokio::time::timeout(Duration::from_secs(5), events.recv())
    .await
    .expect("recv settles")
    .expect("plug Rescan");
  assert_eq!(first.subscription(), plug);
  let second = tokio::time::timeout(Duration::from_secs(10), events.recv())
    .await
    .expect("recv settles")
    .expect("racer Rescan");
  assert!(
    second.subscription() == racer && second.is_rescan(),
    "the raced claim's terminal Rescan IS delivered"
  );
  let h = tokio::time::timeout(Duration::from_secs(5), drain)
    .await
    .expect("the drain returns once the raced debt delivered")
    .expect("drain task");
  assert!(h.owner.needs_rescan.is_empty(), "nothing stranded");
}

/// [`Interest::admits`] gates a whole [`EventKind::Moved`] delivery under the `moved()`
/// interest bit (design §5) — directly constructible now that the umbrella owns the
/// source-neutral vocabulary with the move endpoint in-kind (the fs `MovedEvent`
/// payload had no public constructor, so this arm was previously untestable).
#[test]
fn interest_gates_a_whole_moved_by_the_moved_bit() {
  let moved = EventKind::Moved {
    from: key("/a/src/f"),
  };
  assert!(
    Interest::none().with_moved().admits(&moved),
    "a moved-interested gate admits the whole Moved"
  );
  assert!(
    Interest::all().admits(&moved),
    "the widest gate admits it too"
  );
  assert!(
    !Interest::none()
      .with_created()
      .with_removed()
      .admits(&moved),
    "a gate without the moved bit rejects the whole Moved — it is gated by moved(), \
     not by its endpoints' created/removed projections"
  );
}

/// End-to-end interest regression through the driver's fan-out (design §5): the gate
/// applies to the PROJECTED kind, so a created-only subscription receives a plain
/// `Created` AND a move-IN projection (a raw `Moved` whose destination alone it
/// covers, projected to `Created` before the gate) — but neither a `Modified` nor a
/// whole `Moved` (both endpoints covered, gated by the absent `moved` bit and never
/// smuggled through its endpoint projections).
#[tokio::test]
async fn created_only_subscription_receives_created_and_move_in_only() {
  let mut h = Harness::new();
  let sub = h
    .watch("/a", Interest::none().with_created())
    .await
    .expect("watch /a");

  h.owner.fan_out_and_push(&source_created(1, "/a/f", 0));
  h.owner.fan_out_and_push(&source_modified(1, "/a/f", 1));
  // Both endpoints under /a → projected to the whole Moved, rejected by the absent
  // moved bit.
  h.owner
    .fan_out_and_push(&source_moved(1, "/a/src", "/a/dst", 2));
  // Source OUTSIDE /a → projected to the move-in: a synthesized Created, admitted.
  h.owner
    .fan_out_and_push(&source_moved(1, "/outside/g", "/a/arrived", 3));

  let delivered = h.drain();
  assert_eq!(
    delivered.len(),
    2,
    "exactly the plain Created and the move-in projection are delivered, got {delivered:?}"
  );
  assert!(delivered.iter().all(|ev| ev.subscription() == sub));
  assert!(
    delivered[0].kind().is_created() && delivered[0].key() == key("/a/f").as_slice(),
    "the plain Created is admitted by the created bit"
  );
  assert!(
    delivered[1].kind().is_created() && delivered[1].key() == key("/a/arrived").as_slice(),
    "the move-in projection is a plain Created at the destination — admitted by the \
     created bit, not the moved bit"
  );
  assert!(
    delivered[1].move_from().is_none(),
    "the projection carries no move source — it is not a whole Moved"
  );
}

/// Two subscriptions at the SAME path with heterogeneous interest each keep their own gate
/// (design §4/§5): one root, both interests coexist in the side table.
#[tokio::test]
async fn equal_path_heterogeneous_interest() {
  let mut h = Harness::new();
  let created_only = Interest::none().with_created();
  let removed_only = Interest::none().with_removed();

  let s1 = h.watch("/a", created_only).await.expect("watch /a created");
  let s2 = h.watch("/a", removed_only).await.expect("watch /a removed");

  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "equal paths share one kernel watch"
  );
  assert_eq!(
    h.owner.subsumer.subscription_interest(s1),
    Some(created_only)
  );
  assert_eq!(
    h.owner.subsumer.subscription_interest(s2),
    Some(removed_only)
  );
}

/// The canonical-key adoption (design §4, invariant I2): the subsumer is keyed on the
/// source's reported canonical key, not the planned one — so later canonical events route
/// to the creating subscription instead of missing a `starts_with` on the planned key.
#[tokio::test]
async fn canonical_key_uses_source_key_not_the_planned_one() {
  let mut h = Harness::new();

  h.owner.source.retarget("/a/link", "/a/real");
  let sub = h
    .watch("/a/link", Interest::all())
    .await
    .expect("watch /a/link");

  let roots: Vec<PathBuf> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, _)| PathBuf::from_iter(k))
    .collect();
  assert_eq!(
    roots,
    vec![PathBuf::from("/a/real")],
    "the root is keyed on the source's canonical key, not the planned /a/link"
  );
  assert_eq!(
    h.owner.subsumer.subscription_key(sub),
    Some(key("/a/real").as_slice()),
    "the subscription's coverage key is in the source's coordinate"
  );
  assert!(
    key("/a/real/child").starts_with(h.owner.subsumer.subscription_key(sub).unwrap()),
    "a canonical event routes to the creating subscription (no silent drop)"
  );
}

/// The canonical-race abort (design §4, invariant I2): when the source's reported key
/// diverges in a way that changes subsumption (here it lands UNDER an existing root), the
/// owner disarms the just-armed root and aborts cleanly — no mis-keyed entry, no leak.
#[tokio::test]
async fn canonical_race_that_changes_subsumption_aborts_cleanly() {
  let mut h = Harness::new();
  h.watch("/a", Interest::all()).await.expect("watch /a");

  h.owner.source.retarget("/b", "/a/inside");
  let err = h
    .watch("/b", Interest::all())
    .await
    .expect_err("a subsumption-changing canonical race aborts");
  assert!(
    err.is_canonical_race(),
    "the abort is the retryable canonical-race error, got {err:?}"
  );

  let roots: Vec<PathBuf> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, _)| PathBuf::from_iter(k))
    .collect();
  assert_eq!(
    roots,
    vec![PathBuf::from("/a")],
    "no mis-keyed entry lingers"
  );
  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "the aborted plan leaks no pending reservation"
  );
  assert!(
    matches!(h.owner.source.calls().last(), Some(Call::Disarm(_))),
    "the just-armed root was disarmed on abort"
  );
}

/// Regression (design §4, invariant I2 — the Covered-path canonicalization close):
/// a NON-canonical watch key that resolves **under an already-watched canonical root** must be
/// canonicalized BEFORE classification, so the `Covered` subscription is committed on the
/// **canonical** coordinate its events arrive under — not the raw key. The driver canonicalizes
/// every key at the single choke point ([`Source::canonicalize_key`]) ahead of `plan_watch`, so
/// the covered newcomer is keyed on `/root/b` (the resolved coordinate) and a real canonical event
/// under `/root/b` reaches it.
///
/// Fail-on-old: with the raw-key Covered commit the newcomer is keyed on the non-canonical
/// `/root/link`; a canonical `/root/b/file` event fails its ancestor match, so it is delivered
/// NOTHING (no `Rescan` — the root is alive; only THIS subscription's key never matches). The
/// committed-key and delivery assertions then FAIL.
#[tokio::test]
async fn noncanonical_covered_watch_is_canonicalized_then_receives_events() {
  let mut h = Harness::new();

  // An already-watched canonical root (handle 1).
  let s_root = h
    .watch("/root", Interest::all())
    .await
    .expect("watch /root");

  // A non-canonical key under it (a symlinked child) resolving to the canonical `/root/b`.
  h.owner.source.canonicalizes_to("/root/link", "/root/b");
  let s_link = h
    .watch("/root/link", Interest::all())
    .await
    .expect("the covered non-canonical watch is accepted (canonicalized, not rejected)");

  // No fresh arm — it is Covered by /root — but it is committed on the CANONICAL coordinate.
  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "the covered watch arms nothing (subsumed under /root)"
  );
  assert_eq!(
    h.owner.subsumer.subscription_key(s_link),
    Some(key("/root/b").as_slice()),
    "the covered subscription is keyed on the source's canonical coordinate, not the raw \
     /root/link (fail-on-old: committed verbatim as /root/link)"
  );

  // A real canonical event under /root/b reaches the covered subscription — the whole point.
  h.owner
    .fan_out_and_push(&source_modified(1, "/root/b/file", 0));
  let delivered = h.drain();
  assert!(
    delivered
      .iter()
      .any(|ev| ev.subscription() == s_link && !ev.is_rescan()),
    "a canonical event under /root/b reaches the covered subscription (fail-on-old: keyed on \
     /root/link, the canonical event fails its ancestor match → silently misses)"
  );
  assert!(
    delivered
      .iter()
      .any(|ev| ev.subscription() == s_root && !ev.is_rescan()),
    "…and its covering root's own subscription still receives it too"
  );
}

/// Regression (the reject arm): a watch key the source CANNOT canonicalize (the fs
/// source's non-existent-path case) is rejected with [`WatchError::Canonicalize`] at the choke
/// point — never silently committed as an eventless key. Nothing is recorded and no plan leaks.
#[tokio::test]
async fn watch_whose_key_cannot_be_canonicalized_is_rejected() {
  let mut h = Harness::new();

  h.owner.source.cannot_canonicalize("/ghost");
  let err = h
    .watch("/ghost", Interest::all())
    .await
    .expect_err("a non-canonicalizable key is rejected, not committed");
  assert!(
    err.is_canonicalize(),
    "the rejection is a Canonicalize error, got {err:?}"
  );

  assert_eq!(
    h.owner.source.arm_count(),
    0,
    "a rejected watch arms nothing"
  );
  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "no root is recorded for a rejected watch"
  );
  assert_eq!(
    h.owner.subsumer.pending_len(),
    0,
    "the rejected watch leaks no pending reservation (canonicalize fails before plan_watch)"
  );
  assert!(
    !h.owner.subsumer.view().is_watched(&key("/ghost")),
    "the rejected key is never published watched"
  );
}

/// The `EpochLedger` is reclaimed on EVERY successful unwatch (invariant I4): a watch →
/// stamp/repoint → unwatch churn must not grow the ledger's maps unbounded.
#[tokio::test]
async fn unwatch_reclaims_epoch_ledger_across_churn() {
  let mut h = Harness::new();

  for _ in 0..50 {
    let a = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
    h.owner.epochs.stamp(a, Epoch::new(7));
    let wide = h
      .watch("/a", Interest::all())
      .await
      .expect("watch /a widens");
    let _ = h.drain(); // discard the repoint Rescans
    assert!(h.unwatch(a).is_ok(), "a was live");
    assert!(h.unwatch(wide).is_ok(), "wide was live");
  }

  assert_eq!(
    h.owner.epochs.tracked_len(),
    (0, 0),
    "unwatch reclaims epoch base + high_water on every outcome (no unbounded leak)"
  );
  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "all roots released after the churn"
  );
}

/// The source liveness hook (design §4, Step 4 / invariant I4): `retire_if_dead` retires a
/// root exactly when the source has forgotten it ([`Source::root_key`] is `None` — a
/// terminal coverage loss), and keeps a still-live root (an overflow re-enumeration).
/// Retirement frees the root's index + filter + epoch state through the one retire point.
#[tokio::test]
async fn terminal_rescan_retires_root_overflow_keeps_it() {
  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  h.owner.epochs.stamp(sub, Epoch::new(3));

  // Handle 1 is live: an overflow Rescan (root_key is Some) must NOT retire it.
  h.owner.retire_if_dead(&rescan_event(1, "/a", 0));
  assert_eq!(
    h.owner.subsumer.roots().count(),
    1,
    "an overflow Rescan on a still-live root keeps it"
  );
  assert!(
    h.owner.filters.contains_key(&sub),
    "the live root's subscriber state is untouched"
  );

  // The root dies out of band (root_key now None): the terminal Rescan retires it.
  h.owner.source.kill_root(1);
  h.owner.retire_if_dead(&rescan_event(1, "/a", 0));
  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "a terminal Rescan retires the dead root (I4)"
  );
  assert!(
    !h.owner.filters.contains_key(&sub),
    "retirement frees the filter (I4)"
  );
  assert_eq!(
    h.owner.epochs.tracked_len(),
    (0, 0),
    "retirement frees the epoch state (I4)"
  );
}

/// Regression (design §4, invariant I4): a watched root can surface its own
/// deletion as a user-visible NON-`Rescan` terminal event (a `Removed`) that the lower fs layer
/// FOLLOWS with a terminal `Rescan`. If `retire_if_dead` retired the dead root only on the
/// `Rescan` (the old `!raw.is_rescan()` gate), a caller that observes the `Removed` and
/// re-`watch`es the same path BEFORE the queued terminal `Rescan` is processed would be
/// classified `Covered` by the STILL-RECORDED dead root — handed a subscription backed by NO live
/// source watch, which the later `Rescan` then retires: writes under the recreated root are
/// silently missed.
///
/// A dead root (`Source::root_key` is `None`) is retired on ANY terminal event: the `Removed`
/// force-removes it from the coverage index BEFORE control returns, so the re-`watch` is
/// `Disjoint` → a FRESH source arm. The `Removed` is NOT separately fanned out — the
/// coverage loss is owed as the dominating terminal `Rescan` the retire parks, which re-enumerates
/// the subtree; a redundant ordinary `Removed` would be buffered-then-dropped under debounce.
///
/// Fail-on-old: with the `!raw.is_rescan()` gate the `Removed` returns `false` without retiring,
/// the re-watch is `Covered` (no fresh arm) against the dead handle, and `arm_count` stays 1 → the
/// `== 2` assertion FAILS.
#[tokio::test]
async fn dead_root_removed_before_terminal_rescan_retires_so_rewatch_rearms() {
  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  h.owner.epochs.stamp(sub, Epoch::new(3));
  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "the initial watch armed once"
  );

  // The root dies out of band; the fs layer surfaces the deletion as a user-visible `Removed`
  // (root_key now `None`) BEFORE the terminal `Rescan`.
  h.owner.source.kill_root(1);
  let retired = h.owner.retire_if_dead(&source_removed(1, "/a", 0));
  assert!(
    retired,
    "a dead root retires on the non-`Rescan` terminal event too (run loop skips its own fan-out)"
  );

  // The `Removed` is NOT fanned out as an ordinary event: the coverage loss is owed
  // as the dominating terminal `Rescan` the retire parks, so nothing is delivered inline.
  assert!(
    h.drain().is_empty(),
    "no ordinary terminal event is fanned out — the parked Rescan carries the coverage loss"
  );
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "the retired subscription is owed a dominating terminal Rescan (no silent loss)"
  );

  // The dead root is retired NOW (on the `Removed`, before the terminal `Rescan`), so it has left
  // the coverage index and can no longer cover a watch.
  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "the dead root is retired on the `Removed`, not left recorded until the `Rescan`"
  );

  // The caller re-watches the recreated path BEFORE the queued terminal `Rescan` is processed.
  // With the dead root gone it is `Disjoint` → a FRESH source arm, not `Covered` by the dead handle.
  let resub = h.watch("/a", Interest::all()).await.expect("re-watch /a");
  assert_ne!(resub, sub, "the re-watch mints a fresh subscription");
  assert_eq!(
    h.owner.source.arm_count(),
    2,
    "the re-watch triggers a FRESH arm — NOT `Covered` by the dead root (fail-on-old: stays 1)"
  );

  // The re-watch rides the live re-armed handle (2), so a change under the recreated root is
  // delivered — not silently missed as it would be if the sub were `Covered` by the dead handle.
  h.owner.fan_out_and_push(&source_modified(2, "/a/f", 0));
  let delivered = h.drain();
  assert_eq!(
    delivered.len(),
    1,
    "a change under the recreated root is delivered to the re-watch (not silently missed)"
  );
  assert_eq!(
    delivered[0].subscription(),
    resub,
    "…to the fresh subscription riding the live re-armed handle"
  );
}

/// Regression (the STRUCTURAL close of the dead-root-coverage class): the owner loop
/// is command-biased, so a `watch` queued while a dead root's terminal event is still pending runs
/// FIRST — before `retire_if_dead` consumes that event and force-removes the root. Here the source
/// has forgotten the covering root ([`Source::root_key`] is `None`) but its terminal event has NOT
/// yet reached `retire_if_dead`, so the root is still recorded in the coverage index. A re-`watch`
/// of a path that dead root would cover must NOT be classified `Covered` against the
/// source-forgotten handle: `reconcile_watch` validates the covering root's liveness, retires the
/// dead root (owing its subscriber a dominating terminal `Rescan`), re-plans, and arms a FRESH live
/// root — so an event under that recreated root is delivered, not silently missed. Unlike the terminal-retirement
/// path (which retires eagerly on the `Removed`), this closes the window regardless of
/// terminal-event timing: the validation happens at the `watch`, not on the pending terminal event.
///
/// Fail-on-old: without the `Covered` liveness validation the re-watch binds `Covered` to the dead
/// handle, arms nothing (arm_count stays 1) and leaves the dead root recorded, so the event under
/// the fresh handle routes to no live root → the arm-count, surviving-root, and delivery assertions
/// FAIL.
#[tokio::test]
async fn covered_rewatch_validates_liveness_retires_dead_root_and_rearms() {
  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  h.owner.epochs.stamp(sub, Epoch::new(3));
  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "the initial watch armed once"
  );

  // The covering root dies out of band (`root_key` → None) but its terminal event has NOT yet been
  // processed by `retire_if_dead`: the command-biased loop runs the queued re-`watch` first, with
  // the dead root still recorded in the coverage index.
  h.owner.source.kill_root(1);
  assert_eq!(
    h.owner.subsumer.roots().count(),
    1,
    "the dead root is still RECORDED (its terminal event has not yet retired it)"
  );

  // A re-`watch` of a path the dead root WOULD cover. The structural fix validates the covering
  // root's liveness, finds it dead, retires it, re-plans → `Disjoint` → a FRESH arm.
  let resub = h
    .watch("/a/b", Interest::all())
    .await
    .expect("re-watch /a/b");
  assert_ne!(resub, sub, "the re-watch mints a fresh subscription");
  assert_eq!(
    h.owner.source.arm_count(),
    2,
    "the re-watch validates liveness, retires the dead root, and arms a FRESH live root \
     (fail-on-old: binds `Covered` to the dead handle → stays 1)"
  );

  // The dead /a root left the coverage index; only the fresh /a/b root remains — not two roots,
  // and not the dead one still recorded.
  let roots: Vec<Vec<OsString>> = h.owner.subsumer.roots().map(|(k, _)| k.to_vec()).collect();
  assert_eq!(
    roots,
    vec![key("/a/b")],
    "the dead /a root is retired and replaced by the fresh /a/b root (fail-on-old: /a survives)"
  );

  // The old subscriber's per-sub state is freed, and it is owed a dominating terminal Rescan — the
  // retire-and-replan loses no coverage.
  assert!(
    !h.owner.filters.contains_key(&sub),
    "the dead root's subscriber state is freed on retirement (I4)"
  );
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "…and it is owed a dominating terminal Rescan (no silent loss)"
  );

  // An event under the FRESH live root (handle 2) reaches the re-watch — the whole point: the
  // newcomer rides a live source watch, not the dead handle.
  h.owner.fan_out_and_push(&source_modified(2, "/a/b/f", 0));
  let delivered = h.drain();
  assert_eq!(
    delivered.len(),
    1,
    "a change under the recreated root reaches the re-watch (fail-on-old: bound to the dead handle → 0)"
  );
  assert_eq!(
    delivered[0].subscription(),
    resub,
    "…to the fresh subscription riding the live re-armed handle"
  );
}

/// Regression (design §6 / backpressure doc, no silent loss): a dead root's terminal
/// coverage loss must reach the consumer as the durable, strictly-dominating terminal `Rescan` the
/// retire primitive parks — NOT as an ordinary `Removed` fanned through the debounce coalescer. The
/// earlier path fanned the non-`Rescan` terminal event through `fan_out_and_push`; with
/// debounce that admits it to the coalescer, where — depending on the settle window — it is either
/// buffered-then-dropped by the retire's `drop_subscription` (silently losing the promised event)
/// or surfaces as a redundant second terminal event dominated by the parked Rescan. Either way it is
/// redundant with the dominating terminal Rescan that re-enumerates the subtree, so the fix stops
/// fanning it out.
///
/// The settle window is zero, so a lifecycle event fanned through the coalescer drains at once — the
/// visible face of the buffer-through fan-out the fix removes (a longer window would instead
/// buffer-then-drop it, an equally-wrong silent loss that is simply not observable in the end
/// state).
///
/// Fail-on-old: with `fan_out_and_push(raw)` still routing the `Removed` through the coalescer, the
/// consumer receives that redundant `Removed` in addition to the terminal Rescan → the "nothing
/// reaches the consumer through the coalescer" assertion FAILS.
#[tokio::test]
async fn dead_root_terminal_removed_under_debounce_signals_via_parked_rescan_only() {
  // Debounce ENABLED with an immediate-settle window, so a lifecycle event fanned through the
  // coalescer drains at once rather than buffering — making the buffer-through fan-out observable.
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_millis(0))
    .with_max_hold(Duration::from_millis(0));
  let mut h = Harness::with_coalescer(Some(Coalescer::new(Some(cfg))));
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  h.owner.epochs.stamp(sub, Epoch::new(3));

  // The root dies out of band; the fs surfaces the deletion as a NON-`Rescan` terminal `Removed`.
  h.owner.source.kill_root(1);
  let retired = h.owner.retire_if_dead(&source_removed(1, "/a", 0));
  assert!(retired, "the dead root retires on the terminal `Removed`");

  // No ordinary terminal event reaches the consumer through the coalescer (fail-on-old: the fanned
  // `Removed` drains here under the immediate-settle window).
  assert!(
    h.drain().is_empty(),
    "the dead-root terminal event is NOT fanned through the coalescer (fail-on-old: a redundant \
     `Removed` drains)"
  );
  // Nothing dangles buffered in the coalescer for the retired subscription.
  assert!(
    h.owner
      .coalescer
      .as_ref()
      .and_then(Coalescer::next_deadline)
      .is_none(),
    "no coalescer entry dangles for the retired subscription"
  );
  // The coverage loss is owed as the durable dominating terminal Rescan.
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "the coverage loss is owed as a parked dominating terminal Rescan"
  );

  // It self-drains on the next flush — the consumer learns the root is gone via exactly one Rescan.
  h.owner.flush_pending_rescans();
  let delivered = h.drain();
  assert_eq!(
    delivered.len(),
    1,
    "exactly the terminal Rescan reaches the consumer"
  );
  assert!(
    delivered[0].is_rescan(),
    "…and it is the coverage-loss Rescan"
  );
  assert_eq!(
    delivered[0].subscription(),
    sub,
    "…for the subscription whose root died"
  );
  assert_eq!(
    delivered[0].path(),
    Path::new("/a"),
    "…naming its covered key, which the consumer re-enumerates to discover the root is gone"
  );
}

/// The one residual "dropped wait" case (design driver-golden doc, invariant I1): a `watch` whose
/// caller vanished before the owner could hand back the grant (its reply `oneshot` is closed at send
/// time) is orphaned — its owner-local state (subsumer record, filter, epoch) is purged and, being
/// the root's last subscriber, its kernel watch is released through the **synchronous**
/// [`Source::disarm`] request. The release awaits nothing, so a `Close` queued behind this `Watch` is
/// never blocked on source I/O (invariant II — Close-responsive by construction).
///
/// Non-vacuous: asserts the orphan's owner-local state is fully purged, its last-subscriber `Disarm`
/// op is recorded at cleanup, and the released handle is logically dead immediately
/// (`root_key` → None). Fail-on-old (the pre-golden awaited disarm could block the owner): here the
/// synchronous `release_subscription` records the release at once and never yields.
#[tokio::test]
async fn caller_vanished_after_commit_releases_the_orphan_synchronously() {
  let mut h = Harness::new();

  let (reply, response) = futures_channel::oneshot::channel();
  drop(response); // the caller's wait vanished before the reconcile ran

  h.owner
    .on_watch(key("/a"), (), WatchOptions::new(), reply)
    .await;

  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "the orphaned subscription's owner-local state was purged"
  );
  assert!(h.owner.filters.is_empty(), "its filter state was reclaimed");
  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "the watch armed once (it committed) before being orphaned"
  );
  // The last-subscriber disarm ran SYNCHRONOUSLY at cleanup: a `Disarm(1)` is recorded and the handle
  // is logically dead immediately (`root_key` → None) — no deferral, no await, nothing to block a
  // `Close` queued behind this `Watch`.
  assert!(
    h.owner.source.calls().contains(&Call::Disarm(1)),
    "the committed root's last-subscriber disarm was requested synchronously at cleanup"
  );
  assert_eq!(
    h.owner.source.root_key(1),
    None,
    "the released handle is logically dead immediately"
  );
}

/// The post-commit orphan window (design driver-golden doc, invariant I1): a `watch`
/// whose caller's wait is dropped **after** the owner committed and **successfully sent** the reply,
/// but **before** the wait observed it, must not strand the committed subscription. The reply carries
/// a RAII `WatchGrant`, not a bare `Subscription`; dropping the reply `Receiver` (the vanished wait)
/// drops the grant, whose `Drop` enqueues a `DropOrphan` the owner reconciles away — purging the
/// filter and epoch state and releasing the root's kernel watch through the **synchronous**
/// [`Source::disarm`] request (never an awaited teardown that could block a `Close` behind it).
///
/// This is the residual hole a bare-`Subscription` reply left open: a successful `send` only proves
/// the receiver existed at that instant, never that it polls the value. Distinct from
/// `caller_vanished_after_commit_releases_the_orphan_synchronously`, which drops the receiver
/// **before** the send (the immediate-reconcile edge); here the send succeeds and the grant's `Drop`
/// is the only thing that can detect the drop.
///
/// Fail-on-old: with the bare reply (no grant `Drop`), dropping the receiver drops only the
/// subscription value — nothing is enqueued and the committed subscription stays live — so the
/// `try_recv().expect(..)` (no `DropOrphan` present) and every purge assertion FAIL.
#[tokio::test]
async fn watch_wait_dropped_after_commit_reconciles_the_orphan_away() {
  let mut h = Harness::new();

  // Drive the owner to COMMIT the watch and SUCCESSFULLY send the reply: `response` is held here,
  // so the grant lands in the `oneshot` slot — exactly the post-send, pre-poll window.
  let (reply, response) = futures_channel::oneshot::channel();
  h.owner
    .on_watch(key("/a"), (), WatchOptions::new(), reply)
    .await;
  assert_eq!(
    h.owner.source.arm_count(),
    1,
    "the watch armed once — it committed before the wait was dropped"
  );
  assert!(
    h.owner.subsumer.view().is_watched(&key("/a")),
    "the committed subscription reads watched while the grant is still in flight"
  );

  // The caller's wait vanishes in the post-send-pre-poll window: dropping the receiver drops the
  // grant sitting in the slot, whose `Drop` enqueues a reply-less `Cleanup::DropOrphan` on the
  // dedicated cleanup channel.
  drop(response);

  // Process that `Cleanup::DropOrphan` exactly as the run loop would. `try_recv` (not `recv().await`)
  // so the fail-on-old path — where no cleanup was enqueued — asserts cleanly instead of hanging.
  let cleanup = h
    .owner
    .cleanup_rx
    .try_recv()
    .expect("the dropped grant enqueued a DropOrphan cleanup notice");
  assert!(
    matches!(cleanup, super::Cleanup::DropOrphan(_)),
    "the dropped grant must enqueue exactly a Cleanup::DropOrphan"
  );
  // Apply it exactly as the run loop's cleanup drain does: release the orphan through the unified
  // `release_subscription` — purge owner-local state and request the emptied root's synchronous
  // `source.disarm`.
  h.owner.apply_cleanup(cleanup);

  // The orphan's owner-local state is fully purged — subsumer record, filter, and epoch all released.
  assert!(
    !h.owner.subsumer.view().is_watched(&key("/a")),
    "the orphaned subscription is no longer watched (reconciled away)"
  );
  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "its subsumer root/record is released"
  );
  assert!(h.owner.filters.is_empty(), "its filter entry is purged");
  assert_eq!(
    h.owner.epochs.tracked_len(),
    (0, 0),
    "its epoch state is purged"
  );
  // The kernel disarm ran SYNCHRONOUSLY as part of the DropOrphan dispatch: a `Disarm(1)` is recorded
  // and the released handle is logically dead immediately (`root_key` → None) — no deferral, so a
  // `Close` behind this `DropOrphan` is never blocked on source I/O.
  assert!(
    h.owner.source.calls().contains(&Call::Disarm(1)),
    "the committed root's last-subscriber disarm was requested synchronously at cleanup"
  );
  assert_eq!(
    h.owner.source.root_key(1),
    None,
    "the released handle is logically dead immediately"
  );
}

/// Backpressure (design backpressure doc, checklist #1/#4/#5): a **stalled consumer** fills
/// the bounded event channel, so the owner sheds the affected subscription to a parked
/// dominating `Rescan` instead of blocking or growing memory without bound. The owner never
/// blocks (every `try_emit` returns synchronously); repeated overflow is idempotent (one
/// parked slot, monotone epoch); and on resume the consumer receives exactly one `Rescan`
/// whose epoch strictly dominates every event delivered before it — no silent loss.
#[tokio::test]
async fn stalled_consumer_parks_dominating_rescan_and_resumes() {
  let mut h = Harness::bounded(2);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  // Drive the subscription's epoch high-water up, as genuine deliveries would.
  for raw in 0..3 {
    h.owner.epochs.stamp(sub, Epoch::new(raw));
  }

  // The consumer is stalled (not draining): the two-slot channel fills in-order.
  h.owner.try_emit(modified_event(sub, "/a/f0", 0));
  h.owner.try_emit(modified_event(sub, "/a/f1", 1));
  // The next delivery finds the channel full → shed to a parked dominating Rescan. The call
  // returns synchronously (the owner never awaits the channel — no block, no unbounded
  // growth).
  h.owner.try_emit(modified_event(sub, "/a/f2", 2));
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.epoch),
    Some(Epoch::new(3)),
    "overflow parked a Rescan minted one past the high-water (strictly dominating)"
  );

  // Further overflow while parked is SUPPRESSED and idempotent: no second Rescan is minted,
  // the parked epoch is unchanged, and the channel is not probed again.
  h.owner.try_emit(modified_event(sub, "/a/f3", 3));
  assert_eq!(
    h.owner.needs_rescan.len(),
    1,
    "repeated overflow collapses to one parked Rescan"
  );
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.epoch),
    Some(Epoch::new(3)),
    "the parked epoch is idempotent under repeated overflow"
  );

  // Resume: the consumer drains the two buffered (pre-overflow) deliveries.
  let buffered = h.drain();
  assert_eq!(
    buffered.len(),
    2,
    "the pre-overflow events buffered in-order, not lost"
  );
  assert!(
    buffered.iter().all(|e| !e.is_rescan()),
    "the buffered events are the ordinary deliveries"
  );

  // On the next loop tick the owner retries the parked Rescan; now there is room.
  h.owner.flush_pending_rescans();
  assert!(
    h.owner.needs_rescan.is_empty(),
    "the parked Rescan was delivered on resume"
  );
  let resumed = h.drain();
  assert_eq!(
    resumed.len(),
    1,
    "exactly the dominating Rescan is delivered on resume"
  );
  let rescan = &resumed[0];
  assert!(rescan.is_rescan(), "the shed signal is a Rescan");
  assert_eq!(rescan.subscription(), sub, "…for the affected subscription");
  assert_eq!(
    rescan.path(),
    Path::new("/a"),
    "…naming its covered key to re-enumerate"
  );
  let max_delivered = buffered
    .iter()
    .map(Event::epoch)
    .max()
    .expect("two buffered events");
  assert!(
    rescan.epoch() > max_delivered,
    "the shed Rescan strictly dominates every event delivered before it (no silent loss)"
  );
}

/// Fairness (design backpressure doc): a parked overflow `Rescan` for one subscription
/// never blocks delivery to ANOTHER. With a full channel, subscription A overflows and
/// parks; once a slot drains, an event for subscription B flows through immediately, while a
/// further A delivery is suppressed (dominated by A's still-parked Rescan) rather than
/// jumping ahead of it.
#[tokio::test]
async fn parked_rescan_does_not_block_other_subscriptions() {
  let mut h = Harness::bounded(1);
  let sa = h.watch("/a", Interest::all()).await.expect("watch /a");
  let sb = h.watch("/b", Interest::all()).await.expect("watch /b");

  // Fill the single slot with an A delivery, then overflow A → park A's Rescan. B untouched.
  h.owner.try_emit(modified_event(sa, "/a/f0", 0));
  h.owner.try_emit(modified_event(sa, "/a/f1", 1));
  assert!(
    h.owner.needs_rescan.contains_key(&sa),
    "A overflowed and parked a Rescan"
  );
  assert!(
    !h.owner.needs_rescan.contains_key(&sb),
    "B is unaffected by A's overflow"
  );

  // The consumer makes progress: drain the one buffered A delivery.
  assert_eq!(h.drain().len(), 1, "the pre-overflow A delivery drains");

  // Now a B delivery flows even though A remains parked (fairness), while a further A
  // delivery is suppressed by A's parked Rescan (never delivered ahead of it).
  h.owner.try_emit(modified_event(sb, "/b/f0", 0));
  h.owner.try_emit(modified_event(sa, "/a/f2", 2));
  let after = h.drain();
  assert_eq!(after.len(), 1, "only B's delivery flows; A's is suppressed");
  assert_eq!(
    after[0].subscription(),
    sb,
    "the delivered event belongs to the unparked B"
  );
  assert!(
    h.owner.needs_rescan.contains_key(&sa),
    "A stays parked until its Rescan is flushed"
  );
}

/// Root death with no silent loss on a full channel (design backpressure doc): a watched root
/// **dies while the event channel is full**. The run loop fans out the terminal coverage-loss
/// `Rescan` and THEN retires the dead root, on the same source event. With the channel full
/// the terminal Rescan is *parked*, and retirement must **keep** it (unlike a
/// consumer-initiated unwatch, which drops it) so the resuming consumer still learns the root
/// is gone. Regression test for the co-retire bug where `retire_if_dead` dropped the owed
/// Rescan in the very tick it was parked, leaving the consumer permanently stale.
#[tokio::test]
async fn root_death_while_channel_full_keeps_owed_rescan() {
  let mut h = Harness::bounded(2);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  // Drive the subscription's epoch high-water up, as genuine deliveries would.
  for raw in 0..3 {
    h.owner.epochs.stamp(sub, Epoch::new(raw));
  }

  // The consumer is stalled (not draining): the two-slot channel fills in-order.
  h.owner.try_emit(modified_event(sub, "/a/f0", 0));
  h.owner.try_emit(modified_event(sub, "/a/f1", 1));

  // The root dies out of band. Reproduce the run loop's same-event ordering: fan out the
  // terminal Rescan first, then retire. The fan-out finds the channel full, so the terminal
  // coverage-loss Rescan overflows and parks. It is an already-minted `Rescan`, so it parks at its
  // OWN dominating epoch — its umbrella stamp `base + raw` = 0 + 3, past the high-water of 2 — not a
  // fresh `shed_rescan`; for a source-overflow Rescan on a live root that is the same
  // strictly-dominating value.
  h.owner.source.kill_root(1);
  h.owner.fan_out_and_push(&rescan_event(1, "/a", 3));
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.epoch),
    Some(Epoch::new(3)),
    "the overflowed terminal Rescan parked at its own dominating epoch (base + raw = 3)"
  );

  // Retiring the dead root frees its filter + epoch but must KEEP the parked terminal Rescan
  // — dropping it here is the silent-loss regression this test guards.
  h.owner.retire_if_dead(&rescan_event(1, "/a", 0));
  assert_eq!(
    h.owner.subsumer.roots().count(),
    0,
    "the dead root is retired"
  );
  assert!(
    !h.owner.filters.contains_key(&sub),
    "retirement frees the dead root's filter (I4)"
  );
  assert_eq!(
    h.owner.epochs.tracked_len(),
    (0, 0),
    "retirement frees the dead root's epoch state (I4)"
  );
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "retirement KEEPS the owed terminal Rescan (no silent loss on root death)"
  );

  // Resume: the consumer drains the two buffered pre-death deliveries.
  let buffered = h.drain();
  assert_eq!(
    buffered.len(),
    2,
    "the pre-death events buffered in-order, not lost"
  );

  // The next loop tick retries the parked Rescan; now there is room, so it is delivered.
  h.owner.flush_pending_rescans();
  assert!(
    h.owner.needs_rescan.is_empty(),
    "the parked terminal Rescan self-drained on resume"
  );
  let resumed = h.drain();
  assert_eq!(
    resumed.len(),
    1,
    "exactly the terminal Rescan is delivered on resume — the consumer is not left stale"
  );
  let rescan = &resumed[0];
  assert!(rescan.is_rescan(), "the coverage-loss signal is a Rescan");
  assert_eq!(
    rescan.subscription(),
    sub,
    "…for the subscription whose root died"
  );
  assert_eq!(
    rescan.path(),
    Path::new("/a"),
    "…naming its covered key, which the consumer re-enumerates to discover the root is gone"
  );
  let max_delivered = buffered
    .iter()
    .map(Event::epoch)
    .max()
    .expect("two buffered events");
  assert!(
    rescan.epoch() > max_delivered,
    "the terminal Rescan strictly dominates every event delivered before it (no silent loss)"
  );
}

/// Regression (design backpressure doc §8, epoch calibration / no silent loss): a **widen
/// while the event channel is FULL** so the synthetic re-point `Rescan` overflows into
/// `needs_rescan`. It must park at its OWN epoch — the `repoint` base its new root's genuine events
/// are calibrated to tie — NOT a fresh `shed_rescan` (one past the high-water). Parking at
/// `shed_rescan` (high-water + 1) leaves the parked `Rescan` one *above* the new root's
/// raw-epoch-0 event, so a dominance-applying consumer drops that post-widen event as "dominated"
/// even though it happened AFTER the re-enumeration → silent loss under backpressure.
///
/// Fail-on-old: with the old unconditional `park_rescan`, the parked/delivered `Rescan` is epoch 6
/// (high-water 4 → repoint 5 → shed 6), one above the new root's raw-0 stamp (5) → both the
/// `needs_rescan == 5` and the `raw-0 not below the Rescan` assertions FAIL.
#[tokio::test]
async fn widen_repoint_rescan_parks_at_own_epoch_not_shed_when_channel_full() {
  let mut h = Harness::bounded(2);
  let sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b"); // root 1
  // Drive sb's high-water to 4, as genuine deliveries would.
  for raw in 0..5 {
    h.owner.epochs.stamp(sb, Epoch::new(raw));
  }

  // FILL both slots so the widen's re-point Rescan must overflow-park. `try_emit` never re-stamps a
  // pre-stamped delivery, so these fillers leave sb's high-water at 4.
  h.owner.try_emit(modified_event(sb, "/a/b/f0", 0));
  h.owner.try_emit(modified_event(sb, "/a/b/f1", 1));
  assert!(
    h.owner.needs_rescan.is_empty(),
    "the fillers took both slots without overflowing yet"
  );

  // Widen /a/b → /a: sb is re-pointed. `repoint` rebases sb's epoch_base to high-water.next() = 5
  // and mints the re-point Rescan at 5; sb's new root (handle 2) will stamp its raw-0/raw-1 events
  // 5 + 0 and 5 + 1. The `push_all` of that Rescan finds the channel full → it overflows.
  h.watch("/a", Interest::all())
    .await
    .expect("watch /a widens");

  // the overflowed re-point Rescan parks at its OWN epoch (the repoint base 5), NOT a fresh
  // shed_rescan (high-water.next() = 6). Parking at 6 would sort the new root's raw-0 event (5)
  // below it and drop it as dominated.
  assert_eq!(
    h.owner.needs_rescan.get(&sb).map(|p| p.epoch),
    Some(Epoch::new(5)),
    "the re-point Rescan parked at the repoint base (5), not shed_rescan (6)"
  );

  // Resume: drain the two fillers, then flush the parked re-point Rescan.
  assert_eq!(h.drain().len(), 2, "the two pre-widen fillers drained");
  h.owner.flush_pending_rescans();
  assert!(
    h.owner.needs_rescan.is_empty(),
    "the parked re-point Rescan was delivered on resume"
  );
  let rescan = h
    .drain()
    .into_iter()
    .find(|e| e.subscription() == sb && e.is_rescan())
    .expect("the re-point Rescan was delivered");
  assert_eq!(rescan.path(), Path::new("/a"), "it names the widened root");
  let rescan_epoch = rescan.epoch();
  assert_eq!(
    rescan_epoch,
    Epoch::new(5),
    "the delivered re-point Rescan carries the repoint base (5), not shed_rescan (6)"
  );

  // sb's new root (handle 2) now delivers genuine events; they stamp base + raw = 5 + 0, 5 + 1.
  // Drain after each so the two co-subscribers (sb and the new /a watch) never overflow the
  // two-slot channel.
  h.owner.fan_out_and_push(&source_modified(2, "/a/b/g0", 0));
  let raw0 = h
    .drain()
    .into_iter()
    .find(|e| e.subscription() == sb)
    .expect("sb's new-root raw-0 event was delivered, not suppressed");
  h.owner.fan_out_and_push(&source_modified(2, "/a/b/g1", 1));
  let raw1 = h
    .drain()
    .into_iter()
    .find(|e| e.subscription() == sb)
    .expect("sb's new-root raw-1 event was delivered");

  // The park-unchanged payoff: the new root's raw-0 (epoch 5) is NOT below the delivered Rescan (epoch 5) — it
  // ties, so a dominance-applying consumer keeps it. With the old shed_rescan (Rescan at 6), raw-0
  // (5) sorts BELOW it → dropped as dominated → silent loss of a post-widen change.
  assert_eq!(raw0.epoch(), Epoch::new(5), "raw-0 stamps the repoint base");
  assert_eq!(raw1.epoch(), Epoch::new(6), "raw-1 stamps base + 1");
  assert!(
    raw0.epoch() >= rescan_epoch,
    "the new root's raw-0 genuine event is not dominated by the re-point Rescan (no silent loss)"
  );
  assert!(
    raw1.epoch() >= rescan_epoch,
    "…and raw-1 is not dominated either"
  );
}

/// Sibling (the coalescer-buffered-delta variant of the re-point-epoch hole): when a
/// re-pointed subscription has **buffered pre-widen deltas** in the coalescer and the channel is
/// FULL, `Coalescer::admit(rescan)` flushes those deltas AHEAD of the re-point `Rescan` in
/// `push_all`; the first flushed ordinary delta hits `Full` and parks via `park_rescan` at a fresh
/// `shed_rescan` (one above the repoint base), suppressing the Rescan behind it and dropping the new
/// root's raw-0 as dominated — the same silent loss the direct-overflow fix closed, via the buffer.
/// The fix drops a re-pointed sub's coalescer buffer BEFORE its re-point Rescan (the Rescan
/// dominates those deltas), so nothing flushes ahead and the Rescan parks at its own repoint base.
///
/// Fail-on-old: with the `drop_subscription` before the widen re-point push removed, the buffered
/// delta flushes ahead, parks at `shed_rescan` (high-water 5 → 6), and the parked epoch is 6, not 5.
#[tokio::test]
async fn widen_drops_buffered_coalescer_delta_so_repoint_rescan_parks_at_own_epoch() {
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let mut h = Harness::build(Some(Coalescer::new(Some(cfg))), Some(2));
  let sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b"); // root 1
  for raw in 0..5 {
    h.owner.epochs.stamp(sb, Epoch::new(raw)); // high-water 4
  }

  // A pre-widen delta BUFFERS in the coalescer (long quiet window → admit runs but nothing drains).
  // It is pre-stamped, so it does not move sb's high-water off 4.
  h.owner
    .push_all(vec![modified_event(sb, "/a/b/buffered", 3)]);
  assert!(
    h.owner.needs_rescan.is_empty(),
    "the buffered delta is held in the coalescer, not overflowed"
  );

  // Fill both channel slots so the widen's re-point Rescan push must overflow-park.
  h.owner.try_emit(modified_event(sb, "/a/b/f0", 0));
  h.owner.try_emit(modified_event(sb, "/a/b/f1", 1));

  // Widen /a/b → /a: `repoint` rebases sb's base to high-water.next() = 5 and mints the re-point
  // Rescan at 5. The fix drops sb's coalescer buffer before pushing that Rescan, so the buffered
  // delta cannot flush ahead of it and park at a fresh `shed_rescan` (6).
  h.watch("/a", Interest::all())
    .await
    .expect("watch /a widens");

  assert_eq!(
    h.owner.needs_rescan.get(&sb).map(|p| p.epoch),
    Some(Epoch::new(5)),
    "the buffered delta was dropped, so the re-point Rescan parked at the repoint base (5), \
     not a fresh shed_rescan (6) minted by a buffered delta flushing ahead of it"
  );
}

/// Regression (design backpressure doc, no silent loss): while a subscription is PARKED
/// (its overflow `Rescan` sits in `needs_rescan`), a later SOURCE `Rescan` for a DIFFERENT key
/// under the same root must NOT be discarded — it is an independent coverage-loss signal. The old
/// `try_emit` early-returned for every event of a parked sub, so the second Rescan's subtree was
/// never re-enumerated. The fix merges it into the parked debt, widening the key to the common
/// ancestor that covers BOTH losses.
///
/// Fail-on-old: with the unconditional early return (no merge), the parked key stays `/a/x` and the
/// eventually-delivered Rescan never covers `/a/y` → the common-ancestor assertion FAILS.
#[tokio::test]
async fn a_source_rescan_while_parked_merges_coverage_instead_of_being_dropped() {
  let mut h = Harness::bounded(2);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a"); // root handle 1
  for raw in 0..3 {
    h.owner.epochs.stamp(sub, Epoch::new(raw)); // high-water 2
  }

  // FILL both channel slots so the first source Rescan must overflow-park.
  h.owner.try_emit(modified_event(sub, "/a/f0", 0));
  h.owner.try_emit(modified_event(sub, "/a/f1", 1));

  // A source Rescan for /a/x overflows → parks at its own located key.
  h.owner.fan_out_and_push(&rescan_event(1, "/a/x", 5));
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.key.clone()),
    Some(key("/a/x")),
    "the first source Rescan parked at its own key /a/x"
  );

  // A SECOND source Rescan for a DIFFERENT key /a/y arrives while parked. It must be MERGED, not
  // discarded: the parked key widens to the common ancestor /a, re-enumerating a superset that
  // covers BOTH /a/x and /a/y.
  h.owner.fan_out_and_push(&rescan_event(1, "/a/y", 6));
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.key.clone()),
    Some(key("/a")),
    "the second source Rescan merged into the parked debt, widening the key to the common \
     ancestor /a that covers both losses (not dropped, not left at /a/x)"
  );
}

/// Source-drain no-silent-loss (design backpressure doc, checklist #1):
/// a per-subscription overflow `Rescan` is **parked while the event channel is full**, then
/// the source drains (`next` → `None`) at teardown. The owner must deliver that owed Rescan
/// **before** the stream ends, retrying across the full channel until the resuming consumer
/// frees a slot — never dropping it. Regression: the old teardown flushed only the coalescer
/// tail, dropping the parked Rescan, so a consumer that resumed after source-drain reached
/// stream-end permanently stale. Exercises the exact drain the source-`None` break runs.
#[tokio::test]
async fn source_drain_delivers_owed_parked_rescan_no_silent_loss() {
  let mut h = Harness::bounded(1);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  for raw in 0..2 {
    h.owner.epochs.stamp(sub, Epoch::new(raw));
  }

  // Fill the one slot, then overflow → park a dominating Rescan (the channel stays full).
  h.owner.try_emit(modified_event(sub, "/a/f0", 0));
  h.owner.try_emit(modified_event(sub, "/a/f1", 1));
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.epoch),
    Some(Epoch::new(2)),
    "overflow parked a dominating Rescan while the channel is full"
  );

  // The source drains at teardown. `drain_owed_before_shutdown` retries the owed Rescan across
  // the full channel while the consumer resumes, delivering it before stream end. Run the
  // drain concurrently with the resuming consumer so the retry-under-full path is exercised.
  let events = h.events.clone();
  let owner = &mut h.owner;
  let consumer = async {
    let buffered = events
      .recv()
      .await
      .expect("the buffered pre-overflow event drains first");
    let owed = events
      .recv()
      .await
      .expect("then the owed Rescan is delivered (not dropped)");
    (buffered, owed)
  };
  // Bounded, so a regression (the owed Rescan never delivered) fails cleanly, not hangs.
  let (_, (buffered, owed)) = tokio::time::timeout(Duration::from_secs(10), async {
    tokio::join!(owner.drain_owed_before_shutdown(), consumer)
  })
  .await
  .expect("source-drain teardown delivered the owed Rescan before the deadline (no silent loss)");

  assert!(
    !buffered.is_rescan(),
    "the first delivered event is the buffered ordinary one, in order"
  );
  assert!(owed.is_rescan(), "the owed shed signal is a Rescan");
  assert_eq!(owed.subscription(), sub, "…for the overflowed subscription");
  assert_eq!(
    owed.path(),
    Path::new("/a"),
    "…naming its covered key to re-enumerate"
  );
  assert!(
    owed.epoch() > buffered.epoch(),
    "the owed Rescan strictly dominates every event delivered before it (no silent loss)"
  );
  assert!(
    owner.needs_rescan.is_empty(),
    "source-drain teardown delivered the owed Rescan — nothing left parked"
  );
}

/// Source cancel-safety is load-bearing (design source doc hard contract):
/// the owner drives `source.next()` as one `select!` arm, so a competing command/timer branch
/// **drops the in-flight `next()` future**. A contract-conforming source that dequeues on the
/// poll that returns `Ready` loses nothing across arbitrarily many such cancellations; a source
/// that dequeues on poll START silently loses the in-flight event (the owner parks no Rescan —
/// it never saw it). This reproduces the owner's cancel-then-retry pattern with both a
/// conforming and a violating source, proving the documented contract is what keeps the owner's
/// inline `next()` lossless.
#[tokio::test]
async fn source_next_cancellation_is_lossless_only_when_cancel_safe() {
  use futures_util::FutureExt;

  /// Cancel-SAFE: yields (returns `Pending`) BEFORE consuming, so a poll cancelled here
  /// consumed nothing — the dequeue happens only on the poll that returns `Ready`.
  struct CancelSafe {
    queue: VecDeque<SourceEvent<OsString, u32>>,
    consumed: u32,
  }
  impl Source<OsString> for CancelSafe {
    type Handle = u32;
    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      Ok(key.to_vec())
    }
    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      Ok(Armed::new(1, key.to_vec()))
    }
    fn disarm(&mut self, _handle: u32) {}
    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      tokio::task::yield_now().await; // Pending once BEFORE the dequeue → cancel-safe
      self.consumed += 1;
      self.queue.pop_front()
    }
    fn root_key(&self, _handle: u32) -> Option<Vec<OsString>> {
      Some(key("/w"))
    }
  }

  /// Cancel-UNSAFE: dequeues on poll START and holds the event in the future's local across
  /// the yield — so a cancellation drops the popped event (silent loss, no Rescan owed).
  struct CancelUnsafe {
    queue: VecDeque<SourceEvent<OsString, u32>>,
    lost: u32,
  }
  impl Source<OsString> for CancelUnsafe {
    type Handle = u32;
    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      Ok(key.to_vec())
    }
    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      Ok(Armed::new(1, key.to_vec()))
    }
    fn disarm(&mut self, _handle: u32) {}
    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      let popped = self.queue.pop_front(); // dequeue on poll START (the bug)
      if popped.is_some() {
        self.lost += 1; // provisionally lost until it survives to be returned
      }
      tokio::task::yield_now().await; // a cancellation here drops `popped` → truly lost
      if popped.is_some() {
        self.lost -= 1; // survived to return → not lost
      }
      popped
    }
    fn root_key(&self, _handle: u32) -> Option<Vec<OsString>> {
      Some(key("/w"))
    }
  }

  const N: u64 = 6;
  const CANCELS: u32 = 2;

  /// Reproduces the owner's `select!` arm: `next()` is polled FIRST (as in the loop), yields
  /// `Pending`, then the ready "interrupt" (a stand-in for a command/timer branch) wins → the
  /// in-flight `next()` is dropped. `CANCELS` cancellations, then one `next()` runs to
  /// completion — repeated until the source drains. Returns the delivered events and the
  /// cancellation count.
  async fn drive<S>(source: &mut S) -> (Vec<SourceEvent<OsString, u32>>, u32)
  where
    S: Source<OsString, Handle = u32>,
  {
    let mut delivered = Vec::new();
    let mut cancels = 0u32;
    loop {
      for _ in 0..CANCELS {
        futures_util::select_biased! {
          ev = source.next().fuse() => if let Some(event) = ev { delivered.push(event); },
          _  = std::future::ready(()).fuse() => cancels += 1,
        }
      }
      match source.next().await {
        Some(event) => delivered.push(event),
        None => break,
      }
    }
    (delivered, cancels)
  }

  let queued = || -> VecDeque<_> {
    (0..N)
      .map(|i| source_modified(1, &format!("/w/f{i}"), i))
      .collect()
  };
  let expected: Vec<Vec<OsString>> = (0..N).map(|i| key(&format!("/w/f{i}"))).collect();

  // A cancel-safe source loses NOTHING despite repeated cancellation.
  let mut safe = CancelSafe {
    queue: queued(),
    consumed: 0,
  };
  let (delivered, cancels) = drive(&mut safe).await;
  assert!(
    cancels > 0,
    "the select actually cancelled in-flight next() futures"
  );
  assert_eq!(
    delivered
      .iter()
      .map(|e| e.key().to_vec())
      .collect::<Vec<_>>(),
    expected,
    "a cancel-safe source delivers every event, in order — no loss across cancellation"
  );

  // A cancel-UNSAFE source (dequeue on poll start) silently loses the cancelled-in-flight
  // events — proving the documented cancel-safety contract is load-bearing, not incidental.
  let mut bad = CancelUnsafe {
    queue: queued(),
    lost: 0,
  };
  let (delivered_bad, _) = drive(&mut bad).await;
  assert!(
    delivered_bad.len() < usize::try_from(N).unwrap(),
    "a cancel-unsafe source loses events to cancellation (delivered {} of {N})",
    delivered_bad.len()
  );
  assert!(
    bad.lost > 0,
    "the cancel-unsafe source dropped popped-but-unreturned events (silent loss, no Rescan)"
  );
}

/// Regression (design backpressure doc, no silent loss): a failed widen whose subsumed
/// root cannot re-arm retires it — and when the event channel is **full** (a stalled consumer)
/// the retire must still owe that root's subscriber its dominating terminal `Rescan`. The shared
/// retire primitive **parks** it into `needs_rescan` (root key + a dominating epoch, captured
/// while live) BEFORE `force_remove_root`, so a full channel cannot drop it. Regression: the old
/// code force-removed the root first and only then pushed the Rescan, so on a full channel
/// `park_rescan`'s `subscription_key` lookup found nothing and the owed terminal Rescan was
/// silently dropped. Fail-on-old: with park-before-retire reverted, the `needs_rescan`/resume
/// assertions FAIL.
#[tokio::test]
async fn failed_widen_retire_parks_owed_terminal_rescan_when_channel_full() {
  let mut h = Harness::bounded(1);
  let sb = h.watch("/a/b", Interest::all()).await.expect("watch /a/b"); // root 1
  let _sc = h.watch("/a/c", Interest::all()).await.expect("watch /a/c"); // root 2
  // Drive sb's high-water up so its terminal Rescan has a prior stream to dominate.
  for raw in 0..3 {
    h.owner.epochs.stamp(sb, Epoch::new(raw)); // sb high-water 2
  }
  // FILL the one slot so the retire's terminal Rescan for sb must overflow-park, never deliver
  // inline. The raw funnel does not overflow yet (the slot holds exactly this one delivery).
  h.owner.try_emit(modified_event(sb, "/a/b/f0", 0));
  assert!(
    h.owner.needs_rescan.is_empty(),
    "the pre-widen delivery filled the slot without overflowing yet"
  );

  // Fail the wider arm AND the first restore re-arm (/a/b): restore iterates root-key order
  // (/a/b, /a/c), so /a/b cannot re-arm (retired) while /a/c re-arms (restored).
  h.owner.source.fail_next_arms(2);
  let result = h.watch("/a", Interest::all()).await;
  assert!(result.is_err(), "the failed wider arm surfaces the error");

  assert!(
    !h.owner.subsumer.view().is_watched(&key("/a/b")),
    "the un-re-armable subsumed root is retired"
  );
  // The core of the regression: despite the full channel, the retired root's owed terminal
  // Rescan is durably PARKED (parked before the subsumer state was freed), not dropped.
  assert!(
    h.owner.needs_rescan.contains_key(&sb),
    "the retired root's owed terminal Rescan is parked despite the full channel (not dropped)"
  );

  // Resume: drain the buffered pre-widen delivery, then flush the parked Rescans (bounded 1, so
  // flush + drain twice to release every parked entry).
  let buffered = h.drain();
  assert!(
    buffered
      .iter()
      .any(|e| e.subscription() == sb && !e.is_rescan()),
    "the pre-widen sb delivery drained in order"
  );
  let mut resumed = Vec::new();
  for _ in 0..2 {
    h.owner.flush_pending_rescans();
    resumed.extend(h.drain());
  }
  let sb_rescan = resumed
    .iter()
    .find(|e| e.subscription() == sb && e.is_rescan())
    .expect("sb receives its owed terminal dominating Rescan after resume (no silent loss)");
  assert_eq!(
    sb_rescan.path(),
    Path::new("/a/b"),
    "the terminal Rescan names the retired root the consumer re-enumerates"
  );
  let sb_max = buffered
    .iter()
    .filter(|e| e.subscription() == sb)
    .map(Event::epoch)
    .max()
    .expect("sb had a buffered delivery");
  assert!(
    sb_rescan.epoch() > sb_max,
    "sb's terminal Rescan strictly dominates every event delivered to it before it"
  );
}

/// Regression (design backpressure doc, checklist #5): with debounce enabled a
/// subscription is **parked** (overflow) AND still holds **buffered tail deltas** whose epoch
/// sits at or above its parked `Rescan`'s (the coalescer admits before `try_emit` suppresses).
/// When the source drains, the owner must NOT deliver those tail deltas ahead of the owed
/// `Rescan` — doing so would let a high-water consumer ignore the `Rescan` and leave the overflow
/// loss unrecovered. The drain **purges** a parked sub's tail (its Rescan re-enumerates +
/// dominates them) and delivers the Rescan first. Regression: the old drain flushed the coalescer
/// tail with a bare `try_send` BEFORE the Rescans, so a tail delta with epoch >= the Rescan was
/// delivered before it. Fail-on-old: with bare-`try_send` tail-first restored, FAILS.
#[tokio::test]
async fn source_drain_orders_parked_rescan_before_its_buffered_tail() {
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let mut h = Harness::build(Some(Coalescer::new(Some(cfg))), Some(1));
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  for raw in 0..2 {
    h.owner.epochs.stamp(sub, Epoch::new(raw)); // high-water 1
  }

  // Fill the one slot and overflow → park a dominating Rescan (epoch 2). The raw funnel bypasses
  // the coalescer, so its buffer is untouched by these two.
  h.owner.try_emit(modified_event(sub, "/a/f0", 0));
  h.owner.try_emit(modified_event(sub, "/a/f1", 1));
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.epoch),
    Some(Epoch::new(2)),
    "overflow parked a dominating Rescan while the channel is full"
  );

  // After parking, more deltas arrive and BUFFER in the coalescer (admit runs unconditionally;
  // not yet due, so nothing drains). Their epoch (9) is far above the parked Rescan's (2) —
  // exactly the tail-vs-Rescan ordering hazard.
  h.owner.push_all(vec![modified_event(sub, "/a/g0", 9)]);

  // The source drains at teardown; run the drain concurrently with a resuming consumer that
  // collects every event for the sub up to and including its Rescan.
  let events = h.events.clone();
  let owner = &mut h.owner;
  let consumer = async {
    let mut seen: Vec<Event<OsString, ()>> = Vec::new();
    while let Ok(event) = events.recv().await {
      if event.subscription() == sub {
        let is_rescan = event.is_rescan();
        seen.push(event);
        if is_rescan {
          break;
        }
      }
    }
    seen
  };
  let (_, seen) = tokio::time::timeout(Duration::from_secs(10), async {
    tokio::join!(owner.drain_owed_before_shutdown(), consumer)
  })
  .await
  .expect("the source-drain teardown delivered the owed Rescan before the deadline");

  let rescan_pos = seen
    .iter()
    .position(|e| e.is_rescan())
    .expect("the owed Rescan was delivered");
  let rescan_epoch = seen[rescan_pos].epoch();
  assert_eq!(rescan_epoch, Epoch::new(2), "the owed dominating Rescan");
  for earlier in &seen[..rescan_pos] {
    assert!(
      earlier.epoch() < rescan_epoch,
      "no delta with epoch >= the Rescan's is delivered before it (dominance preserved)"
    );
  }
  assert!(
    !seen.iter().any(|e| e.key() == key("/a/g0").as_slice()),
    "the parked sub's buffered tail delta was purged (dominated by its Rescan), not delivered"
  );
  assert!(
    owner.needs_rescan.is_empty(),
    "the owed Rescan was delivered — nothing left parked"
  );
}

/// Regression (design backpressure doc, invariant II): after the source drains, the owner
/// owes every parked `Rescan` and retries across a full channel — but that retry must stay
/// responsive to shutdown, or a close behind a full channel (a held-but-not-draining receiver keeps
/// it both full and un-closed) waits forever and `close()` hangs. The drain checks the dedicated
/// close signal at the top priority (a non-blocking `try_recv` each iteration AND the first arm of
/// its retry `select!`), so a mid-drain close is surfaced (to be acked) within a bounded deadline.
/// Fail-on-old: with the close-unresponsive (blind-sleep) drain loop, this times out.
#[tokio::test]
async fn source_drain_retry_stays_responsive_to_close() {
  let mut h = Harness::bounded(1);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  for raw in 0..2 {
    h.owner.epochs.stamp(sub, Epoch::new(raw));
  }
  // Fill the one slot and overflow → park a dominating Rescan; the channel stays FULL and its
  // receiver is HELD but never drained, so neither the slot-freed nor the all-receivers-dropped
  // exit can ever fire on its own.
  h.owner.try_emit(modified_event(sub, "/a/f0", 0));
  h.owner.try_emit(modified_event(sub, "/a/f1", 1));
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "overflow parked a Rescan; the channel is full"
  );
  let _held = h.events.clone(); // a receiver that never drains (keeps the channel full + open)

  // Another handle calls close(): the reply rides the dedicated close signal, not the mailbox.
  let (reply, response) = futures_channel::oneshot::channel();
  h.closes
    .try_send(reply)
    .expect("send the close on the dedicated signal");

  // The source-drain retry must service that close rather than spin behind the full channel.
  let returned = tokio::time::timeout(
    Duration::from_secs(10),
    h.owner.drain_owed_before_shutdown(),
  )
  .await
  .expect(
    "the source-drain retry stayed responsive to Close (did not hang behind the full channel)",
  );
  let close_reply = returned.expect("the mid-drain Close is surfaced to the caller to be acked");

  // Ack it exactly as `run` does; the close() caller then completes.
  close_reply.send(Ok(())).expect("ack the Close");
  assert!(
    matches!(response.await, Ok(Ok(()))),
    "close() completes once the drain surfaced and acked its Close"
  );
}

/// Regression (design backpressure doc, no silent loss): a close that INTERRUPTS the
/// source-drain teardown must not skip the final best-effort owed flush. The drain returns the
/// reply at its top-priority close check WITHOUT running an owed pass — but a resuming consumer may
/// have freed a channel slot in the window just before the close arrived, so the now-sendable
/// CLAIMED parked Rescan must still get its last offer. `run`'s tail runs ONE more non-blocking
/// [`drain_owed_once`](super::Owner::drain_owed_once) when the drain returns `Some(reply)` (mirroring
/// the non-drain close path); this test exercises that exact tail sequence (drain → `Some(reply)` →
/// final pass → ack).
///
/// Fail-on-old: without the tail's final pass the owed Rescan is never delivered — the drain returned
/// before any pass and the freed slot goes unused — so a consumer that resumes reaches stream-end
/// permanently stale.
#[tokio::test]
async fn source_drain_close_interrupt_still_runs_a_final_owed_pass() {
  let mut h = Harness::bounded(1);
  // A CLAIMED sub (reconcile_watch commits directly — never recorded `unclaimed`), so its parked
  // debt is genuinely owed and offered by `flush_pending_rescans`, never suppressed.
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  for raw in 0..2 {
    h.owner.epochs.stamp(sub, Epoch::new(raw)); // high-water 1
  }

  // Fill the one slot (a buffered ordinary event) and overflow → park a dominating Rescan (epoch 2).
  h.owner.try_emit(modified_event(sub, "/a/f0", 0));
  h.owner.try_emit(modified_event(sub, "/a/f1", 1));
  assert_eq!(
    h.owner.needs_rescan.get(&sub).map(|p| p.epoch),
    Some(Epoch::new(2)),
    "overflow parked a dominating Rescan while the channel is full"
  );

  // A resuming consumer FREES one slot (drains the buffered ordinary event) — the channel now has
  // room for the owed Rescan.
  let buffered = h
    .events
    .try_recv()
    .expect("the buffered ordinary event drains, freeing a slot");
  assert!(!buffered.is_rescan(), "the freed event is the ordinary one");

  // …then a close is queued on the dedicated signal, all BEFORE the drain runs. The drain observes
  // the already-queued close at its top-priority `try_recv` and returns it WITHOUT running an owed
  // pass — so the freed slot is left unused and the owed Rescan stays parked.
  let (reply, response) = futures_channel::oneshot::channel();
  h.closes
    .try_send(reply)
    .expect("send the close on the dedicated signal");
  let interrupted = h.owner.drain_owed_before_shutdown().await;
  let close_reply = interrupted.expect("the queued close interrupted the drain and is surfaced");
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "the interrupted drain returned before delivering the owed Rescan (the freed slot is unused)"
  );

  // `run`'s tail: because the drain returned `Some(reply)`, run ONE final best-effort owed pass
  // before publish_empty + ack. The slot is now free, so this delivers the owed Rescan.
  h.owner.drain_owed_once();
  // Ack exactly as `run` does; the close() caller then completes.
  close_reply.send(Ok(())).expect("ack the close");
  assert!(
    matches!(response.await, Ok(Ok(()))),
    "close() completes once the drain surfaced and acked its close"
  );

  // The final pass delivered the now-sendable owed Rescan — nothing left parked, and it dominates
  // the earlier ordinary event.
  let owed = h
    .events
    .try_recv()
    .expect("the final owed pass delivered the parked Rescan onto the freed slot");
  assert!(owed.is_rescan(), "the delivered owed signal is a Rescan");
  assert_eq!(owed.subscription(), sub, "…for the overflowed subscription");
  assert_eq!(
    owed.path(),
    Path::new("/a"),
    "…naming its covered key to re-enumerate"
  );
  assert!(
    owed.epoch() > buffered.epoch(),
    "the owed Rescan strictly dominates the ordinary event delivered before it (no silent loss)"
  );
  assert!(
    h.owner.needs_rescan.is_empty(),
    "the final pass delivered the owed Rescan — nothing left parked"
  );
}

/// Regression — the close-time grant-resolution drain is bounded by the grants IN FLIGHT,
/// never by the unbounded PUBLIC backlog. A close that interrupts the source drain runs the tail's
/// [`drain_pending_cleanup`](super::Owner::drain_pending_cleanup) — a full drain of the dedicated
/// cleanup channel — BEFORE its final owed pass, so a subscription the caller claimed (its
/// [`Cleanup::Claim`](super::Cleanup::Claim) queued) has its [`unclaimed`](super::Owner::unclaimed)
/// suppression lifted and its genuinely-owed parked Rescan delivered, even with a deep public
/// `Watch`/`Unwatch` backlog sitting UNWALKED in the mailbox (the O(public backlog) close-ack the old
/// close-time mailbox scan caused). This test drives that exact sequence (drain → `Some(reply)` →
/// cleanup drain → final pass → ack) with ~2000 prefilled public commands.
///
/// Fail-on-old: the retired mailbox scan walked `commands.len()` (the whole 2000-deep public backlog)
/// to find the claim — O(public backlog); the cleanup drain finds it in the dedicated channel (O(grants
/// in flight)) and leaves the 2000 public commands untouched (asserted below).
#[tokio::test]
async fn close_tail_drains_cleanup_not_public_backlog() {
  let mut h = Harness::new(); // unbounded event channel — has capacity for the owed Rescan
  // An UNCLAIMED sub (its grant still in flight) with parked overflow debt: `flush_pending_rescans`
  // SUPPRESSES it while unclaimed, so a plain final pass would withhold it.
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a"); // handle 1
  h.owner.unclaimed.insert(sub);
  for raw in 0..2 {
    h.owner.epochs.stamp(sub, Epoch::new(raw));
  }
  h.owner.park_rescan(sub);
  assert!(
    h.owner.suppressed_rescan.contains_key(&sub),
    "the unclaimed sub's overflow Rescan is parked into the suppressed partition"
  );

  // Prefill ~2000 PUBLIC Watch commands (reply receivers dropped): the deep mailbox backlog the old
  // linearize walked O(n) to find a claim. The cleanup drain must NOT walk them.
  const BACKLOG: usize = 2000;
  for i in 0..BACKLOG {
    let (reply, resp) = futures_channel::oneshot::channel();
    drop(resp);
    h._commands
      .try_send(super::Command::Watch {
        key: key(&format!("/flood{i}")),
        value: (),
        options: WatchOptions::new(),
        reply,
      })
      .expect("prefill a public backlog command");
  }
  // The caller CLAIMS the grant — its `Cleanup::Claim` rides the dedicated cleanup channel (as a
  // defused grant's `try_send` would), NOT the public mailbox.
  h.owner
    .cleanup_tx
    .try_send(super::Cleanup::Claim(sub))
    .expect("enqueue the claim on the dedicated cleanup channel");

  // A close is queued on the dedicated signal BEFORE the drain runs, so the drain returns it at its
  // top-priority `try_recv` WITHOUT running any pass or draining any cleanup.
  let (reply, response) = futures_channel::oneshot::channel();
  h.closes
    .try_send(reply)
    .expect("send the close on the dedicated signal");
  let interrupted =
    tokio::time::timeout(Duration::from_secs(5), h.owner.drain_owed_before_shutdown())
      .await
      .expect("the drain returns promptly (close queued first), not O(public backlog)");
  let close_reply = interrupted.expect("the queued close interrupted the drain and is surfaced");
  assert!(
    h.owner.unclaimed.contains(&sub) && h.owner.suppressed_rescan.contains_key(&sub),
    "the drain returned before draining the queued Cleanup::Claim — the debt is still suppressed"
  );
  assert!(
    h.drain().is_empty(),
    "…and nothing was delivered yet (a bare final pass here would suppress the unclaimed debt)"
  );

  // `run`'s tail on a drain-interrupt: FIRST the full cleanup drain (bounded by grants in flight —
  // one Claim here — NOT the 2000-command backlog), lifting the sub's suppression, THEN one final
  // owed pass.
  h.owner.drain_pending_cleanup();
  assert!(
    !h.owner.unclaimed.contains(&sub),
    "the cleanup drain processed the Claim — suppression lifted"
  );
  h.owner.drain_owed_once();

  // The final pass delivered the now-owed Rescan BEFORE the ack is sent (mirroring `run`'s tail order:
  // drain_pending_cleanup → drain_owed_once → publish_empty → reply.send). Assert it is observable now.
  let delivered = h.drain();
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == sub && e.is_rescan()),
    "the claimed sub's parked Rescan is delivered by the final pass, before the ack"
  );
  assert!(
    h.owner.needs_rescan.is_empty() && h.owner.suppressed_rescan.is_empty(),
    "the owed debt is resolved, not stranded by stale suppression state"
  );
  // The 2000 public commands are UNTOUCHED — the cleanup drain is O(grants in flight), not O(public
  // backlog); they drop unread at teardown (senders see Closed). This is the F1 property.
  assert_eq!(
    h.owner.commands.len(),
    BACKLOG,
    "the public backlog is never walked by the cleanup drain"
  );

  // Ack exactly as `run` does; the close() caller then completes.
  close_reply.send(Ok(())).expect("ack the close");
  assert!(
    matches!(response.await, Ok(Ok(()))),
    "close() completes once the interrupted drain acked its close"
  );
}

/// Close-responsive source drain (design source doc, invariant II): during source-drain teardown a
/// queued [`Cleanup::DropOrphan`](super::Cleanup::DropOrphan) is released through the synchronous
/// [`release_subscription`](super::Owner::release_subscription) — it purges the orphan's owner-local
/// state and requests the emptied root's `source.disarm`, awaiting nothing — so it never wedges the
/// owner, and the close (on its dedicated signal) is surfaced promptly with no scheduling discipline
/// to get wrong. The purge is keyed on the orphan alone, so a live sub's owed Rescan survives it.
///
/// Non-vacuous: with a `Cleanup::DropOrphan` queued on the cleanup channel and a SEPARATE live
/// subscription keeping a Rescan parked behind a full, held-open channel (so the drain loop keeps
/// spinning and draining cleanup), it asserts the drain surfaces the close within the deadline, the
/// orphan is reconciled away (its last-subscriber root released synchronously — a `Disarm` op
/// recorded), and the live sub's owed Rescan survives untouched.
#[tokio::test]
async fn source_drain_dropped_orphan_is_purged_without_blocking_close_on_disarm() {
  let mut h = Harness::bounded(1);

  // A LIVE subscription whose parked overflow Rescan keeps the drain loop spinning (its channel is
  // filled + held open below), plus an ORPHAN on a disjoint root — the `DropOrphan` target and its
  // own root's last subscriber, so releasing it requests that root's `disarm`.
  let live = h.watch("/a", Interest::all()).await.expect("watch /a");
  let orphan = h.watch("/b", Interest::all()).await.expect("watch /b");
  // Monotonic mint: /a = handle 1, /b = handle 2.

  for raw in 0..2 {
    h.owner.epochs.stamp(live, Epoch::new(raw));
  }
  // Fill the one slot and overflow → park the LIVE sub's dominating Rescan; the held receiver keeps
  // the channel full + open, so neither the slot-freed nor the all-receivers-dropped exit can fire.
  h.owner.try_emit(modified_event(live, "/a/f0", 0));
  h.owner.try_emit(modified_event(live, "/a/f1", 1));
  assert!(
    h.owner.needs_rescan.contains_key(&live),
    "overflow parked the live sub's Rescan; the channel is full"
  );
  let _held = h.events.clone(); // a receiver that never drains (keeps the channel full + open)

  // Queue Cleanup::DropOrphan(orphan) on the dedicated cleanup channel. The close is NOT sent yet:
  // a close already pending on the dedicated signal would (correctly) preempt everything, so
  // to exercise the drain SERVICING the DropOrphan we let it arrive only after the drain's first pass.
  // Run the drain concurrently with a sender that yields once (so the drain runs its first
  // top-of-iteration cleanup drain — servicing the queued DropOrphan via the synchronous release —
  // before the close lands), then interrupts it via the dedicated close arm.
  h.owner
    .cleanup_tx
    .try_send(super::Cleanup::DropOrphan(orphan))
    .expect("enqueue the DropOrphan cleanup notice");
  let (reply, response) = futures_channel::oneshot::channel();
  let closes = h.closes.clone();
  let (returned, ()) = tokio::time::timeout(Duration::from_secs(5), async {
    tokio::join!(h.owner.drain_owed_before_shutdown(), async move {
      tokio::task::yield_now().await;
      closes
        .try_send(reply)
        .expect("send the close on the dedicated signal");
    })
  })
  .await
  .expect("the drain serviced the queued DropOrphan and then surfaced the close");
  let close_reply = returned.expect("the mid-drain close is surfaced to the caller to be acked");

  // Ack it exactly as `run` does; the close() caller then completes.
  close_reply.send(Ok(())).expect("ack the close");
  assert!(
    matches!(response.await, Ok(Ok(()))),
    "close() completes once the drain surfaced and acked its close"
  );

  // The orphan's last-subscriber root was released SYNCHRONOUSLY — a `Disarm(2)` recorded at cleanup
  // — while the live sub's owed Rescan (a DIFFERENT sub's `needs_rescan` entry) survived untouched.
  assert!(
    h.owner.source.calls().contains(&Call::Disarm(2)),
    "the teardown released the orphan's last-subscriber root via the synchronous disarm request"
  );
  assert!(
    !h.owner.subsumer.view().is_watched(&key("/b")),
    "the orphan is reconciled away (its per-sub state purged)"
  );
  assert!(
    h.owner.needs_rescan.contains_key(&live),
    "the live sub's owed parked Rescan survived the orphan purge"
  );
}

/// A [`Source`] whose `next()` **parks** until the test drops its `drain` sender, then yields
/// `None` — so a test can watch a key and take a `WatchView` clone, and only THEN drive the
/// owner's source-drain teardown deterministically (no race between the watch command and the
/// source draining). `arm` succeeds and records the root so `root_key` reports it live.
struct DrainableSource {
  next_handle: u32,
  live: HashMap<u32, Vec<OsString>>,
  drain: async_channel::Receiver<std::convert::Infallible>,
}

impl Source<OsString> for DrainableSource {
  type Handle = u32;

  fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
    Ok(key.to_vec())
  }

  async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
    self.next_handle += 1;
    let handle = self.next_handle;
    self.live.insert(handle, key.to_vec());
    Ok(Armed::new(handle, key.to_vec()))
  }

  fn disarm(&mut self, handle: u32) {
    self.live.remove(&handle);
  }

  async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
    // Park until the test drops the drain sender; a closed channel (Err) means "drain now".
    match self.drain.recv().await {
      Ok(never) => match never {},
      Err(_) => None,
    }
  }

  fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
    self.live.get(&handle).cloned()
  }
}

/// Regression (design §5, no stale read plane on teardown): the owner publishes an EMPTY
/// read plane at teardown, so a `WatchView` clone taken while watching stops advertising the (now
/// dead) coverage once the source drains and the stream ends. Exercises the real `run()`
/// source-drain teardown through the public [`Tributaries::with_source`](super::Tributaries).
///
/// Regression: the old teardown dropped the owner without republishing, so a retained view kept
/// reading `is_watched=true` / `covering=Some` for a subscription whose owner task + source are
/// gone — a dedup caller (the indexer) would then skip re-installing it and silently miss changes
/// after rebuilding a fresh watcher. Fail-on-old: without the empty publish, `is_watched` stays
/// true after stream-end → FAILS.
#[tokio::test]
async fn teardown_publishes_empty_read_plane_so_view_stops_advertising_dead_subs() {
  let (drain_tx, drain_rx) = async_channel::bounded::<std::convert::Infallible>(1);
  let source = DrainableSource {
    next_handle: 0,
    live: HashMap::new(),
    drain: drain_rx,
  };
  let mut w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());

  // A view clone taken WHILE watching — the pre-taken handle the regression is about.
  let view = w.view();
  let watched = key("/a");
  let _sub = w
    .watch(watched.clone(), (), WatchOptions::new())
    .await
    .expect("watch /a");
  assert!(view.is_watched(&watched), "the live watch is advertised");
  assert!(
    view.covering(&watched).is_some(),
    "…and attribution resolves it while live"
  );

  // Drain the source: dropping the sender makes `next()` yield None → the source-drain teardown.
  drop(drain_tx);

  // Once the stream ends (next() → None), the owner has torn down and published the empty plane.
  let ended = tokio::time::timeout(Duration::from_secs(10), w.next()).await;
  assert!(
    matches!(ended, Ok(None)),
    "the event stream ends after the source drains + teardown"
  );

  // The pre-taken view now reports nothing watched — the empty read plane published on teardown.
  assert!(
    !view.is_watched(&watched),
    "the retained view stops advertising the dead subscription after teardown (empty read plane)"
  );
  assert!(
    view.covering(&watched).is_none(),
    "…and attribution resolves to nothing (the owner + source are gone)"
  );
}

/// A `u64`-valued [`Owner`] over a [`FakeSource`], with its drainable event stream — the
/// value-baking regression rig. `V = u64` (not [`Harness`]'s `()`) so attribution values are
/// distinguishable; the tests drive the owner's reconcile/emit primitives directly, then assert
/// the baked [`Event::value`] on drained events.
struct OwnerU64 {
  owner: Owner<OsString, u64, TokioRuntime, FakeSource>,
  events: async_channel::Receiver<Event<OsString, u64>>,
  /// Kept alive so the owner's command receiver never observes a closed channel.
  _commands: async_channel::Sender<super::Command<OsString, u64>>,
  /// Kept alive so the owner's sync-admission receiver never observes a closed channel.
  _sync_commands: async_channel::Sender<super::SyncRequest>,
  /// Kept alive so the owner's close receiver never observes a closed channel (these rigs drive
  /// primitives directly and never inject a close).
  _closes: async_channel::Sender<super::CloseReply>,
}

impl OwnerU64 {
  /// Builds the rig with a bounded event channel of `capacity` and an optional coalescer.
  fn new(capacity: usize, coalescer: Option<Coalescer<OsString, u64>>) -> Self {
    let (event_tx, event_rx) = async_channel::bounded(capacity);
    let (command_tx, command_rx) = async_channel::unbounded();
    let (sync_command_tx, sync_command_rx) = async_channel::unbounded::<super::SyncRequest>();
    let (close_tx, close_rx) = async_channel::bounded(1);
    let (cleanup_tx, cleanup_rx) = async_channel::unbounded();
    let owner = Owner {
      source: FakeSource::new(),
      subsumer: Subsumer::new(),
      epochs: EpochLedger::new(),
      filters: HashMap::new(),
      needs_rescan: BTreeMap::new(),
      suppressed_rescan: BTreeMap::new(),
      unclaimed: std::collections::HashSet::new(),
      flush_cursor: None,
      #[cfg(test)]
      last_flush_visited: 0,
      #[cfg(test)]
      test_pre_cut_claims: Vec::new(),
      debounce: None,
      coalescer,
      pending_syncs: Vec::new(),
      sync_seq: 0,
      sync_nonce_seed: std::collections::hash_map::RandomState::new(),
      loss_serial: HashMap::new(),
      loss_gen: std::sync::Arc::new(core::sync::atomic::AtomicU64::new(0)),
      cleanup_tx,
      cleanup_rx,
      commands: command_rx,
      sync_commands: sync_command_rx,
      closes: close_rx,
      events: event_tx,
      #[cfg(debug_assertions)]
      observed_handles: std::collections::HashSet::new(),
      _rt: PhantomData::<TokioRuntime>,
    };
    Self {
      owner,
      events: event_rx,
      _commands: command_tx,
      _sync_commands: sync_command_tx,
      _closes: close_tx,
    }
  }

  /// Drains every event currently queued on the owner's stream.
  fn drain(&self) -> Vec<Event<OsString, u64>> {
    let mut out = Vec::new();
    while let Ok(event) = self.events.try_recv() {
      out.push(event);
    }
    out
  }
}

/// Value baking, ordinary path (design §3): every delivered delta carries its owning
/// subscription's value, baked at emit time. Fail-on-old: with `Event::value` left `None`, the
/// `Some(42)` assertion FAILS.
#[tokio::test]
async fn delivered_delta_carries_owning_subscription_value() {
  let mut rig = OwnerU64::new(8, None);
  let sub = rig
    .owner
    .reconcile_watch(&key("/a"), 42, WatchOptions::new())
    .await
    .expect("watch /a");

  // A raw change under /a fans out to the covering sub; the delivery is baked with the sub's value.
  rig.owner.fan_out_and_push(&source_modified(1, "/a/f", 0));

  let drained = rig.drain();
  assert_eq!(drained.len(), 1, "one delivery for the single covering sub");
  assert_eq!(
    drained[0].subscription(),
    sub,
    "…retagged with its subscription"
  );
  assert_eq!(
    drained[0].value().copied(),
    Some(42),
    "a normal delivered delta carries its owning subscription's value (baked at emit time)"
  );
}

/// Regression (design §3, event attribution survives teardown): a source-drain leaves a queued
/// coalescer **tail delta** (from one live sub) AND an **owed parked Rescan** (from another sub
/// whose root died). The owner tears down — publishing the EMPTY read plane — and only
/// THEN does the consumer drain those queued events. Each must be attributable via its baked
/// [`Event::value`], NOT via the emptied [`WatchView`] (whose `resolve` now answers `None`).
///
/// The terminal (retire) Rescan is the sharp case: its owning sub's subsumer state is force-removed
/// at retire, so the value CANNOT be re-resolved at flush time — it is captured at park time while
/// the sub is live. Fail-on-old: with `Event::value` left `None`, the `Some(7)`/`Some(9)`
/// assertions FAIL, and `resolve` returns `None` post-teardown, so the old resolve-based
/// attribution recovers nothing.
#[tokio::test]
async fn baked_value_attributes_queued_events_after_teardown_empties_view() {
  // Windows long enough that the tail delta never settles on its own — it survives as a coalescer
  // tail the teardown drain flushes.
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let mut rig = OwnerU64::new(8, Some(Coalescer::new(Some(cfg))));

  let a = rig
    .owner
    .reconcile_watch(&key("/a"), 7, WatchOptions::new())
    .await
    .expect("watch /a"); // root handle 1
  let b = rig
    .owner
    .reconcile_watch(&key("/b"), 9, WatchOptions::new())
    .await
    .expect("watch /b"); // root handle 2

  // A view clone taken WHILE both are live — the handle the story is about.
  let view = rig.owner.subsumer.view();
  assert_eq!(
    view.resolve(&key("/b")).map(|s| *s.get()),
    Some(9),
    "the live view attributes /b while its sub is live"
  );

  // subB buffers a coalescer tail delta (baked value 9 at fan-out; not due under the long window).
  rig.owner.fan_out_and_push(&source_modified(2, "/b/g0", 5));

  // subA's root dies: the terminal Rescan is parked with subA's value CAPTURED AT PARK TIME, then
  // its subsumer state is force-removed — after which the value can no longer be resolved.
  rig.owner.retire_root_with_terminal_rescan(1);
  assert!(
    rig.owner.needs_rescan.contains_key(&a),
    "subA owes a parked terminal Rescan"
  );
  assert_eq!(
    rig.owner.subsumer.subscription_value(a),
    None,
    "subA's subsumer state is gone post-retire — its value HAD to be captured at park time"
  );

  // Teardown drain: deliver the owed Rescan and the coalescer tail into the channel.
  rig.owner.drain_owed_once();
  assert_eq!(
    view.resolve(&key("/b")).map(|s| *s.get()),
    Some(9),
    "subB still resolves through the live view just before the empty publish"
  );

  // Publish the EMPTY read plane exactly as `run()` does at teardown: the view now reports
  // nothing watched, so `resolve` can no longer attribute the still-queued events.
  rig.owner.subsumer.publish_empty();
  assert!(
    view.resolve(&key("/b")).is_none(),
    "teardown emptied the view — resolve now attributes NOTHING for the queued tail delta"
  );
  assert!(
    view.resolve(&key("/a")).is_none(),
    "…and nothing for the retired root"
  );

  // The consumer drains AFTER teardown and attributes each event via its BAKED value.
  let drained = rig.drain();

  let rescan = drained
    .iter()
    .find(|e| e.subscription() == a && e.is_rescan())
    .expect("subA's owed terminal Rescan was delivered");
  assert_eq!(
    rescan.value().copied(),
    Some(7),
    "the owed terminal Rescan carries subA's value baked at park time — attribution survives \
     teardown (the emptied view resolves to None)"
  );

  let tail = drained
    .iter()
    .find(|e| e.subscription() == b && !e.is_rescan())
    .expect("subB's coalescer tail delta was delivered");
  assert_eq!(
    tail.value().copied(),
    Some(9),
    "the coalesced tail delta preserved subB's baked value through buffering"
  );

  assert!(
    drained.iter().all(|e| e.value().is_some()),
    "every delivered event carries a baked owning-subscription value (none slips through as None)"
  );
}

/// The full per-subscription-purge class: a **consumer unwatch** must purge the
/// debounce coalescer along with every other per-sub structure, so a delta buffered before the
/// unwatch can never drain to the retired subscription. The coalescer's drain path
/// (`drain_coalescer_due` / the teardown flush → `try_emit`) has no live-subscription check, so an
/// entry left buffered is delivered for a subscription whose `unwatch` already resolved.
///
/// Fail-on-old: without `coalescer.drop_subscription(sub)` on unwatch, the buffered delta survives
/// the unwatch and the flush emits it → the drain is non-empty.
#[tokio::test]
async fn consumer_unwatch_purges_buffered_coalescer_delta() {
  // A long quiet window so `push_all`'s immediate drain buffers the delta rather than emitting it.
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_secs(3600))
    .with_max_hold(Duration::from_secs(7200));
  let mut h = Harness::with_coalescer(Some(Coalescer::new(Some(cfg))));
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");

  // A pre-unwatch delta buffers in the coalescer (long window → admit runs, nothing drains).
  h.owner.push_all(vec![modified_event(sub, "/a/f", 0)]);
  assert!(
    h.drain().is_empty(),
    "the delta is held under the long quiet window, not yet emitted"
  );

  // The consumer unwatches: every per-sub structure — including the coalescer buffer — is purged.
  h.unwatch(sub).expect("sub was live");

  // Both the settle-timer edge and the teardown flush drain the coalescer through `try_emit`;
  // neither may surface anything for the retired subscription.
  h.owner.drain_coalescer_due();
  h.owner.drain_owed_once();
  assert!(
    h.drain().is_empty(),
    "no buffered coalescer event drains for a subscription whose unwatch already resolved"
  );
}

/// The panic-stranding class: a panic in a caller-provided callback the owner runs
/// synchronously (here the admission [`Filter`] predicate at fan-out) unwinds the owner before the
/// normal teardown path empties the read plane. The `impl Drop for Owner` guard publishes an empty
/// plane on **any** owner drop — normal exit OR a panic — so a retained [`WatchView`] never keeps
/// advertising a subscription whose owner task has died (the stale-read-plane mode). The single
/// Drop guard covers the whole class at once: any unwind through the owner future runs it.
///
/// Fail-on-old: with `impl Drop for Owner` removed, dropping the panicked owner leaves the last
/// committed (non-empty) plane published, so the view still reports the sub watched → the final
/// assertion FAILS.
#[tokio::test]
async fn owner_drop_publishes_empty_read_plane_on_a_panicking_caller_callback() {
  let mut h = Harness::new();
  // A filter whose predicate panics when fan-out consults it — the exact caller callback the owner
  // invokes synchronously inside the run loop.
  let sub = h
    .owner
    .reconcile_watch(
      &key("/a"),
      (),
      WatchOptions::new().with_filter(Filter::new(|_| -> bool {
        panic!("caller filter predicate panics inside fan-out")
      })),
    )
    .await
    .expect("watch /a"); // root handle 1

  // A view clone taken while the sub is live — the retained handle the guarantee is about.
  let view = h.owner.subsumer.view();
  assert!(
    view.is_watched(&key("/a")),
    "the live watch is advertised while the sub is live"
  );

  // Drive an event through fan-out so the filter panics and the owner primitive unwinds. Catch it
  // so the test survives to observe the plane, exactly as a runtime drops a panicked task's future.
  let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    h.owner.fan_out_and_push(&source_modified(1, "/a/f", 0));
  }));
  assert!(
    panicked.is_err(),
    "the caller filter panicked, unwinding the owner"
  );
  assert!(
    view.is_watched(&key("/a")),
    "the caught panic left the owner alive — the plane is unchanged until the owner drops"
  );
  let _ = sub;

  // Dropping the owner (as a runtime drops a panicked task's future) runs the Drop guard, which
  // publishes the empty read plane so the retained view stops advertising the now-dead subscription.
  drop(h);
  assert!(
    !view.is_watched(&key("/a")),
    "the Owner Drop guard emptied the read plane on unwind — no stale coverage for a dead owner"
  );
  assert!(
    view.covering(&key("/a")).is_none(),
    "…and attribution resolves to nothing after the guard empties the plane"
  );
}

/// Regression (design source doc, invariant I4 / no false debt):
/// [`release_subscription`](super::Owner::release_subscription) must clear a subscription's
/// owner-local per-sub state — above all its parked overflow [`Rescan`](EventKind::Rescan)
/// — EVEN WHEN the subscription is already absent from the subsumer (terminal-retired). A committed-but-unclaimed
/// watch can be terminal-retired (its terminal Rescan parked, the sub force-removed from the
/// subsumer) while its [`WatchGrant`](super::WatchGrant) still sits in the reply slot; the later
/// [`Cleanup::DropOrphan`](super::Cleanup::DropOrphan) then finds `plan_unwatch` reporting `Unknown`. The fix
/// runs the local purge (keyed on the sub alone) BEFORE that early return, so no parked Rescan is
/// left behind as FALSE debt — a Rescan deliverable for a subscription the caller never received, or
/// endlessly retried on a full channel at source-drain — while a live sibling's parked Rescan stays
/// untouched.
///
/// Fail-on-old: with the early `Unknown` return placed BEFORE the local cleanup, the orphan's parked
/// Rescan lingers in `needs_rescan` and the first assertion fails.
#[tokio::test]
async fn drop_orphan_after_terminal_retire_clears_the_orphans_parked_rescan_no_false_debt() {
  let mut h = Harness::bounded(1);

  // A LIVE sibling on one disjoint root and the ORPHAN on another. Monotonic mint: /a = handle 1
  // (sibling), /b = handle 2 (orphan).
  let sibling = h.watch("/a", Interest::all()).await.expect("watch /a");
  let orphan = h.watch("/b", Interest::all()).await.expect("watch /b");

  // Give the LIVE sibling a parked overflow Rescan: fill the one channel slot, then overflow it — so
  // the test can assert the orphan purge leaves a DIFFERENT sub's owed Rescan untouched.
  for raw in 0..2 {
    h.owner.epochs.stamp(sibling, Epoch::new(raw));
  }
  h.owner.try_emit(modified_event(sibling, "/a/f0", 0));
  h.owner.try_emit(modified_event(sibling, "/a/f1", 1));
  assert!(
    h.owner.needs_rescan.contains_key(&sibling),
    "the sibling's overflow parked a dominating Rescan"
  );

  // Terminal-retire the ORPHAN's root (handle 2): parks the orphan's terminal Rescan into
  // needs_rescan AND force-removes the sub from the subsumer — the committed-but-unclaimed-then-
  // terminal-retired state this pins (plan_unwatch can no longer find it).
  h.owner.retire_root_with_terminal_rescan(2);
  assert!(
    h.owner.needs_rescan.contains_key(&orphan),
    "terminal retirement parked the orphan's owed terminal Rescan"
  );
  assert!(
    !h.owner.subsumer.view().is_watched(&key("/b")),
    "the orphan's root is retired from the subsumer"
  );

  // The orphan's WatchGrant now fires: DropOrphan → release_subscription. The subsumer no longer
  // records the orphan, so plan_unwatch reports Unknown — but the owner-local cleanup MUST still have
  // run (and no last-subscriber disarm is issued, since the root is already terminal-retired).
  let outcome = h.owner.release_subscription(orphan);
  assert!(
    outcome.is_err_and(|err| err.is_unknown_subscription()),
    "a terminal-retired sub is Unknown to the subsumer"
  );

  // the orphan's parked Rescan is GONE (no false debt) …
  assert!(
    !h.owner.needs_rescan.contains_key(&orphan),
    "the orphan's parked Rescan was cleared despite the subsumer reporting Unknown (no false debt)"
  );
  // … while the LIVE sibling's parked Rescan (a DIFFERENT sub) is left untouched.
  assert!(
    h.owner.needs_rescan.contains_key(&sibling),
    "the live sibling's owed parked Rescan survived the orphan purge untouched"
  );
}

/// The umbrella NEVER surfaces `Overlaps` (design source doc; `error::WatchError`): a re-`watch` of a
/// just-orphaned key succeeds even though the orphan's release was requested a moment earlier. A
/// [`Cleanup::DropOrphan`](super::Cleanup::DropOrphan)'s
/// [`release_subscription`](super::Owner::release_subscription) removes the emptied root from the
/// subsumer AND issues the synchronous `source.disarm`, so the re-`watch` is classified `Disjoint`
/// (the subsumer no longer records it) and the source has already applied the release before the
/// fresh arm (disarm contract clause 2) — no flush machinery, no
/// overlap rejection. The re-`watch` arms a FRESH
/// generation-unique handle; the released OLD handle is logically dead and cannot touch it.
///
/// Non-vacuous: the recorded op sequence is exactly `Arm(/a)`, `Disarm(1)`, `Arm(/a)` — the release
/// is recorded BEFORE the re-watch's arm (release-before-subsequent-arm ordering) with only ONE arm
/// for the re-watch (no `Overlaps` retry) — and the OLD handle's `root_key` is `None` while the
/// re-watch is live and covered on a fresh root.
#[tokio::test]
async fn rewatch_of_a_just_orphaned_key_succeeds_no_overlaps() {
  let mut h = Harness::new();

  // Watch /a → its root's only subscriber (handle 1), armed at the source.
  let orphan = h.watch("/a", Interest::all()).await.expect("watch /a");

  // The NORMAL-loop DropOrphan handling: release the orphan's owner-local state (removing /a from the
  // subsumer) AND request its emptied root's synchronous disarm — the FakeSource applies the release
  // immediately, so handle 1 is logically dead at once.
  h.owner
    .release_subscription(orphan)
    .expect("the orphan (its root's last subscriber) is released");
  assert_eq!(
    h.owner.source.root_key(1),
    None,
    "the orphaned root is logically dead immediately (its disarm was a synchronous request)"
  );

  // Re-watch /a: `Disjoint` (the subsumer no longer records it) → arm a FRESH root. The prior release
  // was already applied, so the source never reports `Overlaps` and no retry is needed.
  let rewatched = h
    .watch("/a", Interest::all())
    .await
    .expect("the re-watch never surfaces Overlaps (the prior release was already applied)");
  assert_ne!(
    orphan, rewatched,
    "the re-watch minted a fresh subscription"
  );

  // The op sequence proves the ordering: the orphan's `Disarm(1)` is recorded BEFORE the re-watch's
  // single `Arm(/a)` — release-before-subsequent-arm, and no `Overlaps` retry (exactly two arms).
  assert_eq!(
    h.owner.source.calls(),
    vec![
      Call::Arm(PathBuf::from("/a")),
      Call::Disarm(1),
      Call::Arm(PathBuf::from("/a")),
    ],
    "release recorded before the re-watch's (single) arm — no Overlaps"
  );
  assert_eq!(
    h.owner.source.root_key(1),
    None,
    "the OLD handle stays released — it cannot touch the new root"
  );
  assert!(
    h.owner.subsumer.view().is_watched(&key("/a")),
    "the re-watch is live and covered on a fresh root"
  );
}

/// Golden widen ordering (design source doc, disarm contract clause 2): a widen releases its subsumed
/// narrow roots via the **synchronous** [`Source::disarm`] request and then arms the wider root, so
/// the recorded op sequence shows every `Disarm(narrow…)` BEFORE the `Arm(wider)`, and by the time the
/// wider root is armed those releases are already applied — a conforming source can never report
/// `Overlaps`. Beyond the plain ordering check, this asserts the released narrow handles are logically
/// dead (`root_key` → None) once the wider root is live.
#[tokio::test]
async fn widen_records_releases_before_wider_arm_and_applies_them() {
  let mut h = Harness::new();

  // Two disjoint narrow roots: /a/b (handle 1) and /a/c (handle 2).
  h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  h.watch("/a/c", Interest::all()).await.expect("watch /a/c");

  // Widen to /a: releases the two narrow roots, then arms the wider /a (handle 3).
  h.watch("/a", Interest::all())
    .await
    .expect("watch /a widens over the narrow roots");

  // The op sequence: both narrow arms, then BOTH releases, then the single wider arm — every `Disarm`
  // recorded before `Arm(/a)` (release-before-subsequent-arm ordering, and no `Overlaps` retry).
  assert_eq!(
    h.owner.source.calls(),
    vec![
      Call::Arm(PathBuf::from("/a/b")),
      Call::Arm(PathBuf::from("/a/c")),
      Call::Disarm(1),
      Call::Disarm(2),
      Call::Arm(PathBuf::from("/a")),
    ],
    "the narrow roots are released before the wider root is armed"
  );
  // The releases are APPLIED: the narrow handles are logically dead, and the wider root (handle 3) is
  // live and covers both old narrow keys.
  assert_eq!(h.owner.source.root_key(1), None, "narrow /a/b released");
  assert_eq!(h.owner.source.root_key(2), None, "narrow /a/c released");
  assert_eq!(
    h.owner.source.root_key(3),
    Some(key("/a")),
    "the wider root is armed and live"
  );
  assert!(
    h.owner.subsumer.view().is_watched(&key("/a/b")),
    "the widened root covers the old narrow keys"
  );
}

/// Immediate logical death (design source doc, disarm contract clause 3): after the owner releases a
/// subscription (an unwatch/orphan cleanup), the source's `root_key` answers `None` for the freed
/// handle AT ONCE — even for an FsSource-shaped source whose transport teardown is still pending.
/// Modelled by a double whose `disarm` queues the transport release (applied only at the next `arm`,
/// like the real `Watcher`'s bounded command channel) yet marks the handle logically dead immediately.
#[tokio::test]
async fn release_marks_handle_logically_dead_immediately_even_with_transport_pending() {
  /// An FsSource-shaped double: `disarm` queues the transport teardown (drained at the next `arm`)
  /// but marks the handle logically dead at once via `pending_set`.
  struct PendingReleaseSource {
    next_handle: u32,
    /// Handles whose transport watch is still installed (the "kernel" side); a `disarm` does NOT
    /// remove from here — only the next `arm`'s drain does.
    transport_live: std::collections::HashSet<u32>,
    /// The requested-but-not-yet-applied releases, plus their mirror for O(1) `root_key`.
    pending: VecDeque<u32>,
    pending_set: std::collections::HashSet<u32>,
    keys: HashMap<u32, Vec<OsString>>,
  }

  impl Source<OsString> for PendingReleaseSource {
    type Handle = u32;

    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      Ok(key.to_vec())
    }

    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      // Apply every deferred transport teardown FIRST (mirroring `FsSource::arm`).
      while let Some(released) = self.pending.pop_front() {
        self.transport_live.remove(&released);
        self.pending_set.remove(&released);
        self.keys.remove(&released);
      }
      self.next_handle += 1;
      let handle = self.next_handle;
      self.transport_live.insert(handle);
      self.keys.insert(handle, key.to_vec());
      Ok(Armed::new(handle, key.to_vec()))
    }

    fn disarm(&mut self, handle: u32) {
      // Synchronous request: queue the transport teardown (deferred to the next arm) and mark the
      // handle logically dead immediately.
      if self.pending_set.insert(handle) {
        self.pending.push_back(handle);
      }
    }

    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      None
    }

    fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
      // Logically dead immediately, even though `transport_live` still holds it.
      if self.pending_set.contains(&handle) {
        return None;
      }
      self.keys.get(&handle).cloned()
    }
  }

  let (event_tx, _event_rx) = async_channel::unbounded::<Event<OsString, ()>>();
  let (command_tx, command_rx) = async_channel::unbounded::<super::Command<OsString, ()>>();
  let (_sync_command_tx, sync_command_rx) = async_channel::unbounded::<super::SyncRequest>();
  let (_close_tx, close_rx) = async_channel::bounded::<super::CloseReply>(1);
  let (cleanup_tx, cleanup_rx) = async_channel::unbounded::<super::Cleanup>();
  let mut owner = Owner {
    source: PendingReleaseSource {
      next_handle: 0,
      transport_live: std::collections::HashSet::new(),
      pending: VecDeque::new(),
      pending_set: std::collections::HashSet::new(),
      keys: HashMap::new(),
    },
    subsumer: Subsumer::new(),
    epochs: EpochLedger::new(),
    filters: HashMap::new(),
    needs_rescan: BTreeMap::new(),
    suppressed_rescan: BTreeMap::new(),
    unclaimed: std::collections::HashSet::new(),
    flush_cursor: None,
    #[cfg(test)]
    last_flush_visited: 0,
    #[cfg(test)]
    test_pre_cut_claims: Vec::new(),
    debounce: None,
    coalescer: None,
    pending_syncs: Vec::new(),
    sync_seq: 0,
    sync_nonce_seed: std::collections::hash_map::RandomState::new(),
    loss_serial: HashMap::new(),
    loss_gen: std::sync::Arc::new(core::sync::atomic::AtomicU64::new(0)),
    cleanup_tx,
    cleanup_rx,
    commands: command_rx,
    sync_commands: sync_command_rx,
    closes: close_rx,
    events: event_tx,
    #[cfg(debug_assertions)]
    observed_handles: std::collections::HashSet::new(),
    _rt: PhantomData::<TokioRuntime>,
  };
  let _commands = command_tx; // keep the command channel open (the dropped-handles teardown signal)

  // Watch /a → handle 1, its transport watch installed and live.
  let sub = owner
    .reconcile_watch(&key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a");
  assert_eq!(
    owner.source.root_key(1),
    Some(key("/a")),
    "the armed root is live before release"
  );

  // Release it (the caller unwatch path): the last-subscriber disarm is a synchronous REQUEST — its
  // transport teardown is still queued (applied only at the next arm) …
  owner
    .release_subscription(sub)
    .expect("the subscription is released");

  // … yet the handle is logically dead IMMEDIATELY (contract clause 3) …
  assert_eq!(
    owner.source.root_key(1),
    None,
    "root_key answers None the instant the release is requested"
  );
  // … while its transport watch is genuinely still installed (release pending, applied at next arm).
  assert!(
    owner.source.transport_live.contains(&1),
    "the transport teardown is still pending — an FsSource-shaped deferred release"
  );
}

/// Regression, NORMAL loop (design driver-golden doc, invariant I1 / no false debt):
/// the run loop's top-of-iteration parked-Rescan flush is now **unconditional**, and which parked
/// debt it OFFERS is decided by owner STATE — [`flush_pending_rescans`](super::Owner::flush_pending_rescans)
/// suppresses any entry whose sub is still `unclaimed` (its [`WatchGrant`](super::WatchGrant) in
/// flight). So an orphaned (committed-but-unclaimed, then dropped) subscription's parked terminal
/// [`Rescan`](EventKind::Rescan) is NEVER delivered, no matter how its
/// [`Cleanup::DropOrphan`](super::Cleanup::DropOrphan) interleaves with the flush — closing the TOCTOU the
/// mailbox-idle gate left open (a `DropOrphan` could enqueue after the emptiness probe but before the
/// flush's `try_send`).
///
/// Exercises the REAL spawned [`run`](super::run) loop over a source that arms the watched root live,
/// then on a trigger delivers a terminal event for that root AFTER killing it — and, in the same
/// synchronous terminal-retire step (its `root_key` probe), drops the held reply receiver so the
/// grant's `Drop` enqueues the `DropOrphan`. The committed grant was recorded `unclaimed` by
/// `on_watch`, so its parked terminal Rescan is suppressed by state and the `DropOrphan` then purges
/// it. The event channel has CAPACITY (unbounded), so a non-suppressing flush *would* deliver.
///
/// Fail-on-old: temporarily revert the `unclaimed` suppression in `flush_pending_rescans` (offer every
/// entry) and the parked Rescan is delivered — the drained stream is non-empty and the assertion flips.
#[tokio::test]
async fn unclaimed_orphans_parked_rescan_is_suppressed_by_state_in_the_run_loop() {
  /// The Watch's reply receiver: the owner sends the committed [`WatchGrant`](super::WatchGrant) into
  /// it; the source holds it and drops it during the terminal-retire, so the grant's `Drop` enqueues
  /// the `DropOrphan`.
  type ReplyRx = futures_channel::oneshot::Receiver<Result<super::WatchGrant, WatchError>>;

  /// A source that arms `/a` live, then — on the trigger carrying the Watch's reply receiver —
  /// delivers a terminal `Rescan` for it after killing the root. When `retire_if_dead` probes
  /// `root_key` for the now-dead handle, it drops the held reply receiver, so the grant's `Drop`
  /// enqueues the `DropOrphan` synchronously between the retire's park and the next loop-top flush
  /// (the exact interleaving this pins). After the one terminal event `next` parks, so the loop stays
  /// alive to answer `Close`.
  struct TerminalRetireSource {
    next_handle: u32,
    live: HashMap<u32, Vec<OsString>>,
    trigger: async_channel::Receiver<ReplyRx>,
    held_reply: std::sync::Mutex<Option<ReplyRx>>,
    delivered_terminal: bool,
  }

  impl Source<OsString> for TerminalRetireSource {
    type Handle = u32;

    fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
      Ok(key.to_vec())
    }

    async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
      self.next_handle += 1;
      let handle = self.next_handle;
      self.live.insert(handle, key.to_vec());
      Ok(Armed::new(handle, key.to_vec()))
    }

    fn disarm(&mut self, handle: u32) {
      self.live.remove(&handle);
    }

    async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
      if self.delivered_terminal {
        // The one terminal event is delivered; park so the loop survives to answer `Close`.
        std::future::pending::<Option<SourceEvent<OsString, u32>>>().await
      } else {
        let reply_rx = match self.trigger.recv().await {
          Ok(rx) => rx,
          Err(_) => return None,
        };
        *self.held_reply.lock().unwrap() = Some(reply_rx);
        // Kill the watched root so the terminal-retire's `root_key` probe reports it dead …
        self.live.clear();
        self.delivered_terminal = true;
        // … and deliver its terminal Rescan, which drives the retire-and-park.
        Some(rescan_event(1, "/a", 0))
      }
    }

    fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
      match self.live.get(&handle) {
        Some(k) => Some(k.clone()),
        None => {
          // The dead-root terminal-retire probe: drop the held reply receiver NOW so the grant's
          // `Drop` enqueues the `DropOrphan`. With state suppression the exact interleaving no longer
          // matters (the sub is `unclaimed`, so its parked Rescan is suppressed regardless) — but this
          // still drives the committed-but-unclaimed → terminal-retire → drop path end to end.
          // Idempotent: a later dead probe finds `None`.
          drop(self.held_reply.lock().unwrap().take());
          None
        }
      }
    }
  }

  let (event_tx, event_rx) = async_channel::unbounded::<Event<OsString, ()>>();
  let (command_tx, command_rx) = async_channel::unbounded::<super::Command<OsString, ()>>();
  let (_sync_command_tx, sync_command_rx) = async_channel::unbounded::<super::SyncRequest>();
  let (close_tx, close_rx) = async_channel::bounded::<super::CloseReply>(1);
  let (cleanup_tx, cleanup_rx) = async_channel::unbounded::<super::Cleanup>();
  let (trigger_tx, trigger_rx) = async_channel::unbounded::<ReplyRx>();
  let owner = Owner {
    source: TerminalRetireSource {
      next_handle: 0,
      live: HashMap::new(),
      trigger: trigger_rx,
      held_reply: std::sync::Mutex::new(None),
      delivered_terminal: false,
    },
    subsumer: Subsumer::new(),
    epochs: EpochLedger::new(),
    filters: HashMap::new(),
    needs_rescan: BTreeMap::new(),
    suppressed_rescan: BTreeMap::new(),
    unclaimed: std::collections::HashSet::new(),
    flush_cursor: None,
    #[cfg(test)]
    last_flush_visited: 0,
    #[cfg(test)]
    test_pre_cut_claims: Vec::new(),
    debounce: None,
    coalescer: None,
    pending_syncs: Vec::new(),
    sync_seq: 0,
    sync_nonce_seed: std::collections::hash_map::RandomState::new(),
    loss_serial: HashMap::new(),
    loss_gen: std::sync::Arc::new(core::sync::atomic::AtomicU64::new(0)),
    cleanup_tx,
    cleanup_rx,
    commands: command_rx,
    sync_commands: sync_command_rx,
    closes: close_rx,
    events: event_tx,
    #[cfg(debug_assertions)]
    observed_handles: std::collections::HashSet::new(),
    _rt: PhantomData::<TokioRuntime>,
  };
  let run = tokio::spawn(super::run(owner));

  // A hand-built Watch whose reply receiver is HELD UNPOLLED — the grant lands in the reply slot
  // (committed-but-unclaimed). The receiver is handed to the source (via the trigger) so it can drop
  // it during the terminal-retire.
  let (reply, reply_rx) = futures_channel::oneshot::channel();
  command_tx
    .try_send(super::Command::Watch {
      key: key("/a"),
      value: (),
      options: WatchOptions::new(),
      reply,
    })
    .expect("enqueue the Watch");
  // Hand the reply receiver to the source and trigger the terminal event: the loop commits the watch
  // (recording the grant `unclaimed`), then delivers the terminal event — retire-and-park AND, in the
  // same synchronous step, enqueue the DropOrphan. The parked Rescan is suppressed by state until the
  // DropOrphan purges it.
  trigger_tx
    .try_send(reply_rx)
    .expect("hand the reply receiver to the source");

  // Give the loop a beat to run those iterations to quiescence.
  tokio::time::sleep(Duration::from_millis(250)).await;

  // `close()` still completes (the loop is Close-responsive by construction) — the reply rides the
  // dedicated close signal, not the command mailbox.
  let (close_reply, close_response) = futures_channel::oneshot::channel();
  close_tx
    .try_send(close_reply)
    .expect("send the close on the dedicated signal");
  let acked = tokio::time::timeout(Duration::from_secs(5), close_response)
    .await
    .expect("close() completes within the deadline")
    .expect("the close reply channel stays open");
  assert!(matches!(acked, Ok(())), "close() succeeds");

  // The event stream carries NO event for the orphaned subscription: its parked terminal Rescan was
  // suppressed by the `unclaimed` state and then purged by the DropOrphan. Fail-on-old: revert the
  // suppression and the flush delivers it, so the drain is non-empty.
  let mut delivered = Vec::new();
  while let Ok(event) = event_rx.try_recv() {
    delivered.push(event);
  }
  assert!(
    delivered.is_empty(),
    "the orphan's parked terminal Rescan was suppressed by state — no event for a subscription the \
     caller never obtained (got {} event(s))",
    delivered.len()
  );

  tokio::time::timeout(Duration::from_secs(5), run)
    .await
    .expect("the run task joins after Close")
    .expect("the run task did not panic");
}

/// Regression — a grant left UNPOLLED in the watch reply slot across a source-drain
/// teardown is POISONED: it fired neither `Claim` nor `DropOrphan`, so the teardown's cleanup-channel
/// linearization could not see it, its suppressed parked debt died with the owner, and the stream has
/// already ended. A post-teardown [`defuse`](super::WatchGrant::defuse) must therefore return `Err`
/// (the public `watch` surfaces `Closed`) — never an `Ok` subscription for a stream that ended
/// without its owed Rescan.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn unpolled_grant_across_source_drain_teardown_is_poisoned() {
  let (drain_tx, drain_rx) = async_channel::bounded::<std::convert::Infallible>(1);
  let source = DrainableSource {
    next_handle: 0,
    live: HashMap::new(),
    drain: drain_rx,
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());

  // A hand-built Watch whose reply receiver is HELD UNPOLLED: the owner commits the sub and sends
  // the grant into the slot, where it sits (unclaimed, no Cleanup fired).
  let (reply, response) = futures_channel::oneshot::channel();
  w.commands
    .try_send(super::Command::Watch {
      key: key("/a"),
      value: (),
      options: WatchOptions::new(),
      reply,
    })
    .expect("enqueue the Watch");
  tokio::time::sleep(Duration::from_millis(100)).await; // let the owner commit + send the grant

  // The source drains (next → None): the owner runs its source-drain teardown — the unpolled
  // grant's sub is unclaimed, no Cleanup is queued, so the drain exits and the owner drops.
  drop(drain_tx);
  tokio::time::sleep(Duration::from_millis(200)).await; // let the teardown complete

  // NOW the caller polls the reply: the grant arrives, but its claim's try_send finds the cleanup
  // receiver gone — the grant is poisoned, exactly what the public watch maps to `Closed`.
  let grant = tokio::time::timeout(Duration::from_secs(5), response)
    .await
    .expect("the reply was sent before teardown")
    .expect("the grant sits in the slot")
    .expect("the watch committed successfully");
  assert!(
    grant.defuse().is_err(),
    "a grant polled after the owner tore down is POISONED — watch() surfaces Closed, never a dead Ok"
  );
}

/// Regression — the source-drain exit is ATOMIC with respect to grant claims: the drain
/// CLOSES the cleanup channel before accepting its all-unclaimed exit, so a grant defused in the
/// window after the final emptiness observation but BEFORE the owner drops (the receiver was still
/// alive — the claim-vs-cut race) fails its claim try_send and is POISONED. Fail-on-old: without the in-exit
/// cut, the post-drain defuse lands on the still-open channel and returns a live-looking Ok that no
/// later drain will ever service.
#[tokio::test]
async fn claim_after_the_source_drain_cut_is_poisoned_even_before_owner_drop() {
  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a"); // handle 1
  h.owner.unclaimed.insert(sub);
  // The unpolled grant sits in a caller's reply slot, wired to the SAME cleanup channel.
  let grant = super::WatchGrant::new(sub, h.owner.cleanup_tx.clone());
  // Its root terminal-retires: the owed terminal Rescan parks, suppressed (unclaimed).
  h.owner.retire_root_with_terminal_rescan(1);

  // Source drain runs to its all-unclaimed exit — which now CUTS (closes the cleanup channel),
  // drains pre-cut claims, and runs a final owed pass before returning.
  let returned = tokio::time::timeout(Duration::from_secs(5), h.owner.drain_owed_before_shutdown())
    .await
    .expect("the drain exits promptly");
  assert!(returned.is_none(), "no close interrupted the drain");

  // The OWNER STILL EXISTS (pre-drop window) — yet the claim must already be poisoned.
  assert!(
    grant.defuse().is_err(),
    "a claim after the source-drain cut is poisoned even while the owner is still alive"
  );
  assert!(
    h.drain().iter().all(|e| e.subscription() != sub),
    "the suppressed debt was never delivered for the never-claimed subscription"
  );
}

/// SOURCE-DRAIN teardown under the STATE model (owed = CLAIMED): an UNCLAIMED
/// terminal-retired sub's parked terminal Rescan is suppressed by the owner's `unclaimed` state —
/// never delivered even with event-channel CAPACITY — while a claimed live sub's owed Rescan still
/// delivers, and [`drain_owed_before_shutdown`](super::Owner::drain_owed_before_shutdown) then
/// EXITS instead of spinning on the unclaimed leftover: with NO grant-resolution
/// [`Cleanup`](super::Cleanup) ever arriving, STATE alone withholds the debt AND lets the drain exit —
/// the debt is owed to nobody (the close of the mailbox-idle TOCTOU).
#[tokio::test]
async fn source_drain_suppresses_unclaimed_orphan_debt_and_exits_without_spinning() {
  let mut h = Harness::new(); // unbounded event channel — has capacity
  let live = h.watch("/a", Interest::all()).await.expect("watch /a"); // handle 1
  let orphan = h.watch("/b", Interest::all()).await.expect("watch /b"); // handle 2
  // Model the orphan's grant as still in flight (committed, successfully sent, unclaimed).
  h.owner.unclaimed.insert(orphan);

  // The LIVE (claimed) sub owes a parked dominating Rescan (its overflow debt) …
  for raw in 0..2 {
    h.owner.epochs.stamp(live, Epoch::new(raw));
  }
  h.owner.park_rescan(live);
  // … and the ORPHAN is terminal-retired (parked terminal Rescan + force-removed from the subsumer)
  // while its grant is still unclaimed.
  h.owner.retire_root_with_terminal_rescan(2);
  assert!(
    h.owner.needs_rescan.contains_key(&live) && h.owner.suppressed_rescan.contains_key(&orphan),
    "both parked: the live sub's overflow Rescan offerable, the unclaimed orphan's terminal \
     Rescan in the suppressed partition"
  );

  // NO Cleanup is enqueued: the orphan stays unclaimed with its parked terminal Rescan for the whole
  // drain, so the exit MUST come from STATE (owed-to-nobody), not from the debt being purged away.

  // The drain delivers the claimed sub's owed Rescan, suppresses the unclaimed one, and exits
  // promptly (everything still owed belongs to unclaimed subs — owed to nobody): no spin on an
  // abandoned grant.
  let returned = tokio::time::timeout(Duration::from_secs(5), h.owner.drain_owed_before_shutdown())
    .await
    .expect("the drain exits promptly instead of spinning on unclaimed-only debt");
  assert!(returned.is_none(), "no Close interrupted the drain");

  let delivered = h.drain();
  assert!(
    !delivered.iter().any(|e| e.subscription() == orphan),
    "the unclaimed orphan's parked terminal Rescan is suppressed by state — never delivered"
  );
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == live && e.is_rescan()),
    "the claimed live sub's owed Rescan IS delivered (no-silent-loss for claimed subs)"
  );
  assert!(
    h.owner.needs_rescan.is_empty() && h.owner.suppressed_rescan.contains_key(&orphan),
    "only suppressed (owed-to-nobody) debt remains at exit — the offerable map drained"
  );
}

/// the POST-Close best-effort tail under the STATE model: the tail is a plain
/// [`drain_owed_once`](super::Owner::drain_owed_once) (the retired pre-drain helper is gone — state
/// suppression made it unnecessary). A residual [`Cleanup::DropOrphan`](super::Cleanup::DropOrphan)
/// left UNDRAINED on the cleanup channel (this test runs only `drain_owed_once`, not the cleanup
/// drain) may go entirely unprocessed, yet the unclaimed orphan's parked terminal Rescan is
/// still withheld — suppressed by the owner's `unclaimed` state, not by command timing — while the
/// claimed live sub's owed Rescan is delivered by the same pass. The event channel has CAPACITY, so
/// the flush *would* deliver the orphan's Rescan were it not state-gated.
#[tokio::test]
async fn final_drain_suppresses_unclaimed_orphan_debt_after_close() {
  let mut h = Harness::new(); // unbounded event channel — has capacity
  let live = h.watch("/a", Interest::all()).await.expect("watch /a"); // handle 1
  let orphan = h.watch("/b", Interest::all()).await.expect("watch /b"); // handle 2
  // Model the orphan's grant as still in flight (committed, successfully sent, unclaimed).
  h.owner.unclaimed.insert(orphan);

  for raw in 0..2 {
    h.owner.epochs.stamp(live, Epoch::new(raw));
  }
  h.owner.park_rescan(live);
  h.owner.retire_root_with_terminal_rescan(2);
  assert!(
    h.owner.needs_rescan.contains_key(&live) && h.owner.suppressed_rescan.contains_key(&orphan),
    "both parked: the live sub's overflow Rescan offerable, the unclaimed orphan's terminal \
     Rescan in the suppressed partition"
  );

  // Model the post-Close tail: a residual Cleanup::DropOrphan sits UNDRAINED on the cleanup channel
  // (this test deliberately runs ONLY drain_owed_once, which never touches the cleanup channel), so it
  // goes unprocessed — the suppression must hold from STATE regardless.
  h.owner
    .cleanup_tx
    .try_send(super::Cleanup::DropOrphan(orphan))
    .expect("enqueue the residual DropOrphan cleanup notice");
  h.owner.drain_owed_once();

  let delivered = h.drain();
  assert!(
    !delivered.iter().any(|e| e.subscription() == orphan),
    "the unclaimed orphan's parked Rescan is suppressed by state in the final pass"
  );
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == live && e.is_rescan()),
    "the claimed live sub's owed Rescan IS delivered by the best-effort pass"
  );
}

/// claim-then-deliver: suppression must never become LOSS for a subscription the caller
/// actually obtained. An unclaimed sub's parked terminal Rescan is withheld (retained, not offered);
/// once its [`Cleanup::Claim`](super::Cleanup::Claim) is applied — the caller defused the grant
/// and now holds the sub — the very next flush delivers the parked Rescan: the debt was deferred,
/// never dropped.
#[tokio::test]
async fn claimed_grant_lifts_suppression_and_its_parked_rescan_is_delivered() {
  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a"); // handle 1
  h.owner.unclaimed.insert(sub);

  // Terminal-retire the unclaimed sub's root: its owed terminal Rescan parks into the
  // SUPPRESSED partition — the flush never even visits it.
  h.owner.retire_root_with_terminal_rescan(1);
  assert!(
    h.owner.suppressed_rescan.contains_key(&sub),
    "the terminal Rescan is parked, suppressed"
  );
  h.owner.flush_pending_rescans();
  assert!(
    h.drain().is_empty(),
    "unclaimed: the parked Rescan is suppressed, not offered"
  );
  assert_eq!(
    h.owner.last_flush_visited, 0,
    "…at zero flush cost — suppressed debt lives outside the offerable map"
  );
  assert!(
    h.owner.suppressed_rescan.contains_key(&sub),
    "…and retained (deferred), not dropped"
  );

  // The caller claims: apply the Cleanup::Claim exactly as the run loop's cleanup drain does.
  h.owner.apply_cleanup(super::Cleanup::Claim(sub));
  h.owner.flush_pending_rescans();
  let delivered = h.drain();
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == sub && e.is_rescan()),
    "claimed: the parked terminal Rescan is delivered — suppression never became loss"
  );
  assert!(
    h.owner.needs_rescan.is_empty() && h.owner.suppressed_rescan.is_empty(),
    "the owed debt is resolved once claimed"
  );
}

/// Regression — sustained control-plane load must not starve a CLAIMED sub's parked
/// Rescan: the flush is UNCONDITIONAL again (the retired mailbox-idle gate is reverted), so a live
/// parked Rescan is delivered within a bounded window even while a flood keeps the command mailbox
/// continuously non-empty. Fail-on-old: with the retired `commands.is_empty()` gate, the flood keeps the
/// gate shut and the parked re-point Rescan is withheld past the deadline.
///
/// Setup: a bounded(1) event channel; widening two claimed narrow watches to `/a` mints TWO
/// re-point Rescans — the first fills the only slot, the second overflows and PARKS. A spawned
/// flood then try_sends bursts of `Watch` commands (reply receivers dropped, so each send-fails and
/// is released synchronously) keeping the mailbox busy, while the consumer drains: BOTH re-point
/// Rescans must arrive within the deadline (the parked one flushed between commands).
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn parked_rescan_delivers_under_sustained_command_load() {
  let (_drain_tx, drain_rx) = async_channel::bounded::<std::convert::Infallible>(1);
  let source = DrainableSource {
    next_handle: 0,
    live: HashMap::new(),
    drain: drain_rx,
  };
  let mut w: super::Tributaries<OsString, (), TokioRuntime, u32> = super::Tributaries::with_source(
    source,
    TributariesOptions::new().with_event_capacity(std::num::NonZeroUsize::new(1).expect("nonzero")),
  );

  // Two claimed narrow watches, then the widen: two re-point Rescans — one fills bounded(1), the
  // other parks as overflow debt for a CLAIMED subscription.
  let narrow_b = w
    .watch(key("/a/b"), (), WatchOptions::new())
    .await
    .expect("watch /a/b");
  let narrow_c = w
    .watch(key("/a/c"), (), WatchOptions::new())
    .await
    .expect("watch /a/c");
  let _wide = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("widen to /a");

  // The flood: bursts of Watch commands whose reply receivers are dropped (each send-fails at the
  // owner and is released synchronously) — pure control-plane load keeping the mailbox non-empty.
  let flood_commands = w.commands.clone();
  let flood = tokio::spawn(async move {
    loop {
      for _ in 0..8 {
        let (reply, response) = futures_channel::oneshot::channel();
        drop(response);
        if flood_commands
          .try_send(super::Command::Watch {
            key: key("/flood"),
            value: (),
            options: WatchOptions::new(),
            reply,
          })
          .is_err()
        {
          return;
        }
      }
      tokio::task::yield_now().await;
    }
  });

  // Under sustained load, BOTH re-point Rescans arrive within the deadline: the queued one first,
  // then the PARKED one — flushed by the unconditional per-tick flush despite the busy mailbox.
  let mut seen = std::collections::HashSet::new();
  for _ in 0..2 {
    let event = tokio::time::timeout(Duration::from_secs(5), w.next())
      .await
      .expect("a re-point Rescan is delivered within the deadline despite sustained command load")
      .expect("the stream is open");
    assert!(event.is_rescan(), "the widen minted re-point Rescans");
    seen.insert(event.subscription());
  }
  assert!(
    seen.contains(&narrow_b) && seen.contains(&narrow_c),
    "both re-pointed subscriptions received their Rescan — the parked one was never starved"
  );
  flood.abort();
}

/// Spawns a control-plane flood: bursts of `Watch` commands whose reply receivers are dropped (each
/// send-fails at the owner and is released synchronously) — keeping the command mailbox continuously
/// non-empty, so the command-biased select's other arms can win only through the fairness valve.
fn spawn_command_flood(
  commands: async_channel::Sender<super::Command<OsString, ()>>,
) -> tokio::task::JoinHandle<()> {
  tokio::spawn(async move {
    loop {
      for _ in 0..8 {
        let (reply, response) = futures_channel::oneshot::channel();
        drop(response);
        if commands
          .try_send(super::Command::Watch {
            key: key("/flood"),
            value: (),
            options: WatchOptions::new(),
            reply,
          })
          .is_err()
        {
          return;
        }
      }
      tokio::task::yield_now().await;
    }
  })
}

/// A source whose `next` yields one pre-queued event per trigger message, then parks — the
/// command-flood fairness rig: with the command arm continuously ready, only the run
/// loop's fairness valve can pump these events. `next` is cancellation-safe: the trigger message
/// and the event are consumed on the same poll that returns `Ready`.
struct TriggeredSource {
  next_handle: u32,
  live: HashMap<u32, Vec<OsString>>,
  events: std::collections::VecDeque<SourceEvent<OsString, u32>>,
  trigger: async_channel::Receiver<()>,
  grows: std::sync::Arc<std::sync::atomic::AtomicU32>,
}

impl Source<OsString> for TriggeredSource {
  type Handle = u32;

  fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
    Ok(key.to_vec())
  }

  async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
    self.next_handle += 1;
    let handle = self.next_handle;
    self.live.insert(handle, key.to_vec());
    Ok(Armed::new(handle, key.to_vec()))
  }

  fn disarm(&mut self, handle: u32) {
    self.live.remove(&handle);
  }

  async fn grow(&mut self, _handle: u32, _retained: &[Vec<OsString>]) -> Result<(), WatchError> {
    self.grows.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
    Ok(())
  }

  async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
    match self.trigger.recv().await {
      Ok(()) => self.events.pop_front(),
      Err(_) => None,
    }
  }

  fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
    self.live.get(&handle).cloned()
  }
}

/// Regression — a `Cleanup::Claim` already QUEUED when the source drains must be drained
/// before the drain's all-unclaimed exit: the caller defused the grant (it holds the sub), so the
/// parked terminal Rescan is genuinely owed and must be delivered before the stream ends. The exit
/// predicate reads post-claim state (the cleanup channel is drained and must be observed empty), or
/// suppression becomes permanent loss. Fail-on-old: the old exit takes the all-unclaimed arm
/// before the queued claim is drained — nothing is delivered and the assertion flips.
#[tokio::test]
async fn queued_claim_grant_is_serviced_before_the_source_drain_exit() {
  let mut h = Harness::new(); // unbounded event channel — has capacity
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a"); // handle 1
  h.owner.unclaimed.insert(sub);
  // Terminal-retire the unclaimed sub's root: its owed terminal Rescan parks into the
  // suppressed partition.
  h.owner.retire_root_with_terminal_rescan(1);
  assert!(
    h.owner.suppressed_rescan.contains_key(&sub),
    "the terminal Rescan is parked, suppressed"
  );
  // The caller defuses as the source drains: its Cleanup::Claim is already queued when the drain runs.
  h.owner
    .cleanup_tx
    .try_send(super::Cleanup::Claim(sub))
    .expect("enqueue the Claim on the cleanup channel");

  let returned = tokio::time::timeout(Duration::from_secs(5), h.owner.drain_owed_before_shutdown())
    .await
    .expect("the drain exits promptly after delivering the claimed debt");
  assert!(returned.is_none(), "no Close interrupted the drain");

  let delivered = h.drain();
  assert!(
    delivered
      .iter()
      .any(|e| e.subscription() == sub && e.is_rescan()),
    "the claimed sub's parked Rescan is delivered before the source-drain exit"
  );
  assert!(
    h.owner.needs_rescan.is_empty() && h.owner.suppressed_rescan.is_empty(),
    "the owed debt is resolved, not stranded"
  );
}

/// Regression — a RAW source event is delivered within a bounded window under a
/// sustained command flood: the command-biased select would starve `next()` forever, so the
/// fairness valve (after `COMMAND_FAIRNESS_BUDGET` consecutive command wins, one non-blocking
/// source poll + a due-coalescer drain) is what pumps it. Fail-on-old: without the valve the
/// command arm wins every iteration and the event never surfaces — the recv times out.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn raw_source_event_delivers_under_sustained_command_load() {
  let (trigger_tx, trigger_rx) = async_channel::unbounded::<()>();
  let source = TriggeredSource {
    next_handle: 0,
    live: HashMap::new(),
    events: std::collections::VecDeque::from([SourceEvent::new(
      1,
      key("/a/f"),
      EventKind::Created,
      Location::new(),
      Epoch::new(0),
      Some(ChangeId::new(NonZeroU64::MIN)),
    )]),
    trigger: trigger_rx,
    grows: std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)),
  };
  let mut w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());
  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a"); // handle 1

  let flood = spawn_command_flood(w.commands.clone());
  trigger_tx.try_send(()).expect("release the queued event");

  let event = tokio::time::timeout(Duration::from_secs(5), w.next())
    .await
    .expect("the raw source event is delivered despite the sustained command flood (the valve)")
    .expect("the stream is open");
  assert_eq!(event.subscription(), sub, "routed to the covering sub");
  flood.abort();
}

/// Regression — a live-root coverage-loss `Rescan` pumped through the COMMAND-FAIRNESS
/// VALVE (not the select arm) still degrades the retained-cover record: both source
/// paths run the one `consume_source_event` funnel (retire → degrade → fan out).
/// Fail-on-old: the valve's forced poll went retire → fan-out directly, so under a
/// sustained command flood the `Rescan` delivered while the stale narrowed claim
/// survived — and the next covered newcomer skipped its coverage-re-proving grow.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn valve_pumped_rescan_still_degrades_the_retained_cover() {
  let (trigger_tx, trigger_rx) = async_channel::unbounded::<()>();
  let grows = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
  let source = TriggeredSource {
    next_handle: 0,
    live: HashMap::new(),
    // The queued live-root coverage-loss signal: handle 2 is the wide /a root the widen
    // below arms second.
    events: std::collections::VecDeque::from([SourceEvent::new(
      2,
      key("/a"),
      EventKind::Rescan,
      Location::new(),
      Epoch::new(1),
      None,
    )]),
    trigger: trigger_rx,
    grows: grows.clone(),
  };
  let mut w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());

  // Narrow the wide /a root to {/a/b}: widen /a over /a/b (handle 2), then prune the widener.
  let s_b = w
    .watch(key("/a/b"), (), WatchOptions::new())
    .await
    .expect("watch /a/b");
  let s_a = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a widens");
  w.unwatch(s_a).await.expect("unwatch the widening /a");
  // Consume the widen re-point Rescan so the valve-pumped loss signal is the next delivery.
  let repoint = tokio::time::timeout(Duration::from_secs(5), w.next())
    .await
    .expect("the re-point Rescan arrives")
    .expect("stream open");
  assert!(repoint.is_rescan() && repoint.subscription() == s_b);

  // Under a sustained command flood only the fairness valve pumps the source: the queued
  // loss Rescan must arrive AND degrade the record on that same valve path.
  let flood = spawn_command_flood(w.commands.clone());
  trigger_tx.try_send(()).expect("release the queued Rescan");
  let event = tokio::time::timeout(Duration::from_secs(5), w.next())
    .await
    .expect("the loss Rescan is delivered through the valve despite the flood")
    .expect("stream open");
  assert!(
    event.is_rescan() && event.subscription() == s_b,
    "the coverage-loss Rescan fanned to the covering sub"
  );
  flood.abort();

  // The record degraded on the valve path iff this covered newcomer re-proves via grow.
  assert_eq!(grows.load(std::sync::atomic::Ordering::SeqCst), 0);
  let _s2 = w
    .watch(key("/a/b/x"), (), WatchOptions::new())
    .await
    .expect("the post-loss newcomer commits through a re-proving grow");
  assert_eq!(
    grows.load(std::sync::atomic::Ordering::SeqCst),
    1,
    "the newcomer classified covered-OUTSIDE against the degraded record and grew"
  );
}

/// Regression — the source-drain teardown makes OWED progress under a sustained command
/// flood: its per-iteration command servicing is BOUNDED (COMMAND_FAIRNESS_BUDGET), so a
/// continuously non-empty mailbox cannot starve `drain_owed_once` — an already-CLAIMED parked
/// Rescan is delivered within a bounded window even though the flood keeps the drain from exiting
/// (the exit's empty-mailbox linearization defers, delivery does not).
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn source_drain_delivers_claimed_debt_under_sustained_command_flood() {
  let mut h = Harness::new(); // unbounded event channel — has capacity
  let live = h.watch("/a", Interest::all()).await.expect("watch /a"); // claimed (Harness::watch)
  for raw in 0..2 {
    h.owner.epochs.stamp(live, Epoch::new(raw));
  }
  h.owner.park_rescan(live);

  let flood = spawn_command_flood(h._commands.clone());
  // Under the flood the drain never observes an empty mailbox, so it keeps servicing (and never
  // exits within the window) — but the bounded pre-drain guarantees the owed pass runs every
  // iteration, so the claimed Rescan is delivered long before the timeout lapses.
  let _ = tokio::time::timeout(Duration::from_secs(2), h.owner.drain_owed_before_shutdown()).await;
  flood.abort();
  assert!(
    h.drain()
      .iter()
      .any(|e| e.subscription() == live && e.is_rescan()),
    "the claimed sub's parked Rescan is delivered despite the sustained command flood"
  );
}

/// Regression — DUE debounced output drains within a bounded window under a sustained
/// command flood: the settle-timer arm can never win against a continuously-ready command arm, so
/// the valve's due-coalescer drain is what honors the coalescer's hold bounds. Fail-on-old: without
/// the valve neither the timer nor the source arm ever fires and the buffered event never drains.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn due_debounced_event_drains_under_sustained_command_load() {
  let (trigger_tx, trigger_rx) = async_channel::unbounded::<()>();
  let source = TriggeredSource {
    next_handle: 0,
    live: HashMap::new(),
    events: std::collections::VecDeque::from([SourceEvent::new(
      1,
      key("/a/f"),
      EventKind::Modified,
      Location::new(),
      Epoch::new(0),
      Some(ChangeId::new(NonZeroU64::MIN)),
    )]),
    trigger: trigger_rx,
    grows: std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0)),
  };
  let cfg = DebounceConfig::new()
    .with_quiet_window(Duration::from_millis(20))
    .with_max_hold(Duration::from_millis(100));
  let mut w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new().debounce(cfg));
  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a"); // handle 1

  let flood = spawn_command_flood(w.commands.clone());
  trigger_tx.try_send(()).expect("release the queued event");

  let event = tokio::time::timeout(Duration::from_secs(5), w.next())
    .await
    .expect("the due debounced event drains despite the flood (the valve's due-coalescer drain)")
    .expect("the stream is open");
  assert_eq!(event.subscription(), sub, "routed to the covering sub");
  flood.abort();
}

/// Regression — `close()` is never starved behind an unbounded command backlog:
/// the reply rides a **dedicated** high-priority signal, checked at the TOP priority in the real
/// [`run`](super::run) loop (a non-blocking `try_recv` each iteration AND the first `select!` arm),
/// so a requested shutdown completes within a bounded window no matter how deep the command mailbox
/// is. Exercises the REAL spawned run loop over a source that parks (never drains, so the loop stays
/// alive purely to answer the close), with the unbounded mailbox both PREFILLED with 500 fail-fast
/// `Watch` commands AND kept continuously non-empty by a spawned flood.
///
/// Fail-on-old: with `Close` on the FIFO command mailbox behind the 500-deep backlog + the ongoing
/// flood, the owner would have to chew through the whole backlog (arm/orphan/disarm each) before it
/// ever dequeued the `Close`, so `close()` waits it out and probabilistically times out.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn close_is_not_starved_by_a_prefilled_command_backlog_and_flood() {
  let (_drain_tx, drain_rx) = async_channel::bounded::<std::convert::Infallible>(1);
  let source = DrainableSource {
    next_handle: 0,
    live: HashMap::new(),
    drain: drain_rx, // held open (`_drain_tx` kept alive) so `next()` parks forever
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> = super::Tributaries::with_source(
    source,
    // The mailbox is BOUNDED; size it to this test's full 500-deep
    // prefill so the close-vs-deepest-possible-backlog shape is preserved.
    TributariesOptions::new().with_command_capacity(std::num::NonZeroUsize::new(500).unwrap()),
  );

  // A live claimed subscription, so the owner is genuinely running with real state (the finding's
  // "owner/source kept alive while shutdown is requested").
  w.watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a");

  // Prefill the command mailbox to its FULL configured depth with fail-fast Watch commands: each
  // reply receiver is dropped, so the owner's send-back fails and it releases the orphan
  // synchronously (arm→disarm) — real per-command work the old FIFO `Close` would have queued
  // behind.
  for _ in 0..500 {
    let (reply, response) = futures_channel::oneshot::channel();
    drop(response);
    w.commands
      .try_send(super::Command::Watch {
        key: key("/backlog"),
        value: (),
        options: WatchOptions::new(),
        reply,
      })
      .expect("prefill the command backlog");
  }
  // …and a sustained flood keeping the mailbox continuously non-empty against the real run loop.
  let flood = spawn_command_flood(w.commands.clone());

  // close() rides the dedicated close signal, so it completes within a bounded window DESPITE the
  // 500-deep backlog + ongoing flood on the command mailbox.
  let closed = tokio::time::timeout(Duration::from_secs(5), w.close())
    .await
    .expect("close() completes within the deadline despite the command backlog + flood");
  assert!(matches!(closed, Ok(())), "close() succeeds");
  flood.abort();
}

/// Regression, SOURCE-DRAIN teardown under a flood — a close DURING the owed-Rescan
/// drain is surfaced within a bounded window even while a sustained command flood keeps the mailbox
/// continuously non-empty. [`drain_owed_before_shutdown`](super::Owner::drain_owed_before_shutdown)
/// checks the dedicated close signal FIRST (a non-blocking `try_recv` before its bounded command
/// pre-drain AND the first arm of its retry `select!`), so the close outranks the flood the bounded
/// pre-drain services. A claimed sub's parked overflow Rescan behind a full, held-open channel keeps
/// the drain spinning (so the close-check is genuinely exercised mid-drain).
///
/// Fail-on-old (Close on the FIFO mailbox): behind the flood the drain's command servicing would
/// dequeue flood commands ahead of the `Close`, so the close waits out the never-emptying backlog and
/// times out.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn source_drain_close_is_surfaced_under_sustained_command_flood() {
  let mut h = Harness::bounded(1);
  let live = h.watch("/a", Interest::all()).await.expect("watch /a"); // claimed
  for raw in 0..2 {
    h.owner.epochs.stamp(live, Epoch::new(raw));
  }
  // Fill the one slot and overflow → park the CLAIMED sub's dominating Rescan; the held receiver
  // keeps the channel full + open, so the drain keeps spinning (neither exit fires on its own).
  h.owner.try_emit(modified_event(live, "/a/f0", 0));
  h.owner.try_emit(modified_event(live, "/a/f1", 1));
  assert!(
    h.owner.needs_rescan.contains_key(&live),
    "overflow parked the claimed sub's Rescan; the channel is full"
  );
  let _held = h.events.clone(); // a receiver that never drains (keeps the channel full + open)

  // A sustained command flood keeping the mailbox continuously non-empty during the drain.
  let flood = spawn_command_flood(h._commands.clone());

  // A close rides the dedicated signal.
  let (reply, response) = futures_channel::oneshot::channel();
  h.closes
    .try_send(reply)
    .expect("send the close on the dedicated signal");

  // The source-drain teardown surfaces the close within the deadline despite the flood.
  let returned = tokio::time::timeout(Duration::from_secs(5), h.owner.drain_owed_before_shutdown())
    .await
    .expect("the source-drain teardown surfaced the close despite the sustained command flood");
  let close_reply = returned.expect("the mid-drain close is surfaced to the caller to be acked");
  // Ack it exactly as `run` does; the close() caller then completes.
  close_reply.send(Ok(())).expect("ack the close");
  assert!(
    matches!(response.await, Ok(Ok(()))),
    "close() completes once the drain surfaced and acked its close, under the flood"
  );
  flood.abort();
}

/// F2 + F3 — a `Rescan` at or above a pending cookie's key resolves that
/// barrier `Dominated`, and it does so only AFTER the `Rescan` itself is
/// published to the stream (structurally: `consume_source_event` fans out
/// before it dominates). The postcondition proves both: the caller's reply is
/// `Dominated` AND the covering `Rescan` is drainable, never a reply that
/// outran its cover.
#[tokio::test]
async fn a_rescan_dominates_a_pending_sync_and_the_rescan_is_published() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/a/cookie-1"),
    sub,
    root: handle,
    loss_serial_at_install: 0,
    dominated_at_install: false,
    reply: reply_tx,
  });

  h.owner.consume_source_event(&rescan_event(handle, "/a", 5));

  let events = h.drain();
  assert!(
    events.iter().any(|e| e.kind().is_rescan()),
    "the covering Rescan is on the stream: {events:?}"
  );
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "the barrier resolved Dominated, not Delivered"
  );
}

/// F3 — a cookie that is delivered while its subscription already carries a
/// parked `Rescan` (an earlier loss) resolves `Dominated`, not `Delivered`:
/// the caller must re-enumerate, so reporting a clean delivery would risk
/// stale state.
#[tokio::test]
async fn a_cookie_delivered_over_existing_rescan_debt_resolves_dominated() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  // A bounded, stalled channel so a fanned Rescan sheds to `needs_rescan`.
  let mut h = Harness::bounded(1);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  // Fill the channel, then a Rescan sheds the sub to a parked (needs_rescan) Rescan.
  h.owner.try_emit(modified_event(sub, "/a/x", 1));
  h.owner.consume_source_event(&rescan_event(handle, "/a", 2));
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "the sub carries parked Rescan debt"
  );

  // Now the cookie arrives. Delivery cannot be clean while a Rescan is owed.
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/a/cookie-1"),
    sub,
    root: handle,
    loss_serial_at_install: 0,
    dominated_at_install: false,
    reply: reply_tx,
  });
  h.owner
    .consume_source_event(&source_created(handle, "/a/cookie-1", 3));
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "a cookie delivered over Rescan debt is Dominated"
  );
}

/// R13 — the interest/filter lens at the umbrella boundary (the end-to-end
/// companion to the proto fail-on-old cells): a `Modified`-only subscription
/// cannot be discharged by a structural `Removed` it never sees — only by the
/// covering `Rescan` the Monitor now stands over an erased coverage deficit.
/// The `Removed` for a dark child is FILTERED (never delivered) and does NOT
/// dominate the pending sync; the covering `Rescan` IS delivered (Rescans
/// bypass interest AND filter) and resolves the barrier `Dominated`. This
/// proves the "reached the sub" half of the R13 fix: the covering `Rescan` the
/// proto layer emits when a filter-subject record empties a hole DOES reach a
/// filtered subscriber and dominate its sync, where the structural record —
/// which two prior Monitor-level reviews assumed "converged the consumer" —
/// provably could not.
#[tokio::test]
async fn a_covering_rescan_dominates_a_modified_only_sync_a_removed_cannot() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  // The umbrella's `Interest::new()` is deliver-everything (== all); a
  // Modified-only subscription starts from `none` and opts in Modified alone.
  let sub = h
    .watch("/a", Interest::none().with_modified())
    .await
    .expect("watch /a modified-only");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/a/cookie-1"),
    sub,
    root: handle,
    loss_serial_at_install: 0,
    dominated_at_install: false,
    reply: reply_tx,
  });

  // A structural Removed for the dark child: FILTERED (a Modified-only sub
  // never sees it) and it does NOT dominate the sync — a structural record a
  // filtered sub cannot see must not discharge the barrier.
  h.owner
    .consume_source_event(&source_removed(handle, "/a/child", 5));
  assert!(
    h.drain().iter().all(|e| !e.kind().is_removed()),
    "the Removed is filtered from the Modified-only sub"
  );
  assert!(
    (&mut reply_rx).now_or_never().is_none(),
    "a Removed the sub never sees does not resolve the barrier"
  );

  // The covering Rescan the Monitor now stands over the erased hole: delivered
  // (it bypasses the filter) and dominating.
  h.owner.consume_source_event(&rescan_event(handle, "/a", 6));
  assert!(
    h.drain().iter().any(|e| e.kind().is_rescan()),
    "the covering Rescan reaches the Modified-only sub"
  );
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "the barrier resolved Dominated via the covering Rescan, not a false Delivered"
  );
}

/// F5 — a CALLER unwatch of a subscription with a pending sync fails the
/// barrier `Retired` AND reaps its cookie file (the root is still live, so the
/// marker is real and must not leak).
#[tokio::test]
async fn an_unwatched_subscription_reaps_its_pending_cookie() {
  use futures_util::FutureExt;

  use crate::error::SyncError;

  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/a/cookie-1"),
    sub,
    root: handle,
    loss_serial_at_install: 0,
    dominated_at_install: false,
    reply: reply_tx,
  });

  h.owner.release_subscription(sub).expect("unwatch");

  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Err(SyncError::Retired)))
    ),
    "the caller-unwatched barrier fails Retired"
  );
  assert_eq!(
    h.owner.source.ended_syncs,
    vec![key("/a/cookie-1")],
    "the cookie is reaped, not leaked"
  );
}

/// F6 — an in-place widen whose retarget canonicalizes to a key that does NOT
/// contain the subsumed root ROLLS THE HANDLE BACK to its original coverage
/// (never disarms it while old subscribers are committed) and refuses the
/// newcomer with a canonicalization race.
#[tokio::test]
async fn a_diverging_in_place_widen_rolls_back_and_keeps_old_coverage() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  h.owner.source.supports_replace = true;

  let s_narrow = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let handle = h
    .owner
    .subsumer
    .subscription_root(s_narrow)
    .expect("live root");

  // A pending sync on the sole root's subscriber, installed BEFORE the widen. The exact rollback
  // retargets the preserved stream away and back with NO Rescan, silently missing any change in that
  // window — so the barrier must resolve `Dominated` (re-enumerate), never strand to timeout.
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/a/b/cookie-1"),
    sub: s_narrow,
    root: handle,
    loss_serial_at_install: 0,
    dominated_at_install: false,
    reply: reply_tx,
  });

  // The widen of /a canonicalizes to /z — which does NOT contain /a/b, so it
  // would strand s_narrow. The rollback must restore /a/b on the same handle.
  h.owner
    .source
    .retarget
    .insert(PathBuf::from("/a"), PathBuf::from("/z"));

  let err = h
    .watch("/a", Interest::all())
    .await
    .expect_err("the diverging widen is refused");
  assert!(matches!(err, WatchError::CanonicalRace), "{err:?}");

  // The preserved handle was NEVER disarmed — no Disarm in the ledger.
  assert!(
    !h.owner
      .source
      .calls()
      .iter()
      .any(|c| matches!(c, Call::Disarm(_))),
    "the preserved handle must not be disarmed while old subscribers remain: {:?}",
    h.owner.source.calls()
  );
  // s_narrow still has its coverage: the root is keyed back at /a/b.
  let roots: Vec<PathBuf> = h
    .owner
    .subsumer
    .roots()
    .map(|(k, _)| PathBuf::from_iter(k))
    .collect();
  assert_eq!(
    roots,
    vec![PathBuf::from("/a/b")],
    "the sole root is rolled back to its original coverage"
  );
  assert!(
    h.owner.subsumer.subscription_root(s_narrow).is_some(),
    "the old subscription is still live on the restored root"
  );

  // The rollback rebinds the sole live root's stream: its subscriber's pending sync resolves
  // `Dominated` (re-enumeration meets the barrier), its cookie is reaped, and it owes a dominating
  // `Rescan` that publishes and clears once the consumer drains.
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "the sole root's pending sync resolves Dominated after the exact rollback"
  );
  assert_eq!(
    h.owner.source.ended_syncs,
    vec![key("/a/b/cookie-1")],
    "the dominated barrier's cookie is reaped"
  );
  assert!(
    h.owner.needs_rescan.contains_key(&s_narrow),
    "s_narrow owes a dominating Rescan for the silently-missed retarget window"
  );
  h.owner.flush_pending_rescans();
  let events = h.drain();
  assert!(
    events
      .iter()
      .any(|e| e.subscription() == s_narrow && e.kind().is_rescan()),
    "s_narrow receives the dominating Rescan: {events:?}"
  );
}

/// R2-d — a delta shed to a parked Rescan for a pre-cookie change, then
/// PUBLISHED (cleared from needs_rescan) before the cookie, still resolves the
/// barrier `Dominated`: the sticky loss serial advanced during the window, so
/// even with no debt left parked at resolution the caller is told to
/// re-enumerate rather than trust a false `Delivered`.
#[tokio::test]
async fn a_loss_published_before_the_cookie_still_resolves_dominated() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::bounded(4);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  // Install the barrier FIRST (snapshots the loss serial at 0).
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/a/cookie-1"),
    sub,
    root: handle,
    loss_serial_at_install: h.owner.loss_serial.get(&sub).copied().unwrap_or(0),
    dominated_at_install: false,
    reply: reply_tx,
  });

  // A loss during the window: fill the channel, shed a delta to a parked
  // Rescan, then drain the channel so the parked Rescan PUBLISHES and clears.
  for i in 0..4 {
    h.owner.try_emit(modified_event(sub, "/a/fill", i));
  }
  h.owner.try_emit(modified_event(sub, "/a/lost", 9)); // sheds -> parked
  assert!(h.owner.needs_rescan.contains_key(&sub));
  let _ = h.drain(); // free the channel
  h.owner.flush_pending_rescans(); // publish the parked Rescan -> clears needs_rescan
  assert!(
    !h.owner.needs_rescan.contains_key(&sub),
    "the parked Rescan was published and cleared"
  );

  // The cookie arrives with NO debt parked — but the serial advanced, so
  // Dominated, not a false Delivered.
  h.owner
    .consume_source_event(&source_created(handle, "/a/cookie-1", 20));
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "a loss published before the cookie must still yield Dominated"
  );
}

/// R2-a — a widen's re-pointed subscription with a pending sync resolves
/// `Dominated` at commit time (its stream re-based onto the wider root), never
/// waiting for a cookie on the old handle.
#[tokio::test]
async fn a_widen_resolves_the_repointed_subscriptions_pending_sync() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  let s_narrow = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let handle = h.owner.subsumer.subscription_root(s_narrow).expect("live");

  // A pending sync on the narrow sub.
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/a/b/cookie-1"),
    sub: s_narrow,
    root: handle,
    loss_serial_at_install: 0,
    dominated_at_install: false,
    reply: reply_tx,
  });

  // Widen /a/b -> /a: s_narrow is re-pointed onto the wider root.
  let _wide = h
    .watch("/a", Interest::all())
    .await
    .expect("watch /a widens");

  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "the re-pointed subscription's barrier resolves Dominated at the widen"
  );
}

/// F1 — a barrier installed over debt that ALREADY stood at install (a change lost BEFORE the
/// barrier) resolves `Dominated`, even after that parked Rescan publishes-and-clears before the
/// cookie: at resolution the loss serial is unchanged AND `needs_rescan` is empty, so ONLY the
/// install-time `dominated_at_install` snapshot separates it from a genuinely clean sync. A pre-call
/// loss is exactly what the barrier must not hide behind a false `Delivered`.
///
/// Fail-on-old (no `dominated_at_install`): the published-and-cleared debt leaves a clean flush and
/// an unchanged serial, so the barrier reports `Delivered` — the bug.
#[tokio::test]
async fn a_barrier_installed_over_existing_debt_resolves_dominated() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  // Pre-install debt: the sub already owes a parked Rescan (a change lost before ANY barrier).
  h.owner.park_rescan(sub);
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "the sub owes a parked Rescan before the barrier is installed"
  );

  // Install the barrier through the REAL `on_sync` path, so it snapshots `dominated_at_install` from
  // the standing debt (the loss serial is snapshot too, but will not advance during the window). The
  // caller's generation snapshot is taken here, AFTER the park, so only the standing debt can force
  // the domination — the property this cell pins.
  let loss_gen_at_call = h.owner.loss_gen.load(core::sync::atomic::Ordering::SeqCst);
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.on_sync(sub, loss_gen_at_call, reply_tx).await;
  assert!(
    !h.owner.source.ended_syncs.contains(&key("/a/cookie-1")),
    "the cookie is still pending — not yet reaped"
  );

  // The pre-install Rescan publishes and clears BEFORE the cookie: no debt parked and the serial
  // unchanged at resolution, so only the install-time snapshot can still force Dominated.
  h.owner.flush_pending_rescans();
  assert!(
    !h.owner.needs_rescan.contains_key(&sub),
    "the pre-install Rescan published and cleared"
  );

  // The cookie arrives clean-looking — but the pre-call loss the install captured wins.
  h.owner
    .consume_source_event(&source_created(handle, "/a/cookie-1", 20));
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "a barrier installed over pre-existing debt resolves Dominated, not a false Delivered"
  );
  assert_eq!(
    h.owner.source.ended_syncs,
    vec![key("/a/cookie-1")],
    "the resolved barrier's cookie is reaped"
  );
}

/// A cookie write that completes for a caller who has ALREADY gone is reaped inline, never parked and
/// never orphaned. This is the `on_sync` race's write-first outcome: the fs backend buffered its
/// successful `Ok(path)` just as the caller's deadline fired, so its own send-failure self-reap will
/// not run — reaping here is the only thing that frees the file.
///
/// Fail-on-old (drop the caller-gone check in `admit_begun_cookie`): the completed cookie is parked as
/// a `PendingSync` no one waits on and its file is never `end_sync`ed here — both assertions fail.
#[tokio::test]
async fn a_completed_cookie_for_a_gone_caller_is_reaped_not_installed() {
  use crate::{error::SyncError, source::SyncOutcome};

  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  let (reply_tx, reply_rx) = futures_channel::oneshot::channel::<Result<SyncOutcome, SyncError>>();
  drop(reply_rx);
  let loss_gen_at_call = h.owner.loss_gen.load(core::sync::atomic::Ordering::SeqCst);
  h.owner
    .admit_begun_cookie(key("/a/cookie-1"), sub, handle, loss_gen_at_call, reply_tx);

  assert!(
    h.owner.pending_syncs.is_empty(),
    "a completed cookie for a gone caller parks no barrier"
  );
  assert_eq!(
    h.owner.source.ended_syncs,
    vec![key("/a/cookie-1")],
    "the completed cookie is reaped inline, never orphaned"
  );
}

/// The companion: a completed cookie for a caller still waiting IS parked (and not reaped), so the
/// caller-gone reap above is genuinely gated on the cancellation, not unconditional.
#[tokio::test]
async fn a_completed_cookie_for_a_live_caller_is_parked_not_reaped() {
  use crate::{error::SyncError, source::SyncOutcome};

  let mut h = Harness::new();
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  let (reply_tx, _reply_rx) = futures_channel::oneshot::channel::<Result<SyncOutcome, SyncError>>();
  let loss_gen_at_call = h.owner.loss_gen.load(core::sync::atomic::Ordering::SeqCst);
  h.owner
    .admit_begun_cookie(key("/a/cookie-1"), sub, handle, loss_gen_at_call, reply_tx);

  assert_eq!(
    h.owner.pending_syncs.len(),
    1,
    "a live caller's barrier is parked to await its cookie"
  );
  assert!(
    h.owner.source.ended_syncs.is_empty(),
    "and its cookie is not reaped while the caller still waits"
  );
}

/// A covering `Rescan` DELIVERED (never parked) between the caller's `sync()` call and the barrier's
/// install dominates it too. An already-installed barrier riding that same `Rescan` resolves
/// `Dominated` (the `dominate_*` family does it directly); a barrier whose caller had merely CALLED
/// must not resolve `Delivered` for the change that `Rescan` replaced, or the outcome would depend on
/// how the owner's loop happened to interleave the request with the event.
///
/// The delivered path leaves NO trace the install-time probes can read — nothing parks, so the debt
/// maps stay empty and the per-subscription `loss_serial` never moves. Only the shared generation,
/// advanced at the domination choke point, still carries it.
#[tokio::test]
async fn a_delivered_rescan_between_the_call_and_the_install_resolves_dominated() {
  use core::sync::atomic::Ordering;

  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  // The caller snapshots the shared generation, exactly as `Tributaries::sync` does before enqueueing.
  let loss_gen_at_call = h.owner.loss_gen.load(Ordering::SeqCst);

  // A source `Rescan` is consumed and DELIVERED while the request still sits in the mailbox.
  h.owner
    .consume_source_event(&rescan_event(handle, "/a", 10));
  assert!(
    !h.owner.needs_rescan.contains_key(&sub) && !h.owner.suppressed_rescan.contains_key(&sub),
    "the Rescan was delivered, not parked — no standing debt for the install to snapshot"
  );
  assert_eq!(
    h.owner.loss_serial.get(&sub).copied().unwrap_or(0),
    0,
    "a delivered Rescan never moves the per-subscription serial — the long-window probe is blind too"
  );

  // Install with the caller's stale snapshot, then deliver the cookie over the pristine-looking state.
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.on_sync(sub, loss_gen_at_call, reply_tx).await;
  h.owner
    .consume_source_event(&source_created(handle, "/a/cookie-1", 20));
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "a Rescan delivered between the caller's sync() call and the install resolves Dominated"
  );
  assert_eq!(
    h.owner.source.ended_syncs,
    vec![key("/a/cookie-1")],
    "the dominated barrier's cookie is reaped"
  );
}

/// The CALL-to-INSTALL window: a loss the owner processes AFTER the caller's `sync()` enqueued its
/// request but BEFORE the owner dispatches it resolves the barrier `Dominated`.
///
/// This is the window neither install-time probe can see, and it is why the barrier's loss window is
/// anchored at the caller's call (the shared `loss_gen` snapshot [`super::Tributaries::sync`] takes
/// before it sends) rather than at the install. The loss here parks, publishes and clears entirely
/// inside the window, so by the time `on_sync` runs, `needs_rescan`/`suppressed_rescan` are empty
/// again (no standing debt to snapshot) AND the per-subscription loss serial has already advanced
/// (so the install-to-resolve `lost_during_window` comparison sees no change). Yet the lost change
/// is PRE-CALL — its kernel event predates the caller's `sync()` — so it must never be reported as a
/// clean `Delivered`.
///
/// Fail-on-old (no `loss_gen` term in `dominated_at_install`): both install-time probes read a
/// pristine state and the cookie's flush is clean, so the barrier reports `Delivered` — the bug.
#[tokio::test]
async fn a_loss_between_the_call_and_the_install_resolves_dominated() {
  use core::sync::atomic::Ordering;

  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  // The caller snapshots the shared loss generation exactly as `Tributaries::sync` does — BEFORE the
  // request is enqueued — and the owner has not dispatched it yet.
  let loss_gen_at_call = h.owner.loss_gen.load(Ordering::SeqCst);

  // The owner now processes a PRE-CALL loss while the request sits in the mailbox: the sub sheds to
  // a parked Rescan, which then publishes and clears. Both install-time probes are now blind to it.
  h.owner.park_rescan(sub);
  h.owner.flush_pending_rescans();
  assert!(
    !h.owner.needs_rescan.contains_key(&sub) && !h.owner.suppressed_rescan.contains_key(&sub),
    "the loss published and cleared before the barrier is installed — no standing debt to snapshot"
  );
  assert_ne!(
    h.owner.loss_serial.get(&sub).copied().unwrap_or(0),
    0,
    "the loss serial ALREADY advanced, so the install-to-resolve comparison cannot see this loss"
  );

  // Install with the caller's STALE snapshot — exactly what the queued `SyncRequest` carries.
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.on_sync(sub, loss_gen_at_call, reply_tx).await;

  // The cookie arrives over a state that looks pristine. Only the call-time generation still knows.
  h.owner
    .consume_source_event(&source_created(handle, "/a/cookie-1", 20));
  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "a loss processed between the caller's sync() call and the barrier's install resolves \
     Dominated, never a false Delivered"
  );
}

/// A parked Rescan PUBLISHED in the call-to-install window resolves the barrier `Dominated`, not a
/// false `Delivered`. The caller snapshots the shared generation AFTER a pre-call loss parked (so the
/// park's bump is already folded into the snapshot); the owner then publishes-and-clears that parked
/// Rescan while the request sits in the mailbox. Publishing a parked re-enumeration is a DELIVERED
/// covering Rescan, so it must dominate a barrier whose `sync()` preceded it — the publish half of the
/// same invariant `note_domination` enforces at the delivered-Rescan choke point.
///
/// Fail-on-old (the publish did not advance the generation): by install the debt map is empty, the
/// per-sub loss serial was snapshotted after the park's bump (so it never appears to move), and the
/// generation still equals the caller's snapshot — every probe reads a pristine state and the cookie's
/// flush is clean, so the barrier reports `Delivered` for a pre-call re-enumeration the caller must
/// still process.
#[tokio::test]
async fn a_parked_rescan_published_in_the_call_window_resolves_dominated() {
  use core::sync::atomic::Ordering;

  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  // A pre-call loss parks debt for the sub — its kernel event predates the caller's `sync()`.
  h.owner.park_rescan(sub);
  assert!(
    h.owner.needs_rescan.contains_key(&sub),
    "the pre-call loss parked debt"
  );

  // The caller snapshots the shared generation AFTER the park — so the park's bump is already folded
  // in, exactly as `Tributaries::sync` sees it for a loss that happened before the call.
  let loss_gen_at_call = h.owner.loss_gen.load(Ordering::SeqCst);

  // The owner publishes-and-clears the parked Rescan while the request still sits in the mailbox (the
  // call-to-install window). Without the publish-half domination this advances NEITHER the generation
  // the caller snapshotted NOR the per-sub serial (snapshotted after the park's bump), so both
  // install-time probes go blind.
  h.owner.flush_pending_rescans();
  assert!(
    !h.owner.needs_rescan.contains_key(&sub),
    "the parked Rescan published and cleared before the barrier installs"
  );

  // Install with the caller's stale snapshot, then deliver the cookie over the now-pristine state.
  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.on_sync(sub, loss_gen_at_call, reply_tx).await;
  h.owner
    .consume_source_event(&source_created(handle, "/a/cookie-1", 20));

  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "a parked Rescan published in the call-to-install window resolves Dominated, not a false Delivered"
  );
}

/// An installed barrier is dominated by a DESCENDANT Rescan. A barrier for `/r` (cookie `/r/.cookie`)
/// that receives a Rescan at `/r/x` (a descendant coverage loss) must resolve `Dominated`: a raw
/// source Rescan fans out to every subscriber of its root, so the `/r` subscriber owes a
/// re-enumeration the Rescan stands in for — regardless of where its cookie sits.
///
/// Fail-on-old (domination keyed on cookie-path ancestry, `cookie_key.starts_with(event.key())`):
/// `/r/.cookie` does not start with `/r/x`, so the barrier is not dominated and falsely resolves
/// `Delivered` (here the reply is simply left unresolved).
#[tokio::test]
async fn an_installed_barrier_is_dominated_by_a_descendant_rescan() {
  use futures_util::FutureExt;

  use crate::source::SyncOutcome;

  let mut h = Harness::new();
  let sub = h.watch("/r", Interest::all()).await.expect("watch /r");
  let handle = h.owner.subsumer.subscription_root(sub).expect("live root");

  let (reply_tx, mut reply_rx) = futures_channel::oneshot::channel();
  h.owner.pending_syncs.push(super::PendingSync {
    cookie_key: key("/r/.cookie"),
    sub,
    root: handle,
    loss_serial_at_install: 0,
    dominated_at_install: false,
    reply: reply_tx,
  });

  // A DESCENDANT loss: a Rescan at /r/x re-enumerates a subtree the /r subscriber owns, but its
  // cookie /r/.cookie does not start with /r/x — the old cookie-prefix rule missed exactly this.
  h.owner
    .consume_source_event(&rescan_event(handle, "/r/x", 5));

  assert!(
    matches!(
      (&mut reply_rx).now_or_never(),
      Some(Ok(Ok(SyncOutcome::Dominated)))
    ),
    "a descendant-loss Rescan dominates the /r barrier by subscription, not by cookie ancestry"
  );
}

/// A stale transient-root Rescan left on the preserved handle by a diverging-then-rolled-back in-place
/// widen is CLAMPED to the live root, so it never widens a parked debt to a common ancestor. The
/// diverging `replace(/a → /z)` and the rollback `replace(→ /a/b)` each commit a full-root Rescan on
/// the one preserved handle (the fake now models the real `FsSource`); the stale `Rescan(/z)` rides a
/// handle whose CURRENT root is the rolled-back `/a/b`, disjoint from `/z`.
///
/// Fail-on-old (no clamp): pumping the stale `Rescan(/z)` merges its `/z` key into the rollback's
/// parked `/a/b` debt at their common ancestor (the root `/`), over-owing a re-enumeration of the
/// whole tree. With the clamp the stale key is rewritten to the live root, so the debt stays `/a/b`.
#[tokio::test]
async fn a_transient_root_rescan_after_a_widen_rollback_is_clamped_to_the_live_root() {
  let mut h = Harness::new();
  h.owner.source.supports_replace = true;

  let s_narrow = h.watch("/a/b", Interest::all()).await.expect("watch /a/b");
  let handle = h
    .owner
    .subsumer
    .subscription_root(s_narrow)
    .expect("live root");

  // The widen of /a canonicalizes to the DIVERGENT /z (which does not contain /a/b), so the widen
  // rolls back to /a/b on the preserved handle. Each `replace` enqueues one full-root Rescan on the
  // handle's stream: Rescan(/z) from the divergent retarget, then Rescan(/a/b) from the rollback.
  h.owner
    .source
    .retarget
    .insert(PathBuf::from("/a"), PathBuf::from("/z"));
  let err = h
    .watch("/a", Interest::all())
    .await
    .expect_err("the diverging widen is refused");
  assert!(matches!(err, WatchError::CanonicalRace), "{err:?}");

  // The rollback already parked s_narrow's dominating Rescan at its exact /a/b coverage, and the
  // preserved handle's CURRENT root_key is the rolled-back /a/b.
  assert_eq!(
    h.owner.needs_rescan.get(&s_narrow).map(|p| p.key.clone()),
    Some(key("/a/b")),
    "the rollback owes s_narrow a Rescan at its exact /a/b coverage"
  );
  assert_eq!(h.owner.source.root_key(handle), Some(key("/a/b")));

  // Pump the replace-emitted stream: the stale transient-root Rescan(/z) then the rollback's
  // Rescan(/a/b), both riding the live handle.
  let mut pumped = Vec::new();
  while let Some(ev) = h.owner.source.next().await {
    pumped.push(ev);
  }
  assert_eq!(
    pumped.len(),
    2,
    "each replace enqueued one full-root Rescan: {pumped:?}"
  );
  for ev in &pumped {
    h.owner.consume_source_event(ev);
  }

  // The owed re-enumeration for s_narrow stays at /a/b: the clamp rewrote the stale disjoint /z to
  // the live root, so merging it never widened the debt to a common ancestor (`/`).
  assert_eq!(
    h.owner.needs_rescan.get(&s_narrow).map(|p| p.key.clone()),
    Some(key("/a/b")),
    "the stale /z Rescan was clamped to the live root; the debt never widened to a common ancestor"
  );
}

/// `on_sync` skips an already-canceled reply. If the caller's `sync()` deadline fires during
/// admission — dropping the response receiver — the owner must not mint a token, await `begin_sync`,
/// or park a PendingSync for a reply nobody will read.
#[tokio::test]
async fn on_sync_skips_an_already_canceled_barrier() {
  use crate::{error::SyncError, source::SyncOutcome};

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");

  // The caller already timed out during admission: its response receiver is dropped, so the reply
  // sender reports canceled before the owner does any cookie work.
  let (reply_tx, reply_rx) = futures_channel::oneshot::channel::<Result<SyncOutcome, SyncError>>();
  drop(reply_rx);
  assert!(
    reply_tx.is_canceled(),
    "the reply is canceled before on_sync runs"
  );

  h.owner.on_sync(sub, 0, reply_tx).await;

  assert!(
    h.owner.pending_syncs.is_empty(),
    "no PendingSync is parked for an already-canceled reply"
  );
  // `on_sync` increments `sync_seq` before minting the token / calling `begin_sync`, so an unmoved
  // seq proves the skip precedes any cookie work.
  assert_eq!(
    h.owner.sync_seq, 0,
    "no token was minted — the skip precedes begin_sync"
  );
  assert!(
    h.owner.source.ended_syncs.is_empty(),
    "no cookie was written, so none is reaped"
  );
}

/// Finding 1, the inter-arm race forced deterministically: the fs write COMPLETES (delivers its
/// cookie) between the `select_biased!` pass that polls `begin_sync` and that same pass's
/// cancellation arm. A scripted `begin_sync` future side-effect-delivers the cookie key yet still
/// returns `Pending`, so the now-ready cancellation wins the SAME pass — the exact "the fs sent Ok
/// between select arm 1 and arm 3 of one pass" interleave. The owner never learned the cookie's
/// path (only a completed `begin_sync` returns it), so the ONLY thing that can free the physical
/// cookie is `cancel_sync(token)` on the abandon arm.
///
/// Fail-on-old (drop the `cancel_sync` on the cancellation arm): `cancelled_syncs` is empty and the
/// delivered cookie is orphaned — the leak the whole token-cancel handshake exists to close.
#[tokio::test]
async fn a_write_completing_between_its_poll_and_the_cancel_poll_is_cancelled_by_token() {
  use core::sync::atomic::Ordering;
  use std::task::{Context, Poll, Waker};

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  h.owner.source.sync_script =
    VecDeque::from([ScriptStep::Pending, ScriptStep::PendingThenComplete]);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");

  let loss_gen = h.owner.loss_gen.load(Ordering::SeqCst);
  let (reply_tx, reply_rx) = futures_channel::oneshot::channel();
  let mut cx = Context::from_waker(Waker::noop());
  let mut fut = Box::pin(h.owner.on_sync(sub, loss_gen, reply_tx));

  // Pass #1: `begin_sync`'s first scripted step is `Pending`, and the caller is still waiting
  // (its receiver is alive) so the cancellation arm is pending too — the whole pass parks. This
  // models R7-1's healthy ordering: nothing is ready, so nothing is dropped.
  assert!(
    fut.as_mut().poll(&mut cx).is_pending(),
    "the write is in flight and the caller still waits — the pass parks"
  );

  // The caller's deadline fires: its response receiver drops. On the NEXT pass the write's
  // scripted step delivers the cookie (side effect) yet returns `Pending`, and the now-ready
  // cancellation wins the SAME pass; the write future — holding a ready, unread delivery — is
  // dropped with the `select`.
  drop(reply_rx);
  let admit = fut.as_mut().poll(&mut cx);
  assert!(
    matches!(admit, Poll::Ready(super::SyncAdmit::Done)),
    "the cancellation arm resolves the abandoned barrier in the same pass"
  );
  drop(fut);

  let token = h
    .owner
    .source
    .begun_token
    .expect("begin_sync minted the token");
  assert_eq!(
    h.owner.source.fs_delivered,
    vec![key("/a/cookie-1")],
    "the write DID complete and deliver its cookie between the two arm polls"
  );
  assert!(
    h.owner.source.ended_syncs.is_empty(),
    "no path-precise reap was possible — the owner never learned the cookie key"
  );
  assert_eq!(
    h.owner.source.cancelled_syncs,
    vec![token],
    "the token cancel is the ONLY thing that frees the delivered-but-unread cookie"
  );
  assert!(
    h.owner.pending_syncs.is_empty(),
    "the abandoned barrier parked nothing (leak oracle)"
  );
}

/// The close-arm companion to the inter-arm race: same scripted delivery, but a CLOSE arrives
/// while the write is in flight instead of the caller timing out. On the pass where the write
/// side-effect-delivers its cookie, the close arm — ranked ABOVE cancellation — wins, threading its
/// reply back for teardown AND token-cancelling the delivered-but-unread cookie.
///
/// Fail-on-old (drop the `cancel_sync` on the close arm): `cancelled_syncs` is empty and the
/// delivered cookie is orphaned across the close.
#[tokio::test]
async fn a_write_completing_between_its_poll_and_the_cancel_poll_is_cancelled_by_token_close_arm() {
  use core::sync::atomic::Ordering;
  use std::task::{Context, Poll, Waker};

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  h.owner.source.sync_script =
    VecDeque::from([ScriptStep::Pending, ScriptStep::PendingThenComplete]);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");

  let loss_gen = h.owner.loss_gen.load(Ordering::SeqCst);
  // The caller stays alive (its receiver is held), so only the close can win the race.
  let (reply_tx, _reply_rx) = futures_channel::oneshot::channel();
  let mut cx = Context::from_waker(Waker::noop());
  let mut fut = Box::pin(h.owner.on_sync(sub, loss_gen, reply_tx));

  assert!(
    fut.as_mut().poll(&mut cx).is_pending(),
    "the write is in flight and no close is queued — the pass parks"
  );

  // A close is requested on the dedicated signal while the write is in flight. On the next pass
  // the write delivers its cookie (side effect) yet returns `Pending`, and the close arm wins the
  // SAME pass.
  let (close_reply, _close_resp) =
    futures_channel::oneshot::channel::<Result<(), super::CloseError>>();
  h.closes
    .try_send(close_reply)
    .expect("queue the close on the dedicated signal");

  let admit = fut.as_mut().poll(&mut cx);
  assert!(
    matches!(admit, Poll::Ready(super::SyncAdmit::CloseRequested(_))),
    "the close arm consumes its reply and abandons the in-flight write"
  );
  drop(fut);

  let token = h
    .owner
    .source
    .begun_token
    .expect("begin_sync minted the token");
  assert_eq!(
    h.owner.source.fs_delivered,
    vec![key("/a/cookie-1")],
    "the write delivered its cookie between the two arm polls"
  );
  assert!(
    h.owner.source.ended_syncs.is_empty(),
    "the owner never learned the cookie key — no path-precise reap"
  );
  assert_eq!(
    h.owner.source.cancelled_syncs,
    vec![token],
    "the close abandon token-cancels the delivered-but-unread cookie"
  );
  assert!(
    h.owner.pending_syncs.is_empty(),
    "the abandoned barrier parked nothing (leak oracle)"
  );
}

/// The guard that keeps the token cancel from firing spuriously: when the write is READY on the
/// pass (not merely side-effect-delivering), the write-first bias makes it win the tie over a
/// simultaneously-ready cancellation — R7-1's ordering — and a completed cookie for a gone caller
/// is reaped by PATH (`end_sync`), never token-cancelled. This proves the cancel is confined to the
/// abandon arms, not the admit path.
#[tokio::test]
async fn a_ready_write_still_wins_the_tie_and_is_not_token_cancelled() {
  use core::sync::atomic::Ordering;
  use std::task::{Context, Poll, Waker};

  let mut h = Harness::new();
  h.owner.source.supports_sync = true;
  h.owner.source.sync_script = VecDeque::from([ScriptStep::Pending, ScriptStep::Ready]);
  let sub = h.watch("/a", Interest::all()).await.expect("watch /a");

  let loss_gen = h.owner.loss_gen.load(Ordering::SeqCst);
  let (reply_tx, reply_rx) = futures_channel::oneshot::channel();
  let mut cx = Context::from_waker(Waker::noop());
  let mut fut = Box::pin(h.owner.on_sync(sub, loss_gen, reply_tx));

  assert!(
    fut.as_mut().poll(&mut cx).is_pending(),
    "the write is in flight; the caller still waits — the pass parks"
  );

  // The caller's deadline fires — but on the next pass the write is READY, and the write-first
  // bias makes it win the tie over the simultaneously-ready cancellation. A completed cookie for a
  // gone caller is reaped by PATH, never token-cancelled.
  drop(reply_rx);
  let admit = fut.as_mut().poll(&mut cx);
  assert!(
    matches!(admit, Poll::Ready(super::SyncAdmit::Done)),
    "the ready write wins the tie and admits"
  );
  drop(fut);

  assert_eq!(
    h.owner.source.ended_syncs,
    vec![key("/a/cookie-1")],
    "the completed cookie is reaped by PATH — the R7-1 write-first outcome"
  );
  assert!(
    h.owner.source.cancelled_syncs.is_empty(),
    "a completed write is NOT token-cancelled — the cancel fires only on abandon"
  );
  assert!(
    h.owner.source.fs_delivered.is_empty(),
    "the Ready step resolves the write; it does not side-effect-deliver"
  );
  assert!(
    h.owner.pending_syncs.is_empty(),
    "the gone caller's cookie is reaped inline, not parked (leak oracle)"
  );
}

/// A source for the END-TO-END sync tests: arms roots live, offers the barrier
/// ([`Source::begin_sync`]) returning a deterministic `<dir>/cookie-<seq>` key, and — when `observe`
/// is set — queues that cookie's own artifact event so the driver's funnel matches and resolves the
/// barrier `Delivered`. With `observe` cleared the cookie is never reported, so the barrier can only
/// resolve via the caller's `R::timeout`. `next` parks when its queue is empty, keeping the run loop
/// alive to answer `close`/further requests.
struct SyncSource {
  next_handle: u32,
  live: HashMap<u32, Vec<OsString>>,
  pending: VecDeque<SourceEvent<OsString, u32>>,
  observe: bool,
}

impl Source<OsString> for SyncSource {
  type Handle = u32;

  fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
    Ok(key.to_vec())
  }

  async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
    self.next_handle += 1;
    let handle = self.next_handle;
    self.live.insert(handle, key.to_vec());
    Ok(Armed::new(handle, key.to_vec()))
  }

  fn disarm(&mut self, handle: u32) {
    self.live.remove(&handle);
  }

  fn is_sync_artifact(&self, key: &[OsString]) -> bool {
    key
      .last()
      .and_then(|leaf| leaf.to_str())
      .is_some_and(|leaf| leaf.starts_with("cookie-"))
  }

  async fn begin_sync(
    &mut self,
    handle: u32,
    dir_key: &[OsString],
    token: SyncToken,
  ) -> Result<Vec<OsString>, crate::error::SyncError> {
    let mut cookie_key = dir_key.to_vec();
    cookie_key.push(OsString::from(format!("cookie-{}", token.seq())));
    if self.observe {
      // Report the cookie's own create so the funnel matches it and resolves the barrier.
      self.pending.push_back(SourceEvent::new(
        handle,
        cookie_key.clone(),
        EventKind::Created,
        Location::new(),
        Epoch::new(1),
        Some(ChangeId::new(NonZeroU64::MIN)),
      ));
    }
    Ok(cookie_key)
  }

  async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
    if let Some(event) = self.pending.pop_front() {
      Some(event)
    } else {
      // Park so the run loop survives to answer `close` and later requests.
      std::future::pending::<Option<SourceEvent<OsString, u32>>>().await
    }
  }

  fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
    self.live.get(&handle).cloned()
  }
}

/// F4 — a sync admitted through the DEDICATED mailbox (no longer the key/value-bearing command
/// mailbox) still runs end to end: [`Tributaries::sync`](super::Tributaries::sync) sends a
/// `SyncRequest`, the run loop's sync arm dispatches `on_sync`, the cookie's own event arrives, and
/// the barrier resolves `Delivered` on a clean flush — proving the rewired admission+observation path.
#[tokio::test]
async fn a_sync_admitted_through_the_dedicated_mailbox_still_resolves() {
  use crate::source::SyncOutcome;

  let source = SyncSource {
    next_handle: 0,
    live: HashMap::new(),
    pending: VecDeque::new(),
    observe: true,
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());
  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a");

  let outcome = tokio::time::timeout(Duration::from_secs(5), w.sync(sub, Duration::from_secs(5)))
    .await
    .expect("the sync resolves within the test deadline")
    .expect("the sync succeeds");
  assert!(
    matches!(outcome, SyncOutcome::Delivered),
    "a clean end-to-end sync via the dedicated mailbox resolves Delivered: {outcome:?}"
  );
}

/// F4 — the caller's deadline bounds the barrier: a sync whose cookie is never observed resolves
/// `Err(Timeout)` (the `R::timeout` wrapping admission-plus-observation fires), never a hang.
#[tokio::test]
async fn a_sync_times_out_when_never_observed() {
  use crate::error::SyncError;

  let source = SyncSource {
    next_handle: 0,
    live: HashMap::new(),
    pending: VecDeque::new(),
    observe: false,
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());
  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a");

  let result = tokio::time::timeout(
    Duration::from_secs(5),
    w.sync(sub, Duration::from_millis(100)),
  )
  .await
  .expect("the outer test deadline is not hit — the inner sync timeout fires first");
  assert!(
    matches!(result, Err(SyncError::Timeout)),
    "a barrier whose cookie is never observed times out: {result:?}"
  );
}

/// A SATURATING control-plane flood: it keeps the command mailbox continuously non-empty by
/// REFILLING the slot the owner just drained, and stops only once the channel closes.
///
/// Each command is an `Unwatch` of an ALREADY-RETIRED subscription: the owner answers it
/// `UnknownSubscription` without arming, disarming, or minting any state, so the flood is pure
/// control-plane pressure that allocates nothing however long it runs. (The shared
/// [`spawn_command_flood`] gives up on the first full mailbox, so it cannot hold a *prefilled* one
/// saturated — which is precisely the condition under test here.)
fn spawn_saturating_command_flood(
  commands: async_channel::Sender<super::Command<OsString, ()>>,
  retired: Subscription,
) -> tokio::task::JoinHandle<()> {
  tokio::spawn(async move {
    loop {
      let (reply, response) = futures_channel::oneshot::channel();
      drop(response);
      match commands.try_send(super::Command::Unwatch {
        sub: retired,
        reply,
      }) {
        Ok(()) => {}
        // A FULL mailbox is the goal, not a stop condition: yield, then refill the slot the owner
        // took, so the biased select never observes the mailbox empty.
        Err(async_channel::TrySendError::Full(_)) => tokio::task::yield_now().await,
        // The owner is gone.
        Err(async_channel::TrySendError::Closed(_)) => return,
      }
    }
  })
}

/// Ordinary command traffic must not starve the sync mailbox. The run loop's `select!` is biased and
/// polls `commands.recv()` ahead of the sync arm, so a CONTINUOUSLY-ready command mailbox means the
/// sync arm never wins: an admitted `SyncRequest` would sit until its caller's deadline expired even
/// though the owner is perfectly healthy. (The command-fairness valve does not help — it forces one
/// SOURCE service, then returns to the same ordering.) The loop-top take-at-most-one sync drain gives
/// the two control mailboxes 1:1 service, so the barrier resolves under any flood.
///
/// Run against the REAL `run` loop: the mailbox is prefilled to its full configured depth AND held
/// saturated by a refilling flood for the whole barrier.
///
/// Fail-on-old (no loop-top sync drain): the sync arm never wins against the saturated mailbox, so
/// the barrier starves and the caller's `R::timeout` fires — `Err(Timeout)`.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_command_flood_does_not_starve_the_sync_mailbox() {
  use crate::source::SyncOutcome;

  let source = SyncSource {
    next_handle: 0,
    live: HashMap::new(),
    pending: VecDeque::new(),
    observe: true,
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> = super::Tributaries::with_source(
    source,
    // A deep BOUNDED mailbox, so the prefill below is a genuinely deep backlog the flood can hold
    // saturated — the shape a starved sync arm would never get out from under.
    TributariesOptions::new().with_command_capacity(std::num::NonZeroUsize::new(500).unwrap()),
  );
  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a");

  // A retired subscription for the flood to re-unwatch: answered `UnknownSubscription`, so every
  // flood command is pure control-plane pressure that mints no state.
  let retired = w
    .watch(key("/doomed"), (), WatchOptions::new())
    .await
    .expect("watch /doomed");
  w.unwatch(retired).await.expect("retire /doomed");

  // Saturate the mailbox to its full depth BEFORE the sync is issued, then keep it saturated: the
  // command arm is ready at every single poll of the biased select from here on.
  for _ in 0..500 {
    let (reply, response) = futures_channel::oneshot::channel();
    drop(response);
    w.commands
      .try_send(super::Command::Unwatch {
        sub: retired,
        reply,
      })
      .expect("prefill the command backlog");
  }
  let flood = spawn_saturating_command_flood(w.commands.clone(), retired);

  // The barrier is served by the loop-top fair drain, never by the starved sync arm.
  let outcome = tokio::time::timeout(Duration::from_secs(10), w.sync(sub, Duration::from_secs(5)))
    .await
    .expect("the outer test deadline is not hit — the sync resolves or its own timeout fires");
  flood.abort();
  let outcome = outcome.expect("the sync resolves rather than starving behind the command flood");
  assert!(
    matches!(outcome, SyncOutcome::Delivered),
    "the barrier resolves cleanly under the flood — no loss occurred, so the deliberately GLOBAL \
     loss generation must not fire spuriously on unrelated control-plane traffic: {outcome:?}"
  );
}

/// A source for the HELD-write tests: it arms roots live and offers the barrier
/// ([`Source::begin_sync`]), but `begin_sync` first parks on a gate the test holds shut — modelling a
/// hung backend write (a stuck FUSE/NFS mount) that never returns. `next` parks when idle, keeping the
/// run loop alive so the race's cancellation and close arms can free the owner.
struct HeldBeginSyncSource {
  next_handle: u32,
  live: HashMap<u32, Vec<OsString>>,
  /// `begin_sync` awaits one token here before returning the cookie. The tests never send (and keep
  /// the paired sender alive so the channel stays open rather than erroring), so the write stays held
  /// for the whole test — the only thing that frees the owner is the race, never the write completing.
  begin_gate: async_channel::Receiver<()>,
}

impl Source<OsString> for HeldBeginSyncSource {
  type Handle = u32;

  fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
    Ok(key.to_vec())
  }

  async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
    self.next_handle += 1;
    let handle = self.next_handle;
    self.live.insert(handle, key.to_vec());
    Ok(Armed::new(handle, key.to_vec()))
  }

  fn disarm(&mut self, handle: u32) {
    self.live.remove(&handle);
  }

  fn is_sync_artifact(&self, key: &[OsString]) -> bool {
    key
      .last()
      .and_then(|leaf| leaf.to_str())
      .is_some_and(|leaf| leaf.starts_with("cookie-"))
  }

  async fn begin_sync(
    &mut self,
    _handle: u32,
    dir_key: &[OsString],
    token: SyncToken,
  ) -> Result<Vec<OsString>, crate::error::SyncError> {
    // Park on the test's gate: the write is held until the test releases it (it never does), so the
    // owner would wedge here forever under an inline `begin_sync().await`.
    let _ = self.begin_gate.recv().await;
    let mut cookie_key = dir_key.to_vec();
    cookie_key.push(OsString::from(format!("cookie-{}", token.seq())));
    Ok(cookie_key)
  }

  async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
    // Park so the run loop survives to service the race and later commands.
    std::future::pending::<Option<SourceEvent<OsString, u32>>>().await
  }

  fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
    self.live.get(&handle).cloned()
  }
}

/// A held backend write must not wedge the owner. When the caller's `sync()` deadline fires and drops
/// its response, the race's cancellation arm frees the owner within the caller's own timeout, so the
/// owner resumes servicing its mailbox — a follow-up command resolves promptly.
///
/// Fail-on-old (the inline `begin_sync().await`): after the caller is gone the owner stays parked in
/// the held write forever, so the follow-up watch never resolves and this test hangs to its bound.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_timed_out_sync_frees_the_owner_when_the_caller_drops() {
  use crate::error::SyncError;

  // The gate the test holds shut: `begin_sync` parks on it forever, modelling a hung backend write.
  let (_begin_tx, begin_rx) = async_channel::unbounded::<()>();
  let source = HeldBeginSyncSource {
    next_handle: 0,
    live: HashMap::new(),
    begin_gate: begin_rx,
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());
  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a");

  // The owner dispatches this sync and parks in the held write's race. The caller's deadline fires,
  // dropping the response — which (only with the race) fires the owner's cancellation arm and frees
  // it. A generous outer bound proves the INNER sync timeout is what returns, not a hang.
  let result = tokio::time::timeout(
    Duration::from_secs(10),
    w.sync(sub, Duration::from_millis(300)),
  )
  .await
  .expect("the outer bound is not hit — the inner sync timeout returns");
  assert!(
    matches!(result, Err(SyncError::Timeout)),
    "the held sync times out: {result:?}"
  );

  // The owner must not be wedged in the abandoned write: a subsequent command is serviced promptly.
  // Under the old inline await this watch never resolves (the owner is still parked in the held
  // `begin_sync`), so the bound below fires and the test fails.
  tokio::time::timeout(
    Duration::from_secs(5),
    w.watch(key("/b"), (), WatchOptions::new()),
  )
  .await
  .expect(
    "the owner services a command after the timed-out sync — it is not wedged in the held write",
  )
  .expect("the follow-up watch succeeds");
  assert!(
    w.view().is_watched(&key("/b")),
    "the follow-up watch committed — the owner resumed servicing the mailbox after freeing itself"
  );
}

/// A close during a held write tears down at once — it does not wait the write out. The close arm in
/// `on_sync`'s race outranks the parked `begin_sync`, so a `close` on the dedicated signal wins and
/// rides back to drive teardown, and the abandoned sync's caller then sees the owner gone.
///
/// This is the close-race form's guarantee: with a cancellation-only race, this close would instead
/// block behind the held write until the sync's own (long-lived) caller went away.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_close_during_a_held_sync_tears_down_promptly() {
  use crate::error::SyncError;

  let (_begin_tx, begin_rx) = async_channel::unbounded::<()>();
  let source = HeldBeginSyncSource {
    next_handle: 0,
    live: HashMap::new(),
    begin_gate: begin_rx,
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());
  let sub = w
    .watch(key("/a"), (), WatchOptions::new())
    .await
    .expect("watch /a");

  // A sync with a LONG deadline on a background task, so it never self-cancels: the owner parks in the
  // held write, and the ONLY thing that can free it is the close under test.
  let syncer = {
    let w = w.clone();
    tokio::spawn(async move { w.sync(sub, Duration::from_secs(30)).await })
  };
  // Let the owner dispatch the sync and park in the held `begin_sync` before the close arrives.
  tokio::time::sleep(Duration::from_millis(200)).await;

  // Close must resolve WITHOUT the gate ever opening: the close arm outranks the parked write.
  tokio::time::timeout(Duration::from_secs(5), w.close())
    .await
    .expect("close tears down promptly while the write is held — it does not wait the write out")
    .expect("close acknowledges");

  // The abandoned held sync resolves once the owner tore down: on_sync returned its close verdict
  // without ever sending on the sync's reply, so the dropped reply surfaces `Closed` to the caller.
  let sync_result = tokio::time::timeout(Duration::from_secs(5), syncer)
    .await
    .expect("the held sync resolves once the owner tore down")
    .expect("sync task");
  assert!(
    matches!(sync_result, Err(SyncError::Closed)),
    "the abandoned held sync resolves Closed after the close-driven teardown: {sync_result:?}"
  );
}

/// A source for the HELD-RETARGET tests: it arms roots live and offers the in-place widen
/// ([`Source::replace`]), but `replace` first parks on a gate the test holds shut — modelling a hung
/// backend retarget (a stuck FUSE/NFS mount) that never returns. `next` parks when idle, keeping the
/// run loop alive so the race's close arm can free the owner.
struct HeldReplaceSource {
  next_handle: u32,
  live: HashMap<u32, Vec<OsString>>,
  /// `replace` awaits one token here before returning. The tests never send (and keep the paired
  /// sender alive so the channel stays open rather than erroring), so the retarget stays held for the
  /// whole test — the only thing that frees the owner is the race, never the retarget completing.
  replace_gate: async_channel::Receiver<()>,
  /// When `Some`, the FIRST `replace` skips the gate and commits at this key instead. Pointed
  /// somewhere that contains NO subsumed root, it is the canonicalization race that drives the owner
  /// into the widen's ROLLBACK retarget — so the SECOND `replace`, the rollback, is the held one.
  first_replace_diverges_to: Option<Vec<OsString>>,
  /// `replace` calls so far, so the divergent form above can hold the rollback rather than the widen.
  replace_calls: u32,
}

impl Source<OsString> for HeldReplaceSource {
  type Handle = u32;

  fn canonicalize_key(&self, key: &[OsString]) -> Result<Vec<OsString>, WatchError> {
    Ok(key.to_vec())
  }

  async fn arm(&mut self, key: &[OsString]) -> Result<Armed<OsString, u32>, WatchError> {
    self.next_handle += 1;
    let handle = self.next_handle;
    self.live.insert(handle, key.to_vec());
    Ok(Armed::new(handle, key.to_vec()))
  }

  fn disarm(&mut self, handle: u32) {
    self.live.remove(&handle);
  }

  async fn replace(
    &mut self,
    handle: u32,
    new_key: &[OsString],
  ) -> Result<Armed<OsString, u32>, WatchError> {
    self.replace_calls += 1;
    // The divergent form: commit the widen's retarget at a key the plan does not preserve, so the
    // owner rolls back — and the rollback below is what parks on the gate.
    if self.replace_calls == 1
      && let Some(divergent) = self.first_replace_diverges_to.clone()
    {
      self.live.insert(handle, divergent.clone());
      return Ok(Armed::new(handle, divergent));
    }
    // Park on the test's gate: the retarget is held until the test releases it (it never does), so
    // the owner would wedge here forever under an inline `replace().await`.
    let _ = self.replace_gate.recv().await;
    self.live.insert(handle, new_key.to_vec());
    Ok(Armed::new(handle, new_key.to_vec()))
  }

  async fn next(&mut self) -> Option<SourceEvent<OsString, u32>> {
    // Park so the run loop survives to service the race and later commands.
    std::future::pending::<Option<SourceEvent<OsString, u32>>>().await
  }

  fn root_key(&self, handle: u32) -> Option<Vec<OsString>> {
    self.live.get(&handle).cloned()
  }
}

/// Drives a widen that takes the IN-PLACE [`Source::replace`] path against a source whose retarget is
/// held forever, then closes: shared by the two held-retarget tests, which differ only in WHICH of the
/// widen's two `replace` awaits is the held one. Asserts the finding's acceptance both ways — `close()`
/// resolves without the gate ever opening, and the abandoned widen's `watch()` sees `Closed`.
async fn assert_a_held_retarget_does_not_wedge_close(
  first_replace_diverges_to: Option<Vec<OsString>>,
) {
  // The gate the test holds shut: `replace` parks on it forever, modelling a hung backend retarget.
  let (_replace_tx, replace_rx) = async_channel::unbounded::<()>();
  let source = HeldReplaceSource {
    next_handle: 0,
    live: HashMap::new(),
    replace_gate: replace_rx,
    first_replace_diverges_to,
    replace_calls: 0,
  };
  let w: super::Tributaries<OsString, (), TokioRuntime, u32> =
    super::Tributaries::with_source(source, TributariesOptions::new());
  // The SOLE root the widen below subsumes — which is what sends it down the in-place retarget path
  // (`unwatch.as_slice()` matching `[only]`) rather than release-and-rearm.
  w.watch(key("/a/b"), (), WatchOptions::new())
    .await
    .expect("watch /a/b");

  // The widen on a background task, so it never goes away on its own: the owner parks in the held
  // retarget, and the ONLY thing that can free it is the close under test. (A `watch` caller has no
  // deadline to self-cancel on — and could not interrupt the owner if it did, which is exactly why
  // the race is close-only.)
  let widener = {
    let w = w.clone();
    tokio::spawn(async move { w.watch(key("/a"), (), WatchOptions::new()).await })
  };
  // Let the owner dispatch the widen and park in the held `replace` before the close arrives.
  tokio::time::sleep(Duration::from_millis(200)).await;

  // Close must resolve WITHOUT the gate ever opening: the close arm outranks the parked retarget.
  tokio::time::timeout(Duration::from_secs(5), w.close())
    .await
    .expect("close tears down promptly while the retarget is held — it does not wait it out")
    .expect("close acknowledges");

  // The abandoned widen resolves once the owner tore down: the reconcile returned its close verdict
  // without ever sending on the watch's reply, so the dropped reply surfaces `Closed` to the caller.
  let watch_result = tokio::time::timeout(Duration::from_secs(5), widener)
    .await
    .expect("the held widen resolves once the owner tore down")
    .expect("widen task");
  assert!(
    matches!(watch_result, Err(WatchError::Closed)),
    "the abandoned held widen resolves Closed after the close-driven teardown: {watch_result:?}"
  );
}

/// A held in-place widen RETARGET must not wedge the owner. The owner parks in the held `replace`
/// mid-reconcile; the close on the dedicated signal wins that race, rides back through
/// `dispatch_command` as the same `Flow::Break` the run loop's own close arm produces, and tears down
/// — so `close()`'s bounded-latency contract holds even against a mount that never answers.
///
/// Fail-on-old (the inline `self.source.replace(only, key).await`): the owner stays parked in the held
/// retarget forever, so the run loop never polls `closes` again and `close()` hangs to its bound.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_hung_in_place_replace_does_not_wedge_close() {
  assert_a_held_retarget_does_not_wedge_close(None).await;
}

/// The widen's ROLLBACK retarget is raced too. Here the first `replace` COMMITS at `/z` — a key
/// containing none of the subsumed roots, so `fs_path_preserves_plan` rejects it and the owner rolls
/// the retarget back — and it is that rollback which hangs. Both of the widen's `replace` awaits are
/// therefore proven close-interruptible, not just the first.
///
/// Fail-on-old (the inline rollback `self.source.replace(handle, &only_key).await`): same wedge, one
/// await later — `close()` hangs to its bound.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_hung_in_place_replace_rollback_does_not_wedge_close() {
  assert_a_held_retarget_does_not_wedge_close(Some(key("/z"))).await;
}