ant-node 0.23.0

Pure quantum-proof network node for the Autonomi decentralized network
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
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
//! Replication subsystem for the Autonomi network.
//!
//! Implements Kademlia-style replication with:
//! - Fresh replication with `PoP` verification
//! - Neighbor sync with round-robin cycle management
//! - Batched quorum verification
//! - Storage audit protocol (anti-outsourcing)
//! - `PaidForList` persistence and convergence
//! - Responsibility pruning with hysteresis

// The replication engine intentionally holds `RwLock` read guards across await
// boundaries (e.g. reading sync_history while calling audit_tick). Clippy's
// nursery lint `significant_drop_tightening` flags these, but the guards must
// remain live for the duration of the call.
#![allow(clippy::significant_drop_tightening)]

pub mod admission;
pub mod audit;
pub mod audit_coordinator;
pub(crate) mod audit_metrics;
pub mod bootstrap;
pub mod commitment;
pub mod commitment_state;
pub mod config;
pub mod fresh;
pub mod neighbor_sync;
pub mod paid_list;
pub mod pointer;
pub mod possession;
pub mod protocol;
pub mod pruning;
pub mod quorum;
pub mod recent_provers;
pub mod scheduling;
pub mod slice;
pub mod storage_commitment_audit;
pub mod subtree;
pub mod types;

use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt;
use std::num::NonZeroUsize;
use std::ops::ControlFlow;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime};

use lru::LruCache;
use std::pin::Pin;

use crate::logging::{debug, error, info, warn};
use futures::stream::FuturesUnordered;
use futures::{future::join_all, Future, StreamExt};
use parking_lot::Mutex;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use tokio::sync::broadcast::error::RecvError;
use tokio::sync::{mpsc, Notify, RwLock, Semaphore};
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tokio_util::task::TaskTracker;

use crate::ant_protocol::XorName;
use crate::error::{Error, Result};
use crate::payment::{
    PaymentVerifier, VerificationContext, MAX_PAYMENT_PROOF_SIZE_BYTES,
    MIN_PAYMENT_PROOF_SIZE_BYTES,
};
use crate::pointer::store::PointerStore;
use crate::pointer::{FinalStateWitness, PointerService};
use crate::replication::audit::AuditTickResult;
use crate::replication::audit_coordinator::AuditChallengeCoordinator;
use crate::replication::audit_metrics::{
    AuditResponderClass, AuditResponderDropReason, AuditResponderMetrics,
    ReplicationResponderClass, ResponderAdmissionCeiling,
};
use crate::replication::commitment::{commitment_hash, StorageCommitment};
use crate::replication::commitment_state::{
    PeerCommitmentRecord, PersistedRetention, ResponderCommitmentState, GOSSIP_ANSWERABILITY_TTL,
};
use crate::replication::config::{
    fresh_read_retry_delay, max_parallel_fetch, storage_admission_width, ReplicationConfig,
    MAX_AUDIT_RESPONSES_PER_PEER, MAX_CONCURRENT_AUDIT_RESPONSES, MAX_CONCURRENT_REPLICATION_SENDS,
    MAX_DIGEST_AUDIT_RESPONSES_PER_PEER, MAX_FRESH_READ_ATTEMPTS, MAX_INCOMING_VERIFICATION_KEYS,
    MAX_PENDING_FRESH_OFFERS, MAX_SESSION_POINTER_BINDINGS, MAX_SUBTREE_ROUND1_PER_PEER,
    MAX_SUBTREE_SESSIONS, MAX_VERIFICATION_KEYS_PER_CYCLE, REPLICATION_PROTOCOL_ID,
    SUBTREE_AUDIT_PROTOCOL_ID, SUBTREE_ROUND1_WORK_BURST_BYTES,
    SUBTREE_ROUND1_WORK_REFILL_BYTES_PER_SEC, SUBTREE_SESSION_TTL,
};
use crate::replication::paid_list::PaidList;
use crate::replication::protocol::{
    FreshReplicationResponse, NeighborSyncResponse, ReplicationMessage, ReplicationMessageBody,
    VerificationResponse,
};
use crate::replication::quorum::KeyVerificationOutcome;
use crate::replication::recent_provers::RecentProvers;
use crate::replication::scheduling::{CapacityDisplacement, DeferralOutcome, ReplicationQueues};
use crate::replication::storage_commitment_audit::PointerBindings;
use crate::replication::types::{
    AuditFailureReason, BootstrapClaimObservation, BootstrapState, FailureEvidence,
    NeighborSyncState, PeerSyncRecord, PresenceEvidence, RepairProofs, VerificationEntry,
    VerificationState,
};
use crate::storage::{CapacityVerdict, ChunkStore};
use saorsa_core::identity::{NodeIdentity, PeerId};
use saorsa_core::{DhtNetworkEvent, P2PEvent, P2PNode, TrustEvent};
use saorsa_pqc::api::sig::{MlDsaSecretKey, MlDsaVariant};

/// Count of monetized-pin nominations DROPPED at the bounded ingress channel
/// because it was full (Amendment 2). Process-global because the producer is
/// the payment verifier (a different module) and the drop happens before the
/// per-drainer `received` counter. A non-zero value is the rollout signal that
/// nomination ingress is saturating — benign (penalty-free, lottery/next-
/// payment covered) but worth watching. `Closed` (engine shut down) is not
/// counted: it is not a saturation signal.
static FIRST_AUDIT_INGRESS_DROPPED: AtomicU64 = AtomicU64::new(0);

/// Record one ingress-full drop. Called by the payment verifier's `try_send`
/// sites; read by the drainer's periodic summary.
pub(crate) fn note_monetized_ingress_drop() {
    FIRST_AUDIT_INGRESS_DROPPED.fetch_add(1, Ordering::Relaxed);
}

#[derive(Default)]
struct FirstAuditObservability {
    received: AtomicU64,
    queued: AtomicU64,
    coalesced: AtomicU64,
    duplicates: AtomicU64,
    capacity_evicted: AtomicU64,
    /// An event targeting the local peer itself, dropped at ingress: the node
    /// cannot network-audit itself (no dialable address for the local peer).
    self_target_skipped: AtomicU64,
    /// A strictly-lower-count same-peer nomination that was dropped so a
    /// higher-count pending pin survived. A sustained rise is the signal of an
    /// attempted "erase the inflated pin with a cheaper one" self-suppression.
    suppressed_lower: AtomicU64,
    cooldown_deferred_attempts: AtomicU64,
    rate_deferred_attempts: AtomicU64,
    window_deduped: AtomicU64,
    launched: AtomicU64,
    passed: AtomicU64,
    timed_out: AtomicU64,
    failed: AtomicU64,
    bootstrap_claims: AtomicU64,
    idle: AtomicU64,
    insufficient_keys: AtomicU64,
    outside_answerability_window: AtomicU64,
    inflight: AtomicU64,
}

/// Test-only snapshot of the first-audit scheduler counters.
///
/// Lets e2e tests assert on the scheduler's decisions (e.g. that a
/// self-targeting monetized pin was dropped and never launched) instead of
/// scraping log lines.
#[cfg(any(test, feature = "test-utils"))]
#[derive(Debug, Clone, Copy)]
pub struct FirstAuditStats {
    /// Events ingested from the monetized-pin channel.
    pub received: u64,
    /// Events accepted into the pending first-audit queue.
    pub queued: u64,
    /// Events dropped because they targeted the local peer.
    pub self_target_skipped: u64,
    /// Audits launched.
    pub launched: u64,
    /// Launched audits that passed.
    pub passed: u64,
    /// Launched audits that timed out (non-response lane).
    pub timed_out: u64,
    /// Launched audits that ended in a confirmed failure.
    pub failed: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FirstAuditTerminalOutcome {
    Passed,
    Timeout,
    Failed,
    BootstrapClaim,
    Idle,
    InsufficientKeys,
}

impl FirstAuditTerminalOutcome {
    #[cfg(any(feature = "logging", test))]
    const fn as_str(self) -> &'static str {
        match self {
            Self::Passed => "passed",
            Self::Timeout => "timeout",
            Self::Failed => "failed",
            Self::BootstrapClaim => "bootstrap_claim",
            Self::Idle => "idle",
            Self::InsufficientKeys => "insufficient_keys",
        }
    }
}

fn first_audit_terminal_outcome(result: &AuditTickResult) -> FirstAuditTerminalOutcome {
    match result {
        AuditTickResult::Passed { .. } => FirstAuditTerminalOutcome::Passed,
        AuditTickResult::Failed {
            evidence:
                FailureEvidence::AuditFailure {
                    reason: AuditFailureReason::Timeout,
                    ..
                },
            ..
        } => FirstAuditTerminalOutcome::Timeout,
        AuditTickResult::Failed { .. } => FirstAuditTerminalOutcome::Failed,
        AuditTickResult::BootstrapClaim { .. } => FirstAuditTerminalOutcome::BootstrapClaim,
        AuditTickResult::Idle => FirstAuditTerminalOutcome::Idle,
        AuditTickResult::InsufficientKeys => FirstAuditTerminalOutcome::InsufficientKeys,
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FirstAuditQueueOutcome {
    /// New peer inserted with free capacity.
    Queued,
    /// Collapsed with an existing same-peer entry; the incoming won (higher
    /// count, or equal count and newer) and replaced it.
    Coalesced,
    /// Collapsed with an existing same-peer entry; the incoming LOST because it
    /// had a STRICTLY LOWER count and was dropped, leaving the higher-count pin
    /// in place. This is the attempted cheaper-pin self-erasure signal.
    SuppressedLower,
    /// Collapsed with an existing same-peer entry of EQUAL count; the incoming
    /// lost the freshness tie (it was not the newer of the two) and the
    /// existing entry was retained. Benign, not an attack signal.
    RetainedOnTie,
    /// A DIFFERENT peer's entry was displaced from the capped pending queue
    /// (random victim) to make room.
    CapacityEvicted { peer: PeerId, pin: [u8; 32] },
}

/// Coalesce a monetized nomination into the per-peer pending queue with a
/// SECURITY-AWARE rule (ADR-0004 Amendment 2): keep the pin that most needs
/// auditing — the HIGHEST committed key count for that peer, newest on a tie.
///
/// A strictly-lower-count incoming must NOT displace a higher-count pending pin,
/// otherwise a peer can erase an inflated (audit-worthy) commitment by simply
/// monetizing a cheaper one right after — and a sidecar-only inflated pin has no
/// gossip-lottery backstop. When the incoming loses, the retained entry's LRU
/// recency is left UNTOUCHED (via `peek`), so a flood of low-count nominations
/// cannot promote the retained pin's lane position.
///
/// `incoming_is_newer` distinguishes ordinary enqueue (the incoming arrived
/// last, so it wins an equal-count tie for freshness) from a cooldown-race
/// requeue of an older reserved event (the pending successor is newer, so it
/// wins the tie).
fn coalesce_first_audit_event(
    pending: &mut LruCache<PeerId, MonetizedPinEvent>,
    incoming: MonetizedPinEvent,
    incoming_is_newer: bool,
    rng: &mut StdRng,
) -> FirstAuditQueueOutcome {
    if let Some(existing) = pending.peek(&incoming.peer) {
        // Strictly lower -> the incoming loses and is dropped WITHOUT touching
        // the retained pin's recency (the security-relevant self-erasure case).
        if incoming.key_count < existing.key_count {
            return FirstAuditQueueOutcome::SuppressedLower;
        }
        // Equal count -> keep the fresher; an older incoming loses a benign tie.
        if incoming.key_count == existing.key_count && !incoming_is_newer {
            return FirstAuditQueueOutcome::RetainedOnTie;
        }
        // Strictly higher, or equal-and-newer: the incoming wins. `push` updates
        // the value and bumps MRU; replacing an existing key never evicts a
        // different peer.
        let _ = pending.push(incoming.peer, incoming);
        return FirstAuditQueueOutcome::Coalesced;
    }
    // No same-peer entry. At capacity, displace a UNIFORMLY RANDOM incumbent,
    // never the LRU: deterministic keep-newest would let an ordered batch of
    // `cap` distinct-peer nominations flush a chosen target before any launch
    // lane sees it, cutting the eviction cost from probabilistic to exact. A
    // random victim caps an attacker's per-nomination eviction probability at
    // `1/cap` regardless of arrival order or timing, so suppressing a specific
    // target with confidence `1-e` costs ~`cap*ln(1/e)` distinct-peer paid
    // nominations and is never certain (ADR-0004 Amendment 4).
    if pending.len() >= pending.cap().get() {
        let victim = {
            let idx = rng.gen_range(0..pending.len());
            pending.iter().nth(idx).map(|(p, _)| *p)
        };
        if let Some(victim_peer) = victim {
            if let Some(evicted) = pending.pop(&victim_peer) {
                let _ = pending.push(incoming.peer, incoming);
                return FirstAuditQueueOutcome::CapacityEvicted {
                    peer: victim_peer,
                    pin: evicted.pin,
                };
            }
        }
    }
    match pending.push(incoming.peer, incoming) {
        None => FirstAuditQueueOutcome::Queued,
        // Unreachable: the random-displacement branch above guarantees
        // `len < cap` here (the caller holds `&mut`, so no concurrent insert
        // exists). Kept so any future logic error surfaces as an ACCOUNTED
        // eviction rather than silent loss — but note this arm would be
        // LRU-order, not random, so it must stay unreachable.
        Some((evicted_peer, evicted)) => FirstAuditQueueOutcome::CapacityEvicted {
            peer: evicted_peer,
            pin: evicted.pin,
        },
    }
}

/// ADR-0004 Amendment 2 (E′): slack added to the max launch jitter when
/// prefiltering a nomination's answerability at schedule time, covering the
/// spawn/dispatch latency between the timer firing and the wire challenge so a
/// jitter==MAX pin is not admitted only to fail the authoritative check by a
/// few milliseconds. Tiny against the multi-hour answerability window.
const FIRST_AUDIT_SEND_LATENCY_SLACK: Duration = Duration::from_secs(1);

/// A first audit the limiter recently launched at a peer: when, and the
/// committed key count that was audited (for the count-jump override).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct RecentFirstAudit {
    launched_at: Instant,
    key_count: u32,
}

/// Holds one first-audit in-flight slot; decrements the gauge on drop so a
/// panicking or cancelled audit task can never leak a slot and wedge the
/// [`config::FIRST_AUDIT_MAX_INFLIGHT`] cap shut.
struct FirstAuditInflightSlot(Arc<FirstAuditObservability>);

impl FirstAuditInflightSlot {
    fn acquire(observability: &Arc<FirstAuditObservability>) -> Self {
        observability.inflight.fetch_add(1, Ordering::Relaxed);
        Self(Arc::clone(observability))
    }
}

impl Drop for FirstAuditInflightSlot {
    fn drop(&mut self) {
        self.0.inflight.fetch_sub(1, Ordering::Relaxed);
    }
}

/// ADR-0004 Amendment 2: whether `new_count` exceeds `audited_count` by more
/// than the [`config::FIRST_AUDIT_COUNT_JUMP_NUM`]/
/// [`config::FIRST_AUDIT_COUNT_JUMP_DEN`] ratio (`new > old * NUM / DEN`,
/// overflow-free integer math). A jump re-nominates a peer despite a recent
/// first audit: an inflated SIDECAR-ONLY pin is visible to payment verifiers
/// only, so no gossip-lottery audit can ever cover it.
const fn first_audit_count_jump(audited_count: u32, new_count: u32) -> bool {
    (new_count as u64) * config::FIRST_AUDIT_COUNT_JUMP_DEN
        > (audited_count as u64) * config::FIRST_AUDIT_COUNT_JUMP_NUM
}

/// The launch limiter's verdict for one pending monetized pin.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LimiterVerdict {
    /// Within budget and window: the caller may reserve a launch (and consumes
    /// a token via [`FirstAuditLimiter::reserve_token`] only if it does).
    Admit,
    /// Launch-rate budget or in-flight cap exhausted. Penalty-free: keep the
    /// pin pending and retry on a later tick.
    RateDeferred,
    /// The peer had a first audit within
    /// [`config::FIRST_AUDIT_PEER_REAUDIT_INTERVAL`] and the new pin's count
    /// shows no [`first_audit_count_jump`]. Drop the nomination (the
    /// gossip-lottery re-audit path is unaffected).
    WindowDeduped,
}

/// ADR-0004 Amendment 2: the first-audit launch limiter — a token bucket
/// (launch rate), an in-flight cap, and a per-peer re-audit window that
/// survives pin rotation.
///
/// This is the load-bearing aggregate bound the original scheduler lacked:
/// fleet-wide first-audit pressure becomes `nodes x refill-rate` instead of
/// `uploads x pinned-quotes-per-proof x verifying-storers`. Pure over passed
/// `now`/`inflight` values so every decision is unit-testable without a
/// runtime.
struct FirstAuditLimiter {
    /// Launch tokens available, at most [`config::FIRST_AUDIT_BUDGET_BURST`].
    tokens: u32,
    /// Refill anchor. While the bucket is full this tracks `now` (a full
    /// bucket accrues nothing); while below capacity it advances only by
    /// whole refill intervals so fractional elapsed time is never lost.
    last_refill: Instant,
    /// Peers given a first audit recently, with the audited key count.
    /// Bounded like the drainer's other per-peer maps.
    recent: LruCache<PeerId, RecentFirstAudit>,
}

impl FirstAuditLimiter {
    fn new(now: Instant) -> Self {
        Self {
            tokens: config::FIRST_AUDIT_BUDGET_BURST,
            last_refill: now,
            recent: LruCache::new(
                NonZeroUsize::new(MAX_LAST_COMMITMENT_BY_PEER).unwrap_or(NonZeroUsize::MIN),
            ),
        }
    }

    /// Whether the per-peer re-audit window admits a nomination for `peer`
    /// carrying `key_count` at `now`. Read-only (`peek`), so it is safe to
    /// call at ENQUEUE time — suppressed nominations never occupy pending
    /// slots — without disturbing LRU recency.
    fn window_allows(&self, peer: &PeerId, key_count: u32, now: Instant) -> bool {
        self.recent.peek(peer).is_none_or(|prev| {
            now.saturating_duration_since(prev.launched_at)
                >= config::FIRST_AUDIT_PEER_REAUDIT_INTERVAL
                || first_audit_count_jump(prev.key_count, key_count)
        })
    }

    /// Refill the token bucket for the time elapsed up to `now`.
    fn refill(&mut self, now: Instant) {
        if self.tokens >= config::FIRST_AUDIT_BUDGET_BURST {
            // Full bucket accrues nothing; keep the anchor current so the
            // next consumption starts its interval from here.
            self.last_refill = now;
            return;
        }
        let interval = config::FIRST_AUDIT_LAUNCH_INTERVAL;
        let elapsed = now.saturating_duration_since(self.last_refill);
        let earned = elapsed.as_nanos() / interval.as_nanos().max(1);
        if earned == 0 {
            return;
        }
        let capacity_gap = u128::from(config::FIRST_AUDIT_BUDGET_BURST - self.tokens);
        if earned >= capacity_gap {
            self.tokens = config::FIRST_AUDIT_BUDGET_BURST;
            self.last_refill = now;
        } else {
            // earned < capacity_gap <= u32::MAX, so both casts are lossless.
            self.tokens = self
                .tokens
                .saturating_add(u32::try_from(earned).unwrap_or(u32::MAX));
            let advance = interval.saturating_mul(u32::try_from(earned).unwrap_or(u32::MAX));
            self.last_refill = self.last_refill.checked_add(advance).unwrap_or(now);
        }
    }

    /// Decide whether a pending pin may launch now. Consumes NOTHING: the
    /// caller runs the remaining (per-peer cooldown) gate and calls
    /// [`Self::commit_launch`] only for a launch that actually happens, so a
    /// deferral elsewhere never burns budget or stamps the window.
    fn assess(
        &mut self,
        peer: &PeerId,
        key_count: u32,
        now: Instant,
        inflight: u64,
    ) -> LimiterVerdict {
        if !self.window_allows(peer, key_count, now) {
            return LimiterVerdict::WindowDeduped;
        }
        self.refill(now);
        if inflight >= config::FIRST_AUDIT_MAX_INFLIGHT || self.tokens == 0 {
            return LimiterVerdict::RateDeferred;
        }
        LimiterVerdict::Admit
    }

    /// Consume one launch token for a RESERVATION (ADR-0004 Amendment 2 E′).
    /// Does NOT stamp the per-peer window: the durable `recent` stamp happens
    /// only at [`Self::promote`], after the authoritative post-jitter
    /// answerability check, so a reservation that is later cancelled leaves no
    /// suppression behind.
    fn reserve_token(&mut self) {
        self.tokens = self.tokens.saturating_sub(1);
    }

    /// Return a token consumed by a reservation that was cancelled before it
    /// launched (answerability lapsed or a concurrent gossip audit won the
    /// cooldown). Capped at the burst so a spurious double-refund cannot exceed
    /// the bucket.
    fn refund_token(&mut self) {
        self.tokens = (self.tokens + 1).min(config::FIRST_AUDIT_BUDGET_BURST);
    }

    /// Stamp the per-peer re-audit window for a launch that is ACTUALLY firing
    /// (promotion). Separated from token consumption so suppression is only
    /// ever recorded for a real send.
    fn promote(&mut self, peer: PeerId, key_count: u32, now: Instant) {
        self.recent.put(
            peer,
            RecentFirstAudit {
                launched_at: now,
                key_count,
            },
        );
    }

    /// Test convenience: the pre-E′ atomic "a launch happened" — consume a token
    /// and stamp the window in one call. Production splits these across
    /// reservation and promotion; the limiter's own budget/window unit tests do
    /// not model the jitter reservation and use this shorthand.
    #[cfg(test)]
    fn commit_launch(&mut self, peer: PeerId, key_count: u32, now: Instant) {
        self.reserve_token();
        self.promote(peer, key_count, now);
    }
}

/// ADR-0004 Amendment 2 (E′ B-prefilter): whether a monetized pin is answerable
/// across the ENTIRE launch-jitter window ending at
/// `now + FIRST_AUDIT_LAUNCH_JITTER_MAX + slack`, so committing scheduling state
/// for it cannot later require aborting an out-of-window challenge. The
/// too-future bound is enforced at `now` (a jitter delay only ages a pin
/// forward, never toward the future); the too-old bound is enforced at the
/// latest possible send time. This is a conservative admission prefilter; the
/// authoritative answerability check still runs at promotion against the real
/// send-time wall clock, so A1 (no false conviction) holds regardless of how
/// jitter and the answerability margin are sized. `checked_add` overflow fails
/// closed (skip the pin).
fn quote_answerable_through_nominal_jitter(quote_ts: SystemTime, now: SystemTime) -> bool {
    let Some(latest_send) = now
        .checked_add(config::FIRST_AUDIT_LAUNCH_JITTER_MAX)
        .and_then(|t| t.checked_add(FIRST_AUDIT_SEND_LATENCY_SLACK))
    else {
        return false;
    };
    quote_within_audit_window(quote_ts, now) && quote_within_audit_window(quote_ts, latest_send)
}

/// Open the single first-audit reservation from `pending` if none is
/// outstanding: samples the launch jitter, snapshots the shared cooldown
/// read-only, and delegates to [`FirstAuditScheduler::try_reserve`]. One
/// shared implementation for both drainer call sites — the per-wake launch
/// phase and the pre-overflow opportunity inside the ingress batch — so the
/// two cannot drift.
async fn open_first_audit_reservation(
    scheduler: &mut FirstAuditScheduler,
    cooldown: &RwLock<HashMap<PeerId, Instant>>,
    observability: &Arc<FirstAuditObservability>,
) {
    if scheduler.has_reservation() {
        return;
    }
    let jitter = Duration::from_millis(rand::thread_rng().gen_range(
        0..=u64::try_from(config::FIRST_AUDIT_LAUNCH_JITTER_MAX.as_millis()).unwrap_or(u64::MAX),
    ));
    let inflight = observability.inflight.load(Ordering::Relaxed);
    let reserved = {
        let cooldown = cooldown.read().await;
        scheduler.try_reserve(Instant::now(), inflight, jitter, &cooldown, observability)
    };
    if reserved {
        if let Some(peer) = scheduler.reserved_peer() {
            debug!(
                "First-audit scheduler: audit_trigger=first_monetized outcome=reserved peer={peer} pending={}",
                scheduler.pending_len()
            );
        }
    }
}

/// A far-future `Instant` used to effectively DISABLE the promotion-timer
/// select arm when no reservation is outstanding. The drainer still wakes at
/// least every [`config::FIRST_AUDIT_RETRY_INTERVAL`] via its tick, so this only
/// needs to be comfortably past the next tick.
fn first_audit_far_future() -> Instant {
    Instant::now()
        .checked_add(Duration::from_hours(1))
        .unwrap_or_else(Instant::now)
}

/// ADR-0004 Amendment 2 (E′): one outstanding first-audit reservation. Holds
/// its in-flight slot and launch token from schedule time until the jitter
/// timer fires; the durable suppression (`recent` + `first_audited`) is stamped
/// only if the authoritative post-jitter answerability + cooldown checks pass at
/// promotion, so a cancelled reservation leaves NO suppression behind.
struct FirstAuditReservation {
    event: MonetizedPinEvent,
    ready_at: Instant,
    inflight: FirstAuditInflightSlot,
}

/// ADR-0004 Amendment 2 (E′): the drainer-owned first-audit scheduler. Owns the
/// pending queue, the dedup set, the launch limiter, the alternating lane, and
/// the single outstanding reservation. All mutation is single-threaded in the
/// drainer task; the only asynchrony is the spawned audit I/O (which just holds
/// the moved-in in-flight slot). Kept as a struct so the reserve/promote/cancel
/// state machine is unit-testable with injected clocks.
struct FirstAuditScheduler {
    /// Pins already given a first audit (dedup). A pin enters only at PROMOTION
    /// (a real send), never at reservation, so a cancelled reservation can be
    /// re-nominated.
    first_audited: LruCache<[u8; 32], ()>,
    /// Highest-count-per-peer pending nominations not yet launched. Capped at
    /// [`FIRST_AUDIT_PENDING_CAP`] (what the token budget can launch within
    /// one effective answerability window); at capacity a uniformly RANDOM
    /// incumbent is displaced and counted as `capacity_evicted`, so eviction
    /// of a specific entry can never be forced deterministically. One further
    /// entry may be held in `reserved` outside this queue — a deliberate
    /// one-entry guard slot, so total schedulable occupancy is at most
    /// `cap + 1`; the summary line reports both (`pending`/`reserved`).
    pending: LruCache<PeerId, MonetizedPinEvent>,
    /// Token bucket + per-peer re-audit window.
    limiter: FirstAuditLimiter,
    /// The single outstanding reservation (E′ serializes reservations so the
    /// per-launch lane alternation is preserved and at most one jitter timer is
    /// live).
    reserved: Option<FirstAuditReservation>,
    /// Alternating launch lane, flipped on every PROMOTION (real launch).
    oldest_first_lane: bool,
    /// The local node's own peer ID. A verified payment's quote list includes
    /// the node's own quote, so the verifier emits a monetized-pin event for
    /// the local peer on every payment it verifies. The node cannot
    /// network-audit itself (there is no dialable address for the local peer,
    /// so the challenge fails instantly and is miscounted as a timeout), while
    /// any other payee that verifies the same payment schedules its own first
    /// audit of this node's pin — a self-dial adds no coverage either way.
    /// Such an event is dropped at ingress: never queued, and hence never
    /// launched nor marked first-audited.
    self_peer: PeerId,
    /// RNG for random-victim displacement (ADR-0004 Amendment 4). Owned by the
    /// scheduler so tests can seed it and reproduce eviction sequences exactly;
    /// production seeds from OS entropy at construction.
    rng: StdRng,
}

impl FirstAuditScheduler {
    fn new(now: Instant, self_peer: PeerId) -> Self {
        let dedup_cap = NonZeroUsize::new(MAX_LAST_COMMITMENT_BY_PEER).unwrap_or(NonZeroUsize::MIN);
        let pending_cap = NonZeroUsize::new(FIRST_AUDIT_PENDING_CAP).unwrap_or(NonZeroUsize::MIN);
        Self {
            first_audited: LruCache::new(dedup_cap),
            pending: LruCache::new(pending_cap),
            limiter: FirstAuditLimiter::new(now),
            reserved: None,
            oldest_first_lane: false,
            self_peer,
            rng: StdRng::from_entropy(),
        }
    }

    /// Observability getter: used by the scheduler summary log and unit tests,
    /// both absent from a release `--no-default-features` build, so it is dead
    /// only in that configuration.
    #[cfg_attr(not(feature = "logging"), allow(dead_code))]
    fn pending_len(&self) -> usize {
        self.pending.len()
    }

    /// Observability getter (see [`Self::pending_len`]).
    #[cfg_attr(not(feature = "logging"), allow(dead_code))]
    fn tokens(&self) -> u32 {
        self.limiter.tokens
    }

    /// Observability getter (see [`Self::pending_len`]). Milliseconds since the
    /// signed quote timestamp of the oldest pin still awaiting a first audit:
    /// how close the longest-waiting pending pin is to aging out of the
    /// answerability window. A value climbing toward the window means pending
    /// work is expiring unaudited instead of launching; a small, steady value
    /// means the queue is draining promptly. Returns `0` when `pending` is empty
    /// and saturates to `0` for a future-dated quote (clock skew), never panics.
    #[cfg_attr(not(feature = "logging"), allow(dead_code))]
    fn oldest_pending_quote_age_ms(&self, now: SystemTime) -> u64 {
        self.pending
            .iter()
            .map(|(_, e)| e.quote_ts)
            .min()
            .and_then(|oldest| now.duration_since(oldest).ok())
            .map_or(0, |age| u64::try_from(age.as_millis()).unwrap_or(u64::MAX))
    }

    /// Drop every pending nomination that has aged past the answerability
    /// horizon. `try_reserve` collects expired entries only while it can scan —
    /// under token starvation it returns at the budget gate first — so without
    /// a periodic sweep dead entries squat the capped pending queue (displacing
    /// at capacity) and inflate the `pending`/`oldest_pending_quote_age_ms`
    /// telemetry. Each removal is accounted as `outside_answerability_window`,
    /// exactly like a scan-time expiry. Returns how many entries were dropped;
    /// survivor recency is untouched.
    fn sweep_expired(&mut self, wall_now: SystemTime, obs: &Arc<FirstAuditObservability>) -> usize {
        let expired: Vec<PeerId> = self
            .pending
            .iter()
            .filter(|(_, event)| !quote_answerable_through_nominal_jitter(event.quote_ts, wall_now))
            .map(|(peer, _)| *peer)
            .collect();
        for peer in &expired {
            self.pending.pop(peer);
        }
        if !expired.is_empty() {
            obs.outside_answerability_window.fetch_add(
                u64::try_from(expired.len()).unwrap_or(u64::MAX),
                Ordering::Relaxed,
            );
        }
        expired.len()
    }

    fn has_reservation(&self) -> bool {
        self.reserved.is_some()
    }

    /// Whether admitting `event` would displace a DIFFERENT peer's pending
    /// entry: the queue is at capacity and `event.peer` has no incumbent to
    /// coalesce into. The drainer checks this before enqueueing so pending
    /// work gets a reservation opportunity BEFORE destructive overflow
    /// (ADR-0004 Amendment 4) — a successful reservation moves one entry out
    /// of `pending`, freeing the slot without any eviction.
    fn would_displace(&self, event: &MonetizedPinEvent) -> bool {
        self.pending.len() >= self.pending.cap().get() && self.pending.peek(&event.peer).is_none()
    }

    /// When the outstanding reservation becomes eligible for promotion.
    fn reserved_ready_at(&self) -> Option<Instant> {
        self.reserved.as_ref().map(|r| r.ready_at)
    }

    /// The peer of the outstanding reservation, if any.
    fn reserved_peer(&self) -> Option<PeerId> {
        self.reserved.as_ref().map(|r| r.event.peer)
    }

    /// Admit a monetized nomination into `pending`. Dropped at ingress if it
    /// targets the local peer (see [`Self::self_peer`]); dropped as a duplicate
    /// if already first-audited; the window screen is bypassed for the currently
    /// reserved peer (so a successor is retained across the reservation, never
    /// window-dropped); otherwise window-screened. Coalescing is
    /// highest-count-per-peer (newest on a tie) — a lower-count successor never
    /// displaces a higher-count pending pin. An incumbent that has aged past
    /// the answerability horizon is dropped (and accounted as an expiry) before
    /// coalescing, so a dead pin never vetoes a live nomination. Admission for
    /// a NEW peer at capacity displaces a uniformly RANDOM incumbent (see
    /// [`FIRST_AUDIT_PENDING_CAP`] and [`coalesce_first_audit_event`]):
    /// displacement is accounted as `capacity_evicted` and stamps no
    /// suppression, so a displaced peer's next nomination is judged like any
    /// newcomer. The drainer additionally offers a reservation opportunity
    /// via [`Self::would_displace`] before calling this on an overflowing
    /// arrival.
    fn enqueue(&mut self, event: MonetizedPinEvent, obs: &Arc<FirstAuditObservability>) {
        if event.peer == self.self_peer {
            obs.self_target_skipped.fetch_add(1, Ordering::Relaxed);
            return;
        }
        if self.first_audited.contains(&event.pin) {
            obs.duplicates.fetch_add(1, Ordering::Relaxed);
            return;
        }
        let reserved_peer = self.reserved_peer();
        let is_reserved_peer = reserved_peer == Some(event.peer);
        if !is_reserved_peer
            && !self
                .limiter
                .window_allows(&event.peer, event.key_count, Instant::now())
        {
            obs.window_deduped.fetch_add(1, Ordering::Relaxed);
            return;
        }
        // A pending incumbent past the answerability horizon can never launch,
        // yet its (possibly higher) key count would still win the coalesce
        // against a live incoming — e.g. a fresh post-prune lower-count pin —
        // and under token starvation no reserve scan runs to collect it. Drop
        // it first so the incoming is judged on its own merits.
        let stale_incumbent = self.pending.peek(&event.peer).is_some_and(|existing| {
            !quote_answerable_through_nominal_jitter(existing.quote_ts, SystemTime::now())
        });
        if stale_incumbent {
            self.pending.pop(&event.peer);
            obs.outside_answerability_window
                .fetch_add(1, Ordering::Relaxed);
        }
        // Ordinary enqueue: the incoming arrived last, so it wins an equal-count
        // tie.
        match coalesce_first_audit_event(&mut self.pending, event, true, &mut self.rng) {
            FirstAuditQueueOutcome::Queued => {
                obs.queued.fetch_add(1, Ordering::Relaxed);
            }
            FirstAuditQueueOutcome::Coalesced | FirstAuditQueueOutcome::RetainedOnTie => {
                obs.coalesced.fetch_add(1, Ordering::Relaxed);
            }
            FirstAuditQueueOutcome::SuppressedLower => {
                obs.suppressed_lower.fetch_add(1, Ordering::Relaxed);
            }
            FirstAuditQueueOutcome::CapacityEvicted { .. } => {
                obs.queued.fetch_add(1, Ordering::Relaxed);
                obs.capacity_evicted.fetch_add(1, Ordering::Relaxed);
            }
        }
    }

    /// Attempt to create the single reservation from `pending` (E′ reserve).
    /// Scans in the current lane order and reserves the FIRST eligible pin:
    /// consumes one token, acquires one in-flight slot, and sets `ready_at =
    /// mono_now + jitter`. Does NOT stamp `recent`/`first_audited` and does NOT
    /// flip the lane — those happen only at promotion. Returns whether a
    /// reservation was made. `cooldown` is a read-only snapshot (the
    /// authoritative check-and-stamp is at promotion).
    fn try_reserve(
        &mut self,
        mono_now: Instant,
        inflight: u64,
        jitter: Duration,
        cooldown: &HashMap<PeerId, Instant>,
        obs: &Arc<FirstAuditObservability>,
    ) -> bool {
        if self.reserved.is_some() || self.pending.is_empty() {
            return false;
        }
        self.limiter.refill(mono_now);
        if inflight >= config::FIRST_AUDIT_MAX_INFLIGHT || self.limiter.tokens == 0 {
            obs.rate_deferred_attempts.fetch_add(1, Ordering::Relaxed);
            return false;
        }
        // MRU->LRU order; reverse for the oldest-first lane so the preferred
        // end is at the front.
        let mut ordered: Vec<(usize, PeerId, MonetizedPinEvent)> = self
            .pending
            .iter()
            .enumerate()
            .map(|(i, (p, e))| (i, *p, *e))
            .collect();
        self.pending.clear();
        if self.oldest_first_lane {
            ordered.reverse();
        }
        let mut chosen: Option<MonetizedPinEvent> = None;
        let mut kept: Vec<(usize, PeerId, MonetizedPinEvent)> = Vec::new();
        for (idx, peer, event) in ordered {
            if chosen.is_some() {
                kept.push((idx, peer, event));
                continue;
            }
            if self.first_audited.contains(&event.pin) {
                obs.duplicates.fetch_add(1, Ordering::Relaxed);
                continue; // drop: already audited
            }
            if !quote_answerable_through_nominal_jitter(event.quote_ts, SystemTime::now()) {
                obs.outside_answerability_window
                    .fetch_add(1, Ordering::Relaxed);
                continue; // drop: cannot stay answerable through the jitter
            }
            // Window + budget + inflight. Tokens do not decrease during the
            // scan (reserve happens after the loop) and `inflight` is fixed, so
            // after the upfront budget gate `assess` never returns RateDeferred
            // here; a defensive RateDeferred keeps the pin.
            match self
                .limiter
                .assess(&peer, event.key_count, mono_now, inflight)
            {
                LimiterVerdict::WindowDeduped => {
                    obs.window_deduped.fetch_add(1, Ordering::Relaxed);
                    continue; // drop: recently first-audited, no count jump
                }
                LimiterVerdict::RateDeferred => {
                    kept.push((idx, peer, event));
                    continue; // keep (defensive; unreachable after upfront gate)
                }
                LimiterVerdict::Admit => {}
            }
            if !cooldown_would_allow(cooldown, &peer, mono_now) {
                obs.cooldown_deferred_attempts
                    .fetch_add(1, Ordering::Relaxed);
                kept.push((idx, peer, event));
                continue; // keep: on shared cooldown, retry later
            }
            chosen = Some(event);
        }
        // Restore relative recency (oldest re-put first -> newest stays MRU).
        kept.sort_unstable_by_key(|(idx, _, _)| std::cmp::Reverse(*idx));
        for (_, peer, event) in kept {
            self.pending.put(peer, event);
        }
        let Some(event) = chosen else {
            return false;
        };
        self.limiter.reserve_token();
        let inflight_slot = FirstAuditInflightSlot::acquire(obs);
        let ready_at = mono_now.checked_add(jitter).unwrap_or(mono_now);
        self.reserved = Some(FirstAuditReservation {
            event,
            ready_at,
            inflight: inflight_slot,
        });
        true
    }

    /// Take the outstanding reservation if its jitter has elapsed at `mono_now`.
    fn take_due_reservation(&mut self, mono_now: Instant) -> Option<FirstAuditReservation> {
        if self
            .reserved
            .as_ref()
            .is_some_and(|r| mono_now >= r.ready_at)
        {
            self.reserved.take()
        } else {
            None
        }
    }

    /// Authoritative promotion of a due reservation (E′). The caller holds the
    /// shared cooldown write lock and passes the real send-time `wall_now` and
    /// `mono_now`. Returns `Some((event, slot))` to spawn the audit on a real
    /// launch, or `None` when the launch was cancelled (answerability lapsed
    /// during jitter) or requeued (a concurrent gossip audit won the cooldown).
    /// On both `None` paths the token is refunded, the in-flight slot released,
    /// and NO suppression is recorded.
    ///
    /// Known operational residual: suppression is stamped here, at promotion,
    /// while the wire challenge is sent by the detached task the caller spawns
    /// with the returned event. A task-start or encoding failure between the
    /// two therefore leaves a stamped-but-unsent window. No remote input can
    /// force that failure (it requires local task-spawn/alloc failure), so it
    /// is accepted rather than closed; closing it would require stamping from
    /// inside the spawned task and re-introduce the cancel-leaves-suppression
    /// race this design exists to prevent.
    fn resolve(
        &mut self,
        reservation: FirstAuditReservation,
        wall_now: SystemTime,
        mono_now: Instant,
        cooldown: &mut HashMap<PeerId, Instant>,
        obs: &Arc<FirstAuditObservability>,
    ) -> Option<(MonetizedPinEvent, FirstAuditInflightSlot)> {
        let FirstAuditReservation {
            event, inflight, ..
        } = reservation;
        // Authoritative answerability at the REAL send time.
        if !quote_within_audit_window(event.quote_ts, wall_now) {
            self.limiter.refund_token();
            obs.outside_answerability_window
                .fetch_add(1, Ordering::Relaxed);
            drop(inflight);
            return None; // cancelled; nothing stamped, nothing to roll back
        }
        // Authoritative shared-cooldown check-and-stamp. Losing this race to a
        // concurrent gossip audit requeues the reserved event through the SAME
        // security-aware coalescing: the reserved event is OLDER than any
        // same-peer successor (`incoming_is_newer = false`), so a higher-count
        // reserved event still wins over a lower-count successor (the inflated
        // pin must be audited), while an equal/higher successor is preserved.
        if !cooldown_allows_audit(cooldown, &event.peer, mono_now) {
            self.limiter.refund_token();
            obs.cooldown_deferred_attempts
                .fetch_add(1, Ordering::Relaxed);
            drop(inflight);
            // Account for the requeue outcome (the nomination itself was already
            // counted at ingress, so `queued` is not re-incremented): a capacity
            // eviction of a DIFFERENT peer and a suppressed reserved event are
            // both observable per the ADR funnel.
            match coalesce_first_audit_event(&mut self.pending, event, false, &mut self.rng) {
                FirstAuditQueueOutcome::CapacityEvicted { .. } => {
                    obs.capacity_evicted.fetch_add(1, Ordering::Relaxed);
                }
                FirstAuditQueueOutcome::SuppressedLower => {
                    obs.suppressed_lower.fetch_add(1, Ordering::Relaxed);
                }
                FirstAuditQueueOutcome::Queued
                | FirstAuditQueueOutcome::Coalesced
                | FirstAuditQueueOutcome::RetainedOnTie => {}
            }
            return None;
        }
        // Promote: stamp durable suppression, flip the lane, count the launch.
        self.limiter.promote(event.peer, event.key_count, mono_now);
        self.first_audited.put(event.pin, ());
        self.oldest_first_lane = !self.oldest_first_lane;
        obs.launched.fetch_add(1, Ordering::Relaxed);
        Some((event, inflight))
    }
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Prefix used by saorsa-core's request-response mechanism.
const RR_PREFIX: &str = "/rr/";

/// Bounded handoff from the P2P broadcast receiver to serial non-audit
/// replication processing.
///
/// The receiver fast-paths digest `AuditChallenge`s immediately and queues
/// bulk/non-audit messages here. If this fills, the message is **dropped** —
/// `start_message_handler` must never block on a full queue, because a stalled
/// receiver laps the P2P broadcast ring and silently loses messages it never
/// observed. Every serial-lane class has protocol recovery, so a drop costs a
/// retry rather than the message.
///
/// This bounds a message COUNT, but a queued item is an owned decoded message
/// of up to [`config::MAX_REPLICATION_MESSAGE_SIZE`], so the resident worst
/// case is 64 × 10 MiB = 640 MiB. That ceiling is acknowledged, not designed
/// for: the lane carries small requests and challenges alongside
/// `FreshReplicationOffer`, its one multi-MiB class, so no single count is
/// right for both. Responses never reach here — `replication_payload_from_event`
/// filters them out and their requesters correlate them. ADR-0005 decision 12
/// records the trade and why byte accounting is the correct eventual fix.
const INBOUND_REPLICATION_SERIAL_QUEUE_CAPACITY: usize = 64;

/// Maximum fresh-replication offers processed concurrently, away from the
/// serial non-audit loop.
///
/// Fresh offers can perform an on-chain payment verification and a 4 MiB
/// write. Four workers keep that latency off the responder dispatch path while
/// keeping concurrent EVM/storage pressure small and predictable.
const FRESH_OFFER_WORKER_LIMIT: usize = 4;

/// Maximum fresh offers admitted at once, counting both those running on a
/// worker and those queued for one.
///
/// An admitted offer holds its payload until it completes, so this bounds
/// memory rather than latency: at 4 MiB each, sixteen is a 64 MiB ceiling.
/// Offers past the bound are refused rather than queued or handled inline —
/// handling one on the message loop stalls every other non-audit message behind
/// a payment verification and a multi-MiB write. A refusal is not free: the
/// sender does not read it, so it resurfaces as a missing key at the delayed
/// possession check (5-15 min, ADR-0003) and is charged to this node at audit
/// severity. Only neighbor sync (10-20 min) actually refills the gap, so the
/// bound is sized to make refusal rare rather than cheap.
const FRESH_OFFER_MAX_OUTSTANDING: usize = 16;

/// Distinct payment proofs queued for one fresh-offer key.
///
/// Duplicate offers for a key are routine rather than adversarial: a client PUT
/// is confirmed by `CLOSE_GROUP_MAJORITY` nodes, and *each* of them fans the
/// chunk out to the close group, so a receiver sees the same key from about that
/// many senders. They differ only in their proof — the bytes are provably
/// identical, since `fresh_offer_structural_rejection` has confirmed
/// `key == BLAKE3(data)` before any of them is queued.
///
/// Sizing the queue to the number of legitimate senders is what lets a failing
/// proof fall through to the next one instead of costing the node the record.
///
/// This is a **lifetime budget per entry**, not a queue depth: it counts proofs
/// admitted, and a proof the handler has popped has spent its slot rather than
/// returned it. That distinction is what bounds the work an attacker can buy —
/// at most this many on-chain verifications per key, after which the entry
/// refuses everyone until it closes. Gating on the queue's instantaneous length
/// instead would let a stream of distinct sources refill each popped slot and
/// run unbounded sequential verifications while holding one of the four worker
/// slots. One attempt per source additionally means a single peer can only ever
/// consume one of these.
const MAX_FRESH_OFFER_ATTEMPTS_PER_KEY: usize = crate::ant_protocol::CLOSE_GROUP_MAJORITY;

/// Fresh-offer slots kept reachable by sources that hold none.
///
/// The property worth guaranteeing is that one peer cannot *starve* others, not
/// that any peer is held to a small quota. Expressing the bound as a reserve
/// rather than a quota is what keeps those two apart.
const FRESH_OFFER_RESERVED_FOR_OTHER_SOURCES: usize = 4;

/// Maximum fresh offers admitted from one source peer.
///
/// Without a per-source bound, one peer can hold every slot in the global pool
/// and every honest offer arriving in that window is refused — and because a
/// refusal is later read as absence by the sender's delayed possession check,
/// those refusals land as audit-severity trust penalties on the *refuser*.
/// That makes an unbounded global pool a targeted eviction primitive.
///
/// The bound is deliberately far looser than the other responder classes'
/// (fetch 2, verification 1, neighbor sync 1), because the traffic pattern is
/// the opposite shape. Those are request/response: a requester needs only a
/// couple in flight, so a small quota costs it nothing. Fresh offers are a
/// one-way bulk fan-out, and in the ordinary case a node's offers come almost
/// entirely from ONE peer — whichever node took the client's PUT. A quota sized
/// for request/response traffic therefore binds on completely legitimate
/// uploads: at 2, an ordinary 48-chunk upload had offers refused (see
/// `tests/e2e/fresh_offer_capacity.rs`), every one of them attributed to this
/// ceiling and none to the global pool.
///
/// Sizing it as "the pool minus a reserve" keeps the anti-starvation guarantee
/// exact — [`FRESH_OFFER_RESERVED_FOR_OTHER_SOURCES`] slots always remain for a
/// peer holding none — while leaving a single legitimate fan-out unthrottled.
// The cast is from a compile-time constant difference of two small literals
// (16 - 4), so truncation is impossible; deriving the share keeps the reserve
// relationship visible in the code rather than only in a test.
#[allow(clippy::cast_possible_truncation)]
const FRESH_OFFER_MAX_OUTSTANDING_PER_PEER: u32 =
    (FRESH_OFFER_MAX_OUTSTANDING - FRESH_OFFER_RESERVED_FOR_OTHER_SOURCES) as u32;

/// Maximum paid-list notifications served concurrently.
///
/// A `PaidNotify` runs the same on-chain proof verification as a fresh offer
/// but stores only paid-list metadata, so it needs no chunk-write headroom.
/// Two workers keep cross-peer progress without multiplying concurrent EVM
/// and DHT lookup pressure.
const PAID_NOTIFY_WORKER_LIMIT: usize = 2;

/// Maximum paid-list notifications admitted across workers and their waiters.
///
/// Sized as a **memory** ceiling, not a fairness device, because `PaidNotify`
/// is one-way: the protocol defines no response and the sender never retries,
/// so a refused notify is information permanently lost to this node until a
/// later verification cycle happens to re-derive the key's paid status from a
/// paid-list quorum. A tight admission bound therefore does not shed load, it
/// discards durable state — which is why the class's real protection is
/// [`PAID_NOTIFY_WORKER_LIMIT`], bounding the concurrent EVM and DHT work that
/// is actually expensive, rather than this queue depth.
///
/// A client PUT fans one notify per key out to the whole paid close group, so
/// a single upload legitimately arrives as a burst of tens. At the 512 KiB
/// `MAX_PAYMENT_PROOF_SIZE_BYTES` worst case, sixty-four outstanding is a
/// 32 MiB ceiling — half the fresh-offer admission ceiling, for messages that
/// carry no chunk payload.
const PAID_NOTIFY_MAX_OUTSTANDING: usize = 64;

/// Maximum admitted paid-list notifications from one source peer.
///
/// A quarter of the pool. One peer legitimately supplies a whole upload's
/// worth of notifies, so this must comfortably exceed a typical file's chunk
/// count in flight; it exists to stop a single source evicting every other
/// peer's durable paid-list evidence, not to ration ordinary traffic.
const PAID_NOTIFY_MAX_OUTSTANDING_PER_PEER: u32 = 16;

/// Maximum fetch responses served concurrently.
///
/// Each successful response can upload a 4 MiB chunk. Matching
/// [`MAX_CONCURRENT_REPLICATION_SENDS`] keeps fetch serving to about 12 MiB of
/// simultaneous chunk data, which avoids saturating typical home upload links.
const FETCH_RESPONDER_WORKER_LIMIT: usize = 3;

/// Maximum fetch requests admitted across workers and their bounded waiters.
///
/// Four worker waves absorb short bursts without retaining chunk bytes—the
/// request contains only a key—while the dequeue deadline sheds work before a
/// sustained flood can keep the node serving requests whose callers timed out.
const FETCH_RESPONDER_MAX_OUTSTANDING: usize = FETCH_RESPONDER_WORKER_LIMIT * 4;

/// Maximum admitted fetch requests from one source peer.
///
/// Two requests let one peer pipeline useful reads while leaving at least one
/// of the three workers available to other peers under a single-source flood.
const FETCH_RESPONDER_MAX_OUTSTANDING_PER_PEER: u32 = 2;

/// Maximum verification batches served concurrently.
///
/// Point lookups are fast, but a batch can contain 8,192 of them. Two
/// workers isolate that synchronous work from message dispatch without turning
/// large batches into an I/O fan-out throughput contest.
const VERIFICATION_RESPONDER_WORKER_LIMIT: usize = 2;

/// Maximum verification batches admitted across workers and bounded waiters.
///
/// Four worker waves absorb ordinary cross-peer bursts while the dequeue
/// deadline prevents queued batches from consuming lookup capacity after their
/// requesters have stopped waiting.
const VERIFICATION_RESPONDER_MAX_OUTSTANDING: usize = VERIFICATION_RESPONDER_WORKER_LIMIT * 4;

/// Maximum admitted verification batches from one source peer.
///
/// Senders already aggregate a peer's keys into one batch. One outstanding
/// batch therefore preserves useful work while reserving the other worker and
/// the bounded queue for different peers.
const VERIFICATION_RESPONDER_MAX_OUTSTANDING_PER_PEER: u32 = 1;

/// Maximum neighbor-sync requests served concurrently across source peers.
///
/// Building a response scans local keys and performs DHT lookups, so two
/// workers allow cross-peer progress without multiplying that expensive scan
/// into broad network and storage contention.
const NEIGHBOR_SYNC_RESPONDER_WORKER_LIMIT: usize = 2;

/// Maximum neighbor-sync requests admitted across workers and bounded waiters.
///
/// Four worker waves cover ordinary cadence overlap while keeping retained sync
/// payloads bounded; expired waiters are shed before any key scan begins.
const NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING: usize = NEIGHBOR_SYNC_RESPONDER_WORKER_LIMIT * 4;

/// Maximum admitted neighbor-sync requests from one source peer.
///
/// Sync history must be updated before repair proofs for the same peer. A
/// single slot preserves that ordering while independent peers remain parallel.
const NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING_PER_PEER: u32 = 1;

/// Match an inbound topic against the replication protocol ids, in both the bare
/// gossip form and the `/rr/<id>` request-response form.
///
/// Returns the matched core or [`SUBTREE_AUDIT_PROTOCOL_ID`] id and whether it
/// was the RR form. The matched id is carried into the handler so it can enforce
/// that subtree messages only arrive on the subtree id.
fn match_replication_protocol(topic: &str) -> Option<(&'static str, bool)> {
    for id in [REPLICATION_PROTOCOL_ID, SUBTREE_AUDIT_PROTOCOL_ID] {
        if topic == id {
            return Some((id, false));
        }
        if let Some(rest) = topic.strip_prefix(RR_PREFIX) {
            if rest == id {
                return Some((id, true));
            }
        }
    }
    None
}

/// Whether a decoded body belongs on the protocol id it arrived on:
/// subtree-audit bodies on [`SUBTREE_AUDIT_PROTOCOL_ID`] and every other body,
/// including digest audits, on [`REPLICATION_PROTOCOL_ID`].
///
/// The receive guard drops any mismatch (a cross-version or misrouted message);
/// sharing this one predicate between the guard and its regression test means a
/// change to the rule cannot pass the test unnoticed.
fn body_matches_protocol(body: &ReplicationMessageBody, protocol: &str) -> bool {
    protocol == response_protocol_for(body)
}

/// The protocol id a body belongs on — the single source of truth for BOTH
/// directions: the receive guard ([`body_matches_protocol`]) and the outbound
/// response selector in `send_replication_response_checked`.
///
/// Sharing one function makes the subtree/core isolation symmetric.
fn response_protocol_for(body: &ReplicationMessageBody) -> &'static str {
    if body.is_subtree_audit() {
        SUBTREE_AUDIT_PROTOCOL_ID
    } else {
        REPLICATION_PROTOCOL_ID
    }
}

fn fresh_offer_payment_context() -> VerificationContext {
    VerificationContext::FreshReplication
}

fn paid_notify_payment_context() -> VerificationContext {
    VerificationContext::PaidListAdmission
}

/// Boxed future type for in-flight fetch tasks.
type FetchFuture = Pin<Box<dyn Future<Output = (XorName, Option<FetchOutcome>)> + Send>>;

/// Fresh-offer keys currently being handled, each with the proofs still to try.
///
/// Concurrent duplicates are routine: a client PUT is confirmed by
/// `CLOSE_GROUP_MAJORITY` nodes and each of them fans the chunk out, so one key
/// commonly arrives from several senders at once. Collapsing them onto one
/// handler is what keeps a burst of PUTs from costing a permit, a worker slot,
/// and an on-chain verification per copy.
///
/// What they collapse *onto* is the load-bearing part. Keeping only the key and
/// refusing every later offer outright made the first arrival the only arrival:
/// if its proof failed, the record was lost even though a valid proof for the
/// same bytes was queued behind it, and the resulting absence was charged to
/// this node by the delayed possession check. So the entry holds the bytes once
/// and queues each sender's proof, and the handler works down that queue. A
/// later offer therefore costs a proof, not a payload — sound only because
/// `fresh_offer_structural_rejection` has confirmed `key == BLAKE3(data)` for
/// every one of them before it is queued, which is what makes their bytes
/// provably identical.
///
/// Entries are exact keys rather than hash shards. A node only receives offers
/// for keys it is close to, so the accepted key set clusters tightly around its
/// own ID — any index derived from the key would land nearly every offer on one
/// shard and serialize unrelated keys behind each other. Exact keys keep them
/// independent, and the map stays bounded by [`FRESH_OFFER_MAX_OUTSTANDING`],
/// each entry by [`MAX_FRESH_OFFER_ATTEMPTS_PER_KEY`].
///
/// The critical section is a map insert, pop, or remove and is never held across
/// an await, so this is a blocking mutex rather than an async one.
type FreshOfferInFlight = Arc<Mutex<HashMap<XorName, FreshOfferEntry>>>;

/// One sender's unverified claim to have paid for a key already in flight.
///
/// Carries no payload: the bytes live once in [`FreshOfferEntryGuard`], and the
/// content-address check ran before this attempt was queued.
struct FreshOfferAttempt {
    source: PeerId,
    proof_of_payment: Vec<u8>,
    request_id: u64,
    rr_message_id: Option<String>,
}

/// Proofs still to try for one in-flight key, and who supplied them.
///
/// Deliberately holds neither the bytes nor an arrival time: both belong to the
/// handler that owns the key, and live once in [`FreshOfferEntryGuard`].
struct FreshOfferEntry {
    /// Untried proofs in arrival order. The handler holds the current one.
    pending: VecDeque<FreshOfferAttempt>,
    /// Sources already represented, so one peer cannot fill the queue alone.
    sources: HashSet<PeerId>,
    /// Proofs admitted over this entry's whole life, **never decremented**.
    ///
    /// The budget has to be spent by admissions rather than held by the queue.
    /// The handler pops a proof before verifying it, so gating on
    /// `pending.len()` would let a fresh source refill the slot that pop just
    /// freed, and a stream of distinct sources could then run unbounded
    /// sequential payment verifications while holding one of only four worker
    /// slots. Counting admissions makes the cap a lifetime budget instead.
    admitted: usize,
}

/// How an arriving fresh offer joined the work already in flight for its key.
enum FreshOfferAdmission {
    /// Opened the entry. This offer's handler owns the key and drives every
    /// queued proof, including proofs that arrive later.
    Opened(Box<FreshOfferEntryGuard>),
    /// Merged into an entry another offer opened; its handler reaches this proof
    /// if the ones ahead of it fail.
    Joined,
    /// The entry already holds [`MAX_FRESH_OFFER_ATTEMPTS_PER_KEY`] proofs.
    Full,
    /// This source already has a proof queued for this key.
    DuplicateSource,
}

// Gated on `logging` like `SerialQueueDropReason::as_str`: the only caller is a
// `debug!`, which compiles to nothing without the feature.
#[cfg(feature = "logging")]
impl FreshOfferAdmission {
    /// Why the offer added nothing to the work already queued for its key.
    ///
    /// Local logging only — the wire reason stays uniform so a probing peer
    /// cannot tell a full queue from a repeat of its own proof.
    const fn surplus_reason(&self) -> &'static str {
        match self {
            Self::DuplicateSource => "this source already has a proof queued",
            Self::Full => "the key already holds its full complement of proofs",
            Self::Opened(_) | Self::Joined => "not surplus",
        }
    }
}

/// RAII ownership of one in-flight fresh-offer key.
///
/// Clears the entry on drop, so an early return, an error, or a panic cannot
/// strand a key as permanently in flight. Also owns the single copy of the
/// offered bytes that every queued proof is tried against.
struct FreshOfferEntryGuard {
    in_flight: FreshOfferInFlight,
    key: XorName,
    /// The bytes every attempt for this key shares, held exactly once.
    data: Vec<u8>,
    /// Arrival of the offer that opened the entry.
    received_at: Instant,
    /// Cleared once the entry has been removed, so `drop` cannot delete an entry
    /// a *later* offer has since opened for the same key.
    holds_entry: bool,
}

impl FreshOfferEntryGuard {
    /// Open an entry for this offer's key, or queue its proof behind the entry
    /// already open.
    ///
    /// Takes the offer by value: on `Opened` its bytes move into the guard, and
    /// on every other outcome they are dropped here rather than held for the
    /// lifetime of the entry.
    fn admit(
        in_flight: &FreshOfferInFlight,
        offer: protocol::FreshReplicationOffer,
        source: PeerId,
        request_id: u64,
        rr_message_id: Option<String>,
        received_at: Instant,
    ) -> FreshOfferAdmission {
        let key = offer.key;
        let attempt = FreshOfferAttempt {
            source,
            proof_of_payment: offer.proof_of_payment,
            request_id,
            rr_message_id,
        };

        let mut in_flight_map = in_flight.lock();
        if let Some(entry) = in_flight_map.get_mut(&key) {
            if entry.sources.contains(&source) {
                return FreshOfferAdmission::DuplicateSource;
            }
            // Lifetime budget, not queue depth: a proof the handler has already
            // popped has spent its slot, not returned it.
            if entry.admitted >= MAX_FRESH_OFFER_ATTEMPTS_PER_KEY {
                return FreshOfferAdmission::Full;
            }
            entry.admitted = entry.admitted.saturating_add(1);
            entry.sources.insert(source);
            entry.pending.push_back(attempt);
            return FreshOfferAdmission::Joined;
        }

        let mut sources = HashSet::new();
        sources.insert(source);
        let mut pending = VecDeque::new();
        pending.push_back(attempt);
        in_flight_map.insert(
            key,
            FreshOfferEntry {
                pending,
                sources,
                admitted: 1,
            },
        );
        drop(in_flight_map);
        FreshOfferAdmission::Opened(Box::new(Self {
            in_flight: Arc::clone(in_flight),
            key,
            data: offer.data,
            received_at,
            holds_entry: true,
        }))
    }

    /// The bytes every proof for this key is tried against.
    fn data(&self) -> &[u8] {
        &self.data
    }

    /// When the first offer for this key arrived.
    fn received_at(&self) -> Instant {
        self.received_at
    }

    /// Take the next untried proof, releasing the key when none remain.
    ///
    /// Popping and releasing happen under one lock so a proof arriving exactly
    /// as the queue empties is never silently discarded: it either lands in this
    /// entry and is returned here, or finds no entry and opens its own.
    fn next_attempt(&mut self) -> Option<FreshOfferAttempt> {
        let mut in_flight = self.in_flight.lock();
        let Some(entry) = in_flight.get_mut(&self.key) else {
            self.holds_entry = false;
            return None;
        };
        if let Some(attempt) = entry.pending.pop_front() {
            return Some(attempt);
        }
        in_flight.remove(&self.key);
        self.holds_entry = false;
        None
    }

    /// Release the key and return the proofs that were never tried, so their
    /// senders can be told the outcome the key reached without them.
    fn release(&mut self) -> Vec<FreshOfferAttempt> {
        let mut in_flight = self.in_flight.lock();
        self.holds_entry = false;
        in_flight
            .remove(&self.key)
            .map(|entry| entry.pending.into_iter().collect())
            .unwrap_or_default()
    }
}

impl Drop for FreshOfferEntryGuard {
    fn drop(&mut self) {
        if self.holds_entry {
            self.in_flight.lock().remove(&self.key);
        }
    }
}

/// Shared dependencies for one verification worker cycle.
struct VerificationCycleContext<'a> {
    p2p_node: &'a Arc<P2PNode>,
    paid_list: &'a Arc<PaidList>,
    storage: &'a Arc<ChunkStore>,
    queues: &'a Arc<RwLock<ReplicationQueues>>,
    config: &'a ReplicationConfig,
    bootstrap_state: &'a Arc<RwLock<BootstrapState>>,
    is_bootstrapping: &'a Arc<RwLock<bool>>,
    bootstrap_complete_notify: &'a Arc<Notify>,
    /// v12 §6 holder-eligibility inputs. The verifier downgrades a
    /// peer's Present claim to Unresolved unless they're a credited
    /// holder of the key (i.e. they recently passed a commitment-bound
    /// audit on it under their currently-credited commitment hash).
    last_commitment_by_peer: &'a Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>>,
    ever_capable_peers: &'a Arc<RwLock<HashSet<PeerId>>>,
    recent_provers: &'a Arc<RwLock<RecentProvers>>,
}

/// Fetch worker polling interval in milliseconds.
const FETCH_WORKER_POLL_MS: u64 = 100;

/// Verification worker polling interval in milliseconds.
const VERIFICATION_WORKER_POLL_MS: u64 = 250;

/// Verification cycle duration that is worth surfacing at info level.
const VERIFICATION_CYCLE_SLOW_LOG_MS: u128 = 500;

/// Standard trust event weight for per-operation success/failure signals.
///
/// Used for individual replication fetch outcomes, integrity check failures,
/// and bootstrap claim abuse. Distinct from `AUDIT_FAILURE_TRUST_WEIGHT` which
/// is reserved for confirmed audit failures.
const REPLICATION_TRUST_WEIGHT: f64 = 1.0;
/// Bound trust updates from one verification cycle. A malicious peer can
/// advertise thousands of bad singleton keys at once; a few independent
/// contradictions are enough for the trust engine without flooding it.
const MAX_BAD_HINT_TRUST_REPORTS_PER_PEER_PER_CYCLE: usize = 3;

/// Bootstrap drain check interval in seconds.
const BOOTSTRAP_DRAIN_CHECK_SECS: u64 = 5;

/// Grace period `shutdown()` waits for each long-lived background task to
/// observe the cancellation token and terminate before aborting it.
///
/// Detached tasks are drained without a timeout because storage-capable work
/// may be awaiting a `spawn_blocking` storage operation, which continues running
/// if its async waiter is dropped.
const SHUTDOWN_TASK_DRAIN_TIMEOUT: Duration = Duration::from_secs(10);

/// How often the responder rebuilds + rotates its storage commitment.
///
/// Each rebuild scans the store to compute leaf hashes; for ~10k keys this is
/// sub-100ms (BLAKE3 + tree build). Retention is gossip-anchored, NOT
/// rotation-anchored: the responder stays answerable for the current
/// commitment plus every root it recently gossiped that is still in-window
/// (~2 in steady state), each kept for `GOSSIP_ANSWERABILITY_TTL` (3 h) after
/// its last emission (see `commitment_state`). So the rotation cadence does
/// not by itself bound answerability — a gossiped commitment stays
/// answerable across rotations until its gossip TTL lapses.
///
/// Default: 1 hour, aligned with the worst-case neighbor-sync cooldown
/// (`NEIGHBOR_SYNC_COOLDOWN_SECS = 3600`). Because the gossip TTL (3 h)
/// comfortably exceeds the gap between our rotation and the next gossip
/// arrival at a remote peer, this prevents the "unknown commitment hash" ->
/// Idle audit-skip pattern from being the common case.
///
/// Why not faster: the v12 pin is bound to a specific point-in-time
/// commitment, so rotation isn't security-critical for pin freshness —
/// only for keeping the committed key set current as the responder
/// writes new keys. 1 hour is plenty for that, and slow enough that
/// honest auditors mostly hit `current` or `previous` rather than the
/// "rotated past" case.
const COMMITMENT_ROTATION_INTERVAL_SECS: u64 = 3600;

/// How often the responder retention snapshot is flushed to disk (ADR-0004 A1).
/// Short relative to the answerability TTL (3 h) so a gossip-stamp refresh is
/// durable well before it could matter to a restart, while the write-on-change
/// guard keeps idle nodes from needless disk writes.
const RETENTION_PERSIST_INTERVAL_SECS: u64 = 30;

/// Cadence of the `replication traffic summary (cumulative)` INFO lines
/// (V2-623). A `const` so testnets can drop it to 60s; 300s is the production
/// default that keeps log volume negligible.
const TRAFFIC_SUMMARY_INTERVAL_SECS: u64 = 300;

/// Cadence of the read-only bootstrap state snapshot used to separate drain
/// debt from cache and broader connectivity failures in fleet logs.
#[cfg(feature = "logging")]
const BOOTSTRAP_STATE_SNAPSHOT_INTERVAL_SECS: u64 = 60;

/// Maximum tolerated auditor↔responder wall-clock skew for the first-audit
/// in-window screen (ADR-0004 A1 guardrail A). The screen accepts a monetized pin
/// for first audit only if its SIGNED `quote_ts` lands in
/// `[now - (GOSSIP_ANSWERABILITY_TTL - MONETIZED_AUDIT_SKEW_MARGIN), now + MONETIZED_AUDIT_SKEW_MARGIN]`
/// — fail-closed on BOTH ends: a quote dated too far in the future (a
/// badly-skewed or replayed quote) and one too old (the responder may have aged
/// the pin out) are both skipped, so — with grace removed — a stale/skewed quote
/// cannot frame an honest node. This assumes bounded clock skew (nodes NTP-synced
/// within this margin); a legit first audit fires moments after payment
/// (`quote_ts ≈ now`), far from either bound. The gossip-lottery path (which pins
/// the responder's OWN freshly-gossiped root) is the clock-skew-immune backstop.
/// 30 min dwarfs any realistic honest skew while leaving a wide audit window.
const MONETIZED_AUDIT_SKEW_MARGIN: Duration = Duration::from_mins(30);

/// ADR-0004 A1 (guardrail A): whether a monetized pin's SIGNED `quote_ts` lands
/// inside the answerability window relative to `now`, so first-auditing it cannot
/// false-convict once grace is removed. Fail-closed on BOTH ends (see
/// [`MONETIZED_AUDIT_SKEW_MARGIN`]): a quote more than the skew margin in the
/// future, or older than `GOSSIP_ANSWERABILITY_TTL - margin`, is out of window.
/// All comparisons use `duration_since` (no `Duration` overflow).
fn quote_within_audit_window(quote_ts: SystemTime, now: SystemTime) -> bool {
    let too_future = quote_ts
        .duration_since(now)
        .is_ok_and(|ahead| ahead > MONETIZED_AUDIT_SKEW_MARGIN);
    let audit_cutoff = GOSSIP_ANSWERABILITY_TTL.saturating_sub(MONETIZED_AUDIT_SKEW_MARGIN);
    let too_old = now
        .duration_since(quote_ts)
        .is_ok_and(|age| age >= audit_cutoff);
    !(too_future || too_old)
}

/// Minimum interval between commitment signature verifications for a
/// single peer (v10/v12 §2 step 3 + §11 `DoS`).
///
/// A sybil that bypasses the routing-table gate (e.g. by transient
/// bucket pollution) could otherwise force one ML-DSA-65 verify (~1 ms)
/// per gossip message. This rate limit caps the verify-per-peer rate
/// at 1/min, which is comfortably above the legitimate gossip cadence
/// (the 10-20 min neighbor-sync round on each peer).
const COMMITMENT_SIG_VERIFY_MIN_INTERVAL: Duration = Duration::from_mins(1);

/// Hard cap on the size of `last_commitment_by_peer`.
///
/// Bounds the per-process memory cost of the auditor's per-peer
/// commitment cache. Each entry holds a `StorageCommitment`
/// (~5 KiB: 1952-byte pubkey + 3293-byte signature + small fields).
/// At 4096 entries the cache is ~20 MiB, which comfortably covers a
/// realistic close-group neighborhood. When the cap is hit, one
/// arbitrary existing entry is evicted on insert (`HashMap` iteration
/// order is unspecified; we do not track insertion order). The
/// `PeerRemoved` handler proactively drops entries as the DHT
/// detects departures, and `ingest_peer_commitment` only admits
/// commitments from peers currently in the routing table — together
/// the cap is the third line of defence against sybil/churn flooding.
const MAX_LAST_COMMITMENT_BY_PEER: usize = 4096;

/// ADR-0004 Amendment 4: admission cap for the first-audit pending queue,
/// sized to the launch budget instead of the commitment cache.
///
/// The token bucket can launch at most one audit per
/// [`config::FIRST_AUDIT_LAUNCH_INTERVAL`] (plus the
/// [`config::FIRST_AUDIT_BUDGET_BURST`] allowance), and a pending pin stays
/// launchable for at most the effective answerability window
/// ([`GOSSIP_ANSWERABILITY_TTL`] − [`MONETIZED_AUDIT_SKEW_MARGIN`]). Any
/// occupancy beyond `window / interval + burst` is work that cannot launch
/// before it expires, so admitting it only builds an aging backlog whose
/// telemetry measures the backlog instead of schedulable work.
///
/// At capacity, admission displaces a uniformly RANDOM incumbent (counted as
/// `capacity_evicted`), so under overload `pending` degrades to a budget-sized
/// rolling sample in which no arrival order lets an attacker deterministically
/// flush a chosen target; a fresh pin always enters the sample with an
/// unpredictable chance of near-immediate audit, whereas refusing at the door
/// would let a sustained payment flood guarantee that every later pin is never
/// admitted. See [`coalesce_first_audit_event`] for the displacement rule and
/// ADR-0004 Amendment 4 for the eviction-cost math.
///
/// Derivation uses the STRICT reserve-time horizon: a launch at quote age `a`
/// is usable only if the quote stays answerable through `a` plus the maximum
/// launch jitter and send slack, so the last usable refill is the largest
/// `k` with `k * interval < cutoff - jitter_max - slack`. The unit test
/// simulates the shipped predicate instant-by-instant and must agree.
#[allow(clippy::cast_possible_truncation)] // bounded by TTL-secs/interval-secs + burst (~31)
const FIRST_AUDIT_PENDING_CAP: usize = {
    let strict_horizon_secs = GOSSIP_ANSWERABILITY_TTL
        .as_secs()
        .saturating_sub(MONETIZED_AUDIT_SKEW_MARGIN.as_secs())
        .saturating_sub(config::FIRST_AUDIT_LAUNCH_JITTER_MAX.as_secs())
        .saturating_sub(FIRST_AUDIT_SEND_LATENCY_SLACK.as_secs());
    // Strictly-inside count: a refill landing exactly ON the horizon is dead
    // (`age >= cutoff` rejects), hence the `- 1`. Compile-time division-by-zero
    // if the launch interval is ever zeroed: a zero interval makes the budget
    // (and this cap) meaningless.
    let refills =
        strict_horizon_secs.saturating_sub(1) / config::FIRST_AUDIT_LAUNCH_INTERVAL.as_secs();
    (refills + config::FIRST_AUDIT_BUDGET_BURST as u64) as usize
};

// Compile-time guardrails: the cap must be a usable `LruCache` capacity and
// strictly tighter than the commitment-cache bound it replaced.
const _: () = {
    assert!(FIRST_AUDIT_PENDING_CAP >= 1);
    assert!(FIRST_AUDIT_PENDING_CAP < MAX_LAST_COMMITMENT_BY_PEER);
};

/// Cap on the sticky `ever_capable_peers` set. Bounds memory so a
/// long-running bootstrap node cannot have the set grow without limit
/// from peer-id churn. Sized at 4x `MAX_LAST_COMMITMENT_BY_PEER` so
/// the set comfortably outlives normal LRU churn but still caps the
/// blast radius of identity-rotation attacks. Once full we refuse new
/// inserts (no eviction) — keeps the historic set stable; new v12
/// peers above the cap are treated as legacy on rejoin, which matches
/// the behaviour before this set existed, not a security regression.
const MAX_EVER_CAPABLE_PEERS: usize = 4 * MAX_LAST_COMMITMENT_BY_PEER;

// ---------------------------------------------------------------------------
// ReplicationEngine
// ---------------------------------------------------------------------------

/// The replication engine manages all replication background tasks and state.
pub struct ReplicationEngine {
    /// Replication configuration (shared across spawned tasks).
    config: Arc<ReplicationConfig>,
    /// P2P networking node.
    p2p_node: Arc<P2PNode>,
    /// Local chunk storage.
    storage: Arc<ChunkStore>,
    /// Persistent paid-for-list.
    paid_list: Arc<PaidList>,
    /// Payment verifier for `PoP` validation.
    payment_verifier: Arc<PaymentVerifier>,
    /// Replication pipeline queues.
    queues: Arc<RwLock<ReplicationQueues>>,
    /// Neighbor sync cycle state.
    sync_state: Arc<RwLock<NeighborSyncState>>,
    /// Per-peer sync history (for `RepairOpportunity`).
    ///
    /// This map grows with peer churn and is intentionally unbounded: entries
    /// are lightweight (`PeerSyncRecord` is two fields) and peer IDs are
    /// naturally bounded by the routing table's k-bucket capacity.
    sync_history: Arc<RwLock<HashMap<PeerId, PeerSyncRecord>>>,
    /// Per-peer cooldown for gossip-triggered subtree audits (ADR-0002).
    ///
    /// Records when each peer was last audited so a burst of gossiped
    /// commitment changes cannot spawn back-to-back audits of the same peer.
    /// Bounded by routing-table membership and cleaned on `PeerRemoved`.
    audit_on_gossip_cooldown: Arc<RwLock<HashMap<PeerId, Instant>>>,
    /// Gossip-private lottery attempt window (one roll per peer per window,
    /// win or lose). Kept separate from `audit_on_gossip_cooldown` so a losing
    /// ticket never stamps the shared map and thus never defers a monetized
    /// first audit. Bounded like its sibling and cleaned on `PeerRemoved`.
    gossip_lottery_attempts: Arc<RwLock<HashMap<PeerId, Instant>>>,
    /// Completed local neighbor-sync cycle epoch for proof maturity.
    sync_cycle_epoch: Arc<RwLock<u64>>,
    /// Per-key repair proof tracking for audit eligibility.
    repair_proofs: Arc<RwLock<RepairProofs>>,
    /// Bootstrap state tracking.
    bootstrap_state: Arc<RwLock<BootstrapState>>,
    /// Whether this node is currently bootstrapping.
    is_bootstrapping: Arc<RwLock<bool>>,
    /// Trigger for early neighbor sync (signalled on topology changes).
    sync_trigger: Arc<Notify>,
    /// Notified when `is_bootstrapping` transitions from `true` to `false`.
    bootstrap_complete_notify: Arc<Notify>,
    /// Node identity (for signing storage commitments).
    ///
    /// Phase 3 of the v12 storage-bound audit design. The responder
    /// uses this to sign its periodically-built `StorageCommitment`.
    identity: Arc<NodeIdentity>,
    /// Responder-side commitment state (two-slot atomic rotation).
    ///
    /// Periodically rebuilt from the live key set; gossiped on
    /// outbound `NeighborSyncRequest`/`Response`; consulted by the
    /// commitment-bound audit handler.
    commitment_state: Arc<ResponderCommitmentState>,
    /// Path to the persisted responder retention snapshot
    /// (`{root_dir}/commitment_retention.bin`): reloaded on startup so an honest
    /// node's answerability survives restart (ADR-0004 A1), which is what makes
    /// removing audit grace safe (an unanswerable in-window pin is then provable
    /// misbehaviour, not an honest crash-restart).
    retention_path: PathBuf,
    /// Auditor-side per-peer commitment record (last known commitment +
    /// sticky `commitment_capable` flag).
    ///
    /// Populated whenever an inbound gossip carries a verified
    /// commitment from the sender. Used by `audit_tick` to snapshot
    /// `expected_commitment_hash` into outbound challenges, and by
    /// holder-eligibility (§6) to decide whether a peer's `recent_provers`
    /// proof should be honoured. The sticky `commitment_capable` flag
    /// flips true on first successful ingest and never reverts (§2
    /// step 5).
    last_commitment_by_peer: Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>>,
    /// Sticky set of peer IDs we have EVER seen carrying a v12
    /// commitment, independent of whether their commitment bytes are
    /// still in `last_commitment_by_peer`. The §6 holder-eligibility
    /// closure consults this set to keep treating churned-out
    /// previously-v12 peers as v12-capable (rather than degrading them
    /// to "legacy" credit-unconditionally) when they re-appear on the
    /// network before their next gossip arrives. Bounded growth: even
    /// at one million peers seen over the node's lifetime, the set is
    /// 32 MB.
    ever_capable_peers: Arc<RwLock<HashSet<PeerId>>>,
    /// Auditor-side holder-eligibility cache (v12 §6).
    ///
    /// Recorded on successful commitment-bound audit; read by future
    /// quorum / paid-list eligibility checks (phase-3 stretch).
    recent_provers: Arc<RwLock<RecentProvers>>,
    /// Per-peer last sig-verify attempt timestamp for the §2 step 3 /
    /// §11 `DoS` rate limit. Bumped on EVERY verify attempt (success or
    /// failure) so a peer we've never successfully verified can't burn
    /// CPU on a flood of structurally-plausible-but-invalid gossips.
    /// Lives separately from `last_commitment_by_peer` because that
    /// map's records only exist after a successful verify.
    sig_verify_attempts: Arc<RwLock<HashMap<PeerId, Instant>>>,
    /// Limits concurrent outbound replication sends to prevent bandwidth
    /// saturation on home broadband connections.
    send_semaphore: Arc<Semaphore>,
    /// Bounds how many encoded fresh offers can wait behind `send_semaphore`;
    /// see [`MAX_PENDING_FRESH_OFFERS`].
    pending_offer_semaphore: Arc<Semaphore>,
    /// Fresh offers encoded and handed to their per-peer sends.
    fresh_offers_dispatched: Arc<AtomicU64>,
    /// Bounds concurrent IN-FLIGHT LIGHT audit-responder tasks (responsible-chunk
    /// audits + subtree slice round 2). The heavy subtree round 1 has its own
    /// tighter pool ([`SubtreeRound1Limiter`]). Those are spawned off the serial
    /// message loop so disk
    /// reads don't block replication; the semaphore restores a global
    /// backpressure ceiling so the node can't fan out unbounded `get_raw` reads
    /// / multi-MiB byte serves.
    audit_responder_semaphore: Arc<Semaphore>,
    /// Per-source in-flight audit-responder counts, capped at
    /// [`MAX_AUDIT_RESPONSES_PER_PEER`]. The GLOBAL semaphore alone is not
    /// flood-fair: one peer spamming challenges could occupy every slot and
    /// starve honest auditors, whose dropped challenges then convert to
    /// audit timeouts against HONEST peers (codex-r2 A). This
    /// per-peer cap guarantees no single source can hold more than its share,
    /// so a flood self-throttles without denying service to everyone else.
    audit_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    /// Windowed per-origin capacity and processing-time telemetry for every
    /// request sharing the audit responder pool.
    audit_responder_metrics: Arc<AuditResponderMetrics>,
    /// Shared auditor-side limiter for outbound digest `AuditChallenge`s.
    ///
    /// Responsible-chunk audits, prune confirmations, and possession checks
    /// all use this before sending so local bursts wait instead of breaching
    /// the responder's deployed per-source admission cap.
    audit_challenge_coordinator: Arc<AuditChallengeCoordinator>,
    /// Worker permits for bandwidth-bound fetch responses.
    fetch_responder_worker_semaphore: Arc<Semaphore>,
    /// Admission permits bounding fetch workers and queued waiters together.
    fetch_responder_admission_semaphore: Arc<Semaphore>,
    /// Per-source fetch responder counts for flood-fair admission.
    fetch_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    /// Worker permits for lookup-heavy verification batches.
    verification_responder_worker_semaphore: Arc<Semaphore>,
    /// Admission permits bounding verification workers and queued waiters.
    verification_responder_admission_semaphore: Arc<Semaphore>,
    /// Per-source verification responder counts for flood-fair admission.
    verification_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    /// Worker permits for expensive inbound neighbor-sync requests.
    neighbor_sync_responder_worker_semaphore: Arc<Semaphore>,
    /// Admission permits bounding sync workers and queued waiters together.
    neighbor_sync_responder_admission_semaphore: Arc<Semaphore>,
    /// Per-source sync responder counts; capped at one to preserve ordering.
    neighbor_sync_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    /// Bounded worker permits for expensive fresh-offer handling.
    fresh_offer_worker_semaphore: Arc<Semaphore>,
    /// Admission permits bounding offers running on a worker or queued for one.
    fresh_offer_admission_semaphore: Arc<Semaphore>,
    /// Per-source fresh-offer counts, so one sender cannot hold the whole pool
    /// and turn honest senders' refusals into trust penalties on this node.
    fresh_offer_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    /// Keys claimed by an in-flight fresh-offer handler, so concurrent
    /// duplicates collapse onto one verification and one write.
    fresh_offer_in_flight: FreshOfferInFlight,
    /// Bounded worker permits for paid-list notification proof verification.
    paid_notify_worker_semaphore: Arc<Semaphore>,
    /// Admission permits bounding notifies on a worker or queued for one.
    paid_notify_admission_semaphore: Arc<Semaphore>,
    /// Per-source paid-notify counts for flood-fair admission.
    paid_notify_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    /// Resource controls for the HEAVY subtree-audit round 1: its own
    /// tight admission pool (so a burst of full-subtree hashing can't starve the
    /// light audits), a per-peer rate cooldown, and single-use round-1 → round-2
    /// sessions binding a slice challenge to a matching round 1.
    subtree_round1: SubtreeRound1Limiter,
    /// Receiver for fresh-write events from the chunk PUT handler.
    ///
    /// When present, `start()` spawns the fresh-write drainer, which records
    /// paid-list evidence for each event immediately and forwards it to the
    /// offer dispatcher.
    fresh_write_rx: Option<mpsc::UnboundedReceiver<fresh::FreshWriteEvent>>,
    /// Pointer replication (ADR-0016), when this node stores pointers.
    pointers: Option<Arc<pointer::PointerReplication>>,
    /// Receiver for fresh pointer writes, taken by `start()`.
    pointer_fresh_rx: Option<mpsc::UnboundedReceiver<pointer::PointerFreshWrite>>,
    /// Sender for delayed possession-check events (ADR-0003). Sending a write's
    /// fresh offers (`fresh::send_fresh_offers`) pushes its responsible
    /// close-group peers here; the possession-check scheduler drains the paired
    /// receiver.
    possession_check_tx: mpsc::UnboundedSender<possession::PossessionCheckEvent>,
    /// Receiver paired with `possession_check_tx`; taken by the scheduler task.
    possession_check_rx: Option<mpsc::UnboundedReceiver<possession::PossessionCheckEvent>>,
    /// ADR-0004: sender the payment verifier clones to surface monetized pins
    /// for a deterministic first audit. The matching receiver is drained by
    /// `start_first_audit_drainer`. BOUNDED (Amendment 2): the producer
    /// `try_send`s and drops on a full queue, so ingress memory is capped just
    /// like launches; a dropped nomination is penalty-free — the peer's
    /// gossiped commitments stay lottery-covered and its next settled payment
    /// re-nominates the paid pin.
    monetized_pin_tx: mpsc::Sender<MonetizedPinEvent>,
    /// ADR-0004: receiver half of the monetized-pin channel, taken by
    /// `start_first_audit_drainer`.
    monetized_pin_rx: Option<mpsc::Receiver<MonetizedPinEvent>>,
    /// Counters shared with the first-audit drainer task, so the scheduler's
    /// decisions (queued / launched / self-target skipped / outcome) stay
    /// observable from the engine after the drainer takes the receiver.
    first_audit_observability: Arc<FirstAuditObservability>,
    /// Shutdown token.
    shutdown: CancellationToken,
    /// Background task handles.
    task_handles: Vec<JoinHandle<()>>,
    /// Tracks detached, short-lived work spawned by background producers.
    ///
    /// Fresh-offer and bounded responder workers, audit launches, per-fetch
    /// tasks, and delayed possession checks may retain storage or P2P state
    /// after their producer exits, so shutdown drains them before those
    /// resources may be reopened.
    detached_task_tracker: TaskTracker,
}

impl ReplicationEngine {
    /// Create a new replication engine.
    ///
    /// # Errors
    ///
    /// Returns an error if the `PaidList` LMDB environment cannot be opened
    /// or if the configuration fails validation.
    #[allow(clippy::too_many_arguments)]
    pub async fn new(
        config: ReplicationConfig,
        p2p_node: Arc<P2PNode>,
        storage: Arc<ChunkStore>,
        payment_verifier: Arc<PaymentVerifier>,
        identity: Arc<NodeIdentity>,
        root_dir: &Path,
        fresh_write_rx: mpsc::UnboundedReceiver<fresh::FreshWriteEvent>,
        shutdown: CancellationToken,
    ) -> Result<Self> {
        config.validate().map_err(Error::Config)?;

        let paid_list = Arc::new(
            PaidList::new(root_dir)
                .await
                .map_err(|e| Error::Storage(format!("Failed to open PaidList: {e}")))?,
        );

        let initial_neighbors = NeighborSyncState::new_cycle(Vec::new());
        let config = Arc::new(config);
        let (possession_check_tx, possession_check_rx) = mpsc::unbounded_channel();

        // ADR-0004: monetized-pin channel (verifier -> first-audit drainer).
        // Bounded (Amendment 2): every stage of the first-audit pipeline is
        // now capacity-limited — ingress queue here, pending set (LRU), and
        // launch rate (token bucket).
        let (monetized_pin_tx, monetized_pin_rx) =
            mpsc::channel(config::FIRST_AUDIT_INGRESS_CAPACITY);

        let engine = Self {
            config: Arc::clone(&config),
            p2p_node,
            storage,
            paid_list,
            payment_verifier,
            queues: Arc::new(RwLock::new(ReplicationQueues::new())),
            sync_state: Arc::new(RwLock::new(initial_neighbors)),
            sync_history: Arc::new(RwLock::new(HashMap::new())),
            audit_on_gossip_cooldown: Arc::new(RwLock::new(HashMap::new())),
            gossip_lottery_attempts: Arc::new(RwLock::new(HashMap::new())),
            sync_cycle_epoch: Arc::new(RwLock::new(0)),
            repair_proofs: Arc::new(RwLock::new(RepairProofs::new())),
            bootstrap_state: Arc::new(RwLock::new(BootstrapState::new())),
            is_bootstrapping: Arc::new(RwLock::new(true)),
            sync_trigger: Arc::new(Notify::new()),
            bootstrap_complete_notify: Arc::new(Notify::new()),
            identity,
            commitment_state: Arc::new(ResponderCommitmentState::new()),
            retention_path: root_dir.join("commitment_retention.bin"),
            last_commitment_by_peer: Arc::new(RwLock::new(HashMap::new())),
            ever_capable_peers: Arc::new(RwLock::new(HashSet::new())),
            recent_provers: Arc::new(RwLock::new(RecentProvers::new())),
            sig_verify_attempts: Arc::new(RwLock::new(HashMap::new())),
            send_semaphore: Arc::new(Semaphore::new(MAX_CONCURRENT_REPLICATION_SENDS)),
            pending_offer_semaphore: Arc::new(Semaphore::new(MAX_PENDING_FRESH_OFFERS)),
            fresh_offers_dispatched: Arc::new(AtomicU64::new(0)),
            audit_responder_semaphore: Arc::new(Semaphore::new(MAX_CONCURRENT_AUDIT_RESPONSES)),
            audit_responder_inflight: Arc::new(RwLock::new(HashMap::new())),
            audit_responder_metrics: Arc::new(AuditResponderMetrics::default()),
            audit_challenge_coordinator: Arc::new(AuditChallengeCoordinator::new()),
            fetch_responder_worker_semaphore: Arc::new(Semaphore::new(
                FETCH_RESPONDER_WORKER_LIMIT,
            )),
            fetch_responder_admission_semaphore: Arc::new(Semaphore::new(
                FETCH_RESPONDER_MAX_OUTSTANDING,
            )),
            fetch_responder_inflight: Arc::new(RwLock::new(HashMap::new())),
            verification_responder_worker_semaphore: Arc::new(Semaphore::new(
                VERIFICATION_RESPONDER_WORKER_LIMIT,
            )),
            verification_responder_admission_semaphore: Arc::new(Semaphore::new(
                VERIFICATION_RESPONDER_MAX_OUTSTANDING,
            )),
            verification_responder_inflight: Arc::new(RwLock::new(HashMap::new())),
            neighbor_sync_responder_worker_semaphore: Arc::new(Semaphore::new(
                NEIGHBOR_SYNC_RESPONDER_WORKER_LIMIT,
            )),
            neighbor_sync_responder_admission_semaphore: Arc::new(Semaphore::new(
                NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING,
            )),
            neighbor_sync_responder_inflight: Arc::new(RwLock::new(HashMap::new())),
            fresh_offer_worker_semaphore: Arc::new(Semaphore::new(FRESH_OFFER_WORKER_LIMIT)),
            fresh_offer_admission_semaphore: Arc::new(Semaphore::new(FRESH_OFFER_MAX_OUTSTANDING)),
            fresh_offer_responder_inflight: Arc::new(RwLock::new(HashMap::new())),
            fresh_offer_in_flight: Arc::new(Mutex::new(HashMap::new())),
            paid_notify_worker_semaphore: Arc::new(Semaphore::new(PAID_NOTIFY_WORKER_LIMIT)),
            paid_notify_admission_semaphore: Arc::new(Semaphore::new(PAID_NOTIFY_MAX_OUTSTANDING)),
            paid_notify_responder_inflight: Arc::new(RwLock::new(HashMap::new())),
            subtree_round1: SubtreeRound1Limiter::new(
                config.subtree_round1_responder_cooldown,
                config.subtree_round1_max_concurrent,
            ),
            fresh_write_rx: Some(fresh_write_rx),
            pointers: None,
            pointer_fresh_rx: None,
            possession_check_tx,
            possession_check_rx: Some(possession_check_rx),
            monetized_pin_tx,
            monetized_pin_rx: Some(monetized_pin_rx),
            first_audit_observability: Arc::new(FirstAuditObservability::default()),
            shutdown,
            task_handles: Vec::new(),
            detached_task_tracker: TaskTracker::new(),
        };
        // ADR-0004 A1: reload persisted responder retention BEFORE any task
        // spawns, so an honest restarted node is answerable for its pre-restart
        // pins from the first audit it serves, and the persist loop never races
        // an empty snapshot over the good on-disk file.
        load_commitment_retention(&engine.commitment_state, &engine.retention_path).await;
        Ok(engine)
    }

    /// ADR-0004: a sender the payment verifier uses to surface monetized pins
    /// (commitments that backed a payment) for a first audit. Cloneable; the
    /// engine drains the matching receiver. Bounded: senders must `try_send`
    /// and treat a full queue as a benign drop (Amendment 2 best-effort).
    #[must_use]
    pub fn monetized_pin_sender(&self) -> mpsc::Sender<MonetizedPinEvent> {
        self.monetized_pin_tx.clone()
    }

    /// Get a reference to the `PaidList`.
    #[must_use]
    pub fn paid_list(&self) -> &Arc<PaidList> {
        &self.paid_list
    }

    /// Get a reference to the responder's commitment state. Used by audit
    /// handlers to look up commitments by hash; used by the rotation tick
    /// to install fresh ones.
    #[must_use]
    pub fn commitment_state(&self) -> &Arc<ResponderCommitmentState> {
        &self.commitment_state
    }

    /// Get a reference to the auditor's last-commitment-by-peer table.
    #[must_use]
    pub fn last_commitment_by_peer(&self) -> &Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>> {
        &self.last_commitment_by_peer
    }

    /// Get a reference to the holder-eligibility cache. Phase-3 stretch:
    /// will be read by quorum / paid-list eligibility checks.
    #[must_use]
    pub fn recent_provers(&self) -> &Arc<RwLock<RecentProvers>> {
        &self.recent_provers
    }

    /// Test-only: rebuild + rotate this node's storage commitment now over its
    /// current key set (normally on a 1h timer). Lets a test commit to chunks it
    /// just stored without waiting for the rotation cadence.
    ///
    /// # Errors
    ///
    /// Propagates any error from reading the local key set or building/signing
    /// the commitment.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn rebuild_commitment_now(&self) -> Result<()> {
        rebuild_and_rotate_commitment(
            &self.storage,
            self.pointers.as_ref().map(|p| p.store()),
            &self.identity,
            &self.commitment_state,
            &self.p2p_node,
            &self.config,
        )
        .await
    }

    /// Test-only: directly seed this node's cached commitment for `peer`,
    /// simulating "we received `peer`'s gossiped commitment" without depending
    /// on neighbor-sync propagation timing. Lets a two-node audit test pin the
    /// peer's commitment deterministically.
    #[cfg(any(feature = "test-utils", test))]
    pub async fn inject_peer_commitment_for_test(
        &self,
        peer: &PeerId,
        commitment: StorageCommitment,
    ) {
        let now = Instant::now();
        self.last_commitment_by_peer
            .write()
            .await
            .insert(*peer, PeerCommitmentRecord::from_verified(commitment, now));
        self.ever_capable_peers.write().await.insert(*peer);
    }

    /// Test-only: isolate a monetized first audit of `peer` from live gossip
    /// audits. Suppresses new gossip lottery attempts for the current cooldown
    /// window and clears any shared cooldown stamped by an earlier gossip
    /// launch. The locks follow the production gossip-audit lock order.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn isolate_first_audit_for_test(&self, peer: &PeerId) {
        let now = Instant::now();
        let mut lottery_attempts = self.gossip_lottery_attempts.write().await;
        let mut launched = self.audit_on_gossip_cooldown.write().await;
        lottery_attempts.insert(*peer, now);
        launched.remove(peer);
    }

    /// Test-only: run ONE subtree audit against `peer` right now, pinned to the
    /// commitment this node has cached for it (from gossip), over the live wire.
    /// Returns the audit outcome so tests can assert honest-pass / adversary-fail
    /// in a real two-node setting without waiting for the gossip cadence.
    ///
    /// Returns `AuditTickResult::Idle` if we have no cached commitment for the
    /// peer yet (gossip hasn't reached us). Gated to test builds.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn audit_peer_now(&self, peer: &PeerId) -> audit::AuditTickResult {
        let target = {
            let map = self.last_commitment_by_peer.read().await;
            map.get(peer)
                .and_then(PeerCommitmentRecord::last_commitment)
                .and_then(|c| commitment_hash(c).map(|h| (h, c.key_count)))
        };
        let Some((pin, key_count)) = target else {
            return audit::AuditTickResult::Idle;
        };
        let credit = storage_commitment_audit::AuditCredit {
            recent_provers: &self.recent_provers,
        };
        storage_commitment_audit::run_subtree_audit(
            &self.p2p_node,
            &self.config,
            peer,
            pin,
            key_count,
            Some(&credit),
        )
        .await
    }

    /// Test-only: snapshot the first-audit scheduler's counters. Lets e2e
    /// tests assert what the live drainer decided for an injected
    /// [`MonetizedPinEvent`] (dropped as self-target vs queued and launched).
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn first_audit_stats(&self) -> FirstAuditStats {
        let o = &self.first_audit_observability;
        FirstAuditStats {
            received: o.received.load(Ordering::Relaxed),
            queued: o.queued.load(Ordering::Relaxed),
            self_target_skipped: o.self_target_skipped.load(Ordering::Relaxed),
            launched: o.launched.load(Ordering::Relaxed),
            passed: o.passed.load(Ordering::Relaxed),
            timed_out: o.timed_out.load(Ordering::Relaxed),
            failed: o.failed.load(Ordering::Relaxed),
        }
    }

    /// Test-only: run the possession check immediately for `key` against
    /// `peers`, bypassing the scheduler's randomised 5-15 minute settle delay.
    ///
    /// Penalises any peer that does not hold `key` at `AuditChallenge`
    /// severity (ADR-0003). Lets e2e tests assert the detection+penalty path
    /// deterministically without waiting for the scheduled check.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn run_possession_check_now(&self, key: XorName, peers: Vec<PeerId>) {
        possession::run_possession_check(
            key,
            peers,
            &self.p2p_node,
            &self.storage,
            &self.config,
            &self.sync_state,
            &self.audit_challenge_coordinator,
            &self.shutdown,
        )
        .await;
    }

    /// Test-only: place `key` directly into the fetch queue as though a
    /// verification cycle had just promoted it, with `sources` as its
    /// verified holders. Returns whether the key was enqueued.
    ///
    /// Bypasses admission, verification, and the promotion-time
    /// responsibility pre-filter, modelling a promotion decision that has
    /// since gone stale (topology churn between promotion and download).
    /// The only guard left standing is the per-attempt recheck inside
    /// `execute_single_fetch` — exactly the gate e2e tests use this seam to
    /// exercise.
    ///
    /// A replica hint for `key` that reached pending verification first is
    /// dropped, so the key enters the fetch queue as it would have without
    /// it. Callers host the chunk on a holder before calling this, which makes
    /// it advertisable, and a change to the holder's closest peers starts a
    /// neighbour-sync round at once. Left in place, the hint's entry made
    /// `enqueue_fetch` refuse the key, and with one holder and no paid-list
    /// entry it does not reach a quorum, so the key was not fetched that way
    /// either. A key already in the fetch queue or in flight, or a full fetch
    /// queue, still returns `false`.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn enqueue_fetch_for_test(&self, key: XorName, sources: Vec<PeerId>) -> bool {
        let distance = crate::client::xor_distance(&key, self.p2p_node.peer_id().as_bytes());
        let mut queues = self.queues.write().await;
        queues.remove_pending(&key);
        queues.enqueue_fetch(key, distance, sources)
    }

    /// Test-only: whether `key` is still tracked in any fetch-pipeline stage
    /// (pending verification, fetch queue, or in-flight fetch).
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn fetch_pipeline_contains_for_test(&self, key: &XorName) -> bool {
        self.queues.read().await.contains_key(key)
    }

    /// Test-only: whether `key` is in the fetch queue or in flight, which is
    /// where a candidate placed by [`Self::enqueue_fetch_for_test`] stays
    /// until it resolves.
    ///
    /// Such a candidate carries no verification retry metadata, so it is never
    /// requeued for verification: once it leaves these two stages it is done.
    /// Pending verification is left out on purpose. The chunk the test hosted
    /// stays advertisable, so a replica hint can put the key there after the
    /// candidate resolved, and counting that entry would hold a test's wait
    /// loop until its deadline.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn fetch_queued_or_in_flight_for_test(&self, key: &XorName) -> bool {
        self.queues.read().await.fetch_queued_or_in_flight(key)
    }

    /// Test-only: place `key` into pending verification as though `hinter` had
    /// just advertised it with a replica hint. Returns whether it was admitted.
    ///
    /// Enters the pipeline one stage earlier than
    /// [`Self::enqueue_fetch_for_test`], so what the verification cycle itself
    /// decides about the key is observable.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn enqueue_pending_verify_for_test(&self, key: XorName, hinter: PeerId) -> bool {
        let now = Instant::now();
        let entry = VerificationEntry {
            state: VerificationState::PendingVerify,
            verified_sources: Vec::new(),
            tried_sources: HashSet::new(),
            created_at: now,
            next_verify_at: now,
            hint_sources: HashSet::from([hinter]),
            replica_hint_sources: HashSet::from([hinter]),
            unresolved_retries: 0,
            no_holder_reported: false,
        };
        self.queues
            .write()
            .await
            .add_pending_verify(key, entry)
            .admitted()
    }

    /// Test-only: how far ahead `key`'s next verification round is scheduled,
    /// or `None` when the key is not pending verification.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn pending_verify_delay_for_test(&self, key: &XorName) -> Option<Duration> {
        self.queues.read().await.get_pending(key).map(|entry| {
            entry
                .next_verify_at
                .saturating_duration_since(Instant::now())
        })
    }

    /// Replicate pointers too (ADR-0016): the records in `store`, and the
    /// fresh writes the pointer PUT handler sends on `fresh_writes`.
    ///
    /// The PUT handler is not wired to ask this engine anything before it
    /// takes a final state (ADR-0018); [`Self::with_pointer_service`] wires
    /// both.
    ///
    /// Call before [`Self::start`].
    pub fn with_pointers(
        &mut self,
        store: PointerStore,
        fresh_writes: mpsc::UnboundedReceiver<pointer::PointerFreshWrite>,
    ) {
        self.pointers = Some(Arc::new(pointer::PointerReplication::new(
            store,
            Arc::clone(&self.storage),
            Arc::clone(&self.p2p_node),
            Arc::clone(&self.payment_verifier),
            Arc::clone(&self.config),
            Arc::clone(&self.is_bootstrapping),
            Arc::clone(&self.send_semaphore),
            self.shutdown.clone(),
            self.detached_task_tracker.clone(),
        )));
        self.pointer_fresh_rx = Some(fresh_writes);
    }

    /// Replicate the pointers `service` stores (ADR-0016), and wire the
    /// service to this engine both ways: it hands each newly stored paid
    /// state here to be offered on, and asks here before it takes a final
    /// state (ADR-0018).
    ///
    /// Call before [`Self::start`].
    pub fn with_pointer_service(&mut self, service: &PointerService) {
        let (writes, fresh_writes) = mpsc::unbounded_channel();
        service.attach_fresh_writes(writes);
        self.with_pointers(service.store().clone(), fresh_writes);
        if let Some(replication) = &self.pointers {
            let witness: Arc<dyn FinalStateWitness> = replication.clone();
            service.attach_final_state_witness(witness);
        }
    }

    /// The pointer replication, when enabled. Tests use it to drive rounds.
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn pointer_replication(&self) -> Option<&Arc<pointer::PointerReplication>> {
        self.pointers.as_ref()
    }

    /// Test-only: pending-offer permits not currently held by an encoded
    /// fresh offer. Equals [`MAX_PENDING_FRESH_OFFERS`] when no fresh
    /// replication is in flight, which is how tests prove a burst of writes
    /// drained without leaking a permit.
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn pending_offer_permits_available(&self) -> usize {
        self.pending_offer_semaphore.available_permits()
    }

    /// Test-only: fresh offers encoded and handed to their per-peer sends
    /// since the engine was created. A write skipped because its chunk is gone
    /// is not counted.
    #[cfg(any(test, feature = "test-utils"))]
    #[must_use]
    pub fn fresh_offers_dispatched(&self) -> u64 {
        self.fresh_offers_dispatched.load(Ordering::Relaxed)
    }

    /// Test-only: take every outbound replication send permit. Until the
    /// returned permit is dropped, encoded fresh offers wait behind the send
    /// stage, so a test can fill the pending-offer budget deterministically.
    /// The send semaphore is never closed, so this returns `Some`.
    #[cfg(any(test, feature = "test-utils"))]
    pub async fn hold_replication_sends(&self) -> Option<tokio::sync::OwnedSemaphorePermit> {
        let all = u32::try_from(MAX_CONCURRENT_REPLICATION_SENDS).ok()?;
        Arc::clone(&self.send_semaphore)
            .acquire_many_owned(all)
            .await
            .ok()
    }

    /// Start all background tasks.
    ///
    /// `dht_events` must be subscribed **before** `P2PNode::start()` so that
    /// the `BootstrapComplete` event emitted during DHT bootstrap is not
    /// missed by the bootstrap-sync gate.
    pub fn start(&mut self, dht_events: tokio::sync::broadcast::Receiver<DhtNetworkEvent>) {
        if !self.task_handles.is_empty() {
            error!("ReplicationEngine::start() called while already running — ignoring");
            return;
        }
        info!("Starting replication engine");

        self.start_message_handler();
        // Takes out of service the rotted chunks that round-1 proofs report.
        self.start_corrupt_recheck_worker();
        self.start_neighbor_sync_loop();
        self.start_self_lookup_loop();
        // Audit #2 (responsible-chunk): periodic tick auditing peers for the
        // chunks they SHOULD store (responsibility + prior hint).
        self.start_audit_loop();
        // Audit #1 (storage-commitment) is gossip-triggered in the message
        // handler when a peer's commitment is ingested, not on a periodic tick.
        self.start_commitment_rotation_loop();
        self.start_retention_persist_loop();
        self.start_fetch_worker();
        self.start_verification_worker();
        self.start_bootstrap_sync(dht_events);
        self.start_fresh_replication();
        self.start_possession_check_scheduler();
        if let Some(pointers) = &self.pointers {
            self.task_handles.push(pointers.start_verification_loop());
            if let Some(writes) = self.pointer_fresh_rx.take() {
                self.task_handles.push(pointers.start_fresh_drainer(writes));
            }
        }
        // ADR-0004: deterministic first audit of commitments that backed a
        // payment (surfaced by the verifier cross-check).
        self.start_first_audit_drainer();
        // V2-623: periodic cumulative per-variant traffic accounting.
        self.start_traffic_summary_loop();
        // V2-884: read-only bootstrap state for cache/drain/connectivity triage.
        #[cfg(feature = "logging")]
        self.start_bootstrap_state_snapshot_loop();
        #[cfg(feature = "logging")]
        self.start_audit_responder_summary_loop();

        info!(
            "Replication engine started with {} background tasks",
            self.task_handles.len()
        );
    }

    /// Returns `true` if the node is still in the replication bootstrap phase.
    ///
    /// During bootstrap, audit challenges return `Bootstrapping` instead of
    /// digests, and neighbor sync responses carry `bootstrapping: true`.
    pub async fn is_bootstrapping(&self) -> bool {
        *self.is_bootstrapping.read().await
    }

    /// Wait until the replication bootstrap phase completes.
    ///
    /// Returns immediately if bootstrap has already completed. Useful for
    /// readiness probes, health checks, and test harnesses that need the
    /// node to be fully operational before proceeding.
    ///
    /// Returns `true` if bootstrap completed within the timeout, `false`
    /// if the timeout elapsed first.
    pub async fn wait_for_bootstrap_complete(&self, timeout: Duration) -> bool {
        // Register the notification future *before* checking the flag so that
        // a transition between the read and the await is not missed.
        let notified = self.bootstrap_complete_notify.notified();
        tokio::pin!(notified);
        notified.as_mut().enable();

        if !*self.is_bootstrapping.read().await {
            return true;
        }

        tokio::time::timeout(timeout, notified).await.is_ok()
    }

    /// Cancel all background tasks and wait for them to terminate.
    ///
    /// This must be awaited before dropping the engine when the caller needs
    /// the `Arc<ChunkStore>` references held by background tasks to be
    /// released (e.g. before reopening the same store).
    ///
    /// When this returns, no engine-spawned task still holds
    /// `Arc<ChunkStore>` or `Arc<PaidList>`, and no blocking storage operation
    /// (read or write, against either the chunk store or the paid-list LMDB
    /// environment) is still running.  Engine tasks race their work against
    /// the shutdown token; a dropped future may leave a `spawn_blocking`
    /// operation running detached, so this method additionally waits
    /// for both storage layers to go quiescent before returning.
    pub async fn shutdown(&mut self) {
        self.shutdown.cancel();
        for (i, mut handle) in self.task_handles.drain(..).enumerate() {
            match tokio::time::timeout(SHUTDOWN_TASK_DRAIN_TIMEOUT, &mut handle).await {
                Ok(Ok(())) => {}
                Ok(Err(e)) if e.is_cancelled() => {}
                Ok(Err(e)) => warn!("Replication task {i} panicked during shutdown: {e}"),
                Err(_) => {
                    warn!(
                        "Replication task {i} did not stop within {}s, aborting",
                        SHUTDOWN_TASK_DRAIN_TIMEOUT.as_secs()
                    );
                    handle.abort();
                    // `abort` only requests cancellation. Await the handle so
                    // synchronous sections finish and the task drops every
                    // storage/P2P clone before we claim producer quiescence.
                    match handle.await {
                        Ok(()) => {}
                        Err(e) if e.is_cancelled() => {}
                        Err(e) => warn!("Replication task {i} panicked after abort: {e}"),
                    }
                }
            }
        }

        // Close the responder worker pools. Detached tasks still queued for a
        // worker take the `Err` arm of `acquire_owned()` and exit immediately
        // rather than waiting for a slot that will never free up — without
        // this, work admitted just before shutdown would queue behind the last
        // in-flight batch and hold the drain below open for its full duration.
        self.fresh_offer_worker_semaphore.close();
        self.paid_notify_worker_semaphore.close();
        self.fetch_responder_worker_semaphore.close();
        self.verification_responder_worker_semaphore.close();
        self.neighbor_sync_responder_worker_semaphore.close();

        // All producers have stopped, so close and drain their detached work.
        // A started storage operation must run to completion: dropping an async
        // waiter does not cancel `spawn_blocking`, and would let shutdown return
        // while a blocking storage operation is still running.
        //
        // Deliberately unbounded: every worker has to release its
        // `Arc<ChunkStore>` before the caller may reopen the store, whose lock
        // admits one process at a time, and a timeout here could return with one
        // still held.
        // What makes that safe is that every detached task is now guaranteed to
        // finish — the pools above are closed, stale work is shed at dequeue,
        // and the one genuinely unbounded await (payment verification) races
        // `self.shutdown` via `verify_payment_until_shutdown`.
        self.detached_task_tracker.close();
        self.detached_task_tracker.wait().await;

        // Every producer is gone, but a select! racing the shutdown token may
        // have dropped a future while it awaited a storage `spawn_blocking` op
        // (fetch `storage.put`, prune `storage.delete` /
        // `paid_list.remove_batch`, verification `paid_list.insert`).  The
        // detached blocking closure owns a cloned `Env`; wait for both
        // environments to go quiescent — bounded by one in-flight transaction
        // per op — so the caller may reopen them.
        self.storage.wait_idle().await;
        self.paid_list.wait_idle().await;
    }

    /// Trigger an early neighbor sync round.
    ///
    /// Useful after topology changes (new nodes joining, network heal after
    /// partition) when the caller wants replication to converge faster than
    /// the regular 10-20 minute cadence.
    pub fn trigger_neighbor_sync(&self) {
        self.sync_trigger.notify_one();
    }

    /// Execute fresh replication for a newly stored record: announce it, then
    /// send its offers and schedule the delayed possession check for the
    /// responsible close-group peers (ADR-0003), as the PUT path does.
    ///
    /// Unlike the PUT path, this waits for a pending-offer permit before
    /// returning, and it offers the caller's bytes rather than reading them
    /// back from storage.
    pub async fn replicate_fresh(&self, key: &XorName, data: &[u8], proof_of_payment: &[u8]) {
        fresh::announce_paid_write(
            key,
            proof_of_payment,
            &self.paid_list,
            &self.p2p_node,
            &self.config,
        )
        .await;
        // Never closed, so this cannot fail; the arm keeps the call panic-free.
        let Ok(pending_offer) = Arc::clone(&self.pending_offer_semaphore)
            .acquire_owned()
            .await
        else {
            return;
        };
        fresh::send_fresh_offers(
            &self.fresh_offer_context(),
            key,
            data.to_vec(),
            proof_of_payment.to_vec(),
            pending_offer,
        )
        .await;
    }

    /// Handles the offer dispatcher and the direct entry point share.
    fn fresh_offer_context(&self) -> fresh::FreshOfferContext {
        fresh::FreshOfferContext {
            p2p_node: Arc::clone(&self.p2p_node),
            config: Arc::clone(&self.config),
            send_semaphore: Arc::clone(&self.send_semaphore),
            possession_check_tx: self.possession_check_tx.clone(),
            dispatched: Arc::clone(&self.fresh_offers_dispatched),
        }
    }

    // =======================================================================
    // Background task launchers
    // =======================================================================

    /// Spawn fresh replication's two stages, joined by an unbounded FIFO of
    /// key + proof events: the drainer, which announces every write at arrival
    /// rate, and the offer dispatcher, the only permit-gated stage.
    fn start_fresh_replication(&mut self) {
        let Some(writes) = self.fresh_write_rx.take() else {
            return;
        };
        let (offer_tx, offer_rx) = mpsc::unbounded_channel();
        self.start_fresh_write_drainer(writes, offer_tx.clone());
        self.start_fresh_offer_dispatcher(offer_rx, offer_tx);
    }

    /// Spawn a task that drains the fresh-write channel: it announces each
    /// newly-stored chunk and hands it to the offer dispatcher.
    fn start_fresh_write_drainer(
        &mut self,
        mut rx: mpsc::UnboundedReceiver<fresh::FreshWriteEvent>,
        offer_tx: mpsc::UnboundedSender<fresh::FreshOfferEvent>,
    ) {
        let p2p = Arc::clone(&self.p2p_node);
        let paid_list = Arc::clone(&self.paid_list);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();

        let handle = tokio::spawn(async move {
            loop {
                let event = tokio::select! {
                    () = shutdown.cancelled() => break,
                    event = rx.recv() => {
                        let Some(event) = event else { break };
                        event
                    }
                };
                // Stage one never waits for chunk back-pressure: the paid-list
                // entry and PaidNotify are what let peers repair the key later,
                // so every queued write gets them at arrival rate.
                fresh::announce_paid_write(
                    &event.key,
                    &event.payment_proof,
                    &paid_list,
                    &p2p,
                    &config,
                )
                .await;
                if offer_tx
                    .send(fresh::FreshOfferEvent {
                        key: event.key,
                        payment_proof: event.payment_proof,
                        read_attempts: 0,
                    })
                    .is_err()
                {
                    break;
                }
            }
            debug!("Fresh-write drainer shut down");
        });
        self.task_handles.push(handle);
    }

    /// Spawn the fresh-offer dispatcher: the only stage of fresh replication
    /// that waits for a pending-offer permit.
    ///
    /// For each forwarded write it acquires a permit, reads the chunk back
    /// from storage and dispatches the offers. A missing chunk is skipped; a
    /// failed read is retried up to `MAX_FRESH_READ_ATTEMPTS` times, backing
    /// off over about a minute with the permit released in between, so a
    /// transient I/O fault does not lose the write's replication. The retry
    /// waits on its own task, never on the dispatcher.
    fn start_fresh_offer_dispatcher(
        &mut self,
        mut rx: mpsc::UnboundedReceiver<fresh::FreshOfferEvent>,
        offer_tx: mpsc::UnboundedSender<fresh::FreshOfferEvent>,
    ) {
        let storage = Arc::clone(&self.storage);
        let pending_offer_semaphore = Arc::clone(&self.pending_offer_semaphore);
        let ctx = self.fresh_offer_context();
        let shutdown = self.shutdown.clone();
        let retries = self.detached_task_tracker.clone();

        let handle = tokio::spawn(async move {
            loop {
                let event = tokio::select! {
                    () = shutdown.cancelled() => break,
                    event = rx.recv() => {
                        let Some(event) = event else { break };
                        event
                    }
                };
                // Wait for a pending-offer permit before touching the chunk so a
                // send backlog holds queued events, not encoded chunk buffers.
                let pending_offer = tokio::select! {
                    () = shutdown.cancelled() => break,
                    permit = Arc::clone(&pending_offer_semaphore).acquire_owned() => {
                        let Ok(permit) = permit else { break };
                        permit
                    }
                };
                let key_hex = hex::encode(event.key);
                // The same verified read the fetch path serves from. The bytes
                // were content-checked when they were stored, but they come
                // off disk now, possibly long after, and every receiver
                // charges the sender for an offer that does not hash to its
                // key. A chunk that fails verification is quarantined by this
                // read, so its retry finds it gone and skips it.
                let data = match storage.get(&event.key).await {
                    Ok(Some(data)) => data,
                    Ok(None) => {
                        debug!("Chunk {key_hex} no longer stored, skipping fresh replication");
                        continue;
                    }
                    Err(e) => {
                        // The permit is released as this iteration ends, well
                        // before any retry is due.
                        let attempts = event.read_attempts + 1;
                        if attempts >= MAX_FRESH_READ_ATTEMPTS {
                            warn!(
                                "Giving up fresh replication of {key_hex} after {attempts} failed reads: {e}"
                            );
                            continue;
                        }
                        // One warning per write: a store-wide fault fails the
                        // whole backlog, once per retry.
                        if attempts == 1 {
                            warn!(
                                "Failed to read chunk {key_hex} for fresh replication; will retry: {e}"
                            );
                        } else {
                            debug!(
                                "Failed to read chunk {key_hex} for fresh replication (attempt {attempts}): {e}"
                            );
                        }
                        // Wait on a task of its own: a read fault is often
                        // shared (exhausted descriptors), and sleeping here
                        // would hold every healthy write queued behind this
                        // one for the whole delay, once per failed read. The
                        // delay grows, so a store-wide fault spends a write's
                        // attempts over about a minute rather than seconds.
                        let retry_tx = offer_tx.clone();
                        let retry_shutdown = shutdown.clone();
                        let delay = fresh_read_retry_delay(attempts);
                        retries.spawn(async move {
                            tokio::select! {
                                () = retry_shutdown.cancelled() => {}
                                () = tokio::time::sleep(delay) => {
                                    let _ = retry_tx.send(fresh::FreshOfferEvent {
                                        read_attempts: attempts,
                                        ..event
                                    });
                                }
                            }
                        });
                        continue;
                    }
                };
                fresh::send_fresh_offers(
                    &ctx,
                    &event.key,
                    data,
                    event.payment_proof,
                    pending_offer,
                )
                .await;
            }
            debug!("Fresh-offer dispatcher shut down");
        });
        self.task_handles.push(handle);
    }

    /// Spawn the possession-check scheduler (ADR-0003).
    ///
    /// Drains scheduled possession-check events and, for each, waits a
    /// randomised 5-15 minute settle delay before probing every responsible
    /// peer for actual possession. A peer that fails to prove possession or does
    /// not answer is penalised at `AuditChallenge` severity.
    fn start_possession_check_scheduler(&mut self) {
        let Some(mut rx) = self.possession_check_rx.take() else {
            return;
        };
        let p2p = Arc::clone(&self.p2p_node);
        let storage = Arc::clone(&self.storage);
        let config = Arc::clone(&self.config);
        let sync_state = Arc::clone(&self.sync_state);
        let audit_challenge_coordinator = Arc::clone(&self.audit_challenge_coordinator);
        let shutdown = self.shutdown.clone();
        let detached_task_tracker = self.detached_task_tracker.clone();

        let handle = tokio::spawn(async move {
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    event = rx.recv() => {
                        let Some(event) = event else { break };
                        // Spawn a per-chunk delayed check so the drain loop
                        // keeps pace with the write rate. Each check sleeps the
                        // randomised settle delay, then probes every peer.
                        let p2p = Arc::clone(&p2p);
                        let storage = Arc::clone(&storage);
                        let config = Arc::clone(&config);
                        let sync_state = Arc::clone(&sync_state);
                        let audit_challenge_coordinator = Arc::clone(&audit_challenge_coordinator);
                        let shutdown = shutdown.clone();
                        let delay_min = config.possession_check_delay_min;
                        let delay_max = config.possession_check_delay_max;
                        detached_task_tracker.spawn(async move {
                            let delay = possession::random_delay(delay_min, delay_max);
                            // Race shutdown against the whole settle-then-probe
                            // sequence, not just the sleep. Once a check starts it
                            // may park on the per-target audit coordinator, which
                            // has no deadline of its own: with the settle delay
                            // inside the branch BODY the select is already resolved,
                            // so those waiters would drain only at the probe timeout
                            // (roughly `queued / per-target-limit` probes deep) while
                            // `detached_task_tracker.wait()` — deliberately unbounded
                            // for the storage contract — held shutdown open.
                            //
                            // Dropping this future mid-probe is safe and is the same
                            // shape the neighbor-sync round uses: a parked coordinator
                            // acquire releases its counted reference via
                            // `ReferenceGuard`, and a dropped storage `spawn_blocking` is
                            // covered by the storage-quiescence wait in `shutdown`.
                            tokio::select! {
                                () = shutdown.cancelled() => {}
                                () = async {
                                    tokio::time::sleep(delay).await;
                                    possession::run_possession_check(
                                        event.key,
                                        event.peers,
                                        &p2p,
                                        &storage,
                                        &config,
                                        &sync_state,
                                        &audit_challenge_coordinator,
                                        &shutdown,
                                    )
                                    .await;
                                } => {}
                            }
                        });
                    }
                }
            }
            debug!("Possession-check scheduler shut down");
        });
        self.task_handles.push(handle);
    }

    /// ADR-0004: drain monetized pins surfaced by the verifier cross-check and
    /// run a **deterministic first audit** of each — the same `run_subtree_audit`
    /// as the gossip path, under the same per-peer cooldown and concurrency
    /// caps, but with the probability lottery BYPASSED (the lottery governs
    /// re-audits only). Deduped by pin via a bounded set so a pin gets one
    /// deterministic first audit; a peer minting fresh pins faster than the
    /// cooldown forfeits the older ones' coverage, never the newest's (the
    /// channel surfaces newest pins as they are monetized).
    #[allow(clippy::too_many_lines)]
    fn start_first_audit_drainer(&mut self) {
        let Some(mut rx) = self.monetized_pin_rx.take() else {
            return;
        };
        let gossip_audit = GossipAuditTrigger {
            p2p_node: Arc::clone(&self.p2p_node),
            config: Arc::clone(&self.config),
            recent_provers: Arc::clone(&self.recent_provers),
            sync_state: Arc::clone(&self.sync_state),
            cooldown: Arc::clone(&self.audit_on_gossip_cooldown),
            detached_task_tracker: self.detached_task_tracker.clone(),
            lottery_attempts: Arc::clone(&self.gossip_lottery_attempts),
        };
        let shutdown = self.shutdown.clone();
        let observability = Arc::clone(&self.first_audit_observability);
        let self_peer = *self.p2p_node.peer_id();

        let handle = tokio::spawn(async move {
            // ADR-0004 Amendment 2 (E'): the drainer-owned first-audit
            // scheduler. Payments only NOMINATE pins; the token bucket launches
            // them at a fixed per-node rate, and durable suppression is stamped
            // only at PROMOTION (after an authoritative post-jitter answerability
            // + cooldown check), so a cancelled reservation leaves nothing behind.
            let mut scheduler = FirstAuditScheduler::new(Instant::now(), self_peer);
            // Periodic retry tick so budget/cooldown-deferred pins get retried
            // even when no new nomination arrives. `Skip` collapses a backlog.
            let mut tick = tokio::time::interval(config::FIRST_AUDIT_RETRY_INTERVAL);
            tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
            let mut last_summary = Instant::now();
            loop {
                // The reservation's jitter deadline is a wake source: if a
                // reservation is outstanding, sleep until it is due; otherwise
                // a far-future deadline effectively disables that arm and only
                // shutdown/rx/tick wake the loop. Recreated each iteration so a
                // newly reserved (earlier) deadline is honoured next turn.
                let promotion_due = scheduler
                    .reserved_ready_at()
                    .unwrap_or_else(first_audit_far_future);
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    event = rx.recv() => match event {
                        Some(e) => {
                            observability.received.fetch_add(1, Ordering::Relaxed);
                            // Pre-overflow reservation opportunity: before an
                            // arrival may displace a DIFFERENT peer, let the
                            // queue launch — a successful reservation frees the
                            // slot without eviction. One reservation can be
                            // outstanding at a time, so this consumes at most
                            // one slot per jitter window; arrivals beyond it
                            // fall through to random displacement (ADR-0004
                            // Amendment 4).
                            if scheduler.would_displace(&e) {
                                open_first_audit_reservation(
                                    &mut scheduler,
                                    &gossip_audit.cooldown,
                                    &observability,
                                )
                                .await;
                            }
                            scheduler.enqueue(e, &observability);
                            // Drain a bounded burst so a flood cannot starve the
                            // launch phase.
                            let mut drained = 1usize;
                            while drained < config::FIRST_AUDIT_DRAIN_BATCH {
                                match rx.try_recv() {
                                    Ok(e) => {
                                        observability.received.fetch_add(1, Ordering::Relaxed);
                                        if scheduler.would_displace(&e) {
                                            open_first_audit_reservation(
                                                &mut scheduler,
                                                &gossip_audit.cooldown,
                                                &observability,
                                            )
                                            .await;
                                        }
                                        scheduler.enqueue(e, &observability);
                                        drained += 1;
                                    }
                                    Err(_) => break,
                                }
                            }
                        }
                        None => break,
                    },
                    _ = tick.tick() => {}
                    () = tokio::time::sleep_until(promotion_due.into()) => {}
                }

                // 1) Promote a due reservation. Resolved after EVERY wake (not
                //    only when the timer arm wins) so a continuously-ready `rx`
                //    cannot indefinitely delay a due promotion. The authoritative
                //    answerability + cooldown check-and-stamp happens here under
                //    the shared cooldown write lock, immediately before the send.
                if let Some(reservation) = scheduler.take_due_reservation(Instant::now()) {
                    let promoted = {
                        let mut cooldown = gossip_audit.cooldown.write().await;
                        scheduler.resolve(
                            reservation,
                            SystemTime::now(),
                            Instant::now(),
                            &mut cooldown,
                            &observability,
                        )
                    };
                    if let Some((event, inflight_slot)) = promoted {
                        debug!(
                            "First-audit scheduler: audit_trigger=first_monetized outcome=launched peer={} pin={} key_count={} inflight={}",
                            event.peer, hex::encode(event.pin), event.key_count,
                            observability.inflight.load(Ordering::Relaxed)
                        );
                        let trigger = gossip_audit.clone();
                        let audit_observability = Arc::clone(&observability);
                        // Track the launch so `shutdown()`'s detached drain
                        // covers it. This task outlives the drainer loop that
                        // spawned it — the loop breaks on the shutdown token
                        // while the audit is still awaiting its response — so a
                        // bare spawn would let an in-flight subtree audit keep
                        // running, and keep its `Arc<P2PNode>`, past the point
                        // `shutdown()` claims every engine task has stopped.
                        let tracker = trigger.detached_task_tracker.clone();
                        tracker.spawn(async move {
                            // The jitter already elapsed as the reservation's
                            // timer; the slot is held for the audit's duration
                            // and released on drop (panic-safe).
                            let inflight_slot = inflight_slot;
                            let started = Instant::now();
                            let credit = storage_commitment_audit::AuditCredit {
                                recent_provers: &trigger.recent_provers,
                            };
                            let result = storage_commitment_audit::run_subtree_audit(
                                &trigger.p2p_node,
                                &trigger.config,
                                &event.peer,
                                event.pin,
                                event.key_count,
                                Some(&credit),
                            )
                            .await;
                            let outcome = first_audit_terminal_outcome(&result);
                            match outcome {
                                FirstAuditTerminalOutcome::Passed => {
                                    audit_observability.passed.fetch_add(1, Ordering::Relaxed);
                                }
                                FirstAuditTerminalOutcome::Timeout => {
                                    audit_observability
                                        .timed_out
                                        .fetch_add(1, Ordering::Relaxed);
                                }
                                FirstAuditTerminalOutcome::Failed => {
                                    audit_observability.failed.fetch_add(1, Ordering::Relaxed);
                                }
                                FirstAuditTerminalOutcome::BootstrapClaim => {
                                    audit_observability
                                        .bootstrap_claims
                                        .fetch_add(1, Ordering::Relaxed);
                                }
                                FirstAuditTerminalOutcome::Idle => {
                                    audit_observability.idle.fetch_add(1, Ordering::Relaxed);
                                }
                                FirstAuditTerminalOutcome::InsufficientKeys => {
                                    audit_observability
                                        .insufficient_keys
                                        .fetch_add(1, Ordering::Relaxed);
                                }
                            }
                            drop(inflight_slot);
                            debug!(
                                "First-audit scheduler: audit_trigger=first_monetized outcome={} peer={} pin={} key_count={} elapsed_ms={} inflight={}",
                                outcome.as_str(),
                                event.peer, hex::encode(event.pin), event.key_count,
                                started.elapsed().as_millis(),
                                audit_observability.inflight.load(Ordering::Relaxed)
                            );
                            handle_subtree_audit_result(
                                &result,
                                &trigger.p2p_node,
                                &trigger.sync_state,
                                &trigger.recent_provers,
                                &trigger.config,
                            )
                            .await;
                        });
                    }
                }

                // 2) Open the single reservation from the pending queue if none
                //    is outstanding (read-only cooldown snapshot; the
                //    authoritative stamp is at promotion). Serializing
                //    reservations preserves per-launch lane alternation and keeps
                //    at most one jitter timer live.
                open_first_audit_reservation(
                    &mut scheduler,
                    &gossip_audit.cooldown,
                    &observability,
                )
                .await;

                if last_summary.elapsed() >= config::FIRST_AUDIT_SUMMARY_INTERVAL {
                    // Token-independent hygiene: collect entries that aged past
                    // the answerability horizon while the budget gate kept the
                    // reserve scan from running, so the summary below reports
                    // only live work.
                    let swept = scheduler.sweep_expired(SystemTime::now(), &observability);
                    if swept > 0 {
                        debug!(
                            "First-audit scheduler: audit_trigger=first_monetized outcome=expired_swept count={swept} pending={}",
                            scheduler.pending_len()
                        );
                    }
                    info!(
                        "First-audit scheduler summary: audit_trigger=first_monetized ingress_dropped={} received={} queued={} coalesced={} suppressed_lower={} duplicates={} capacity_evicted={} self_target_skipped={} cooldown_deferred_attempts={} rate_deferred_attempts={} window_deduped={} launched={} passed={} timeout={} failed={} bootstrap_claims={} idle={} insufficient_keys={} outside_answerability_window={} pending={} pending_cap={} reserved={} oldest_pending_quote_age_ms={} inflight={} tokens={}",
                        FIRST_AUDIT_INGRESS_DROPPED.load(Ordering::Relaxed),
                        observability.received.load(Ordering::Relaxed),
                        observability.queued.load(Ordering::Relaxed),
                        observability.coalesced.load(Ordering::Relaxed),
                        observability.suppressed_lower.load(Ordering::Relaxed),
                        observability.duplicates.load(Ordering::Relaxed),
                        observability.capacity_evicted.load(Ordering::Relaxed),
                        observability.self_target_skipped.load(Ordering::Relaxed),
                        observability.cooldown_deferred_attempts.load(Ordering::Relaxed),
                        observability.rate_deferred_attempts.load(Ordering::Relaxed),
                        observability.window_deduped.load(Ordering::Relaxed),
                        observability.launched.load(Ordering::Relaxed),
                        observability.passed.load(Ordering::Relaxed),
                        observability.timed_out.load(Ordering::Relaxed),
                        observability.failed.load(Ordering::Relaxed),
                        observability.bootstrap_claims.load(Ordering::Relaxed),
                        observability.idle.load(Ordering::Relaxed),
                        observability.insufficient_keys.load(Ordering::Relaxed),
                        observability.outside_answerability_window.load(Ordering::Relaxed),
                        scheduler.pending_len(),
                        FIRST_AUDIT_PENDING_CAP,
                        u8::from(scheduler.has_reservation()),
                        scheduler.oldest_pending_quote_age_ms(SystemTime::now()),
                        observability.inflight.load(Ordering::Relaxed),
                        scheduler.tokens(),
                    );
                    last_summary = Instant::now();
                }
            }
            debug!("First-audit drainer shut down");
        });
        self.task_handles.push(handle);
    }

    #[allow(clippy::too_many_lines)]
    fn start_message_handler(&mut self) {
        let mut p2p_events = self.p2p_node.subscribe_events();
        let mut dht_events = self.p2p_node.dht_manager().subscribe_events();
        let p2p = Arc::clone(&self.p2p_node);
        let storage = Arc::clone(&self.storage);
        let paid_list = Arc::clone(&self.paid_list);
        let payment_verifier = Arc::clone(&self.payment_verifier);
        let queues = Arc::clone(&self.queues);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();
        let is_bootstrapping = Arc::clone(&self.is_bootstrapping);
        let bootstrap_state = Arc::clone(&self.bootstrap_state);
        let bootstrap_complete_notify = Arc::clone(&self.bootstrap_complete_notify);
        let sync_history = Arc::clone(&self.sync_history);
        let sync_cycle_epoch = Arc::clone(&self.sync_cycle_epoch);
        let repair_proofs = Arc::clone(&self.repair_proofs);
        let sync_trigger = Arc::clone(&self.sync_trigger);
        let my_commitment_state = Arc::clone(&self.commitment_state);
        let last_commitment_by_peer = Arc::clone(&self.last_commitment_by_peer);
        let ever_capable_peers = Arc::clone(&self.ever_capable_peers);
        let recent_provers = Arc::clone(&self.recent_provers);
        let sig_verify_attempts = Arc::clone(&self.sig_verify_attempts);
        let audit_on_gossip_cooldown = Arc::clone(&self.audit_on_gossip_cooldown);
        let gossip_lottery_attempts = Arc::clone(&self.gossip_lottery_attempts);
        let sync_state = Arc::clone(&self.sync_state);
        let audit_responder_semaphore = Arc::clone(&self.audit_responder_semaphore);
        let audit_responder_inflight = Arc::clone(&self.audit_responder_inflight);
        let audit_responder_metrics = Arc::clone(&self.audit_responder_metrics);
        let fetch_responder_worker_semaphore = Arc::clone(&self.fetch_responder_worker_semaphore);
        let fetch_responder_admission_semaphore =
            Arc::clone(&self.fetch_responder_admission_semaphore);
        let fetch_responder_inflight = Arc::clone(&self.fetch_responder_inflight);
        let verification_responder_worker_semaphore =
            Arc::clone(&self.verification_responder_worker_semaphore);
        let verification_responder_admission_semaphore =
            Arc::clone(&self.verification_responder_admission_semaphore);
        let verification_responder_inflight = Arc::clone(&self.verification_responder_inflight);
        let neighbor_sync_responder_worker_semaphore =
            Arc::clone(&self.neighbor_sync_responder_worker_semaphore);
        let neighbor_sync_responder_admission_semaphore =
            Arc::clone(&self.neighbor_sync_responder_admission_semaphore);
        let neighbor_sync_responder_inflight = Arc::clone(&self.neighbor_sync_responder_inflight);
        let fresh_offer_worker_semaphore = Arc::clone(&self.fresh_offer_worker_semaphore);
        let fresh_offer_admission_semaphore = Arc::clone(&self.fresh_offer_admission_semaphore);
        let fresh_offer_responder_inflight = Arc::clone(&self.fresh_offer_responder_inflight);
        let fresh_offer_in_flight = Arc::clone(&self.fresh_offer_in_flight);
        let paid_notify_worker_semaphore = Arc::clone(&self.paid_notify_worker_semaphore);
        let paid_notify_admission_semaphore = Arc::clone(&self.paid_notify_admission_semaphore);
        let paid_notify_responder_inflight = Arc::clone(&self.paid_notify_responder_inflight);
        let detached_task_tracker = self.detached_task_tracker.clone();
        let subtree_round1 = self.subtree_round1.clone();

        // ADR-0002 gossip-audit trigger: bundled state so an ingested *changed*
        // commitment can spawn a probabilistic, cooldown-gated subtree audit.
        let gossip_audit = GossipAuditTrigger {
            p2p_node: Arc::clone(&p2p),
            config: Arc::clone(&config),
            recent_provers: Arc::clone(&recent_provers),
            sync_state: Arc::clone(&sync_state),
            cooldown: Arc::clone(&audit_on_gossip_cooldown),
            detached_task_tracker: detached_task_tracker.clone(),
            lottery_attempts: Arc::clone(&gossip_lottery_attempts),
        };

        let handler_context = ReplicationMessageHandlerContext {
            p2p_node: Arc::clone(&p2p),
            storage,
            paid_list,
            payment_verifier,
            queues,
            config: Arc::clone(&config),
            is_bootstrapping,
            bootstrap_state,
            sync_history,
            sync_cycle_epoch,
            repair_proofs: Arc::clone(&repair_proofs),
            last_commitment_by_peer: Arc::clone(&last_commitment_by_peer),
            ever_capable_peers,
            sig_verify_attempts: Arc::clone(&sig_verify_attempts),
            my_commitment_state,
            gossip_audit,
            audit_responder_semaphore,
            audit_responder_inflight,
            audit_responder_metrics,
            subtree_round1,
            fetch_responder_worker_semaphore,
            fetch_responder_admission_semaphore,
            fetch_responder_inflight,
            verification_responder_worker_semaphore,
            verification_responder_admission_semaphore,
            verification_responder_inflight,
            neighbor_sync_responder_worker_semaphore,
            neighbor_sync_responder_admission_semaphore,
            neighbor_sync_responder_inflight,
            fresh_offer_worker_semaphore,
            fresh_offer_admission_semaphore,
            fresh_offer_responder_inflight,
            fresh_offer_in_flight,
            paid_notify_worker_semaphore,
            paid_notify_admission_semaphore,
            paid_notify_responder_inflight,
            pointers: self.pointers.clone(),
            shutdown: shutdown.clone(),
            detached_task_tracker,
        };

        let (replication_tx, mut replication_rx) =
            mpsc::channel::<InboundReplicationMessage>(INBOUND_REPLICATION_SERIAL_QUEUE_CAPACITY);
        let serial_context = handler_context.clone();
        let serial_shutdown = shutdown.clone();
        let serial_handle = tokio::spawn(async move {
            loop {
                tokio::select! {
                    () = serial_shutdown.cancelled() => break,
                    inbound = replication_rx.recv() => {
                        let Some(inbound) = inbound else { break };
                        let source = inbound.source;
                        match handle_replication_message(
                            &source,
                            inbound.msg,
                            &serial_context,
                            inbound.received_at,
                            inbound.rr_message_id.as_deref(),
                        )
                        .await
                        {
                            Ok(()) => {}
                            Err(e) => {
                                debug!("Replication message from {source} error: {e}");
                            }
                        }
                    }
                }
            }
            debug!("Replication non-audit serial handler shut down");
        });
        self.task_handles.push(serial_handle);

        let handle = tokio::spawn(async move {
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    event = p2p_events.recv() => {
                        let event = match event {
                            Ok(event) => event,
                            Err(error) => match handle_replication_event_recv_error(&error) {
                                ControlFlow::Continue(()) => continue,
                                ControlFlow::Break(()) => break,
                            },
                        };
                        let Some((source, payload, inbound_protocol, rr_message_id)) =
                            replication_payload_from_event(event)
                        else {
                            continue;
                        };
                        let received_at = Instant::now();
                        let msg = match ReplicationMessage::decode(&payload) {
                            Ok(msg) => msg,
                            Err(e) => {
                                debug!("Replication message from {source} decode error: {e}");
                                continue;
                            }
                        };
                        if !body_matches_protocol(&msg.body, inbound_protocol) {
                            debug!(
                                "Dropping replication body (variant {}) on protocol \
                                 {inbound_protocol}: wrong id for its family \
                                 (cross-version or misrouted)",
                                msg.body.variant_index()
                            );
                            continue;
                        }
                        if let Some(class) = audit_responder_class(&msg.body) {
                            handler_context
                                .audit_responder_metrics
                                .record_received(source, class);
                        }
                        let inbound = InboundReplicationMessage {
                            source,
                            msg,
                            rr_message_id,
                            received_at,
                        };
                        if matches!(
                            inbound.msg.body,
                            ReplicationMessageBody::AuditChallenge(_)
                                | ReplicationMessageBody::SubtreeAuditChallenge(_)
                                | ReplicationMessageBody::SubtreeSliceChallenge(_)
                        ) {
                            let source = inbound.source;
                            match handle_replication_message(
                                &source,
                                inbound.msg,
                                &handler_context,
                                inbound.received_at,
                                inbound.rr_message_id.as_deref(),
                            )
                            .await
                            {
                                Ok(()) => {}
                                Err(e) => {
                                    debug!("Replication message from {source} error: {e}");
                                }
                            }
                            continue;
                        }
                        if let Err(dropped) = try_enqueue_serial_message(&replication_tx, inbound) {
                            if dropped.audit_responder_class.is_some() {
                                handler_context
                                    .audit_responder_metrics
                                    .record_serial_queue_drop(dropped.source);
                            }
                            // Every serial-lane class has protocol recovery: syncs rerun,
                            // fetches retry, verification re-asks, and a dropped fresh
                            // offer is refilled by neighbor sync (the possession check
                            // penalises the absence but never re-offers). Responses are
                            // covered by their requester's deadline/retry path. Never run
                            // a bulk handler here: event receipt must remain non-blocking.
                            warn!(
                                message_class = dropped.message_class,
                                source = %dropped.source,
                                queue_depth = dropped.queue_depth,
                                reason = dropped.reason.as_str(),
                                "Replication serial-queue message dropped"
                            );
                        }
                    }
                    // Gap 4: Topology churn handling (Section 13).
                    //
                    // The DHT routing table emits KClosestPeersChanged when the
                    // K-closest peer set actually changes, which is the precise
                    // signal for triggering neighbor sync. This replaces the
                    // previous approach of checking every PeerConnected /
                    // PeerDisconnected event against the close group.
                    dht_event = dht_events.recv() => {
                        let dht_event = match dht_event {
                            Ok(event) => event,
                            Err(RecvError::Lagged(missed)) => {
                                // Under heavy churn the broadcast buffer can overflow
                                // and drop routing-table events — the moment
                                // convergence matters most. A dropped
                                // KClosestPeersChanged means its entrants were never
                                // queued and its departures were never pruned, so
                                // draining priority_order cannot recover either.
                                // Resync from ground truth instead: snapshot the
                                // current close-peer set, prune pending peers that
                                // left it during the lost window (retain_sync_peers,
                                // as the normal topology-change path does), and queue
                                // every member. Dedup (queue_priority_peers) and
                                // per-peer cooldown (select_next_sync_peer) drop peers
                                // already queued or recently synced, so only genuine
                                // entrants surface.
                                warn!(
                                    "Missed {missed} DHT routing events (broadcast lag); resynchronizing close-peer set for neighbor sync"
                                );
                                let self_id = *p2p.peer_id();
                                let neighbors = neighbor_sync::snapshot_close_neighbors(
                                    &p2p,
                                    &self_id,
                                    config.neighbor_sync_scope,
                                )
                                .await;
                                let neighbor_set =
                                    neighbors.iter().copied().collect::<HashSet<_>>();
                                let (requeued, sync_removals) = {
                                    let mut state = sync_state.write().await;
                                    let sync_removals =
                                        state.retain_sync_peers(&neighbor_set);
                                    let requeued = state.queue_priority_peers(neighbors);
                                    (requeued, sync_removals)
                                };
                                if sync_removals > 0 {
                                    debug!(
                                        "Resync after broadcast lag pruned {sync_removals} departed pending sync entries"
                                    );
                                }
                                if requeued > 0 {
                                    debug!(
                                        "Resync after broadcast lag queued {requeued} close peers for priority neighbor sync"
                                    );
                                    sync_trigger.notify_one();
                                }
                                continue;
                            }
                            Err(RecvError::Closed) => {
                                // A closed broadcast channel never yields again;
                                // continuing would spin the select! loop forever.
                                warn!(
                                    "DHT event stream closed on replication branch; stopping message handler"
                                );
                                break;
                            }
                        };
                        match dht_event {
                            DhtNetworkEvent::KClosestPeersChanged { old, new, .. } => {
                                let old_peers = old
                                    .iter()
                                    .take(config.neighbor_sync_scope)
                                    .copied()
                                    .collect::<HashSet<_>>();
                                let new_scoped = new
                                    .iter()
                                    .take(config.neighbor_sync_scope)
                                    .copied()
                                    .collect::<Vec<_>>();
                                let new_peers =
                                    new_scoped.iter().copied().collect::<HashSet<_>>();
                                let entrants = new_scoped
                                    .iter()
                                    .copied()
                                    .filter(|peer| !old_peers.contains(peer))
                                    .collect::<Vec<_>>();
                                let entrant_count = entrants.len();
                                let (priority_insertions, sync_removals) = {
                                    let mut state = sync_state.write().await;
                                    let sync_removals = state.retain_sync_peers(&new_peers);
                                    let priority_insertions = state.queue_priority_peers(entrants);
                                    (priority_insertions, sync_removals)
                                };
                                if priority_insertions > 0 {
                                    debug!(
                                        "K-closest peers changed, queued {priority_insertions}/{entrant_count} new close peers for priority neighbor sync and pruned {sync_removals} departed pending sync entries"
                                    );
                                } else {
                                    debug!(
                                        "K-closest peers changed, no additional close peers queued, pruned {sync_removals} departed pending sync entries, triggering early neighbor sync"
                                    );
                                }
                                sync_trigger.notify_one();
                            }
                            DhtNetworkEvent::PeerRemoved { peer_id } => {
                                sync_state.write().await.remove_peer(&peer_id);
                                repair_proofs.write().await.remove_peer(&peer_id);
                                if let Some(pointers) = &handler_context.pointers {
                                    pointers.forget_peer(&peer_id);
                                }
                                update_bootstrap_after_peer_removed(
                                    &peer_id,
                                    &handler_context.bootstrap_state,
                                    &handler_context.queues,
                                    &handler_context.is_bootstrapping,
                                    &bootstrap_complete_notify,
                                )
                                .await;
                                // v12: drop the commitment bytes and the
                                // recent-prover credit so a churn / sybil
                                // attacker cannot leave behind one
                                // StorageCommitment per identity in
                                // `last_commitment_by_peer`. Also drop the
                                // sig-verify rate-limit timestamp.
                                last_commitment_by_peer.write().await.remove(&peer_id);
                                recent_provers.write().await.forget_peer(&peer_id);
                                sig_verify_attempts.write().await.remove(&peer_id);
                                // Same for the gossip-audit cooldown (ADR-0002)
                                // and the lottery-attempt window.
                                audit_on_gossip_cooldown.write().await.remove(&peer_id);
                                gossip_lottery_attempts.write().await.remove(&peer_id);
                                // The sticky `commitment_capable` flag is
                                // preserved orthogonally via
                                // `ever_capable_peers` — even after this
                                // removal, a re-joining peer continues to
                                // be treated as v12-capable rather than
                                // legacy (§3 shield).
                            }
                            _ => {}
                        }
                    }
                }
            }
            debug!("Replication message handler shut down");
        });
        self.task_handles.push(handle);
    }

    fn start_neighbor_sync_loop(&mut self) {
        let pointers = self.pointers.clone();
        let p2p = Arc::clone(&self.p2p_node);
        let storage = Arc::clone(&self.storage);
        let paid_list = Arc::clone(&self.paid_list);
        let queues = Arc::clone(&self.queues);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();
        let sync_state = Arc::clone(&self.sync_state);
        let sync_history = Arc::clone(&self.sync_history);
        let sync_cycle_epoch = Arc::clone(&self.sync_cycle_epoch);
        let repair_proofs = Arc::clone(&self.repair_proofs);
        let is_bootstrapping = Arc::clone(&self.is_bootstrapping);
        let bootstrap_state = Arc::clone(&self.bootstrap_state);
        let sync_trigger = Arc::clone(&self.sync_trigger);
        let commitment_state = Arc::clone(&self.commitment_state);
        let last_commitment_by_peer = Arc::clone(&self.last_commitment_by_peer);
        let ever_capable_peers = Arc::clone(&self.ever_capable_peers);
        let sig_verify_attempts = Arc::clone(&self.sig_verify_attempts);
        let audit_challenge_coordinator = Arc::clone(&self.audit_challenge_coordinator);
        let detached_task_tracker = self.detached_task_tracker.clone();
        // ADR-0002: a peer's commitment also arrives on the sync RESPONSE path
        // (we initiated, they piggybacked theirs). Carry a gossip-audit trigger
        // here too so a peer that only ever answers — never initiates sync —
        // is still audited; otherwise it could fully evade auditing.
        let gossip_audit = GossipAuditTrigger {
            p2p_node: Arc::clone(&p2p),
            config: Arc::clone(&config),
            recent_provers: Arc::clone(&self.recent_provers),
            sync_state: Arc::clone(&sync_state),
            cooldown: Arc::clone(&self.audit_on_gossip_cooldown),
            detached_task_tracker,
            lottery_attempts: Arc::clone(&self.gossip_lottery_attempts),
        };

        let handle = tokio::spawn(async move {
            loop {
                // Park for the periodic tick or an explicit trigger ONLY when no
                // priority (topology-change) peers are queued. `sync_trigger` is a
                // coalescing `Notify`, so a churn burst that queues many entrants
                // produces a single wakeup; parking after draining one batch would
                // leave the rest waiting up to a full periodic tick. `priority_order`
                // is the durable record of pending work, so drain it back-to-back —
                // each round removes the peers it selects (`select_next_sync_peer`),
                // so the drain terminates once the queue empties.
                if !sync_state.read().await.has_priority_peers() {
                    let interval = config.random_neighbor_sync_interval();
                    tokio::select! {
                        () = shutdown.cancelled() => break,
                        () = tokio::time::sleep(interval) => {}
                        () = sync_trigger.notified() => {
                            debug!("Neighbor sync triggered by topology change");
                        }
                    }
                }
                // Wrap the sync round in a select so shutdown cancels
                // in-progress network operations rather than waiting for
                // the full round to complete.
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = run_neighbor_sync_round(
                        &p2p,
                        &storage,
                        &paid_list,
                        &queues,
                        &config,
                        &sync_state,
                        &sync_history,
                        &sync_cycle_epoch,
                        &repair_proofs,
                        &is_bootstrapping,
                        &bootstrap_state,
                        &commitment_state,
                        &last_commitment_by_peer,
                        &ever_capable_peers,
                        &sig_verify_attempts,
                        &audit_challenge_coordinator,
                        &gossip_audit,
                        pointers.as_ref(),
                    ) => {}
                }
            }
            debug!("Neighbor sync loop shut down");
        });
        self.task_handles.push(handle);
    }

    fn start_self_lookup_loop(&mut self) {
        let p2p = Arc::clone(&self.p2p_node);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();

        let handle = tokio::spawn(async move {
            loop {
                let interval = config.random_self_lookup_interval();
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(interval) => {
                        if let Err(e) = p2p.dht_manager().trigger_self_lookup().await {
                            debug!("Self-lookup failed: {e}");
                        }
                    }
                }
            }
            debug!("Self-lookup loop shut down");
        });
        self.task_handles.push(handle);
    }

    /// Spawn the one task that takes chunks audits found rotted out of service.
    ///
    /// One, so that the rechecks hold at most one blocking thread between them however
    /// many audits report rot; see [`ChunkStore::run_corrupt_rechecks`]. Tracked with
    /// the detached storage work rather than the engine's loops, because shutdown aborts
    /// a loop that outlasts its drain timeout and a recheck must not be dropped part-way.
    /// The worker itself stops at the shutdown token, between rechecks.
    fn start_corrupt_recheck_worker(&self) {
        let storage = Arc::clone(&self.storage);
        let shutdown = self.shutdown.clone();
        self.detached_task_tracker.spawn(async move {
            storage.run_corrupt_rechecks(&shutdown).await;
            debug!("Corrupt-chunk recheck worker shut down");
        });
    }

    /// Periodic responsible-chunk audit loop (audit #2): every
    /// [`ReplicationConfig::random_audit_tick_interval`] (~10-20 min), audit one
    /// eligible close peer for the chunks it *should* be storing (by
    /// responsibility and prior repair hint), independent of the gossip-triggered
    /// storage-commitment audit. Waits for bootstrap to drain, then runs one tick
    /// immediately and periodically thereafter.
    fn start_audit_loop(&mut self) {
        let p2p = Arc::clone(&self.p2p_node);
        let storage = Arc::clone(&self.storage);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();
        let sync_history = Arc::clone(&self.sync_history);
        let sync_cycle_epoch = Arc::clone(&self.sync_cycle_epoch);
        let repair_proofs = Arc::clone(&self.repair_proofs);
        let bootstrap_state = Arc::clone(&self.bootstrap_state);
        let is_bootstrapping = Arc::clone(&self.is_bootstrapping);
        let sync_state = Arc::clone(&self.sync_state);
        let audit_challenge_coordinator = Arc::clone(&self.audit_challenge_coordinator);

        let handle = tokio::spawn(async move {
            // Invariant 19: wait for bootstrap to drain before starting audits.
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => return,
                    () = tokio::time::sleep(
                        std::time::Duration::from_secs(BOOTSTRAP_DRAIN_CHECK_SECS)
                    ) => {
                        if bootstrap_state.read().await.is_drained() {
                            break;
                        }
                    }
                }
            }

            // Run one audit tick immediately after bootstrap drain.
            {
                let bootstrapping = *is_bootstrapping.read().await;
                let result = {
                    let history = sync_history.read().await;
                    let current_sync_epoch = *sync_cycle_epoch.read().await;
                    audit::audit_tick_with_repair_proofs(
                        &p2p,
                        &storage,
                        &config,
                        &history,
                        &repair_proofs,
                        &audit_challenge_coordinator,
                        current_sync_epoch,
                        bootstrapping,
                    )
                    .await
                };
                handle_audit_result(&result, &p2p, &sync_state, &config).await;
            }

            // Then run periodically.
            loop {
                let interval = config.random_audit_tick_interval();
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(interval) => {
                        let bootstrapping = *is_bootstrapping.read().await;
                        let result = {
                            let history = sync_history.read().await;
                            let current_sync_epoch = *sync_cycle_epoch.read().await;
                            audit::audit_tick_with_repair_proofs(
                                &p2p,
                                &storage,
                                &config,
                                &history,
                                &repair_proofs,
                                &audit_challenge_coordinator,
                                current_sync_epoch,
                                bootstrapping,
                            )
                            .await
                        };
                        handle_audit_result(&result, &p2p, &sync_state, &config).await;
                    }
                }
            }
            debug!("Audit loop shut down");
        });
        self.task_handles.push(handle);
    }

    /// Periodically rebuild + sign + rotate the responder's storage
    /// commitment.
    ///
    /// Phase 3 of the v12 storage-bound audit. Once per
    /// [`COMMITMENT_ROTATION_INTERVAL_SECS`], the responder reads the
    /// current key set, builds a Merkle tree (for content-addressed
    /// chunks `bytes_hash == key`, so no chunk re-read is needed), signs
    /// the root with the node's `MlDsaSecretKey`, and rotates the result
    /// into `commitment_state`. Old `previous` slot is dropped by the
    /// rotate (per `ResponderCommitmentState::rotate`).
    ///
    /// Skips if the key set is empty (no commitment to make) — the
    /// auditor side falls back to the legacy plain-digest path for
    /// peers that have never gossiped a commitment.
    fn start_commitment_rotation_loop(&mut self) {
        let storage = Arc::clone(&self.storage);
        let identity = Arc::clone(&self.identity);
        let commitment_state = Arc::clone(&self.commitment_state);
        let shutdown = self.shutdown.clone();
        let p2p = Arc::clone(&self.p2p_node);
        let config = Arc::clone(&self.config);
        let sync_trigger = Arc::clone(&self.sync_trigger);
        let recent_provers = Arc::clone(&self.recent_provers);
        let pointer_store = self.pointers.as_ref().map(|p| p.store().clone());

        let handle = tokio::spawn(async move {
            // Build the first commitment immediately on startup so a
            // restarted node can answer commitment-bound audits right
            // away — otherwise current() stays None for a full rotation
            // interval and audits silently fall back to legacy.
            //
            // After the first build, trigger an immediate neighbor-sync
            // round so the new commitment gossips out within seconds.
            // Without this, after a restart remote auditors keep pinning
            // the pre-restart (rotated-away) hash until their normal
            // sync cadence elapses — up to 1 h in the worst case,
            // during which time commitment-bound audits hit "unknown
            // commitment hash" -> Idle no-ops.
            // ML-DSA signatures are randomized so we cannot reproduce
            // the pre-restart hash; the only honest path to recovery
            // is fast re-gossip.
            // ADR-0004 A1: retention was reloaded in `new()` (before any task
            // spawned), so this initial rebuild no-ops when the key set is
            // unchanged — preserving the reloaded current pin; otherwise the
            // reloaded roots stay answerable as retained slots until their gossip
            // TTL lapses. Persistence is handled by the retention-persist loop.
            if let Err(e) = rebuild_and_rotate_commitment(
                &storage,
                pointer_store.as_ref(),
                &identity,
                &commitment_state,
                &p2p,
                &config,
            )
            .await
            {
                warn!("Initial commitment build failed: {e}");
            } else {
                sync_trigger.notify_one();
            }
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(
                        std::time::Duration::from_secs(COMMITMENT_ROTATION_INTERVAL_SECS)
                    ) => {
                        if let Err(e) = rebuild_and_rotate_commitment(
                            &storage,
                            pointer_store.as_ref(),
                            &identity,
                            &commitment_state,
                            &p2p,
                            &config,
                        ).await {
                            warn!("Commitment rotation failed: {e}");
                        }
                        // Piggyback a sweep of expired recent_provers
                        // entries on the rotation tick (same cadence,
                        // 1 h). is_credited_holder already honours the
                        // TTL on read, but the sweep reclaims memory
                        // for entries we'll never re-read.
                        let dropped = recent_provers.write().await.sweep_expired(
                            std::time::Instant::now()
                        );
                        if dropped > 0 {
                            debug!("recent_provers: swept {dropped} expired entries");
                        }
                    }
                }
            }
            debug!("Commitment rotation loop shut down");
        });
        self.task_handles.push(handle);
    }

    /// ADR-0004 A1: periodically flush the responder retention snapshot to disk
    /// (write-on-change) so answerability — including gossip-stamp refreshes and
    /// rotations — survives a restart. Flushes once immediately, then every
    /// `RETENTION_PERSIST_INTERVAL_SECS`, and once more on shutdown.
    fn start_retention_persist_loop(&mut self) {
        let commitment_state = Arc::clone(&self.commitment_state);
        let retention_path = self.retention_path.clone();
        let shutdown = self.shutdown.clone();
        let handle = tokio::spawn(async move {
            let mut last: Option<Vec<u8>> = None;
            persist_retention_if_changed(&commitment_state, &retention_path, &mut last).await;
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => {
                        persist_retention_if_changed(&commitment_state, &retention_path, &mut last)
                            .await;
                        break;
                    }
                    () = tokio::time::sleep(std::time::Duration::from_secs(
                        RETENTION_PERSIST_INTERVAL_SECS,
                    )) => {
                        persist_retention_if_changed(&commitment_state, &retention_path, &mut last)
                            .await;
                    }
                }
            }
            debug!("Commitment retention persist loop shut down");
        });
        self.task_handles.push(handle);
    }

    /// Periodically emit the cumulative replication traffic summaries: the
    /// per-variant line (V2-623) and the per-peer top-10 served-bytes line
    /// (V2-684). Both read process-global counter tables maintained by the
    /// encode/decode and serve choke points in [`protocol`]; needs no engine
    /// state.
    fn start_traffic_summary_loop(&mut self) {
        let shutdown = self.shutdown.clone();
        let handle = tokio::spawn(async move {
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(std::time::Duration::from_secs(
                        TRAFFIC_SUMMARY_INTERVAL_SECS,
                    )) => {
                        protocol::log_traffic_summary();
                        protocol::log_served_peers_summary();
                        protocol::log_audit_outcome_summary();
                        audit_metrics::log_responder_admission_summary();
                        // V2-834 Part D: chunk RPC, upgrade and EVM RPC attribution
                        // ride the same cadence.
                        crate::storage::traffic::log_chunk_rpc_traffic_summary();
                        crate::upgrade::traffic::log_upgrade_traffic_summary();
                        crate::payment::traffic::log_evm_rpc_summary();
                    }
                }
            }
            debug!("Replication traffic summary loop shut down");
        });
        self.task_handles.push(handle);
    }

    /// Periodically expose the state that gates bootstrap completion and prune
    /// admission. The state counts are copied before reading `is_bootstrapping`
    /// so this diagnostic path never nests async locks.
    #[cfg(feature = "logging")]
    fn start_bootstrap_state_snapshot_loop(&mut self) {
        let shutdown = self.shutdown.clone();
        let bootstrap_state = Arc::clone(&self.bootstrap_state);
        let is_bootstrapping = Arc::clone(&self.is_bootstrapping);
        let started_at = Instant::now();
        let handle = tokio::spawn(async move {
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(Duration::from_secs(
                        BOOTSTRAP_STATE_SNAPSHOT_INTERVAL_SECS,
                    )) => {
                        let (
                            drained,
                            pending_peer_requests,
                            pending_keys,
                            capacity_rejected_sources,
                        ) = {
                            let state = bootstrap_state.read().await;
                            (
                                state.drained,
                                state.pending_peer_requests,
                                state.pending_keys.len(),
                                state.capacity_rejected_sources.len(),
                            )
                        };
                        let bootstrapping = *is_bootstrapping.read().await;
                        let snapshot_state = if drained && !bootstrapping {
                            "healthy"
                        } else if drained {
                            "drained_waiting_for_bootstrap_flag"
                        } else {
                            "pending"
                        };
                        info!(
                            drained,
                            pending_peer_requests,
                            pending_keys,
                            capacity_rejected_sources,
                            is_bootstrapping = bootstrapping,
                            snapshot_state,
                            uptime_secs = started_at.elapsed().as_secs(),
                            "Replication bootstrap state snapshot"
                        );
                        if drained && !bootstrapping {
                            info!(
                                "Replication bootstrap diagnostics reached healthy state; stopping periodic snapshots"
                            );
                            break;
                        }
                    }
                }
            }
            debug!("Bootstrap state snapshot loop shut down");
        });
        self.task_handles.push(handle);
    }

    /// Periodically rank remote peers using the shared audit responder pool and
    /// report handler-only versus end-to-end latency. Windowed values make a
    /// burst visible even after a long-running node has accumulated traffic.
    #[cfg(feature = "logging")]
    fn start_audit_responder_summary_loop(&mut self) {
        let shutdown = self.shutdown.clone();
        let metrics = Arc::clone(&self.audit_responder_metrics);
        let semaphore = Arc::clone(&self.audit_responder_semaphore);
        let handle = tokio::spawn(async move {
            let mut window_started = Instant::now();
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => {
                        log_audit_responder_summary(
                            &metrics,
                            &semaphore,
                            window_started.elapsed(),
                        );
                        break;
                    }
                    () = tokio::time::sleep(config::AUDIT_RESPONDER_SUMMARY_INTERVAL) => {
                        log_audit_responder_summary(
                            &metrics,
                            &semaphore,
                            window_started.elapsed(),
                        );
                        window_started = Instant::now();
                    }
                }
            }
            debug!("Audit responder summary loop shut down");
        });
        self.task_handles.push(handle);
    }

    #[allow(clippy::too_many_lines, clippy::option_if_let_else)]
    fn start_fetch_worker(&mut self) {
        let p2p = Arc::clone(&self.p2p_node);
        let storage = Arc::clone(&self.storage);
        let queues = Arc::clone(&self.queues);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();
        let bootstrap_state = Arc::clone(&self.bootstrap_state);
        let is_bootstrapping = Arc::clone(&self.is_bootstrapping);
        let bootstrap_complete_notify = Arc::clone(&self.bootstrap_complete_notify);
        let detached_task_tracker = self.detached_task_tracker.clone();
        let concurrency = max_parallel_fetch();

        info!("Fetch worker concurrency set to {concurrency} (hardware threads)");

        let handle = tokio::spawn(async move {
            // Each in-flight future yields (key, Option<FetchOutcome>) so we
            // always recover the key — even if the inner task panics.
            let mut in_flight = FuturesUnordered::<FetchFuture>::new();

            loop {
                // Fill up to `concurrency` slots from the queue.
                {
                    let mut q = queues.write().await;
                    while in_flight.len() < concurrency {
                        let Some(candidate) = q.dequeue_fetch() else {
                            break;
                        };
                        let fetch_key = candidate.key;
                        let Some(&source) = candidate.sources.first() else {
                            warn!(
                                "Fetch candidate {} has no sources; requeueing for verification",
                                hex::encode(fetch_key)
                            );
                            let _ = q.requeue_candidate_for_verification(
                                candidate,
                                config.verification_request_timeout,
                            );
                            continue;
                        };
                        q.start_dequeued_fetch(candidate, source);

                        let p2p = Arc::clone(&p2p);
                        let storage = Arc::clone(&storage);
                        let config = Arc::clone(&config);
                        let token = shutdown.clone();
                        let tracker = detached_task_tracker.clone();
                        in_flight.push(Box::pin(async move {
                            // Tracked so shutdown() still awaits the task if
                            // this awaiter is dropped (e.g. the worker is
                            // aborted): it holds Arc<ChunkStore> and must
                            // not outlive the engine.
                            let handle = tracker.spawn(async move {
                                // Cancel-aware: abort when the engine shuts down.
                                tokio::select! {
                                    () = token.cancelled() => FetchOutcome {
                                        key: fetch_key,
                                        result: FetchResult::SourceFailed,
                                    },
                                    outcome = execute_single_fetch(
                                        p2p, storage, config, fetch_key, source,
                                    ) => outcome,
                                }
                            });
                            match handle.await {
                                Ok(outcome) => (outcome.key, Some(outcome)),
                                Err(e) => {
                                    error!(
                                        "Fetch task for {} panicked: {e}",
                                        hex::encode(fetch_key)
                                    );
                                    (fetch_key, None)
                                }
                            }
                        }));
                    }
                } // release queues write lock

                if in_flight.is_empty() {
                    // No work — wait for new items or shutdown.
                    tokio::select! {
                        () = shutdown.cancelled() => break,
                        () = tokio::time::sleep(
                            std::time::Duration::from_millis(FETCH_WORKER_POLL_MS)
                        ) => continue,
                    }
                }

                // Wait for the next fetch to complete and process the result.
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    Some((key, maybe_outcome)) = in_flight.next() => {
                        let mut q = queues.write().await;
                        let terminal = if let Some(outcome) = maybe_outcome {
                            match apply_fetch_result(
                                &mut q,
                                &key,
                                &outcome.result,
                                config.verification_request_timeout,
                            ) {
                                FetchFollowUp::Terminal => true,
                                FetchFollowUp::RequeuedForVerification => false,
                                FetchFollowUp::RetryFrom(next_peer) => {
                                    // Spawn a new fetch task for the next source.
                                    let p2p = Arc::clone(&p2p);
                                    let storage = Arc::clone(&storage);
                                    let config = Arc::clone(&config);
                                    let token = shutdown.clone();
                                    let tracker = detached_task_tracker.clone();
                                    let fetch_key = key;
                                    in_flight.push(Box::pin(async move {
                                        // Tracked for the same reason as the
                                        // initial fetch spawn above.
                                        let handle = tracker.spawn(async move {
                                            tokio::select! {
                                                () = token.cancelled() => FetchOutcome {
                                                    key: fetch_key,
                                                    result: FetchResult::SourceFailed,
                                                },
                                                outcome = execute_single_fetch(
                                                    p2p, storage, config, fetch_key, next_peer,
                                                ) => outcome,
                                            }
                                        });
                                        match handle.await {
                                            Ok(outcome) => (outcome.key, Some(outcome)),
                                            Err(e) => {
                                                error!(
                                                    "Fetch task for {} panicked: {e}",
                                                    hex::encode(fetch_key)
                                                );
                                                (fetch_key, None)
                                            }
                                        }
                                    }));
                                    false
                                }
                            }
                        } else {
                            // Task panicked — retry verification when this was
                            // a verified repair, otherwise reclaim the slot.
                            !q.requeue_fetch_for_verification(
                                &key,
                                config.verification_request_timeout,
                            )
                        };

                        // Shrink bootstrap pending set on terminal exit.
                        if terminal {
                            drop(q); // release queues lock before acquiring bootstrap_state
                            if !bootstrap_state.read().await.is_drained() {
                                bootstrap_state.write().await.remove_key(&key);
                                let q = queues.read().await;
                                if bootstrap::check_bootstrap_drained(
                                    &bootstrap_state,
                                    &q,
                                )
                                .await
                                {
                                    complete_bootstrap(
                                        &is_bootstrapping,
                                        &bootstrap_complete_notify,
                                    ).await;
                                }
                            }
                        }
                    }
                }
            }

            // Cancel and drain remaining in-flight fetches on shutdown.
            // The CancellationToken is already cancelled by this point, so
            // spawned tasks will see cancellation via their select! branches.
            while in_flight.next().await.is_some() {}
            debug!("Fetch worker shut down");
        });
        self.task_handles.push(handle);
    }

    fn start_verification_worker(&mut self) {
        let p2p = Arc::clone(&self.p2p_node);
        let storage = Arc::clone(&self.storage);
        let queues = Arc::clone(&self.queues);
        let paid_list = Arc::clone(&self.paid_list);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();
        let bootstrap_state = Arc::clone(&self.bootstrap_state);
        let is_bootstrapping = Arc::clone(&self.is_bootstrapping);
        let bootstrap_complete_notify = Arc::clone(&self.bootstrap_complete_notify);
        let last_commitment_by_peer = Arc::clone(&self.last_commitment_by_peer);
        let ever_capable_peers = Arc::clone(&self.ever_capable_peers);
        let recent_provers = Arc::clone(&self.recent_provers);

        let handle = tokio::spawn(async move {
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(
                        std::time::Duration::from_millis(VERIFICATION_WORKER_POLL_MS)
                    ) => {
                        let ctx = VerificationCycleContext {
                            p2p_node: &p2p,
                            paid_list: &paid_list,
                            storage: &storage,
                            queues: &queues,
                            config: &config,
                            bootstrap_state: &bootstrap_state,
                            is_bootstrapping: &is_bootstrapping,
                            bootstrap_complete_notify: &bootstrap_complete_notify,
                            last_commitment_by_peer: &last_commitment_by_peer,
                            ever_capable_peers: &ever_capable_peers,
                            recent_provers: &recent_provers,
                        };
                        run_verification_cycle(ctx).await;
                    }
                }
            }
            debug!("Verification worker shut down");
        });
        self.task_handles.push(handle);
    }

    /// Gap 3: Run a one-shot bootstrap sync on startup.
    ///
    /// Waits for saorsa-core to emit `DhtNetworkEvent::BootstrapComplete`
    /// (indicating the routing table is populated) before snapshotting
    /// close neighbors. Falls back after a timeout so bootstrap nodes
    /// (which have no peers and therefore never receive the event) still
    /// proceed.
    ///
    /// After the gate, finds close neighbors, syncs with each in
    /// round-robin batches, admits returned hints into the verification
    /// pipeline, and tracks discovered keys for bootstrap drain detection.
    #[allow(clippy::too_many_lines)]
    fn start_bootstrap_sync(
        &mut self,
        dht_events: tokio::sync::broadcast::Receiver<DhtNetworkEvent>,
    ) {
        let p2p = Arc::clone(&self.p2p_node);
        let storage = Arc::clone(&self.storage);
        let paid_list = Arc::clone(&self.paid_list);
        let queues = Arc::clone(&self.queues);
        let config = Arc::clone(&self.config);
        let shutdown = self.shutdown.clone();
        let is_bootstrapping = Arc::clone(&self.is_bootstrapping);
        let bootstrap_state = Arc::clone(&self.bootstrap_state);
        let bootstrap_complete_notify = Arc::clone(&self.bootstrap_complete_notify);
        let sync_cycle_epoch = Arc::clone(&self.sync_cycle_epoch);
        let repair_proofs = Arc::clone(&self.repair_proofs);
        let my_commitment_state = Arc::clone(&self.commitment_state);
        let last_commitment_by_peer = Arc::clone(&self.last_commitment_by_peer);
        let ever_capable_peers = Arc::clone(&self.ever_capable_peers);
        let sig_verify_attempts = Arc::clone(&self.sig_verify_attempts);

        let handle = tokio::spawn(async move {
            // Wait for DHT bootstrap to complete before snapshotting
            // neighbors. The routing table is empty until saorsa-core
            // finishes its FIND_NODE rounds and bucket refreshes.
            let gate = bootstrap::wait_for_bootstrap_complete(
                dht_events,
                config.bootstrap_complete_timeout_secs,
                &shutdown,
            )
            .await;

            if gate == bootstrap::BootstrapGateResult::Shutdown {
                return;
            }

            let self_id = *p2p.peer_id();
            let neighbors =
                neighbor_sync::snapshot_close_neighbors(&p2p, &self_id, config.neighbor_sync_scope)
                    .await;

            if neighbors.is_empty() {
                info!("Bootstrap sync: no close neighbors found, marking drained");
                bootstrap::mark_bootstrap_drained(&bootstrap_state).await;
                complete_bootstrap(&is_bootstrapping, &bootstrap_complete_notify).await;
                return;
            }

            let neighbor_count = neighbors.len();
            info!("Bootstrap sync: syncing with {neighbor_count} close neighbors");

            // Process neighbors in batches of NEIGHBOR_SYNC_PEER_COUNT.
            for batch in neighbors.chunks(config.neighbor_sync_peer_count) {
                if shutdown.is_cancelled() {
                    break;
                }

                let mut hints_by_peer = neighbor_sync::build_sync_hints_for_peers(
                    batch,
                    &storage,
                    &paid_list,
                    &p2p,
                    config.close_group_size,
                    config.paid_list_close_group_size,
                )
                .await;

                // Keep the batch outstanding until every response has been
                // admitted and bootstrap accounting is updated. The verification
                // worker uses this counter as a batch barrier, so it cannot race
                // the source aggregation below.
                bootstrap::increment_pending_requests(&bootstrap_state, batch.len()).await;
                let bootstrapping = *is_bootstrapping.read().await;

                let sync_futures = batch.iter().map(|peer| {
                    let peer = *peer;
                    let hints = hints_by_peer.remove(&peer).unwrap_or_default();
                    // Atomically snapshot + mark-gossiped for each emitted
                    // bootstrap request so we remain answerable for it.
                    let commitment = my_commitment_state
                        .current_for_gossip()
                        .map(|binding| binding.commitment().clone());
                    let p2p = &p2p;
                    let config = &config;
                    async move {
                        let outcome = neighbor_sync::sync_with_peer_with_hints(
                            &peer,
                            p2p,
                            config,
                            bootstrapping,
                            hints,
                            commitment,
                        )
                        .await;
                        (peer, outcome)
                    }
                });
                let completed = join_all(sync_futures).await;

                // Process response metadata before exposing any of this batch's
                // hints to verification.
                for (peer, outcome) in &completed {
                    if let Some(outcome) = outcome {
                        // Ingest the peer's piggybacked commitment from the
                        // response (same verification as the request path).
                        // Bootstrap is the FIRST gossip we receive from most
                        // peers, so this populates last_commitment_by_peer.
                        //
                        // We intentionally do NOT trigger a gossip-audit here:
                        // during bootstrap this node may itself still be
                        // bootstrapping (audits are gated on that), and the
                        // close-group/RT view is not yet stable. The peer is
                        // audited on the first STEADY-STATE neighbor-sync round
                        // after bootstrap drains (request + response paths both
                        // trigger), which is within one sync cycle — so caching
                        // the commitment here is sufficient and there is no
                        // coverage gap (ADR-0002).
                        ingest_peer_commitment(
                            peer,
                            outcome.response.commitment.as_ref(),
                            &p2p,
                            &last_commitment_by_peer,
                            &ever_capable_peers,
                            &sig_verify_attempts,
                        )
                        .await; // sig_verify_attempts in scope from line ~1080

                        if !outcome.response.bootstrapping {
                            record_sent_replica_hints(
                                peer,
                                &outcome.sent_replica_hints,
                                &repair_proofs,
                                &sync_cycle_epoch,
                            )
                            .await;
                        }
                    }
                }

                let pending_keys: HashSet<XorName> = {
                    let q = queues.read().await;
                    q.pending_keys().into_iter().collect()
                };
                let admission_futures = completed.iter().map(async |(_, outcome)| match outcome {
                    Some(outcome) if !outcome.response.bootstrapping => Some(
                        admission::admit_hints(
                            &self_id,
                            &outcome.response.replica_hints,
                            &outcome.response.paid_hints,
                            &p2p,
                            &config,
                            &storage,
                            &paid_list,
                            &pending_keys,
                        )
                        .await,
                    ),
                    _ => None,
                });
                let admitted = join_all(admission_futures).await;

                // Queue every peer's admitted hints under one write lock. Once
                // released, source-count ordering sees the complete batch.
                let (batch_outcomes, batch_discovered) = {
                    let mut q = queues.write().await;
                    let outcomes = completed
                        .iter()
                        .zip(admitted)
                        .filter_map(|((peer, _), admitted)| {
                            admitted.map(|admitted| {
                                (
                                    *peer,
                                    queue_admitted_hints(peer, admitted, &storage, &mut q),
                                )
                            })
                        })
                        .collect::<Vec<_>>();
                    let live_discovered = outcomes
                        .iter()
                        .flat_map(|(_, outcome)| outcome.discovered.iter().copied())
                        .filter(|key| q.contains_key(key))
                        .collect::<HashSet<_>>();
                    (outcomes, live_discovered)
                };

                publish_bootstrap_admission_outcomes(
                    &bootstrap_state,
                    &batch_outcomes,
                    &batch_discovered,
                )
                .await;

                bootstrap::decrement_pending_requests(&bootstrap_state, batch.len()).await;
            }

            // Check drain condition.
            {
                let q = queues.read().await;
                if bootstrap::check_bootstrap_drained(&bootstrap_state, &q).await {
                    complete_bootstrap(&is_bootstrapping, &bootstrap_complete_notify).await;
                }
            }

            info!("Bootstrap sync completed");
        });
        self.task_handles.push(handle);
    }
}

// ===========================================================================
// Free functions for background tasks
// ===========================================================================

#[cfg(feature = "logging")]
fn log_audit_responder_summary(
    metrics: &AuditResponderMetrics,
    semaphore: &Semaphore,
    window: Duration,
) {
    let snapshot = metrics.take_snapshot();
    if snapshot.origins.is_empty() {
        return;
    }

    let total = snapshot.total;
    let [digest_received, subtree_received, byte_received, commitment_pin_received] =
        total.received_by_class;
    let global_inflight =
        MAX_CONCURRENT_AUDIT_RESPONSES.saturating_sub(semaphore.available_permits());
    info!(
        target: "ant_node::replication::audit_responder",
        window_ms = window.as_millis(),
        source_count = snapshot.origins.len(),
        received = total.received(),
        digest_received,
        subtree_received,
        byte_received,
        commitment_pin_received,
        admitted = total.admitted,
        global_pool_drops = total.global_pool_drops,
        per_peer_cap_drops = total.per_peer_cap_drops,
        serial_queue_drops = total.serial_queue_drops,
        completed = total.completed,
        send_failures = total.send_failures,
        processing_avg_ms = total.processing_avg_ms(),
        processing_max_ms = total.processing_max_ms,
        total_avg_ms = total.total_avg_ms(),
        total_max_ms = total.total_max_ms,
        global_inflight,
        global_limit = MAX_CONCURRENT_AUDIT_RESPONSES,
        peak_global_inflight = total.peak_global_inflight,
        "Audit responder summary"
    );

    for (index, origin) in snapshot
        .origins
        .iter()
        .take(config::AUDIT_RESPONDER_TOP_ORIGINS)
        .enumerate()
    {
        let Some(source) = origin.source else {
            continue;
        };
        let [digest_received, subtree_received, byte_received, commitment_pin_received] =
            origin.received_by_class;
        info!(
            target: "ant_node::replication::audit_responder",
            rank = index + 1,
            source = %source,
            received = origin.received(),
            digest_received,
            subtree_received,
            byte_received,
            commitment_pin_received,
            admitted = origin.admitted,
            global_pool_drops = origin.global_pool_drops,
            per_peer_cap_drops = origin.per_peer_cap_drops,
            serial_queue_drops = origin.serial_queue_drops,
            completed = origin.completed,
            send_failures = origin.send_failures,
            processing_avg_ms = origin.processing_avg_ms(),
            processing_max_ms = origin.processing_max_ms,
            total_avg_ms = origin.total_avg_ms(),
            total_max_ms = origin.total_max_ms,
            peak_global_inflight = origin.peak_global_inflight,
            peak_peer_inflight = origin.peak_peer_inflight,
            "Audit responder top origin"
        );
    }
}

/// Which ceiling rejected an audit-responder admission attempt.
///
/// Stable, machine-readable so a production log-scrape can bucket drops by
/// cause. A 24 h node log collapsed 117 dropped responsible replies into a
/// single opaque "capacity reached" line; this splits the two distinct causes
/// so the next such investigation can tell a global-pool exhaustion (the whole
/// node is saturated) from a per-peer cap hit (one source is self-throttling)
/// without re-instrumenting.
///
/// This branch runs a SINGLE shared audit-responder pool for all challenge
/// kinds (responsible / subtree / byte), so there is no separate slow/fast
/// pool to distinguish here — the `kind=` log field already separates the
/// responsible (fast-path) challenge from the heavier subtree/byte ones. If a
/// dedicated slow pool is later split out, add its variants here.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ResponderRejectReason {
    /// The global [`MAX_CONCURRENT_AUDIT_RESPONSES`] semaphore had no permit
    /// free; the per-peer cap was not the binding constraint.
    GlobalPoolFull,
    /// `source` already held its [`MAX_AUDIT_RESPONSES_PER_PEER`] in-flight
    /// share, so the global permit was never attempted.
    PerPeerCapFull,
}

impl ResponderRejectReason {
    /// Stable token emitted as `reason=<token>` in drop logs. Keep these values
    /// frozen — production log tooling greps for them.
    fn as_str(self) -> &'static str {
        match self {
            Self::GlobalPoolFull => "global_pool_full",
            Self::PerPeerCapFull => "per_peer_cap_full",
        }
    }
}

impl From<ResponderRejectReason> for AuditResponderDropReason {
    fn from(reason: ResponderRejectReason) -> Self {
        match reason {
            ResponderRejectReason::GlobalPoolFull => Self::GlobalPoolFull,
            ResponderRejectReason::PerPeerCapFull => Self::PerPeerCapFull,
        }
    }
}

impl From<ResponderRejectReason> for ResponderAdmissionCeiling {
    fn from(reason: ResponderRejectReason) -> Self {
        match reason {
            ResponderRejectReason::GlobalPoolFull => Self::GlobalPool,
            ResponderRejectReason::PerPeerCapFull => Self::PerPeerShare,
        }
    }
}

/// Cumulative fresh-offer admission refusals in this process.
///
/// **Expected value in healthy operation is zero.** A node that refuses a
/// legitimate fresh offer does not merely shed load: the sender transmits
/// offers one-way and never reads the refusal, so the chunk's absence
/// resurfaces at the sender's delayed possession check and is charged to the
/// refusing node at audit severity (ADR-0003). Reaching this ceiling therefore
/// means the node is either under-provisioned for its offered load or its
/// admission share is mis-sized — and is accruing unearned trust damage either
/// way. Operators should alarm on any sustained non-zero rate.
///
/// Process-global rather than per-engine, which is exactly right in production
/// (one node per process) and adequate in multi-node test harnesses, where the
/// useful assertion is that *no* node in the fleet refused.
#[must_use]
pub fn fresh_offer_admission_refusals() -> u64 {
    audit_metrics::responder_admission_drops(ReplicationResponderClass::FreshOffer)
}

/// Cumulative paid-list notification admission refusals in this process.
///
/// As with [`fresh_offer_admission_refusals`], zero is the expected value.
/// `PaidNotify` is one-way with no retry, so a refusal discards durable
/// paid-list evidence outright rather than pushing work back onto a requester.
#[must_use]
pub fn paid_notify_admission_refusals() -> u64 {
    audit_metrics::responder_admission_drops(ReplicationResponderClass::PaidNotify)
}

/// Fresh-offer refusals caused by the class-wide pool being exhausted.
///
/// Split from [`fresh_offer_refusals_per_peer_share`] because the two indict
/// different things: this one says the node was saturated across all sources
/// and is genuinely under-provisioned for its offered load.
#[must_use]
pub fn fresh_offer_refusals_global_pool() -> u64 {
    audit_metrics::responder_admission_drops_by_ceiling(
        ReplicationResponderClass::FreshOffer,
        ResponderAdmissionCeiling::GlobalPool,
    )
}

/// Fresh-offer refusals caused by one source exhausting its per-peer share.
///
/// For fresh offers this usually indicts the *share's size* rather than the
/// sender: the legitimate traffic pattern is bulk from a single peer — the one
/// node handling a client's PUT and fanning it out — so a share sized for
/// request/response traffic will refuse ordinary uploads.
#[must_use]
pub fn fresh_offer_refusals_per_peer_share() -> u64 {
    audit_metrics::responder_admission_drops_by_ceiling(
        ReplicationResponderClass::FreshOffer,
        ResponderAdmissionCeiling::PerPeerShare,
    )
}

/// Why an audit-responder admission attempt failed, with the decision-time
/// capacity counters that let a drop be logged with full context.
///
/// `global_inflight`/`peer_inflight` are best-effort snapshots taken as the
/// decision was made (the two ceilings are read under different locks, so they
/// are not a single atomic view), but they are exact enough to tell a saturated
/// node from a single self-throttling flooder.
#[derive(Debug, Clone, Copy)]
struct ResponderAdmissionFailure {
    reason: ResponderRejectReason,
    /// Global permits in use across the whole engine at decision time.
    global_inflight: usize,
    /// Configured global ceiling ([`MAX_CONCURRENT_AUDIT_RESPONSES`]).
    global_limit: usize,
    /// In-flight audit responders already held for `source` at decision time.
    peer_inflight: u32,
    /// Configured per-peer ceiling ([`MAX_AUDIT_RESPONSES_PER_PEER`]).
    peer_limit: u32,
}

impl fmt::Display for ResponderAdmissionFailure {
    /// Renders the stable `reason=... global_inflight=... global_limit=...
    /// peer_inflight=... peer_limit=...` suffix appended to every drop log.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "reason={} global_inflight={} global_limit={} peer_inflight={} peer_limit={}",
            self.reason.as_str(),
            self.global_inflight,
            self.global_limit,
            self.peer_inflight,
            self.peer_limit,
        )
    }
}

/// RAII admission for one audit-responder task: holds the GLOBAL permit and,
/// on drop, decrements the PER-PEER in-flight count. Moving this into the
/// spawned task ties both bounds to the task's exact lifetime — no manual
/// decrement to forget on an early return or panic.
struct ResponderGuard {
    _permit: tokio::sync::OwnedSemaphorePermit,
    _peer_slot: PeerResponderSlot,
    global_inflight: usize,
    peer_inflight: u32,
}

type AuditResponderGuard = ResponderGuard;
type AuditResponderAdmissionFailure = ResponderAdmissionFailure;

/// Provisional or admitted per-peer responder slot.
///
/// The slot is claimed before the global permit is attempted. Keeping that
/// claim under RAII means cancellation while rolling a failed admission back
/// cannot strand a peer at its cap. Once admission succeeds this guard moves
/// into [`ResponderGuard`] and remains held for the worker's exact lifetime.
struct PeerResponderSlot {
    inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    peer: PeerId,
}

#[derive(Clone)]
struct ReplicationMessageHandlerContext {
    p2p_node: Arc<P2PNode>,
    storage: Arc<ChunkStore>,
    paid_list: Arc<PaidList>,
    payment_verifier: Arc<PaymentVerifier>,
    queues: Arc<RwLock<ReplicationQueues>>,
    config: Arc<ReplicationConfig>,
    is_bootstrapping: Arc<RwLock<bool>>,
    bootstrap_state: Arc<RwLock<BootstrapState>>,
    sync_history: Arc<RwLock<HashMap<PeerId, PeerSyncRecord>>>,
    sync_cycle_epoch: Arc<RwLock<u64>>,
    repair_proofs: Arc<RwLock<RepairProofs>>,
    last_commitment_by_peer: Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>>,
    ever_capable_peers: Arc<RwLock<HashSet<PeerId>>>,
    sig_verify_attempts: Arc<RwLock<HashMap<PeerId, Instant>>>,
    my_commitment_state: Arc<ResponderCommitmentState>,
    gossip_audit: GossipAuditTrigger,
    audit_responder_semaphore: Arc<Semaphore>,
    audit_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    audit_responder_metrics: Arc<AuditResponderMetrics>,
    subtree_round1: SubtreeRound1Limiter,
    fetch_responder_worker_semaphore: Arc<Semaphore>,
    fetch_responder_admission_semaphore: Arc<Semaphore>,
    fetch_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    verification_responder_worker_semaphore: Arc<Semaphore>,
    verification_responder_admission_semaphore: Arc<Semaphore>,
    verification_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    neighbor_sync_responder_worker_semaphore: Arc<Semaphore>,
    neighbor_sync_responder_admission_semaphore: Arc<Semaphore>,
    neighbor_sync_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    fresh_offer_worker_semaphore: Arc<Semaphore>,
    fresh_offer_admission_semaphore: Arc<Semaphore>,
    fresh_offer_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    fresh_offer_in_flight: FreshOfferInFlight,
    paid_notify_worker_semaphore: Arc<Semaphore>,
    paid_notify_admission_semaphore: Arc<Semaphore>,
    paid_notify_responder_inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    /// Pointer replication, when this node stores pointers.
    pointers: Option<Arc<pointer::PointerReplication>>,
    /// The engine's shutdown token, for detached responder work.
    ///
    /// Workers on [`Self::detached_task_tracker`] race this around their
    /// *network* phase only — never around a storage `spawn_blocking` await,
    /// where dropping the awaiter would detach a live transaction. This is
    /// what lets `shutdown()` keep its unbounded `tracker.wait()` and still
    /// terminate: the wait stays safe because it is now guaranteed finite.
    shutdown: CancellationToken,
    /// Shared tracker for detached work so `shutdown()` can await release of
    /// storage and P2P resources after all producer tasks have stopped.
    detached_task_tracker: TaskTracker,
}

struct InboundReplicationMessage {
    source: PeerId,
    msg: ReplicationMessage,
    rr_message_id: Option<String>,
    received_at: Instant,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SerialQueueDropReason {
    Full,
    Closed,
}

#[cfg(feature = "logging")]
impl SerialQueueDropReason {
    const fn as_str(self) -> &'static str {
        match self {
            Self::Full => "full",
            Self::Closed => "closed",
        }
    }
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(not(any(feature = "logging", test)), allow(dead_code))]
struct SerialQueueDrop {
    source: PeerId,
    message_class: &'static str,
    audit_responder_class: Option<AuditResponderClass>,
    queue_depth: usize,
    reason: SerialQueueDropReason,
}

fn try_enqueue_serial_message(
    sender: &mpsc::Sender<InboundReplicationMessage>,
    inbound: InboundReplicationMessage,
) -> std::result::Result<(), SerialQueueDrop> {
    let source = inbound.source;
    let message_class = replication_message_class(&inbound.msg.body);
    let audit_responder_class = audit_responder_class(&inbound.msg.body);
    let queue_depth = sender.max_capacity().saturating_sub(sender.capacity());
    sender.try_send(inbound).map_err(|error| {
        let (reason, dropped) = match error {
            mpsc::error::TrySendError::Full(dropped) => (SerialQueueDropReason::Full, dropped),
            mpsc::error::TrySendError::Closed(dropped) => (SerialQueueDropReason::Closed, dropped),
        };
        audit_metrics::record_serial_queue_overflow_drop(&dropped.msg.body);
        SerialQueueDrop {
            source,
            message_class,
            audit_responder_class,
            queue_depth,
            reason,
        }
    })
}

const fn replication_message_class(body: &ReplicationMessageBody) -> &'static str {
    match body {
        ReplicationMessageBody::FreshReplicationOffer(_) => "fresh_offer",
        ReplicationMessageBody::FreshReplicationResponse(_) => "fresh_response",
        ReplicationMessageBody::PaidNotify(_) => "paid_notify",
        ReplicationMessageBody::NeighborSyncRequest(_) => "neighbor_sync_request",
        ReplicationMessageBody::NeighborSyncResponse(_) => "neighbor_sync_response",
        ReplicationMessageBody::VerificationRequest(_) => "verification_request",
        ReplicationMessageBody::VerificationResponse(_) => "verification_response",
        ReplicationMessageBody::FetchRequest(_) => "fetch_request",
        ReplicationMessageBody::FetchResponse(_) => "fetch_response",
        ReplicationMessageBody::AuditChallenge(_) => "audit_challenge",
        ReplicationMessageBody::AuditResponse(_) => "audit_response",
        ReplicationMessageBody::SubtreeAuditChallenge(_) => "subtree_audit_challenge",
        ReplicationMessageBody::SubtreeAuditResponse(_) => "subtree_audit_response",
        ReplicationMessageBody::SubtreeSliceChallenge(_) => "subtree_slice_challenge",
        ReplicationMessageBody::SubtreeSliceResponse(_) => "subtree_slice_response",
        ReplicationMessageBody::GetCommitmentByPin(_) => "commitment_pin_request",
        ReplicationMessageBody::GetCommitmentByPinResponse(_) => "commitment_pin_response",
        ReplicationMessageBody::PointerFreshOffer(_) => "pointer_fresh_offer",
        ReplicationMessageBody::PointerHints(_) => "pointer_hints",
        ReplicationMessageBody::PointerFetchRequest(_) => "pointer_fetch_request",
        ReplicationMessageBody::PointerFetchResponse(_) => "pointer_fetch_response",
        ReplicationMessageBody::PointerStateRequest(_) => "pointer_state_request",
        ReplicationMessageBody::PointerStateResponse(_) => "pointer_state_response",
    }
}

const fn audit_responder_class(body: &ReplicationMessageBody) -> Option<AuditResponderClass> {
    match body {
        ReplicationMessageBody::AuditChallenge(_) => Some(AuditResponderClass::Digest),
        ReplicationMessageBody::SubtreeAuditChallenge(_) => Some(AuditResponderClass::Subtree),
        ReplicationMessageBody::SubtreeSliceChallenge(_) => Some(AuditResponderClass::Byte),
        ReplicationMessageBody::GetCommitmentByPin(_) => Some(AuditResponderClass::CommitmentPin),
        _ => None,
    }
}

impl AuditResponderClass {
    const fn per_peer_limit(self) -> u32 {
        match self {
            Self::Digest => MAX_DIGEST_AUDIT_RESPONSES_PER_PEER,
            Self::Subtree | Self::Byte | Self::CommitmentPin => MAX_AUDIT_RESPONSES_PER_PEER,
        }
    }
}

fn handle_replication_event_recv_error(error: &RecvError) -> ControlFlow<()> {
    match error {
        RecvError::Lagged(missed) => {
            audit_metrics::record_replication_event_lagged(*missed);
            warn!(
                "Missed {missed} P2P events on replication branch (broadcast lag); \
                 replication messages may have been dropped before dispatch"
            );
            ControlFlow::Continue(())
        }
        RecvError::Closed => {
            // A closed broadcast channel never yields again, so the branch
            // would otherwise be immediately ready on every select! iteration.
            warn!("P2P event stream closed on replication branch; stopping message handler");
            ControlFlow::Break(())
        }
    }
}

fn replication_payload_from_event(
    event: P2PEvent,
) -> Option<(PeerId, Vec<u8>, &'static str, Option<String>)> {
    let P2PEvent::Message {
        topic,
        source: Some(source),
        data,
        ..
    } = event
    else {
        return None;
    };

    let (matched_id, is_rr) = match_replication_protocol(&topic)?;
    if !is_rr {
        return Some((source, data, matched_id, None));
    }
    if is_rr {
        return P2PNode::parse_request_envelope(&data)
            .filter(|(_, is_resp, _)| !is_resp)
            .map(|(msg_id, _, payload)| (source, payload, matched_id, Some(msg_id)));
    }
    None
}

impl Drop for PeerResponderSlot {
    fn drop(&mut self) {
        // Decrement (and prune to keep the map bounded) without blocking the
        // async runtime: a short lock on a tiny map.
        //
        // Fast path: if the (uncontended, tiny) lock is free, decrement inline
        // with no spawn. Otherwise defer to a task — but only if a runtime is
        // actually current, so `Drop` during shutdown (no runtime) can never
        // panic. A missed decrement at shutdown is harmless: the whole map is
        // being dropped with the engine.
        let peer = self.peer;
        if let Ok(mut map) = self.inflight.try_write() {
            if let Some(n) = map.get_mut(&peer) {
                *n = n.saturating_sub(1);
                if *n == 0 {
                    map.remove(&peer);
                }
            }
            return;
        }
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            let inflight = Arc::clone(&self.inflight);
            handle.spawn(async move {
                let mut map = inflight.write().await;
                if let Some(n) = map.get_mut(&peer) {
                    *n = n.saturating_sub(1);
                    if *n == 0 {
                        map.remove(&peer);
                    }
                }
            });
        }
    }
}

/// Try to admit one bounded responder task for `source`: claim a per-peer slot
/// and then a global permit. The limits are parameters so independent responder
/// classes can reuse the same flood-fair admission and cancellation semantics.
async fn admit_bounded_responder(
    semaphore: &Arc<Semaphore>,
    inflight: &Arc<RwLock<HashMap<PeerId, u32>>>,
    source: &PeerId,
    global_limit: usize,
    peer_limit: u32,
) -> std::result::Result<ResponderGuard, ResponderAdmissionFailure> {
    let global_inflight = |sem: &Semaphore| global_limit.saturating_sub(sem.available_permits());

    let admitted_peer_inflight = {
        let mut map = inflight.write().await;
        let entry = map.entry(*source).or_insert(0);
        if *entry >= peer_limit {
            let peer_inflight = *entry;
            drop(map);
            return Err(ResponderAdmissionFailure {
                reason: ResponderRejectReason::PerPeerCapFull,
                global_inflight: global_inflight(semaphore),
                global_limit,
                peer_inflight,
                peer_limit,
            });
        }
        *entry += 1;
        *entry
    };

    let peer_slot = PeerResponderSlot {
        inflight: Arc::clone(inflight),
        peer: *source,
    };
    let Ok(permit) = Arc::clone(semaphore).try_acquire_owned() else {
        return Err(ResponderAdmissionFailure {
            reason: ResponderRejectReason::GlobalPoolFull,
            global_inflight: global_inflight(semaphore),
            global_limit,
            peer_inflight: admitted_peer_inflight.saturating_sub(1),
            peer_limit,
        });
    };

    Ok(ResponderGuard {
        _permit: permit,
        _peer_slot: peer_slot,
        global_inflight: global_inflight(semaphore),
        peer_inflight: admitted_peer_inflight,
    })
}

/// A live round-1 → round-2 subtree-audit session: proof of a matching round 1.
struct SubtreeSession {
    commitment_hash: [u8; 32],
    nonce: [u8; 32],
    inserted: Instant,
    /// What round 1 bound for each pointer it proved, so round 2 serves that
    /// record however many updates land in between (ADR-0019).
    pointer_bindings: PointerBindings,
}

/// Responder-wide token bucket over the chunk bytes round-1 proof building may
/// read and hash, refilled continuously at
/// [`SUBTREE_ROUND1_WORK_REFILL_BYTES_PER_SEC`] up to
/// [`SUBTREE_ROUND1_WORK_BURST_BYTES`].
///
/// Deliberately keyed by nothing. The per-peer cooldown limits how often one
/// identity may ask, so it is refilled by acquiring more identities; this is
/// charged for work done regardless of who asked, so it is not.
///
/// Charged after the fact, with the bytes the proof actually covered: the cost
/// of a request is not known until the pinned commitment has been resolved and
/// its subtree selected, both of which happen inside the handler. Admission
/// therefore asks only whether the balance is positive, and a proof that costs
/// more than is left drives the balance NEGATIVE rather than stopping at zero.
/// Carrying the debt is what makes the bound real: without it a maximal proof
/// would cost the same as a trivial one, since either way the next request only
/// has to wait for the balance to climb back above zero. With it, sustained
/// throughput settles at refill ÷ cost-per-proof, so expensive proofs are
/// admitted proportionally less often.
struct Round1WorkBudget {
    /// Signed, so an over-large proof leaves debt to work off.
    balance: i64,
    last_refill: Instant,
}

impl Round1WorkBudget {
    /// Deepest debt carried, so one huge proof cannot lock out honest audits
    /// for longer than the burst takes to refill.
    const MAX_DEBT: i64 = -SUBTREE_ROUND1_WORK_BURST_BYTES;
    /// Nanoseconds per second, for the sub-second part of a refill.
    const NANOS_PER_SEC: i64 = 1_000_000_000;

    fn new() -> Self {
        Self {
            balance: SUBTREE_ROUND1_WORK_BURST_BYTES,
            last_refill: Instant::now(),
        }
    }

    /// Add the tokens accrued since the last touch, capped at the burst size.
    fn refill(&mut self, now: Instant) {
        let elapsed = now.saturating_duration_since(self.last_refill);
        self.last_refill = now;
        let whole_secs = i64::try_from(elapsed.as_secs())
            .unwrap_or(i64::MAX)
            .saturating_mul(SUBTREE_ROUND1_WORK_REFILL_BYTES_PER_SEC);
        let sub_sec = i64::from(elapsed.subsec_nanos())
            .saturating_mul(SUBTREE_ROUND1_WORK_REFILL_BYTES_PER_SEC)
            / Self::NANOS_PER_SEC;
        self.balance = self
            .balance
            .saturating_add(whole_secs.saturating_add(sub_sec))
            .min(SUBTREE_ROUND1_WORK_BURST_BYTES);
    }

    /// Whether budget remains for a new round-1 proof.
    fn has_budget(&mut self, now: Instant) -> bool {
        self.refill(now);
        self.balance > 0
    }

    /// Charge `bytes` of completed proof work, carrying debt down to
    /// [`Self::MAX_DEBT`].
    fn charge(&mut self, bytes: i64, now: Instant) {
        self.refill(now);
        self.balance = self.balance.saturating_sub(bytes).max(Self::MAX_DEBT);
    }
}

/// Resource controls for the HEAVY subtree-audit round 1: a tight
/// admission pool separate from the light responsible/slice audits, a per-peer
/// rate cooldown, and single-use round-1 → round-2 sessions so a round-2 slice
/// challenge is only served after a matching round 1.
///
/// It also holds the responder-wide [`Round1WorkBudget`], the only one of those
/// bounds not keyed by peer identity, and so the only one that bounds sustained
/// work rather than concurrency or per-identity frequency.
#[derive(Clone)]
struct SubtreeRound1Limiter {
    semaphore: Arc<Semaphore>,
    /// Global ceiling on concurrent round-1 proofs (config-driven;
    /// [`MAX_CONCURRENT_SUBTREE_ROUND1`] in production). Held alongside the
    /// semaphore because the per-peer admission helper needs the same number.
    max_concurrent: usize,
    inflight: Arc<RwLock<HashMap<PeerId, u32>>>,
    cooldown: Arc<RwLock<HashMap<PeerId, Instant>>>,
    /// Per-peer minimum spacing between served round-1 proofs (config-driven;
    /// [`SUBTREE_ROUND1_RESPONDER_COOLDOWN`] in production, near-zero in tests).
    cooldown_interval: Duration,
    sessions: Arc<RwLock<HashMap<(PeerId, u64), SubtreeSession>>>,
    /// Identity-independent ceiling on sustained round-1 work.
    work: Arc<RwLock<Round1WorkBudget>>,
}

impl SubtreeRound1Limiter {
    /// `max_concurrent` of 0 would wedge the responder into refusing every
    /// round-1 proof, which reads on the auditor side as a silent whole-fleet
    /// audit outage. Clamp to at least one so a mis-set config degrades to slow
    /// rather than to off.
    fn new(cooldown_interval: Duration, max_concurrent: usize) -> Self {
        let max_concurrent = max_concurrent.max(1);
        Self {
            semaphore: Arc::new(Semaphore::new(max_concurrent)),
            max_concurrent,
            inflight: Arc::new(RwLock::new(HashMap::new())),
            cooldown: Arc::new(RwLock::new(HashMap::new())),
            cooldown_interval,
            sessions: Arc::new(RwLock::new(HashMap::new())),
            work: Arc::new(RwLock::new(Round1WorkBudget::new())),
        }
    }

    /// Charge completed round-1 proof work against the responder-wide budget.
    async fn charge_work(&self, content_bytes: i64) {
        self.work
            .write()
            .await
            .charge(content_bytes, Instant::now());
    }

    /// Admit one heavy round-1 proof for `source`: take a concurrency permit
    /// FIRST (so a full pool never wastes the peer's cooldown allowance), then
    /// the responder-wide work budget, then the per-peer rate cooldown. `None`
    /// drops the challenge (the remote auditor applies its own graced-timeout
    /// policy).
    async fn admit(&self, source: &PeerId) -> Option<AuditResponderGuard> {
        let guard = admit_bounded_responder(
            &self.semaphore,
            &self.inflight,
            source,
            self.max_concurrent,
            MAX_SUBTREE_ROUND1_PER_PEER,
        )
        .await
        .ok()?;
        // Checked before the per-peer cooldown is stamped, so a peer refused for
        // want of budget is not also charged its next allowance.
        if !self.work.write().await.has_budget(Instant::now()) {
            return None; // guard drops here, releasing the permit + slot
        }
        let now = Instant::now();
        let mut cooldown = self.cooldown.write().await;
        if let Some(&last) = cooldown.get(source) {
            if now.duration_since(last) < self.cooldown_interval {
                return None; // guard drops here, releasing the permit + slot
            }
        }
        // Evict lapsed entries (their cooldown has expired, so they no longer
        // limit) and cap capacity, so peer-id churn can't grow this map unbounded.
        cooldown.retain(|_, &mut last| now.duration_since(last) < self.cooldown_interval);
        if cooldown.len() >= MAX_SUBTREE_SESSIONS {
            if let Some(oldest) = cooldown.iter().min_by_key(|(_, &t)| t).map(|(k, _)| *k) {
                cooldown.remove(&oldest);
            }
        }
        cooldown.insert(*source, now);
        Some(guard)
    }

    /// Record a single-use session once a round-1 proof is built and about to be
    /// sent, so the matching round 2 is admitted exactly once.
    ///
    /// The session keeps what round 1 bound for each pointer it proved, while
    /// every live session together holds no more than
    /// [`MAX_SESSION_POINTER_BINDINGS`] of them. A session whose bindings do
    /// not fit is not opened and `false` is returned, so its proof is not
    /// sent and round 1 is answered `Transient` instead: the bindings of
    /// sessions already answered are never given up, because their round 2 is
    /// owed them (ADR-0019).
    async fn open_session(
        &self,
        source: PeerId,
        challenge_id: u64,
        commitment_hash: [u8; 32],
        nonce: [u8; 32],
        pointer_bindings: PointerBindings,
    ) -> bool {
        let now = Instant::now();
        let mut sessions = self.sessions.write().await;
        sessions.retain(|_, e| now.duration_since(e.inserted) < SUBTREE_SESSION_TTL);
        // Checked before anything is evicted, so a session refused here costs
        // no other session its place.
        let held: usize = sessions.values().map(|e| e.pointer_bindings.len()).sum();
        if held.saturating_add(pointer_bindings.len()) > MAX_SESSION_POINTER_BINDINGS {
            return false;
        }
        if sessions.len() >= MAX_SUBTREE_SESSIONS {
            if let Some(oldest) = sessions
                .iter()
                .min_by_key(|(_, e)| e.inserted)
                .map(|(k, _)| *k)
            {
                sessions.remove(&oldest);
            }
        }
        sessions.insert(
            (source, challenge_id),
            SubtreeSession {
                commitment_hash,
                nonce,
                inserted: now,
                pointer_bindings,
            },
        );
        true
    }

    /// Atomically consume the round-2 session for this exchange. `Some` iff a
    /// live session matching `(source, challenge_id, commitment_hash, nonce)`
    /// existed (and is now removed), carrying what its round 1 bound for each
    /// pointer; a miss silently drops round 2 to the graced timeout lane
    /// (sessions are ephemeral and can be lost across a restart).
    async fn consume_session(
        &self,
        source: &PeerId,
        challenge_id: u64,
        commitment_hash: &[u8; 32],
        nonce: &[u8; 32],
    ) -> Option<PointerBindings> {
        let mut sessions = self.sessions.write().await;
        let matches = sessions.get(&(*source, challenge_id)).is_some_and(|e| {
            Instant::now().duration_since(e.inserted) < SUBTREE_SESSION_TTL
                && &e.commitment_hash == commitment_hash
                && &e.nonce == nonce
        });
        if matches {
            sessions
                .remove(&(*source, challenge_id))
                .map(|e| e.pointer_bindings)
        } else {
            None
        }
    }
}

/// Outcome of admitting a round-2 slice challenge.
enum SliceAdmission {
    /// Admitted: the guard holds the global permit and the per-peer slot, and
    /// the single-use round-1 session has been consumed, yielding what its
    /// round 1 bound for each pointer.
    Admitted(AuditResponderGuard, PointerBindings),
    /// Refused at a responder ceiling. The round-1 session is left INTACT.
    Capacity(AuditResponderAdmissionFailure),
    /// No live round-1 session matched this challenge.
    NoSession,
}

/// Admit a round-2 slice challenge: take the responder permit BEFORE consuming
/// the single-use round-1 session.
///
/// The order is the point: a single-use token must not be spent on work that is
/// then refused. Consuming first and testing admission second leaves the session
/// destroyed by a purely local capacity drop, so the refusal is not recoverable
/// even in principle.
///
/// Scope of the benefit today, stated honestly: `request_slice_proof` issues one
/// `send_request` and maps any failure straight to `SliceRound::Timeout`, so the
/// auditor does not currently re-send round 2 within an audit. The preserved
/// session is therefore not yet *recovering* an audit — it keeps a refusal
/// truthful (temporary means temporary) and keeps the invariant available for a
/// retry, rather than baking "capacity drop is permanent" into the state
/// machine. If an application-level retry is ruled out for good, this ordering
/// still costs nothing over the alternative.
///
/// Cost of the ordering: the permit and per-peer slot are held across the
/// session probe, which is one in-memory map lookup and no chunk work. The
/// per-peer cap still bounds a peer sending unsessioned challenges to the same
/// share it could already occupy with well-formed ones, so the admission
/// surface is unchanged.
async fn admit_slice_challenge(
    semaphore: &Arc<Semaphore>,
    inflight: &Arc<RwLock<HashMap<PeerId, u32>>>,
    round1: &SubtreeRound1Limiter,
    source: &PeerId,
    challenge: &protocol::SubtreeSliceChallenge,
) -> SliceAdmission {
    let guard =
        match admit_audit_responder(semaphore, inflight, source, AuditResponderClass::Byte).await {
            Ok(guard) => guard,
            Err(failure) => return SliceAdmission::Capacity(failure),
        };
    let Some(pointer_bindings) = round1
        .consume_session(
            source,
            challenge.challenge_id,
            &challenge.expected_commitment_hash,
            &challenge.nonce,
        )
        .await
    else {
        // Release the permit and per-peer slot before the caller replies: no
        // chunk work follows, so holding them would shrink the pool for nothing.
        drop(guard);
        return SliceAdmission::NoSession;
    };
    SliceAdmission::Admitted(guard, pointer_bindings)
}

/// Try to admit one audit-responder task for `source`: take a global permit AND
/// a per-peer slot (both bounded). Returns `Err` with the binding ceiling and
/// its decision-time counters (caller drops the challenge, leaving the remote
/// auditor to apply that audit path's timeout policy) if either ceiling is hit,
/// so one flooder can neither exhaust the global pool's effect on others nor
/// exceed its own per-peer share (codex-r2 A). The `Err` reason lets the caller
/// log exactly WHY the drop happened rather than a single opaque "capacity
/// reached".
async fn admit_audit_responder(
    semaphore: &Arc<Semaphore>,
    inflight: &Arc<RwLock<HashMap<PeerId, u32>>>,
    source: &PeerId,
    class: AuditResponderClass,
) -> std::result::Result<AuditResponderGuard, AuditResponderAdmissionFailure> {
    let global_limit = MAX_CONCURRENT_AUDIT_RESPONSES;
    let peer_limit = class.per_peer_limit();
    admit_bounded_responder(semaphore, inflight, source, global_limit, peer_limit).await
}

/// Handle an incoming replication protocol message.
///
/// When `rr_message_id` is `Some`, the request arrived via the `/rr/`
/// request-response path and the response must be sent via `send_response`
/// so saorsa-core can route it back to the waiting `send_request` caller.
#[allow(clippy::too_many_lines)]
async fn handle_replication_message(
    source: &PeerId,
    msg: ReplicationMessage,
    ctx: &ReplicationMessageHandlerContext,
    received_at: Instant,
    rr_message_id: Option<&str>,
) -> Result<()> {
    match msg.body {
        ReplicationMessageBody::FreshReplicationOffer(offer) => {
            dispatch_fresh_offer(
                *source,
                offer,
                ctx,
                msg.request_id,
                received_at,
                rr_message_id,
            )
            .await
        }
        ReplicationMessageBody::PaidNotify(notify) => {
            dispatch_paid_notify(*source, notify, ctx, received_at).await
        }
        ReplicationMessageBody::NeighborSyncRequest(request) => {
            let bootstrapping = *ctx.is_bootstrapping.read().await;
            // Phase-3 storage-bound audit: store the sender's
            // commitment for use as `expected_commitment_hash` in
            // future audits. Verify signature before storing so a peer
            // cannot inject a forged commitment for someone else.
            if let Some(target) = ingest_peer_commitment(
                source,
                request.commitment.as_ref(),
                &ctx.p2p_node,
                &ctx.last_commitment_by_peer,
                &ctx.ever_capable_peers,
                &ctx.sig_verify_attempts,
            )
            .await
            {
                maybe_trigger_gossip_audit(&ctx.gossip_audit, source, target).await;
            }
            dispatch_neighbor_sync_request(
                *source,
                request,
                ctx,
                bootstrapping,
                // Atomically snapshot + mark-gossiped: emitted in the sync
                // response, so we must stay answerable for it (ADR-0002).
                ctx.my_commitment_state
                    .current_for_gossip()
                    .map(|b| b.commitment().clone()),
                msg.request_id,
                received_at,
                rr_message_id,
            )
            .await
        }
        ReplicationMessageBody::VerificationRequest(request) => {
            dispatch_verification_request(
                *source,
                request,
                ctx,
                msg.request_id,
                received_at,
                rr_message_id,
            )
            .await
        }
        ReplicationMessageBody::FetchRequest(request) => {
            dispatch_fetch_request(
                *source,
                request,
                ctx,
                msg.request_id,
                received_at,
                rr_message_id,
            )
            .await
        }
        ReplicationMessageBody::AuditChallenge(challenge) => {
            // Responsible-chunk audit (audit #2) responder: answer with per-key
            // possession digests. This same handler also answers the
            // prune-confirmation audit, which sends the same `AuditChallenge`
            // wire message.
            //
            // Answering digests the stored bytes of every challenged key, so —
            // like the subtree/byte audits below — run it on a detached task off
            // this serial message loop. Handling it inline lets one challenge
            // block all other replication traffic until its digests complete
            // (head-of-line blocking). The same flood-fair admission applies: a
            // global ceiling AND a per-peer cap, dropping the challenge if either
            // is hit. A dropped challenge reads as a penalised timeout to the
            // auditor, so the caps must remain high enough for honest audit load;
            // the per-peer share still prevents one flooder from starving others.
            let class = AuditResponderClass::Digest;
            let guard = match admit_audit_responder(
                &ctx.audit_responder_semaphore,
                &ctx.audit_responder_inflight,
                source,
                class,
            )
            .await
            {
                Ok(guard) => guard,
                Err(failure) => {
                    protocol::record_audit_drop(protocol::AuditDropKind::Responsible);
                    audit_metrics::record_admission_drop(class);
                    ctx.audit_responder_metrics
                        .record_drop(*source, failure.reason.into());
                    warn!(
                        target: "ant_node::replication::audit_responder",
                        event = "admission_dropped",
                        kind = "responsible",
                        responder_class = class.as_str(),
                        source = %source,
                        challenge_id = challenge.challenge_id,
                        key_count = challenge.keys.len(),
                        request_response = rr_message_id.is_some(),
                        dispatch_ms = received_at.elapsed().as_millis(),
                        reason = failure.reason.as_str(),
                        global_inflight = failure.global_inflight,
                        global_limit = failure.global_limit,
                        peer_inflight = failure.peer_inflight,
                        peer_limit = failure.peer_limit,
                        "Audit responder admission dropped"
                    );
                    return Ok(());
                }
            };
            let admission_global_inflight = guard.global_inflight;
            let admission_peer_inflight = guard.peer_inflight;
            ctx.audit_responder_metrics.record_admitted(
                *source,
                admission_global_inflight,
                admission_peer_inflight,
            );
            info!(
                target: "ant_node::replication::audit_responder",
                event = "admitted",
                kind = "responsible",
                responder_class = class.as_str(),
                source = %source,
                challenge_id = challenge.challenge_id,
                key_count = challenge.keys.len(),
                request_response = rr_message_id.is_some(),
                dispatch_ms = received_at.elapsed().as_millis(),
                global_inflight = admission_global_inflight,
                global_limit = MAX_CONCURRENT_AUDIT_RESPONSES,
                peer_inflight = admission_peer_inflight,
                peer_limit = class.per_peer_limit(),
                "Audit responder request admitted"
            );
            let bootstrapping = *ctx.is_bootstrapping.read().await;
            let dispatch_latency = received_at.elapsed();
            audit_metrics::record_digest_dispatch_latency(dispatch_latency);
            debug!(
                audit_type = "digest_responder",
                dispatch_latency_ms = dispatch_latency.as_millis(),
                source = %source,
                "Audit challenge dispatch latency measured"
            );
            let storage = Arc::clone(&ctx.storage);
            let p2p_node = Arc::clone(&ctx.p2p_node);
            let source = *source;
            let request_id = msg.request_id;
            let rr_message_id = rr_message_id.map(ToOwned::to_owned);
            let responder_metrics = Arc::clone(&ctx.audit_responder_metrics);
            ctx.detached_task_tracker.spawn(async move {
                let _guard = guard; // global permit + per-peer slot, held until done
                let worker_started = Instant::now();
                match handle_audit_challenge_msg(
                    &source,
                    &challenge,
                    &storage,
                    &p2p_node,
                    bootstrapping,
                    ReplyRoute {
                        request_id,
                        rr_message_id: rr_message_id.as_deref(),
                        protocol: REPLICATION_PROTOCOL_ID,
                    },
                )
                .await
                {
                    Ok(completion) => log_audit_responder_completion(
                        &responder_metrics,
                        source,
                        class,
                        "responsible",
                        challenge.challenge_id,
                        challenge.keys.len(),
                        completion.response_kind,
                        completion.sent,
                        received_at,
                        worker_started,
                        completion.processing,
                        completion.response_send,
                    ),
                    Err(e) => debug!("Audit challenge from {source} error: {e}"),
                }
            });
            Ok(())
        }
        ReplicationMessageBody::SubtreeAuditChallenge(challenge) => {
            // Gossip-triggered storage-bound subtree audit (ADR-0002). The
            // responder rebuilds the WHOLE nonce-selected subtree, reading every
            // leaf's bytes from disk (`get_raw` × ~sqrt(N) leaves). Run it on a
            // detached task so this serial message loop is never blocked on disk
            // I/O — otherwise one audit stalls all replication traffic (§5).
            //
            // A bounded, flood-fair admission restores backpressure (codex#1 +
            // codex-r2 A): a global ceiling AND a per-peer cap. If either is hit
            // we drop this challenge. Subtree auditors grace timeout
            // non-responses, so capacity drops throttle flooders without turning
            // into trust penalties (and one source cannot starve other peers,
            // since its share is capped per-peer).
            let class = AuditResponderClass::Subtree;
            info!(
                "Audit challenge received: kind=subtree source={source} request_response={}",
                rr_message_id.is_some(),
            );
            // Round 1 is the HEAVY path (rebuilds + hashes the whole sqrt-subtree),
            // so it uses its own tight admission pool + per-peer rate cooldown,
            // separate from the light responsible/slice audits, and a miss silently
            // drops (subtree auditors grace timeouts).
            let Some(guard) = ctx.subtree_round1.admit(source).await else {
                protocol::record_audit_drop(protocol::AuditDropKind::Subtree);
                warn!(
                    target: "ant_node::replication::audit_responder",
                    event = "admission_dropped",
                    kind = "subtree",
                    responder_class = class.as_str(),
                    source = %source,
                    challenge_id = challenge.challenge_id,
                    request_response = rr_message_id.is_some(),
                    dispatch_ms = received_at.elapsed().as_millis(),
                    reason = "heavy_pool_cooldown_or_work_budget",
                    "Audit responder admission dropped"
                );
                return Ok(());
            };
            let admission_global_inflight = guard.global_inflight;
            let admission_peer_inflight = guard.peer_inflight;
            ctx.audit_responder_metrics.record_admitted(
                *source,
                admission_global_inflight,
                admission_peer_inflight,
            );
            info!(
                target: "ant_node::replication::audit_responder",
                event = "admitted",
                kind = "subtree",
                responder_class = class.as_str(),
                source = %source,
                challenge_id = challenge.challenge_id,
                request_response = rr_message_id.is_some(),
                dispatch_ms = received_at.elapsed().as_millis(),
                global_inflight = admission_global_inflight,
                global_limit = ctx.subtree_round1.max_concurrent,
                peer_inflight = admission_peer_inflight,
                peer_limit = MAX_SUBTREE_ROUND1_PER_PEER,
                "Audit responder request admitted"
            );
            let bootstrapping = *ctx.is_bootstrapping.read().await;
            let storage = Arc::clone(&ctx.storage);
            let p2p_node = Arc::clone(&ctx.p2p_node);
            let my_commitment_state = Arc::clone(&ctx.my_commitment_state);
            let pointer_store = ctx.pointers.as_ref().map(|p| p.store().clone());
            let source = *source;
            let request_id = msg.request_id;
            let rr_message_id = rr_message_id.map(ToOwned::to_owned);
            let responder_metrics = Arc::clone(&ctx.audit_responder_metrics);
            let subtree_round1 = ctx.subtree_round1.clone();
            ctx.detached_task_tracker.spawn(async move {
                let _guard = guard; // global permit + per-peer slot, held until done
                let worker_started = Instant::now();
                let processing_started = Instant::now();
                let storage_commitment_audit::Round1Work {
                    response,
                    content_bytes,
                    corrupt_keys,
                } = storage_commitment_audit::handle_subtree_challenge_measured_with_pointers(
                    &challenge,
                    &storage,
                    pointer_store.as_ref(),
                    p2p_node.peer_id(),
                    bootstrapping,
                    Some(&my_commitment_state),
                )
                .await;
                let processing = processing_started.elapsed();
                // Committed chunks this proof found rotted go to the chunk store's
                // single recheck worker. Queueing never waits, so no number of
                // audits over a rotted chunk can pile up cleanup work outside this
                // task's admission.
                storage.report_corrupt(&corrupt_keys);
                // Charge the work actually done, on EVERY outcome.
                //
                // This used to charge only the `Proof` arm, reasoning that the
                // rejecting paths either read nothing or reflected this node's
                // own broken storage. The second half of that was wrong: a
                // retained commitment containing one unreadable key still costs
                // a full run of reads and keyed-BLAKE3 passes over every leaf
                // before it, and then rejects. An attacker who finds such a
                // commitment could replay subtrees over it indefinitely for
                // free. The per-peer cooldown does not catch that either, since
                // it is escapable by rotating identity — the responder-wide
                // budget is the only bound that applies, so it has to see the
                // work. A zero charge is a no-op, so the untouched paths are
                // unaffected.
                subtree_round1.charge_work(content_bytes).await;
                // A round-1 proof authorizes exactly one matching round 2: open a
                // single-use session so a slice challenge cannot be served without
                // a live round-1 exchange.
                let mut response = response;
                if let crate::replication::protocol::SubtreeAuditResponse::Proof { .. } = &response
                {
                    let opened = subtree_round1
                        .open_session(
                            source,
                            challenge.challenge_id,
                            challenge.expected_commitment_hash,
                            challenge.nonce,
                            storage_commitment_audit::pointer_bindings(&response),
                        )
                        .await;
                    // A proof round 2 could not be answered for is not sent.
                    // The node says so instead, as for a local read error: the
                    // auditor's timeout lane, with no trust penalty, where
                    // silence would read as a peer that did not answer
                    // (ADR-0019).
                    if !opened {
                        protocol::record_audit_drop(protocol::AuditDropKind::Subtree);
                        warn!(
                            target: "ant_node::replication::audit_responder",
                            event = "admission_dropped",
                            kind = "subtree",
                            responder_class = class.as_str(),
                            source = %source,
                            challenge_id = challenge.challenge_id,
                            request_response = rr_message_id.is_some(),
                            reason = "pointer_binding_budget",
                            "Audit responder admission dropped"
                        );
                        response = crate::replication::protocol::SubtreeAuditResponse::Rejected {
                            challenge_id: challenge.challenge_id,
                            kind: protocol::RejectKind::Transient,
                            reason: "pointer binding budget full".to_string(),
                        };
                    }
                }
                let response_kind = subtree_audit_response_kind(&response);
                let work_items = subtree_audit_response_work_items(&response);
                let response_send_started = Instant::now();
                let sent = send_replication_response_checked(
                    &source,
                    &p2p_node,
                    request_id,
                    ReplicationMessageBody::SubtreeAuditResponse(response),
                    rr_message_id.as_deref(),
                    None,
                )
                .await;
                let response_send = response_send_started.elapsed();
                log_audit_responder_completion(
                    &responder_metrics,
                    source,
                    class,
                    "subtree",
                    challenge.challenge_id,
                    work_items,
                    response_kind,
                    sent,
                    received_at,
                    worker_started,
                    processing,
                    response_send,
                );
            });
            Ok(())
        }
        ReplicationMessageBody::SubtreeSliceChallenge(challenge) => {
            // Round 2 of the storage audit (ADR-0002 / V2-685): open one 1 KiB
            // block of each auditor-selected spot-check key with a Bao verified
            // slice + nonced block-tree opening. Reads chunk bytes from disk to
            // build the proofs, so it runs off the serial loop under the light
            // audit pool's global and per-peer ceilings.
            let class = AuditResponderClass::Byte;
            info!(
                "Audit challenge received: kind=slice source={source} request_response={}",
                rr_message_id.is_some(),
            );
            let (guard, pointer_bindings) = match admit_slice_challenge(
                &ctx.audit_responder_semaphore,
                &ctx.audit_responder_inflight,
                &ctx.subtree_round1,
                source,
                &challenge,
            )
            .await
            {
                SliceAdmission::Admitted(guard, pointer_bindings) => (guard, pointer_bindings),
                SliceAdmission::Capacity(failure) => {
                    protocol::record_audit_drop(protocol::AuditDropKind::Slice);
                    audit_metrics::record_admission_drop(class);
                    ctx.audit_responder_metrics
                        .record_drop(*source, failure.reason.into());
                    warn!(
                        target: "ant_node::replication::audit_responder",
                        event = "admission_dropped",
                        kind = "byte",
                        responder_class = class.as_str(),
                        source = %source,
                        challenge_id = challenge.challenge_id,
                        key_count = challenge.openings.len(),
                        request_response = rr_message_id.is_some(),
                        dispatch_ms = received_at.elapsed().as_millis(),
                        reason = failure.reason.as_str(),
                        global_inflight = failure.global_inflight,
                        global_limit = failure.global_limit,
                        peer_inflight = failure.peer_inflight,
                        peer_limit = failure.peer_limit,
                        "Audit responder admission dropped"
                    );
                    return Ok(());
                }
                // No live round-1 session: reply with a cheap `Transient` rejection
                // rather than dropping silently. Sessions are ephemeral (an honest
                // responder that restarts between rounds loses its session), and an
                // unanswered `send_request` would make saorsa-core record a
                // transport trust failure against that honest responder — an
                // ongoing effect, not just a rollout-window one. A `Transient`
                // reply routes the auditor to the graced timeout lane (no trust
                // penalty; the responder re-earns pinned credit on the next audit)
                // and does no chunk work, so it is not a DoS lever.
                SliceAdmission::NoSession => {
                    protocol::record_audit_drop(protocol::AuditDropKind::Slice);
                    debug!(
                        "Slice challenge without a live round-1 session source={source} \
                         challenge_id={} → Transient reject",
                        challenge.challenge_id
                    );
                    send_replication_response_checked(
                        source,
                        &ctx.p2p_node,
                        msg.request_id,
                        ReplicationMessageBody::SubtreeSliceResponse(
                            protocol::SubtreeSliceResponse::Rejected {
                                challenge_id: challenge.challenge_id,
                                kind: protocol::RejectKind::Transient,
                                reason: "no live round-1 session".to_string(),
                            },
                        ),
                        rr_message_id,
                        None,
                    )
                    .await;
                    return Ok(());
                }
            };
            let admission_global_inflight = guard.global_inflight;
            let admission_peer_inflight = guard.peer_inflight;
            ctx.audit_responder_metrics.record_admitted(
                *source,
                admission_global_inflight,
                admission_peer_inflight,
            );
            info!(
                target: "ant_node::replication::audit_responder",
                event = "admitted",
                kind = "byte",
                responder_class = class.as_str(),
                source = %source,
                challenge_id = challenge.challenge_id,
                key_count = challenge.openings.len(),
                request_response = rr_message_id.is_some(),
                dispatch_ms = received_at.elapsed().as_millis(),
                global_inflight = admission_global_inflight,
                global_limit = MAX_CONCURRENT_AUDIT_RESPONSES,
                peer_inflight = admission_peer_inflight,
                peer_limit = class.per_peer_limit(),
                "Audit responder request admitted"
            );
            let bootstrapping = *ctx.is_bootstrapping.read().await;
            let storage = Arc::clone(&ctx.storage);
            let p2p_node = Arc::clone(&ctx.p2p_node);
            let my_commitment_state = Arc::clone(&ctx.my_commitment_state);
            let pointer_store = ctx.pointers.as_ref().map(|p| p.store().clone());
            let source = *source;
            let request_id = msg.request_id;
            let rr_message_id = rr_message_id.map(ToOwned::to_owned);
            let responder_metrics = Arc::clone(&ctx.audit_responder_metrics);
            ctx.detached_task_tracker.spawn(async move {
                let _guard = guard; // global permit + per-peer slot, held until done
                let worker_started = Instant::now();
                let processing_started = Instant::now();
                let response =
                    storage_commitment_audit::handle_subtree_slice_challenge_with_pointer_bindings(
                        &challenge,
                        &storage,
                        pointer_store.as_ref(),
                        &pointer_bindings,
                        p2p_node.peer_id(),
                        bootstrapping,
                        Some(&my_commitment_state),
                    )
                    .await;
                let processing = processing_started.elapsed();
                let response_kind = subtree_slice_response_kind(&response);
                let response_send_started = Instant::now();
                let sent = send_replication_response_checked(
                    &source,
                    &p2p_node,
                    request_id,
                    ReplicationMessageBody::SubtreeSliceResponse(response),
                    rr_message_id.as_deref(),
                    None,
                )
                .await;
                let response_send = response_send_started.elapsed();
                log_audit_responder_completion(
                    &responder_metrics,
                    source,
                    class,
                    "byte",
                    challenge.challenge_id,
                    challenge.openings.len(),
                    response_kind,
                    sent,
                    received_at,
                    worker_started,
                    processing,
                    response_send,
                );
            });
            Ok(())
        }
        ReplicationMessageBody::GetCommitmentByPin(ref request) => {
            // ADR-0004: answer a commitment-by-pin fetch from the retained set
            // only. `lookup_by_hash` is an allocation-light read over the
            // bounded slot set; it returns the live current commitment or any
            // still-answerable recently-gossiped/quoted one. A miss is reported
            // as `NotRetained` (graced, never confirmed) rather than an error,
            // so an aged-out pin can never brand an honest node.
            //
            // Reuse the audit-responder admission guard (global ceiling + per-peer
            // cap) so a flood of fetches cannot drive unbounded commitment
            // clone/encode/send work; over-limit is dropped, which the fetching
            // peer graces exactly like a missed audit response.
            let class = AuditResponderClass::CommitmentPin;
            let guard = match admit_audit_responder(
                &ctx.audit_responder_semaphore,
                &ctx.audit_responder_inflight,
                source,
                class,
            )
            .await
            {
                Ok(guard) => guard,
                Err(failure) => {
                    audit_metrics::record_admission_drop(class);
                    ctx.audit_responder_metrics
                        .record_drop(*source, failure.reason.into());
                    warn!(
                        target: "ant_node::replication::audit_responder",
                        event = "admission_dropped",
                        kind = "commitment_pin",
                        responder_class = class.as_str(),
                        source = %source,
                        challenge_id = msg.request_id,
                        request_response = rr_message_id.is_some(),
                        dispatch_ms = received_at.elapsed().as_millis(),
                        reason = failure.reason.as_str(),
                        global_inflight = failure.global_inflight,
                        global_limit = failure.global_limit,
                        peer_inflight = failure.peer_inflight,
                        peer_limit = failure.peer_limit,
                        "Audit responder admission dropped"
                    );
                    return Ok(());
                }
            };
            ctx.audit_responder_metrics.record_admitted(
                *source,
                guard.global_inflight,
                guard.peer_inflight,
            );
            info!(
                target: "ant_node::replication::audit_responder",
                event = "admitted",
                kind = "commitment_pin",
                responder_class = class.as_str(),
                source = %source,
                challenge_id = msg.request_id,
                request_response = rr_message_id.is_some(),
                dispatch_ms = received_at.elapsed().as_millis(),
                global_inflight = guard.global_inflight,
                global_limit = MAX_CONCURRENT_AUDIT_RESPONSES,
                peer_inflight = guard.peer_inflight,
                peer_limit = class.per_peer_limit(),
                "Audit responder request admitted"
            );
            let worker_started = Instant::now();
            let processing_started = Instant::now();
            let response = ctx.my_commitment_state.lookup_by_hash(&request.pin).map_or(
                protocol::GetCommitmentByPinResponse::NotRetained { pin: request.pin },
                |built| protocol::GetCommitmentByPinResponse::Found {
                    commitment: built.commitment().clone(),
                },
            );
            let processing = processing_started.elapsed();
            let response_kind = commitment_pin_response_kind(&response);
            let response_send_started = Instant::now();
            let sent = send_replication_response_checked(
                source,
                &ctx.p2p_node,
                msg.request_id,
                ReplicationMessageBody::GetCommitmentByPinResponse(response),
                rr_message_id,
                None,
            )
            .await;
            let response_send = response_send_started.elapsed();
            log_audit_responder_completion(
                &ctx.audit_responder_metrics,
                *source,
                class,
                "commitment_pin",
                msg.request_id,
                1,
                response_kind,
                sent,
                received_at,
                worker_started,
                processing,
                response_send,
            );
            drop(guard);
            Ok(())
        }
        // Pointers (ADR-0016). A node that does not store pointers ignores
        // them, and so is never marked capable and never asked anything.
        ReplicationMessageBody::PointerFreshOffer(offer) => {
            if let Some(pointers) = &ctx.pointers {
                pointers.accept_offer_detached(*source, offer);
            }
            Ok(())
        }
        ReplicationMessageBody::PointerHints(hints) => {
            if let Some(pointers) = &ctx.pointers {
                pointers.handle_hints(*source, hints.hints).await;
            }
            Ok(())
        }
        ReplicationMessageBody::PointerFetchRequest(request) => {
            if let Some(pointers) = &ctx.pointers {
                pointers
                    .serve_fetch_detached(
                        *source,
                        request,
                        msg.request_id,
                        rr_message_id.map(ToOwned::to_owned),
                    )
                    .await;
            }
            Ok(())
        }
        ReplicationMessageBody::PointerStateRequest(request) => {
            if let Some(pointers) = &ctx.pointers {
                pointers
                    .serve_state_detached(
                        *source,
                        request,
                        msg.request_id,
                        rr_message_id.map(ToOwned::to_owned),
                    )
                    .await;
            }
            Ok(())
        }
        // Response messages are handled by their respective request initiators.
        ReplicationMessageBody::FreshReplicationResponse(_)
        | ReplicationMessageBody::NeighborSyncResponse(_)
        | ReplicationMessageBody::VerificationResponse(_)
        | ReplicationMessageBody::FetchResponse(_)
        | ReplicationMessageBody::AuditResponse(_)
        | ReplicationMessageBody::SubtreeAuditResponse(_)
        | ReplicationMessageBody::SubtreeSliceResponse(_)
        | ReplicationMessageBody::GetCommitmentByPinResponse(_)
        | ReplicationMessageBody::PointerFetchResponse(_)
        | ReplicationMessageBody::PointerStateResponse(_) => Ok(()),
    }
}

// ---------------------------------------------------------------------------
// Per-message-type handlers
// ---------------------------------------------------------------------------

/// Verify a payment proof, yielding early if the engine is shutting down.
///
/// Returns `None` when shutdown won the race, meaning no verdict was reached
/// and the caller must abandon the message without storing or penalising.
///
/// Payment verification is pure network I/O — an EVM round trip and, on the
/// merkle path, an iterative Kademlia lookup bounded only by the verifier's
/// own `CLOSENESS_LOOKUP_TIMEOUT`. Racing it against the shutdown token is
/// what keeps `ReplicationEngine::shutdown()`'s deliberately unbounded
/// `detached_task_tracker.wait()` finite: the wait may block only on work that
/// is guaranteed to end.
///
/// Deliberately NOT applied to `storage.put`: that awaits `spawn_blocking`, so
/// dropping its awaiter would detach a live storage operation and break the
/// very contract the unbounded wait exists to uphold.
async fn verify_payment_until_shutdown(
    payment_verifier: &Arc<PaymentVerifier>,
    key: &XorName,
    proof_of_payment: &[u8],
    context: VerificationContext,
    shutdown: &CancellationToken,
) -> Option<Result<crate::payment::PaymentStatus>> {
    tokio::select! {
        () = shutdown.cancelled() => None,
        result = payment_verifier.verify_payment(key, Some(proof_of_payment), context) => {
            Some(result)
        }
    }
}

/// A fresh offer rejected on payload shape alone.
struct FreshOfferRejection {
    /// Wire-visible reason returned to the sender.
    reason: String,
    /// Whether the sender is charged for it. Reserved for offers no honest
    /// node would construct, so an ordinary capacity refusal stays free.
    penalise: bool,
}

/// Reject a fresh offer on payload shape, or return `None` to admit it.
///
/// Pure and non-blocking by construction: these are the checks answerable from
/// the payload alone, run on the serial message loop *before* the in-flight
/// claim and the admission permit, so a malformed offer can neither lock an
/// honest offer out of its key nor occupy one of the scarce
/// [`FRESH_OFFER_MAX_OUTSTANDING`] slots for the duration of a payment
/// verification. Anything needing I/O — routing lookups, capacity checks, proof
/// verification — stays on the worker.
///
/// The content-address hash is deliberately included despite costing real CPU
/// on the loop: it is what makes the entry below *earned* rather than merely
/// asserted. Its cost is self-limiting — BLAKE3 runs at GB/s while offers
/// arrive at link speed, so a flooder can never make this loop hash faster than
/// it can deliver the bytes, and delivering them already cost it more than the
/// hash costs us. The receive path has in any case already deserialised the
/// same buffer.
///
/// Every rejection here is penalised. These are the conditions no honest sender
/// can produce — an absent, undersized, oversized, or non-matching payload — as
/// distinct from a capacity refusal, which is this node's own state and stays
/// free. Payment *outcomes* are deliberately not judged here or anywhere: a
/// proof that fails to verify may simply have been read against a lagging chain
/// view, and penalising that would charge honest senders in correlated bursts.
fn fresh_offer_structural_rejection(
    key: &XorName,
    data: &[u8],
    proof_of_payment: &[u8],
) -> Option<FreshOfferRejection> {
    // Rule 5: reject if PoP is missing. An honest sender always attaches one,
    // and an absent proof is the cheapest possible slot-filler, so this is the
    // first thing checked.
    if proof_of_payment.is_empty() {
        return Some(FreshOfferRejection {
            reason: "Missing proof of payment".to_string(),
            penalise: true,
        });
    }

    // Bound the proof before it can be queued. The verifier applies the same
    // limits, but only once the offer reaches a worker — too late here, because
    // an entry retains a proof per queued sender, and a proof is capped on the
    // wire only by MAX_REPLICATION_MESSAGE_SIZE. Without this an offer carrying
    // a byte of data and a 10 MiB "proof" would be admitted, and
    // MAX_FRESH_OFFER_ATTEMPTS_PER_KEY of them per key across
    // FRESH_OFFER_MAX_OUTSTANDING keys would dwarf the payload ceiling the
    // admission bound exists to enforce.
    if proof_of_payment.len() < MIN_PAYMENT_PROOF_SIZE_BYTES {
        return Some(FreshOfferRejection {
            reason: format!(
                "Proof of payment {} is below the minimum {MIN_PAYMENT_PROOF_SIZE_BYTES} bytes",
                proof_of_payment.len(),
            ),
            penalise: true,
        });
    }
    if proof_of_payment.len() > MAX_PAYMENT_PROOF_SIZE_BYTES {
        return Some(FreshOfferRejection {
            reason: format!(
                "Proof of payment {} exceeds the maximum {MAX_PAYMENT_PROOF_SIZE_BYTES} bytes",
                proof_of_payment.len(),
            ),
            penalise: true,
        });
    }

    // Enforce chunk size invariant: the normal PUT path rejects data larger
    // than MAX_CHUNK_SIZE; the replication receive path must do the same to
    // prevent peers from pushing oversized records through replication.
    if data.len() > crate::ant_protocol::MAX_CHUNK_SIZE {
        return Some(FreshOfferRejection {
            reason: format!(
                "Data size {} exceeds maximum chunk size {}",
                data.len(),
                crate::ant_protocol::MAX_CHUNK_SIZE,
            ),
            penalise: true,
        });
    }

    // Mirror the normal PUT path: the advertised key must be the content
    // address of the supplied bytes. Ordered last so an oversized payload is
    // refused without being hashed.
    //
    // This is the check that makes the entry safe to open, and safe for later
    // offers to be folded into. Until it passes, the sender has only *asserted*
    // an association between the key and the bytes. Opening an entry on an
    // assertion would let any peer that knows a key — every recipient of its
    // `PaidNotify`, not just the close group — seize it with junk; folding a
    // later offer into an entry on an assertion would be worse still, since the
    // handler tries each queued proof against the *opener's* bytes and would be
    // storing bytes nobody offered for that proof.
    let computed_key = crate::client::compute_address(data);
    if computed_key != *key {
        return Some(FreshOfferRejection {
            reason: format!(
                "Content address mismatch: expected {}, computed {}",
                hex::encode(key),
                hex::encode(computed_key),
            ),
            penalise: true,
        });
    }

    None
}

/// Tell `source` its offer for `key` was not taken.
///
/// Note the sender does not currently read this: `fresh::send_fresh_offers` uses
/// one-way `send_message`, so the refusal is observed only as a later absence by
/// the delayed possession check. Recorded in ADR-0005 as a known gap.
async fn refuse_fresh_offer(
    source: &PeerId,
    key: XorName,
    reason: String,
    ctx: &ReplicationMessageHandlerContext,
    request_id: u64,
    rr_message_id: Option<&str>,
) {
    send_replication_response(
        source,
        &ctx.p2p_node,
        request_id,
        ReplicationMessageBody::FreshReplicationResponse(FreshReplicationResponse::Rejected {
            key,
            reason,
        }),
        rr_message_id,
    )
    .await;
}

/// Refuse an offer that failed [`fresh_offer_structural_rejection`], charging
/// the sender when the defect is one no honest node could produce.
///
/// Kept apart from the capacity refusal in `dispatch_fresh_offer` because the
/// two say opposite things: this is the sender's fault, that one is ours.
async fn refuse_malformed_fresh_offer(
    source: &PeerId,
    key: XorName,
    rejection: FreshOfferRejection,
    ctx: &ReplicationMessageHandlerContext,
    request_id: u64,
    rr_message_id: Option<&str>,
) {
    if rejection.penalise {
        warn!(
            "Rejecting fresh offer for key {} from {source}: {}",
            hex::encode(key),
            rejection.reason
        );
        ctx.p2p_node
            .report_trust_event(
                source,
                TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
            )
            .await;
    } else {
        debug!(
            "Fresh offer for {} from {source} refused before admission: {}",
            hex::encode(key),
            rejection.reason
        );
    }
    refuse_fresh_offer(
        source,
        key,
        rejection.reason,
        ctx,
        request_id,
        rr_message_id,
    )
    .await;
}

/// Tell senders whose proofs joined an entry that was torn down before a handler
/// ever ran for it.
///
/// Only reachable in the window between opening an entry and failing to take an
/// admission permit. Their proofs are answered rather than dropped with the
/// entry, so the refusal count reflects what actually happened.
async fn refuse_stranded_fresh_offers(
    key: XorName,
    stranded: Vec<FreshOfferAttempt>,
    ctx: &ReplicationMessageHandlerContext,
) {
    for attempt in stranded {
        refuse_fresh_offer(
            &attempt.source,
            key,
            "Receiver at fresh-offer capacity".to_string(),
            ctx,
            attempt.request_id,
            attempt.rr_message_id.as_deref(),
        )
        .await;
    }
}

/// Admit a fresh offer for handling on a worker, or refuse it.
///
/// This runs on the serial non-audit message loop, so it must stay cheap: every
/// path here is a set insert, a permit try, or a small response send. The offer
/// itself — an on-chain payment verification and a multi-MiB write — always
/// runs on a tracked worker task, never inline, because stalling this loop backs
/// up the inbound queue and ultimately drops replication messages wholesale.
///
/// Admission is bounded **per source** as well as globally
/// ([`FRESH_OFFER_MAX_OUTSTANDING_PER_PEER`]). Without that share one peer can
/// hold every slot, and since a refusal is later read as absence by the
/// sender's delayed possession check, the resulting refusals land as
/// audit-severity penalties on this node rather than on the flooder.
///
/// The structurally-invalid checks run **before** the entry is opened and before
/// the permit is taken, so junk can neither seize a key nor occupy a slot for
/// the duration of a payment verification.
///
/// Only the offer that *opens* an entry costs a permit and a worker. Later
/// offers for the same key contribute their proof and are answered by whatever
/// the opener's handler concludes, which is what keeps the routine
/// `CLOSE_GROUP_MAJORITY`-way duplication of a client PUT down to one permit and
/// one verification per key.
async fn dispatch_fresh_offer(
    source: PeerId,
    offer: protocol::FreshReplicationOffer,
    ctx: &ReplicationMessageHandlerContext,
    request_id: u64,
    received_at: Instant,
    rr_message_id: Option<&str>,
) -> Result<()> {
    if let Some(rejection) =
        fresh_offer_structural_rejection(&offer.key, &offer.data, &offer.proof_of_payment)
    {
        refuse_malformed_fresh_offer(
            &source,
            offer.key,
            rejection,
            ctx,
            request_id,
            rr_message_id,
        )
        .await;
        return Ok(());
    }

    // Join the work already in flight for this key before taking an admission
    // permit, so the duplicates a single client PUT produces cost one permit
    // between them rather than one each.
    //
    // Reaching this point means `fresh_offer_structural_rejection` has confirmed
    // `key == BLAKE3(data)`, so this offer's bytes are provably identical to any
    // already held for the key. That is what lets its payload be dropped here
    // and only its proof retained: the handler will try that proof against the
    // bytes it already has.
    let key = offer.key;
    let admission_outcome = FreshOfferEntryGuard::admit(
        &ctx.fresh_offer_in_flight,
        offer,
        source,
        request_id,
        rr_message_id.map(ToOwned::to_owned),
        received_at,
    );
    let mut entry = match admission_outcome {
        FreshOfferAdmission::Opened(entry) => entry,
        FreshOfferAdmission::Joined => {
            // No response yet: this proof has not been judged. The handler that
            // owns the key answers every queued sender once it settles.
            debug!(
                "Fresh offer for {} from {source} queued behind the handler already \
                 holding the key",
                hex::encode(key)
            );
            return Ok(());
        }
        // Surplus, not suspect: the proofs already queued are enough to place
        // the record, so neither case is charged as misbehaviour.
        surplus => {
            debug!(
                "Fresh offer for {} from {source} refused: {}",
                hex::encode(key),
                surplus.surplus_reason()
            );
            refuse_fresh_offer(
                &source,
                key,
                "Duplicate offer already in flight".to_string(),
                ctx,
                request_id,
                rr_message_id,
            )
            .await;
            return Ok(());
        }
    };

    let admission = match admit_bounded_responder(
        &ctx.fresh_offer_admission_semaphore,
        &ctx.fresh_offer_responder_inflight,
        &source,
        FRESH_OFFER_MAX_OUTSTANDING,
        FRESH_OFFER_MAX_OUTSTANDING_PER_PEER,
    )
    .await
    {
        Ok(guard) => guard,
        Err(failure) => {
            audit_metrics::record_responder_admission_drop(
                ReplicationResponderClass::FreshOffer,
                failure.reason.into(),
            );
            // WARN, not DEBUG: a healthy node should never refuse a legitimate
            // fresh offer. The sender does not read this refusal, so it
            // resurfaces as a missing key at the delayed possession check and
            // is charged to US at audit severity. Log the binding ceiling
            // locally; the wire reason stays generic so a probing peer cannot
            // map out this node's live occupancy.
            warn!(
                responder_class = "fresh_offer",
                source = %source,
                key = %hex::encode(key),
                "Fresh offer refused at admission; this node will be penalised for the \
                 resulting absence: {failure}"
            );
            // Release the key explicitly rather than on drop, so the next offer
            // opens a fresh entry rather than queueing behind a handler that was
            // never spawned — and so any proof that joined in the window between
            // opening the entry and failing here is answered rather than
            // discarded with it.
            let stranded = entry.release();
            refuse_fresh_offer(
                &source,
                key,
                "Receiver at fresh-offer capacity".to_string(),
                ctx,
                request_id,
                rr_message_id,
            )
            .await;
            refuse_stranded_fresh_offers(key, stranded, ctx).await;
            return Ok(());
        }
    };

    let ctx = ctx.clone();
    // Track the worker so `ReplicationEngine::shutdown()` can await it: it holds
    // an `Arc<ChunkStore>` while writing, and the shutdown contract requires
    // those references be released before the caller reopens the environment.
    ctx.detached_task_tracker
        .clone()
        .spawn(run_fresh_offer_worker(key, entry, ctx, admission));
    Ok(())
}

/// Body of one tracked fresh-offer worker: drive one key to a verdict.
///
/// Split out so `dispatch_fresh_offer` stays a readable admission decision.
/// A started handler is never cancelled: `storage.put()` awaits
/// `spawn_blocking`, and dropping that awaiter would detach the live storage
/// transaction. Shutdown responsiveness comes from the closed worker semaphore
/// and from `handle_fresh_offer` racing the token around payment verification.
///
/// Works down the key's queued proofs rather than judging the key on the first
/// one. A proof that fails to establish payment disqualifies its *sender*, not
/// the record: the bytes are content-addressed and identical across every offer,
/// so the next sender's proof is tried against the copy already in hand — no
/// second payload, no second permit, no refetch. Only when a failure is about
/// the key itself (not responsible, no capacity, shutting down) does rotating
/// become pointless, because every remaining proof would meet the same wall.
async fn run_fresh_offer_worker(
    key: XorName,
    mut entry: Box<FreshOfferEntryGuard>,
    ctx: ReplicationMessageHandlerContext,
    admission: ResponderGuard,
) {
    // The admission permit is released on completion, freeing the payload's
    // memory budget and the opening source's per-peer share. The entry releases
    // the key itself, either as the queue empties or on drop.
    let _admission = admission;
    let received_at = entry.received_at();
    // Wait for a worker slot here rather than in the caller. The worker bound
    // caps concurrent EVM and storage pressure; making the message loop wait on
    // it is what put that pressure back on the loop.
    //
    // The semaphore is closed by `shutdown()`, so this arm is the prompt exit
    // path for offers still queued behind a worker when the engine stops — not
    // dead code.
    let Ok(_worker) = Arc::clone(&ctx.fresh_offer_worker_semaphore)
        .acquire_owned()
        .await
    else {
        debug!(
            "Fresh offer for {} dropped: worker pool shut down",
            hex::encode(key)
        );
        return;
    };
    // Fresh offers are fire-and-forget (`send_message`), so there is no
    // requester deadline to honour and a late store is still genuinely useful.
    // The threshold is the point past which the offer has already cost us what
    // it was going to cost: the possession check (ADR-0003) has run by then and
    // charged the absence to this node, and it does not re-offer the key —
    // repair comes later from neighbor sync. Shedding here trades a copy that
    // would have arrived after the penalty for the multi-MiB payload it is
    // holding, and keeps a backlog from outliving its own admission bound.
    //
    // Note this is a memory-pressure decision, not a redundancy one: the copy
    // is late, not useless, so on a node with headroom finishing it would still
    // beat waiting for neighbor sync.
    if request_is_stale(received_at, ctx.config.possession_check_delay_min) {
        debug!(
            responder_class = "fresh_offer",
            key = %hex::encode(key),
            request_age_ms = received_at.elapsed().as_millis(),
            "Superseded fresh offer shed at dequeue"
        );
        return;
    }

    let mut verdict = FreshOfferOutcome::Abandoned;
    while let Some(attempt) = entry.next_attempt() {
        match handle_fresh_offer(&key, entry.data(), &attempt, &ctx).await {
            Ok(outcome) => {
                verdict = outcome;
                match outcome {
                    // Settled: the record is down and every remaining sender
                    // wanted exactly that. Abandoned: the key cannot be placed
                    // by anyone right now, so the proofs behind this one would
                    // fail identically. Either way there is nothing left to try.
                    FreshOfferOutcome::Settled | FreshOfferOutcome::Abandoned => break,
                    // This sender's claim to have paid did not hold up. Say so
                    // to that sender alone and try the next proof.
                    FreshOfferOutcome::ProofRejected => {}
                }
            }
            Err(e) => {
                debug!(
                    "Fresh replication offer for {} from {} error: {e}",
                    hex::encode(key),
                    attempt.source
                );
                verdict = FreshOfferOutcome::Abandoned;
                break;
            }
        }
    }

    // Answer the senders whose proofs were never reached. A stored record is
    // what they were asking for, so it is an acceptance even though their own
    // proof went untested.
    let unreached = entry.release();
    if unreached.is_empty() {
        return;
    }
    let response = match verdict {
        FreshOfferOutcome::Settled => FreshReplicationResponse::Accepted { key },
        FreshOfferOutcome::ProofRejected | FreshOfferOutcome::Abandoned => {
            FreshReplicationResponse::Rejected {
                key,
                reason: "Record could not be stored from any offered proof".to_string(),
            }
        }
    };
    for attempt in unreached {
        send_replication_response(
            &attempt.source,
            &ctx.p2p_node,
            attempt.request_id,
            ReplicationMessageBody::FreshReplicationResponse(response.clone()),
            attempt.rr_message_id.as_deref(),
        )
        .await;
    }
}

/// What one proof concluded, and whether another proof for the same key is worth
/// trying.
///
/// The distinction the worker acts on is *what the failure was about*. A
/// rejected proof is a statement about its sender; anything else is a statement
/// about the key or this node, which no other sender's proof can change.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FreshOfferOutcome {
    /// The record is stored. The key is done.
    Settled,
    /// This proof did not establish payment. A different sender's proof for the
    /// same bytes may still succeed.
    ProofRejected,
    /// Nothing about this key can succeed right now — not responsible, no disk,
    /// shutting down, or a write that failed. Rotating repeats the failure.
    Abandoned,
}

// A linear sequence of protocol steps — structural recheck, responsibility,
// capacity, payment, write — each with its own refusal response. Splitting it
// would scatter one decision across several functions.
#[allow(clippy::too_many_lines)]
async fn handle_fresh_offer(
    key: &XorName,
    data: &[u8],
    attempt: &FreshOfferAttempt,
    ctx: &ReplicationMessageHandlerContext,
) -> Result<FreshOfferOutcome> {
    let source = &attempt.source;
    let request_id = attempt.request_id;
    let rr_message_id = attempt.rr_message_id.as_deref();
    let p2p_node = &ctx.p2p_node;
    let config = &ctx.config;
    let self_id = *p2p_node.peer_id();

    // Defence in depth: `dispatch_fresh_offer` already applied these — missing
    // proof, bad proof size, oversized payload, content-address mismatch —
    // before opening the entry or taking an admission permit, so nothing
    // reaching a worker should trip them. Kept so the handler is correct in
    // isolation. Note this re-checks the *pairing* actually being used: these
    // bytes against this attempt's proof.
    if let Some(rejection) = fresh_offer_structural_rejection(key, data, &attempt.proof_of_payment)
    {
        if rejection.penalise {
            warn!(
                "Rejecting fresh offer for key {}: {}",
                hex::encode(key),
                rejection.reason
            );
            p2p_node
                .report_trust_event(
                    source,
                    TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                )
                .await;
        }
        send_replication_response(
            source,
            p2p_node,
            request_id,
            ReplicationMessageBody::FreshReplicationResponse(FreshReplicationResponse::Rejected {
                key: *key,
                reason: rejection.reason,
            }),
            rr_message_id,
        )
        .await;
        // Structurally invalid is a fact about this attempt, not the key.
        return Ok(FreshOfferOutcome::ProofRejected);
    }

    // Rule 7: check storage admission. Fresh chunk receivers accept across the
    // paid-close-group neighbourhood (`paid_list_close_group_size`, = K_BUCKET_SIZE,
    // the same width client PUTs use), not just the close group plus a small
    // margin (ADR-0003). During full-node shunning a healthy replica's routing
    // table may still list closer full nodes it hasn't evicted yet, ranking it
    // outside the narrow window in its own view; the wider accept window absorbs
    // that transient skew so the chunk still lands. Retention (pruning) stays at
    // `storage_admission_width`, so steady-state replication is unchanged.
    if !admission::is_responsible(&self_id, key, p2p_node, config.paid_list_close_group_size).await
    {
        send_replication_response(
            source,
            p2p_node,
            request_id,
            ReplicationMessageBody::FreshReplicationResponse(FreshReplicationResponse::Rejected {
                key: *key,
                reason: "Not in storage-admission range for this key".to_string(),
            }),
            rr_message_id,
        )
        .await;
        // About the key and this node's place in the routing table, not about
        // the sender: every queued proof would be refused identically.
        return Ok(FreshOfferOutcome::Abandoned);
    }

    // Disk-space pre-check — mirror the PUT handler (V2-411). A full node can
    // never store this record, so reject it before the expensive payment
    // verification (EVM on-chain query / merkle pool work) rather than verifying
    // and only then failing at `storage.put` below. Reuses the cached capacity
    // check (passing results only, so freed space is detected promptly), and the
    // store path keeps its own check as defence-in-depth.
    if let Err(e) = ctx.storage.check_capacity() {
        info!(
            target: "ant_node::storage::disk_precheck",
            key = %hex::encode(key),
            "Rejecting fresh replication offer before payment verification: {e}"
        );
        send_replication_response(
            source,
            p2p_node,
            request_id,
            ReplicationMessageBody::FreshReplicationResponse(FreshReplicationResponse::Rejected {
                key: *key,
                reason: e.to_string(),
            }),
            rr_message_id,
        )
        .await;
        // A full disk is this node's condition, not the sender's.
        return Ok(FreshOfferOutcome::Abandoned);
    }

    // Gap 1: Validate PoP via PaymentVerifier. Fresh replication is still
    // part of the immediate write fan-out: this receiver is about to store the
    // record as if the client had PUT it here directly. Storage admission
    // was checked above before proof work. FreshReplication verification is
    // identical to ClientPut — store-strength cache semantics, paid-quote
    // issuer K-closeness checks for single-node proofs, merkle candidate
    // closeness for merkle proofs, and the same price-floor policy — the
    // distinct context only labels price-floor telemetry.
    let Some(verification) = verify_payment_until_shutdown(
        &ctx.payment_verifier,
        key,
        &attempt.proof_of_payment,
        fresh_offer_payment_context(),
        &ctx.shutdown,
    )
    .await
    else {
        debug!(
            "Fresh offer for {} from {source} abandoned: engine shutting down \
             during payment verification",
            hex::encode(key)
        );
        return Ok(FreshOfferOutcome::Abandoned);
    };
    match verification {
        Ok(status) if status.can_store() => {
            debug!("PoP validated for fresh offer key {}", hex::encode(key));
        }
        // The chain has no record of this payment. That is a statement about
        // *this sender's* proof, so the key rotates to the next one rather than
        // being written off — the whole point of queueing them.
        //
        // Deliberately unpenalised. `PaymentRequired` means "no payment found",
        // not "definitively unpaid", so a lagging or reorganising chain view
        // renders honest senders indistinguishable from forgers, and penalising
        // would land on the entire close group at once. The structural checks in
        // `fresh_offer_structural_rejection` are where misbehaviour is charged,
        // because those cannot be produced by an honest sender at all.
        Ok(_) => {
            debug!(
                "Fresh offer for {} from {source}: no payment found for this proof",
                hex::encode(key)
            );
            send_replication_response(
                source,
                p2p_node,
                request_id,
                ReplicationMessageBody::FreshReplicationResponse(
                    FreshReplicationResponse::Rejected {
                        key: *key,
                        reason: "Payment verification failed: payment required".to_string(),
                    },
                ),
                rr_message_id,
            )
            .await;
            return Ok(FreshOfferOutcome::ProofRejected);
        }
        // A verification *error* is usually ours — an unreachable or failing EVM
        // endpoint — so it is neither penalised nor treated as this sender's
        // fault. Another proof still gets its turn, since the next call may well
        // succeed against a recovered endpoint.
        Err(e) => {
            warn!("PoP verification error for key {}: {e}", hex::encode(key));
            send_replication_response(
                source,
                p2p_node,
                request_id,
                ReplicationMessageBody::FreshReplicationResponse(
                    FreshReplicationResponse::Rejected {
                        key: *key,
                        reason: format!("Payment verification error: {e}"),
                    },
                ),
                rr_message_id,
            )
            .await;
            return Ok(FreshOfferOutcome::ProofRejected);
        }
    }

    // Rule 6: add to PaidForList.
    if let Err(e) = ctx.paid_list.insert(key).await {
        warn!("Failed to add key to PaidForList: {e}");
    }

    // Store the record.
    match ctx.storage.put(key, data).await {
        Ok(_) => {
            send_replication_response(
                source,
                p2p_node,
                request_id,
                ReplicationMessageBody::FreshReplicationResponse(
                    FreshReplicationResponse::Accepted { key: *key },
                ),
                rr_message_id,
            )
            .await;
            Ok(FreshOfferOutcome::Settled)
        }
        Err(e) => {
            send_replication_response(
                source,
                p2p_node,
                request_id,
                ReplicationMessageBody::FreshReplicationResponse(
                    FreshReplicationResponse::Rejected {
                        key: *key,
                        reason: e.to_string(),
                    },
                ),
                rr_message_id,
            )
            .await;
            // The payment was good and the write still failed, so this is local
            // storage trouble that another proof cannot get around.
            Ok(FreshOfferOutcome::Abandoned)
        }
    }
}

/// Admit a paid-list notification for handling on a worker, or drop it.
///
/// `PaidNotify` used to be handled inline on the serial non-audit loop, which
/// meant one message could park every other non-audit message behind a full
/// payment verification. That verification is not a bounded local computation:
/// on the merkle path it performs an iterative Kademlia lookup capped only by
/// the verifier's `CLOSENESS_LOOKUP_TIMEOUT`, so a single notify could stall
/// the loop for minutes while the bounded inbound queue behind it overflowed
/// and dropped unrelated replication traffic wholesale.
///
/// It now follows the same detached-responder pattern as neighbor sync and
/// verification (ADR-0005 decision 2): bounded globally and per source, shed
/// when stale, and run on a tracked worker.
///
/// `PaidNotify` is one-way — the protocol defines no response variant — so a
/// refused notify is simply dropped. The sender's own paid-list convergence
/// (periodic neighbor sync and the verification cycle's paid-list quorum) is
/// the recovery path, exactly as for a notify lost in transit.
async fn dispatch_paid_notify(
    source: PeerId,
    notify: protocol::PaidNotify,
    ctx: &ReplicationMessageHandlerContext,
    received_at: Instant,
) -> Result<()> {
    let admission = match admit_bounded_responder(
        &ctx.paid_notify_admission_semaphore,
        &ctx.paid_notify_responder_inflight,
        &source,
        PAID_NOTIFY_MAX_OUTSTANDING,
        PAID_NOTIFY_MAX_OUTSTANDING_PER_PEER,
    )
    .await
    {
        Ok(guard) => guard,
        Err(failure) => {
            audit_metrics::record_responder_admission_drop(
                ReplicationResponderClass::PaidNotify,
                failure.reason.into(),
            );
            // WARN for the same reason as fresh offers: PaidNotify is one-way,
            // so a refusal discards durable paid-list evidence outright rather
            // than pushing work back onto a requester.
            warn!(
                responder_class = "paid_notify",
                source = %source,
                key = %hex::encode(notify.key),
                "Paid notify dropped at admission — paid-list evidence lost                  until a verification cycle re-derives it: {failure}"
            );
            return Ok(());
        }
    };

    let ctx = ctx.clone();
    let worker_semaphore = Arc::clone(&ctx.paid_notify_worker_semaphore);
    // Deliberately NOT the verification request deadline used by the
    // request/response responders. Nobody is waiting on a notify, and shedding
    // one discards durable paid-list evidence rather than a reply a requester
    // has already given up on. The threshold is instead the point at which the
    // pull path would have learned the same fact anyway: one slow-cadence
    // neighbor-sync interval, after which the verification cycle's paid-list
    // quorum makes this notify genuinely redundant.
    let queue_max_age = ctx.config.neighbor_sync_interval_max;
    ctx.detached_task_tracker.clone().spawn(async move {
        let _admission = admission;
        // Closed by `shutdown()`, so this is the prompt exit path for notifies
        // still queued behind a worker when the engine stops.
        let Ok(_worker) = worker_semaphore.acquire_owned().await else {
            debug!("Paid notify from {source} dropped: worker pool shut down");
            return;
        };
        if request_is_stale(received_at, queue_max_age) {
            debug!(
                responder_class = "paid_notify",
                source = %source,
                request_age_ms = received_at.elapsed().as_millis(),
                "Redundant paid notify shed at dequeue"
            );
            return;
        }
        if let Err(e) = handle_paid_notify(
            &source,
            &notify,
            &ctx.paid_list,
            &ctx.payment_verifier,
            &ctx.p2p_node,
            &ctx.config,
            &ctx.shutdown,
        )
        .await
        {
            debug!("Paid notify from {source} error: {e}");
        }
    });

    Ok(())
}

async fn handle_paid_notify(
    _source: &PeerId,
    notify: &protocol::PaidNotify,
    paid_list: &Arc<PaidList>,
    payment_verifier: &Arc<PaymentVerifier>,
    p2p_node: &Arc<P2PNode>,
    config: &ReplicationConfig,
    shutdown: &CancellationToken,
) -> Result<()> {
    let self_id = *p2p_node.peer_id();

    // Rule 3: validate PoP presence before adding.
    if notify.proof_of_payment.is_empty() {
        return Ok(());
    }

    // Check if we're in PaidCloseGroup for this key.
    if !admission::is_in_paid_close_group(
        &self_id,
        &notify.key,
        p2p_node,
        config.paid_list_close_group_size,
    )
    .await
    {
        return Ok(());
    }

    // Gap 1: Validate PoP via PaymentVerifier. PaidNotify admits fresh
    // paid-list metadata, so local paid-list close-group membership was checked
    // above before proof work. The verifier then runs the same payment proof
    // checks as ClientPut while writing a paid-list-strength cache entry.
    let Some(verification) = verify_payment_until_shutdown(
        payment_verifier,
        &notify.key,
        &notify.proof_of_payment,
        paid_notify_payment_context(),
        shutdown,
    )
    .await
    else {
        debug!(
            "Paid notify for {} abandoned: engine shutting down during payment \
             verification",
            hex::encode(notify.key)
        );
        return Ok(());
    };
    match verification {
        Ok(status) if status.can_store() => {
            debug!(
                "PoP validated for paid notify key {}",
                hex::encode(notify.key)
            );
        }
        Ok(_) => {
            warn!(
                "Paid notify rejected: payment required for key {}",
                hex::encode(notify.key)
            );
            return Ok(());
        }
        Err(e) => {
            warn!(
                "PoP verification error for paid notify key {}: {e}",
                hex::encode(notify.key)
            );
            return Ok(());
        }
    }

    if let Err(e) = paid_list.insert(&notify.key).await {
        warn!("Failed to add paid notify key to PaidForList: {e}");
    }

    Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn dispatch_neighbor_sync_request(
    source: PeerId,
    request: protocol::NeighborSyncRequest,
    ctx: &ReplicationMessageHandlerContext,
    is_bootstrapping: bool,
    my_commitment: Option<StorageCommitment>,
    request_id: u64,
    received_at: Instant,
    rr_message_id: Option<&str>,
) -> Result<()> {
    let guard = match admit_bounded_responder(
        &ctx.neighbor_sync_responder_admission_semaphore,
        &ctx.neighbor_sync_responder_inflight,
        &source,
        NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING,
        NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING_PER_PEER,
    )
    .await
    {
        Ok(guard) => guard,
        Err(failure) => {
            audit_metrics::record_responder_admission_drop(
                ReplicationResponderClass::NeighborSync,
                failure.reason.into(),
            );
            warn!(
                responder_class = "neighbor_sync",
                source = %source,
                "Neighbor-sync response dropped at admission: {failure}"
            );
            return Ok(());
        }
    };
    let pointers = ctx.pointers.clone();
    let worker_semaphore = Arc::clone(&ctx.neighbor_sync_responder_worker_semaphore);
    let p2p_node = Arc::clone(&ctx.p2p_node);
    let storage = Arc::clone(&ctx.storage);
    let paid_list = Arc::clone(&ctx.paid_list);
    let queues = Arc::clone(&ctx.queues);
    let config = Arc::clone(&ctx.config);
    let bootstrap_state = Arc::clone(&ctx.bootstrap_state);
    let sync_history = Arc::clone(&ctx.sync_history);
    let sync_cycle_epoch = Arc::clone(&ctx.sync_cycle_epoch);
    let repair_proofs = Arc::clone(&ctx.repair_proofs);
    let request_timeout = ctx.config.verification_request_timeout;
    let rr_message_id = rr_message_id.map(ToOwned::to_owned);
    ctx.detached_task_tracker.spawn(async move {
        let _guard = guard;
        let Ok(_worker_permit) = worker_semaphore.acquire_owned().await else {
            debug!(
                responder_class = "neighbor_sync",
                source = %source,
                "Neighbor-sync responder worker pool closed before dequeue"
            );
            return;
        };
        if request_is_stale(received_at, request_timeout) {
            audit_metrics::record_responder_staleness_shed(ReplicationResponderClass::NeighborSync);
            debug!(
                responder_class = "neighbor_sync",
                source = %source,
                request_age_ms = received_at.elapsed().as_millis(),
                "Stale neighbor-sync request shed at dequeue"
            );
            return;
        }
        // A peer whose sync request is admitted and fresh gets our pointer
        // hints as well, including a node that is bootstrapping, which is how
        // it learns the pointers it should hold. Only such a request: each
        // answer scans every pointer held, and a refused or stale one must
        // cost nothing.
        if let Some(pointers) = &pointers {
            pointers.answer_sync_with_hints(source);
        }
        if let Err(e) = handle_neighbor_sync_request(
            &source,
            &request,
            &p2p_node,
            &storage,
            &paid_list,
            &queues,
            &config,
            is_bootstrapping,
            &bootstrap_state,
            &sync_history,
            &sync_cycle_epoch,
            &repair_proofs,
            my_commitment,
            request_id,
            rr_message_id.as_deref(),
        )
        .await
        {
            debug!(source = %source, "Neighbor-sync request handling failed: {e}");
        }
    });

    Ok(())
}

#[allow(clippy::too_many_arguments)]
async fn handle_neighbor_sync_request(
    source: &PeerId,
    request: &protocol::NeighborSyncRequest,
    p2p_node: &Arc<P2PNode>,
    storage: &Arc<ChunkStore>,
    paid_list: &Arc<PaidList>,
    queues: &Arc<RwLock<ReplicationQueues>>,
    config: &ReplicationConfig,
    is_bootstrapping: bool,
    bootstrap_state: &Arc<RwLock<BootstrapState>>,
    sync_history: &Arc<RwLock<HashMap<PeerId, PeerSyncRecord>>>,
    sync_cycle_epoch: &Arc<RwLock<u64>>,
    repair_proofs: &Arc<RwLock<RepairProofs>>,
    my_commitment: Option<StorageCommitment>,
    request_id: u64,
    rr_message_id: Option<&str>,
) -> Result<()> {
    let self_id = *p2p_node.peer_id();

    // No per-request hint count limit: the wire message size limit
    // (MAX_REPLICATION_MESSAGE_SIZE) already caps the payload. Unlike audit
    // challenges, sync hints don't drive expensive computation — they just
    // enter the verification queue. A per-request limit here would break
    // bootstrap replication for newly-joined nodes with 0 stored chunks.

    // Build response (outbound hints).
    let (response, sent_replica_hints, sender_in_rt) =
        neighbor_sync::handle_sync_request_with_proofs(
            source,
            request,
            p2p_node,
            storage,
            paid_list,
            config,
            is_bootstrapping,
            my_commitment.clone(),
        )
        .await;

    // Send response.
    let response_sent = send_replication_response_checked(
        source,
        p2p_node,
        request_id,
        ReplicationMessageBody::NeighborSyncResponse(response),
        rr_message_id,
        None,
    )
    .await;

    // Process inbound hints only if sender is in LocalRT (Rule 4-6).
    if !sender_in_rt {
        return Ok(());
    }

    // Update sync history for this peer before recording repair proofs so a
    // same-tick audit cannot combine a fresh key proof with stale peer maturity.
    {
        let mut history = sync_history.write().await;
        let record = history.entry(*source).or_insert(PeerSyncRecord {
            last_sync: None,
            cycles_since_sync: 0,
        });
        record.last_sync = Some(Instant::now());
        record.cycles_since_sync = 0;
    }

    if response_sent && !request.bootstrapping {
        record_sent_replica_hints(source, &sent_replica_hints, repair_proofs, sync_cycle_epoch)
            .await;
    }

    // Admit inbound hints and queue for verification.
    let outcome = admit_and_queue_hints(
        &self_id,
        source,
        &request.replica_hints,
        &request.paid_hints,
        p2p_node,
        config,
        storage,
        paid_list,
        queues,
    )
    .await;

    // Track discovered keys for bootstrap drain detection so that hints
    // admitted via inbound sync requests are not missed. Capacity-rejected
    // hints keep this source on the "not yet drained" list until its next
    // sync re-admits them; a clean cycle clears the source.
    if is_bootstrapping {
        let live_discovered = outcome.discovered.clone();
        let outcomes = [(*source, outcome)];
        publish_bootstrap_admission_outcomes(bootstrap_state, &outcomes, &live_discovered).await;
    }

    Ok(())
}

/// Test-only record of who *sent* a verification request covering a watched key.
///
/// The capacity gate's entire effect is a request that is never sent, and a
/// request that was not sent leaves no production counter anywhere — not on the
/// sender, whose traffic counters are process-global and so cannot separate one
/// node of a single-process testnet from another, and not on the responder,
/// which simply never hears from it. Recording who asked is what lets a test
/// tell "held the key back" apart from "probed the close group, was refused at
/// the dial, and then held the key back", which are otherwise identical from the
/// queue's point of view.
///
/// Arming replaces the previous watch, so the outer map holds exactly the keys
/// the current test named, and each inner map holds at most one entry per node
/// in the process. Nothing is recorded until a test arms it, and the armed path
/// costs one read lock on the sending side.
///
/// Counting is per key because a node that cannot write legitimately keeps
/// sending verification requests for keys it has not yet authorized — that is
/// `PaidForList` convergence, which the gate leaves alone — so a per-peer total
/// would assert something untrue.
///
/// It counts *send attempts*, deliberately, recorded immediately before the wire
/// call. A responder sheds requests at admission and as stale, so a
/// receiver-side count would report zero for probes that were really sent, and
/// an assertion that this node asked nobody would pass on the strength of the
/// receiver dropping the question. It is not proof of delivery: a send that
/// fails immediately, with no route or during shutdown, is still counted.
///
/// `OnceLock` rather than `LazyLock`, which needs a newer Rust than this crate's
/// MSRV.
#[cfg(any(test, feature = "test-utils"))]
type VerificationWatch = HashMap<XorName, HashMap<PeerId, usize>>;

#[cfg(any(test, feature = "test-utils"))]
static VERIFICATION_WATCH: std::sync::OnceLock<std::sync::RwLock<VerificationWatch>> =
    std::sync::OnceLock::new();

#[cfg(any(test, feature = "test-utils"))]
fn verification_watch() -> &'static std::sync::RwLock<VerificationWatch> {
    VERIFICATION_WATCH.get_or_init(|| std::sync::RwLock::new(HashMap::new()))
}

/// Test-only: start counting verification requests for `keys`, from zero.
///
/// Replaces any previous watch rather than adding to it, so the map is bounded
/// by the keys of the test that armed it last and nothing accumulates across a
/// process running many tests.
#[cfg(any(test, feature = "test-utils"))]
pub fn watch_verification_requests_for_test(keys: &[XorName]) {
    if let Ok(mut watch) = verification_watch().write() {
        watch.clear();
        for key in keys {
            watch.insert(*key, HashMap::new());
        }
    }
}

/// Record that `requester` sent a verification request covering `keys`, for
/// whichever of them are watched. A poisoned lock is ignored rather than
/// propagated: this is observation for tests and must never change behaviour.
#[cfg(any(test, feature = "test-utils"))]
pub(crate) fn record_verification_request_sent(requester: &PeerId, keys: &[XorName]) {
    // Fast path under a read lock: with nothing watched — every production
    // build, and every test that did not ask — this is all the sender pays.
    match verification_watch().read() {
        Ok(watch) if watch.is_empty() => return,
        Ok(watch) if !keys.iter().any(|key| watch.contains_key(key)) => return,
        Ok(_) => {}
        Err(_) => return,
    }
    if let Ok(mut watch) = verification_watch().write() {
        for key in keys {
            if let Some(by_peer) = watch.get_mut(key) {
                *by_peer.entry(*requester).or_insert(0) += 1;
            }
        }
    }
}

/// Test-only: how many times `requester` has asked this process about `key`.
/// Zero unless the key was registered with `watch_verification_requests_for_test`.
#[cfg(any(test, feature = "test-utils"))]
#[must_use]
pub fn verification_requests_for_key_from_for_test(requester: &PeerId, key: &XorName) -> usize {
    verification_watch().read().map_or(0, |watch| {
        watch
            .get(key)
            .and_then(|by_peer| by_peer.get(requester))
            .copied()
            .unwrap_or(0)
    })
}

async fn handle_verification_request(
    source: &PeerId,
    request: &protocol::VerificationRequest,
    storage: &Arc<ChunkStore>,
    paid_list: &Arc<PaidList>,
    p2p_node: &Arc<P2PNode>,
    request_id: u64,
    rr_message_id: Option<&str>,
) -> Result<()> {
    #[derive(Clone, Copy)]
    enum CachedPaidLookup {
        NotChecked,
        Checked(Option<bool>),
    }

    #[derive(Clone, Copy)]
    struct CachedVerificationLookup {
        present: Option<bool>,
        paid: CachedPaidLookup,
    }

    if verification_request_exceeds_limit(request.keys.len()) {
        warn!(
            "Verification request from {source} has {} keys, exceeding max {MAX_INCOMING_VERIFICATION_KEYS}; rejecting batch",
            request.keys.len(),
        );
        send_verification_results(source, p2p_node, request_id, Vec::new(), rr_message_id).await;
        return Ok(());
    }

    let requested_keys = request.keys.as_slice();

    if request.paid_list_check_indices.len() > request.keys.len() {
        warn!(
            "Verification request from {source} has {} paid-list indices for {} keys; rejecting batch",
            request.paid_list_check_indices.len(),
            request.keys.len(),
        );
        send_verification_results(source, p2p_node, request_id, Vec::new(), rr_message_id).await;
        return Ok(());
    }

    let keys_len = u32::try_from(requested_keys.len()).unwrap_or(u32::MAX);
    let paid_check_set: HashSet<u32> = request
        .paid_list_check_indices
        .iter()
        .copied()
        .filter(|&idx| {
            if idx >= keys_len {
                warn!(
                    "Verification request from {source}: paid_list_check_index {idx} out of bounds (keys.len() = {})",
                    requested_keys.len(),
                );
                false
            } else {
                true
            }
        })
        .collect();

    let mut results = Vec::with_capacity(requested_keys.len());
    let mut lookup_cache: HashMap<XorName, CachedVerificationLookup> = HashMap::new();
    for (i, key) in requested_keys.iter().enumerate() {
        let needs_paid = paid_check_set.contains(&u32::try_from(i).unwrap_or(u32::MAX));
        let cached = lookup_cache.entry(*key).or_insert_with(|| {
            let present = match storage.exists(key) {
                Ok(present) => Some(present),
                Err(e) => {
                    warn!(
                        "Verification request from {source}: failed to check storage for {}: {e}",
                        hex::encode(key)
                    );
                    None
                }
            };
            CachedVerificationLookup {
                present,
                paid: CachedPaidLookup::NotChecked,
            }
        });

        if needs_paid && matches!(cached.paid, CachedPaidLookup::NotChecked) {
            cached.paid = CachedPaidLookup::Checked(match paid_list.contains(key) {
                Ok(paid) => Some(paid),
                Err(e) => {
                    warn!(
                        "Verification request from {source}: failed to check paid-list for {}: {e}",
                        hex::encode(key)
                    );
                    None
                }
            });
        }

        let paid = if needs_paid {
            match cached.paid {
                CachedPaidLookup::Checked(paid) => paid,
                CachedPaidLookup::NotChecked => None,
            }
        } else {
            None
        };

        if cached.present.is_none() && paid.is_none() {
            continue;
        }

        results.push(protocol::KeyVerificationResult {
            key: *key,
            present: cached.present.unwrap_or(false),
            paid,
        });
    }

    send_verification_results(source, p2p_node, request_id, results, rr_message_id).await;

    Ok(())
}

async fn dispatch_verification_request(
    source: PeerId,
    request: protocol::VerificationRequest,
    ctx: &ReplicationMessageHandlerContext,
    request_id: u64,
    received_at: Instant,
    rr_message_id: Option<&str>,
) -> Result<()> {
    let guard = match admit_bounded_responder(
        &ctx.verification_responder_admission_semaphore,
        &ctx.verification_responder_inflight,
        &source,
        VERIFICATION_RESPONDER_MAX_OUTSTANDING,
        VERIFICATION_RESPONDER_MAX_OUTSTANDING_PER_PEER,
    )
    .await
    {
        Ok(guard) => guard,
        Err(failure) => {
            audit_metrics::record_responder_admission_drop(
                ReplicationResponderClass::Verification,
                failure.reason.into(),
            );
            warn!(
                responder_class = "verification",
                source = %source,
                "Verification response dropped at admission: {failure}"
            );
            return Ok(());
        }
    };

    let worker_semaphore = Arc::clone(&ctx.verification_responder_worker_semaphore);
    let storage = Arc::clone(&ctx.storage);
    let paid_list = Arc::clone(&ctx.paid_list);
    let p2p_node = Arc::clone(&ctx.p2p_node);
    let request_timeout = ctx.config.verification_request_timeout;
    let rr_message_id = rr_message_id.map(ToOwned::to_owned);
    ctx.detached_task_tracker.spawn(async move {
        let _guard = guard;
        let Ok(_worker_permit) = worker_semaphore.acquire_owned().await else {
            debug!(
                responder_class = "verification",
                source = %source,
                "Verification responder worker pool closed before dequeue"
            );
            return;
        };
        if request_is_stale(received_at, request_timeout) {
            audit_metrics::record_responder_staleness_shed(ReplicationResponderClass::Verification);
            debug!(
                responder_class = "verification",
                source = %source,
                request_age_ms = received_at.elapsed().as_millis(),
                "Stale verification request shed at dequeue"
            );
            return;
        }
        if let Err(e) = handle_verification_request(
            &source,
            &request,
            &storage,
            &paid_list,
            &p2p_node,
            request_id,
            rr_message_id.as_deref(),
        )
        .await
        {
            debug!(source = %source, "Verification request handling failed: {e}");
        }
    });

    Ok(())
}

const fn verification_request_exceeds_limit(key_count: usize) -> bool {
    key_count > MAX_INCOMING_VERIFICATION_KEYS
}

async fn send_verification_results(
    source: &PeerId,
    p2p_node: &Arc<P2PNode>,
    request_id: u64,
    results: Vec<protocol::KeyVerificationResult>,
    rr_message_id: Option<&str>,
) {
    send_replication_response(
        source,
        p2p_node,
        request_id,
        ReplicationMessageBody::VerificationResponse(VerificationResponse { results }),
        rr_message_id,
    )
    .await;
}

async fn dispatch_fetch_request(
    source: PeerId,
    request: protocol::FetchRequest,
    ctx: &ReplicationMessageHandlerContext,
    request_id: u64,
    received_at: Instant,
    rr_message_id: Option<&str>,
) -> Result<()> {
    let guard = match admit_bounded_responder(
        &ctx.fetch_responder_admission_semaphore,
        &ctx.fetch_responder_inflight,
        &source,
        FETCH_RESPONDER_MAX_OUTSTANDING,
        FETCH_RESPONDER_MAX_OUTSTANDING_PER_PEER,
    )
    .await
    {
        Ok(guard) => guard,
        Err(failure) => {
            audit_metrics::record_responder_admission_drop(
                ReplicationResponderClass::Fetch,
                failure.reason.into(),
            );
            warn!(
                responder_class = "fetch",
                source = %source,
                "Fetch response dropped at admission: {failure}"
            );
            return Ok(());
        }
    };

    let worker_semaphore = Arc::clone(&ctx.fetch_responder_worker_semaphore);
    let storage = Arc::clone(&ctx.storage);
    let p2p_node = Arc::clone(&ctx.p2p_node);
    let request_timeout = ctx.config.fetch_request_timeout;
    let rr_message_id = rr_message_id.map(ToOwned::to_owned);
    ctx.detached_task_tracker.spawn(async move {
        let _guard = guard;
        let Ok(_worker_permit) = worker_semaphore.acquire_owned().await else {
            debug!(
                responder_class = "fetch",
                source = %source,
                "Fetch responder worker pool closed before dequeue"
            );
            return;
        };
        if request_is_stale(received_at, request_timeout) {
            audit_metrics::record_responder_staleness_shed(ReplicationResponderClass::Fetch);
            debug!(
                responder_class = "fetch",
                source = %source,
                request_age_ms = received_at.elapsed().as_millis(),
                "Stale fetch request shed at dequeue"
            );
            return;
        }
        if let Err(e) = handle_fetch_request(
            &source,
            &request,
            &storage,
            &p2p_node,
            request_id,
            rr_message_id.as_deref(),
        )
        .await
        {
            debug!(source = %source, "Fetch request handling failed: {e}");
        }
    });

    Ok(())
}

fn request_is_stale(received_at: Instant, timeout: Duration) -> bool {
    received_at.elapsed() >= timeout
}

/// Turn the responder's storage read into the answer it sends back.
///
/// The whole distinction the fetch lanes rest on is made here. A key this node does not
/// hold reads as `Ok(None)` and is answered `NotFound`. A read that fails, from an I/O
/// fault, an exhausted descriptor table, or a failed integrity check, is answered `Error`.
/// Nothing about a node giving chunks up produces the second.
fn fetch_response_for(key: XorName, read: Result<Option<Vec<u8>>>) -> protocol::FetchResponse {
    match read {
        Ok(Some(data)) => protocol::FetchResponse::Success { key, data },
        Ok(None) => protocol::FetchResponse::NotFound { key },
        Err(e) => protocol::FetchResponse::Error {
            key,
            reason: format!("{e}"),
        },
    }
}

async fn handle_fetch_request(
    source: &PeerId,
    request: &protocol::FetchRequest,
    storage: &Arc<ChunkStore>,
    p2p_node: &Arc<P2PNode>,
    request_id: u64,
    rr_message_id: Option<&str>,
) -> Result<()> {
    let response = fetch_response_for(request.key, storage.get(&request.key).await);

    send_replication_response(
        source,
        p2p_node,
        request_id,
        ReplicationMessageBody::FetchResponse(response),
        rr_message_id,
    )
    .await;

    Ok(())
}

/// Responder for an incoming `AuditChallenge` (responsible-chunk audit #2, and
/// the prune-confirmation audit, which reuses the same wire message): reply with
/// per-key possession digests.
struct AuditResponderCompletion {
    response_kind: &'static str,
    sent: bool,
    processing: Duration,
    response_send: Duration,
}

async fn handle_audit_challenge_msg(
    source: &PeerId,
    challenge: &protocol::AuditChallenge,
    storage: &Arc<ChunkStore>,
    p2p_node: &Arc<P2PNode>,
    is_bootstrapping: bool,
    reply: ReplyRoute<'_>,
) -> Result<AuditResponderCompletion> {
    #[allow(clippy::cast_possible_truncation)]
    let stored_chunks = storage.current_chunks().map_or(0, |c| c as usize);
    info!(
        "Audit challenge received: kind=responsible keys={} bootstrapping={} request_response={}",
        challenge.keys.len(),
        is_bootstrapping,
        reply.rr_message_id.is_some(),
    );

    let processing_started = Instant::now();
    let response = audit::handle_audit_challenge(
        challenge,
        storage,
        p2p_node.peer_id(),
        is_bootstrapping,
        stored_chunks,
    )
    .await;
    let processing = processing_started.elapsed();
    let response_kind = audit_response_kind(&response);

    let response_send_started = Instant::now();
    let sent = send_replication_response_checked(
        source,
        p2p_node,
        reply.request_id,
        ReplicationMessageBody::AuditResponse(response),
        reply.rr_message_id,
        Some(reply.protocol),
    )
    .await;
    let response_send = response_send_started.elapsed();

    Ok(AuditResponderCompletion {
        response_kind,
        sent,
        processing,
        response_send,
    })
}

#[allow(clippy::too_many_arguments)]
fn log_audit_responder_completion(
    metrics: &AuditResponderMetrics,
    source: PeerId,
    class: AuditResponderClass,
    kind: &'static str,
    challenge_id: u64,
    work_items: usize,
    response_kind: &'static str,
    sent: bool,
    received_at: Instant,
    worker_started: Instant,
    processing: Duration,
    response_send: Duration,
) {
    let dispatch = worker_started.saturating_duration_since(received_at);
    let total = received_at.elapsed();
    metrics.record_completed(source, processing, total, sent);
    if sent {
        info!(
            target: "ant_node::replication::audit_responder",
            event = "completed",
            kind,
            responder_class = class.as_str(),
            source = %source,
            challenge_id,
            work_items,
            response = response_kind,
            sent,
            dispatch_ms = dispatch.as_millis(),
            processing_ms = processing.as_millis(),
            response_send_ms = response_send.as_millis(),
            total_ms = total.as_millis(),
            "Audit responder request completed"
        );
    } else {
        warn!(
            target: "ant_node::replication::audit_responder",
            event = "completed",
            kind,
            responder_class = class.as_str(),
            source = %source,
            challenge_id,
            work_items,
            response = response_kind,
            sent,
            dispatch_ms = dispatch.as_millis(),
            processing_ms = processing.as_millis(),
            response_send_ms = response_send.as_millis(),
            total_ms = total.as_millis(),
            "Audit responder request completed without sending a reply"
        );
    }
}

/// Where a reply goes and how it is addressed.
///
/// Grouped because the audit responder needs all three together and one of them
/// is not derivable from the body: during the rollout window a possession reply
/// may have to go out on the core id rather than its family's own (see
/// [`is_legacy_possession_challenge`]).
struct ReplyRoute<'a> {
    /// Correlates the reply with the challenge, at the replication layer.
    request_id: u64,
    /// Present when the challenge arrived over request-response, in which case
    /// the reply rides the same exchange back.
    rr_message_id: Option<&'a str>,
    /// The protocol id to answer on.
    protocol: &'static str,
}

fn audit_response_kind(response: &protocol::AuditResponse) -> &'static str {
    match response {
        protocol::AuditResponse::Digests { .. } => "digests",
        protocol::AuditResponse::Bootstrapping { .. } => "bootstrapping",
        protocol::AuditResponse::Rejected { .. } => "rejected",
    }
}

fn subtree_audit_response_kind(response: &protocol::SubtreeAuditResponse) -> &'static str {
    match response {
        protocol::SubtreeAuditResponse::Proof { .. } => "proof",
        protocol::SubtreeAuditResponse::Bootstrapping { .. } => "bootstrapping",
        protocol::SubtreeAuditResponse::Rejected { .. } => "rejected",
    }
}

fn subtree_audit_response_work_items(response: &protocol::SubtreeAuditResponse) -> usize {
    match response {
        protocol::SubtreeAuditResponse::Proof { proof, .. } => proof.leaves.len(),
        protocol::SubtreeAuditResponse::Bootstrapping { .. }
        | protocol::SubtreeAuditResponse::Rejected { .. } => 0,
    }
}

fn subtree_slice_response_kind(response: &protocol::SubtreeSliceResponse) -> &'static str {
    match response {
        protocol::SubtreeSliceResponse::Items { .. } => "items",
        protocol::SubtreeSliceResponse::Bootstrapping { .. } => "bootstrapping",
        protocol::SubtreeSliceResponse::Rejected { .. } => "rejected",
    }
}

fn commitment_pin_response_kind(response: &protocol::GetCommitmentByPinResponse) -> &'static str {
    match response {
        protocol::GetCommitmentByPinResponse::Found { .. } => "found",
        protocol::GetCommitmentByPinResponse::NotRetained { .. } => "not_retained",
    }
}

// ---------------------------------------------------------------------------
// Message sending helper
// ---------------------------------------------------------------------------

/// Send a replication response message as a best-effort reply.
///
/// Encode and send failures are logged by the checked helper. Most response
/// paths do not need to branch on send success, so this wrapper keeps those
/// call sites explicit about their best-effort behavior.
async fn send_replication_response(
    peer: &PeerId,
    p2p_node: &Arc<P2PNode>,
    request_id: u64,
    body: ReplicationMessageBody,
    rr_message_id: Option<&str>,
) {
    let _ =
        send_replication_response_checked(peer, p2p_node, request_id, body, rr_message_id, None)
            .await;
}

/// Send a replication response message and report whether it was accepted.
///
/// Returns `true` after the message is encoded and accepted by the P2P send
/// path. Returns `false` after logging an encode or send failure. Repair-proof
/// recording uses this to avoid trusting hints that were not actually sent.
///
/// When `rr_message_id` is `Some`, the response is sent via the `/rr/`
/// request-response path so saorsa-core can route it back to the caller's
/// `send_request` future. Otherwise it is sent as a plain message.
///
/// `protocol` overrides the id the body would route on by family. Exactly one
/// caller needs it: a possession challenge from a peer that predates the family
/// split has to be answered where it was asked (see
/// [`is_legacy_possession_challenge`]). Everything else passes `None` and gets
/// [`response_protocol_for`], which is what keeps send and receive symmetric.
async fn send_replication_response_checked(
    peer: &PeerId,
    p2p_node: &Arc<P2PNode>,
    request_id: u64,
    body: ReplicationMessageBody,
    rr_message_id: Option<&str>,
    protocol: Option<&'static str>,
) -> bool {
    let msg = ReplicationMessage { request_id, body };
    let encoded = match msg.encode() {
        Ok(data) => data,
        Err(e) => {
            warn!("Failed to encode replication response: {e}");
            return false;
        }
    };
    // V2-684: per-peer served-bytes attribution for the heavy serve paths.
    // `FetchResponse` carries ~99% of served bytes; `NeighborSyncResponse` is
    // included for completeness. Other response variants (verification/audit/
    // commitment) are intentionally excluded — the round-2 audit reply is now a
    // few-KB verified slice (V2-685), not a full-chunk transfer, so it is light.
    if matches!(
        msg.body,
        ReplicationMessageBody::FetchResponse(_) | ReplicationMessageBody::NeighborSyncResponse(_)
    ) {
        protocol::record_served(peer, encoded.len());
    }
    let protocol = protocol.unwrap_or_else(|| response_protocol_for(&msg.body));
    let result = if let Some(msg_id) = rr_message_id {
        p2p_node
            .send_response(peer, protocol, msg_id, encoded)
            .await
    } else {
        p2p_node.send_message(peer, protocol, encoded, &[]).await
    };
    if let Err(e) = result {
        debug!("Failed to send replication response to {peer}: {e}");
        return false;
    }
    true
}

async fn record_sent_replica_hints(
    peer: &PeerId,
    hints: &[neighbor_sync::SentReplicaHint],
    repair_proofs: &Arc<RwLock<RepairProofs>>,
    sync_cycle_epoch: &Arc<RwLock<u64>>,
) {
    if hints.is_empty() {
        return;
    }

    let hinted_at_epoch = *sync_cycle_epoch.read().await;
    let mut proofs = repair_proofs.write().await;
    for hint in hints {
        if proofs.record_replica_hint_sent(*peer, hint.key, &hint.close_peers, hinted_at_epoch) {
            debug!(
                "Recorded repair hint proof for peer {peer} and key {}",
                hex::encode(hint.key)
            );
        }
    }
}

// ---------------------------------------------------------------------------
// Neighbor sync round
// ---------------------------------------------------------------------------

/// Run one neighbor sync round.
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
async fn run_neighbor_sync_round(
    p2p_node: &Arc<P2PNode>,
    storage: &Arc<ChunkStore>,
    paid_list: &Arc<PaidList>,
    queues: &Arc<RwLock<ReplicationQueues>>,
    config: &ReplicationConfig,
    sync_state: &Arc<RwLock<NeighborSyncState>>,
    sync_history: &Arc<RwLock<HashMap<PeerId, PeerSyncRecord>>>,
    sync_cycle_epoch: &Arc<RwLock<u64>>,
    repair_proofs: &Arc<RwLock<RepairProofs>>,
    is_bootstrapping: &Arc<RwLock<bool>>,
    bootstrap_state: &Arc<RwLock<BootstrapState>>,
    commitment_state: &Arc<ResponderCommitmentState>,
    last_commitment_by_peer: &Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>>,
    ever_capable_peers: &Arc<RwLock<HashSet<PeerId>>>,
    sig_verify_attempts: &Arc<RwLock<HashMap<PeerId, Instant>>>,
    audit_challenge_coordinator: &Arc<AuditChallengeCoordinator>,
    gossip_audit: &GossipAuditTrigger,
    pointers: Option<&Arc<pointer::PointerReplication>>,
) {
    let self_id = *p2p_node.peer_id();
    let bootstrapping = *is_bootstrapping.read().await;

    // Check if cycle is complete; start new one if needed.
    // We check under a read lock, then release it before the expensive
    // prune pass and DHT snapshot so other tasks are not starved.
    let cycle_complete = sync_state.read().await.is_cycle_complete();
    if cycle_complete {
        // A completed local neighbor-sync cycle advances the epoch component
        // of repair-proof maturity. The per-key wall-clock minimum age is
        // checked when audits are selected.
        {
            let mut history = sync_history.write().await;
            for record in history.values_mut() {
                record.cycles_since_sync = record.cycles_since_sync.saturating_add(1);
            }
        }
        {
            let mut epoch = sync_cycle_epoch.write().await;
            *epoch = epoch.saturating_add(1);
        }

        // Post-cycle pruning (Section 11) — runs without holding sync_state.
        // Prune candidacy is unconditional once the hysteresis elapses;
        // bootstrap state only defers the remote prune-confirmation audits
        // until bootstrap has drained.
        let allow_remote_prune_audits = !bootstrapping && bootstrap_state.read().await.is_drained();
        pruning::run_prune_pass_with_context(pruning::PrunePassContext {
            self_id: &self_id,
            storage,
            paid_list,
            p2p_node,
            config,
            sync_state,
            repair_proofs,
            allow_remote_prune_audits,
            commitment_state: Some(commitment_state),
            audit_challenge_coordinator,
        })
        .await;
        if let Some(pointers) = pointers {
            pointers
                .prune_pass(allow_remote_prune_audits, Some(commitment_state))
                .await;
        }

        // Take fresh close-neighbor snapshot (DHT query, no lock held).
        let neighbors =
            neighbor_sync::snapshot_close_neighbors(p2p_node, &self_id, config.neighbor_sync_scope)
                .await;

        // Now re-acquire write lock and re-check before swapping cycle.
        let mut state = sync_state.write().await;
        if state.is_cycle_complete() {
            // Preserve cooldown and bootstrap-claim tracking across cycles.
            // Claims have a 24h lifecycle vs 10-20 min cycles — dropping them
            // would reset the abuse detection timer every cycle.
            let old_sync_times = std::mem::take(&mut state.last_sync_times);
            let old_bootstrap_claims = std::mem::take(&mut state.bootstrap_claims);
            let old_bootstrap_claim_history = std::mem::take(&mut state.bootstrap_claim_history);
            let old_prune_cursor = state.prune_cursor;
            *state = NeighborSyncState::new_cycle(neighbors);
            state.last_sync_times = old_sync_times;
            state.bootstrap_claims = old_bootstrap_claims;
            state.bootstrap_claim_history = old_bootstrap_claim_history;
            state.prune_cursor = old_prune_cursor;
        }
    }

    // Select batch of peers.
    let batch = {
        let mut state = sync_state.write().await;
        neighbor_sync::select_sync_batch(
            &mut state,
            config.neighbor_sync_peer_count,
            config.neighbor_sync_cooldown,
        )
    };

    if batch.is_empty() {
        return;
    }

    debug!("Neighbor sync: syncing with {} peers", batch.len());
    if let Some(pointers) = pointers {
        pointers.push_hints_detached(batch.clone());
    }

    // Snapshot our current commitment once per round so all peers in
    // this batch see the same thing (gossip is the responder's attestation;
    // same value across the batch is fine and reduces RwLock churn). Atomically
    // snapshot + mark-gossiped so we stay answerable for exactly what we emit
    // (ADR-0002 retention), with no TOCTOU vs a concurrent retire/rotate.
    let my_commitment = commitment_state
        .current_for_gossip()
        .map(|b| b.commitment().clone());

    let mut hints_by_peer = neighbor_sync::build_sync_hints_for_peers(
        &batch,
        storage,
        paid_list,
        p2p_node,
        config.close_group_size,
        config.paid_list_close_group_size,
    )
    .await;

    // Sync with each peer in the batch.
    for peer in &batch {
        let hints = hints_by_peer.remove(peer).unwrap_or_default();
        let outcome = neighbor_sync::sync_with_peer_with_hints(
            peer,
            p2p_node,
            config,
            bootstrapping,
            hints,
            my_commitment.clone(),
        )
        .await;

        if let Some(outcome) = outcome {
            handle_sync_response(
                &self_id,
                peer,
                &outcome.response,
                &outcome.sent_replica_hints,
                p2p_node,
                config,
                bootstrapping,
                bootstrap_state,
                storage,
                paid_list,
                queues,
                sync_state,
                sync_history,
                sync_cycle_epoch,
                repair_proofs,
                last_commitment_by_peer,
                ever_capable_peers,
                sig_verify_attempts,
                gossip_audit,
            )
            .await;
        } else {
            // Sync failed -- remove peer and try to fill slot.
            let replacement = {
                let mut state = sync_state.write().await;
                neighbor_sync::handle_sync_failure(&mut state, peer, config.neighbor_sync_cooldown)
            };

            // Attempt sync with the replacement peer (if one was found).
            if let Some(replacement_peer) = replacement {
                let mut replacement_hints = neighbor_sync::build_sync_hints_for_peers(
                    std::slice::from_ref(&replacement_peer),
                    storage,
                    paid_list,
                    p2p_node,
                    config.close_group_size,
                    config.paid_list_close_group_size,
                )
                .await;
                let hints = replacement_hints
                    .remove(&replacement_peer)
                    .unwrap_or_default();
                let replacement_outcome = neighbor_sync::sync_with_peer_with_hints(
                    &replacement_peer,
                    p2p_node,
                    config,
                    bootstrapping,
                    hints,
                    my_commitment.clone(),
                )
                .await;

                if let Some(outcome) = replacement_outcome {
                    handle_sync_response(
                        &self_id,
                        &replacement_peer,
                        &outcome.response,
                        &outcome.sent_replica_hints,
                        p2p_node,
                        config,
                        bootstrapping,
                        bootstrap_state,
                        storage,
                        paid_list,
                        queues,
                        sync_state,
                        sync_history,
                        sync_cycle_epoch,
                        repair_proofs,
                        last_commitment_by_peer,
                        ever_capable_peers,
                        sig_verify_attempts,
                        gossip_audit,
                    )
                    .await;
                }
            }
        }
    }
}

/// Process a successful neighbor sync response: record the sync, check for
/// bootstrap claim abuse, and admit inbound hints.
#[allow(clippy::too_many_arguments)]
async fn handle_sync_response(
    self_id: &PeerId,
    peer: &PeerId,
    resp: &NeighborSyncResponse,
    sent_replica_hints: &[neighbor_sync::SentReplicaHint],
    p2p_node: &Arc<P2PNode>,
    config: &ReplicationConfig,
    bootstrapping: bool,
    bootstrap_state: &Arc<RwLock<BootstrapState>>,
    storage: &Arc<ChunkStore>,
    paid_list: &Arc<PaidList>,
    queues: &Arc<RwLock<ReplicationQueues>>,
    sync_state: &Arc<RwLock<NeighborSyncState>>,
    sync_history: &Arc<RwLock<HashMap<PeerId, PeerSyncRecord>>>,
    sync_cycle_epoch: &Arc<RwLock<u64>>,
    repair_proofs: &Arc<RwLock<RepairProofs>>,
    last_commitment_by_peer: &Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>>,
    ever_capable_peers: &Arc<RwLock<HashSet<PeerId>>>,
    sig_verify_attempts: &Arc<RwLock<HashMap<PeerId, Instant>>>,
    gossip_audit: &GossipAuditTrigger,
) {
    // Ingest the peer's commitment if they piggybacked one on the response.
    // Same verification as the request path (peer-id binding + signature);
    // forged commitments are dropped at the edge. A *changed* commitment here
    // is a gossip-audit trigger just like on the request path — so a peer that
    // only ever answers sync (never initiates) is still audited (ADR-0002).
    if let Some(target) = ingest_peer_commitment(
        peer,
        resp.commitment.as_ref(),
        p2p_node,
        last_commitment_by_peer,
        ever_capable_peers,
        sig_verify_attempts,
    )
    .await
    {
        maybe_trigger_gossip_audit(gossip_audit, peer, target).await;
    }

    // Record successful sync.
    {
        let mut state = sync_state.write().await;
        neighbor_sync::record_successful_sync(&mut state, peer);
    }
    {
        let mut history = sync_history.write().await;
        let record = history.entry(*peer).or_insert(PeerSyncRecord {
            last_sync: None,
            cycles_since_sync: 0,
        });
        record.last_sync = Some(Instant::now());
        record.cycles_since_sync = 0;
    }

    // Process inbound hints from response (skip if peer is bootstrapping).
    if resp.bootstrapping {
        // Gap 6: BootstrapClaimAbuse grace period enforcement.
        // Separate state mutation from network I/O to avoid holding the
        // write lock across report_trust_event.
        let should_report = {
            let now = Instant::now();
            let mut state = sync_state.write().await;
            match state.observe_bootstrap_claim(*peer, now, config.bootstrap_claim_grace_period) {
                BootstrapClaimObservation::WithinGrace { .. } => false,
                BootstrapClaimObservation::PastGrace { first_seen } => {
                    warn!(
                        "Peer {peer} has been claiming bootstrap for {:?}, \
                         exceeding grace period of {:?} — reporting abuse",
                        now.duration_since(first_seen),
                        config.bootstrap_claim_grace_period,
                    );
                    true
                }
                BootstrapClaimObservation::Repeated { first_seen } => {
                    warn!(
                        "Peer {peer} repeated bootstrap claim after previously stopping; \
                         first claim was {:?} ago — reporting abuse",
                        now.duration_since(first_seen),
                    );
                    true
                }
            }
        };
        if should_report {
            p2p_node
                .report_trust_event(
                    peer,
                    TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                )
                .await;
        }
    } else {
        // Peer is not claiming bootstrap; clear active claim while retaining
        // history so the peer cannot start a second grace window later.
        {
            let mut state = sync_state.write().await;
            state.clear_active_bootstrap_claim(peer);
        }
        record_sent_replica_hints(peer, sent_replica_hints, repair_proofs, sync_cycle_epoch).await;
        let outcome = admit_and_queue_hints(
            self_id,
            peer,
            &resp.replica_hints,
            &resp.paid_hints,
            p2p_node,
            config,
            storage,
            paid_list,
            queues,
        )
        .await;

        // Track discovered keys for bootstrap drain detection so that hints
        // admitted via regular neighbor sync are not missed. Capacity-
        // rejected hints keep this source on the "not yet drained" list
        // until its next sync replays them; a clean cycle clears it.
        if bootstrapping {
            let live_discovered = outcome.discovered.clone();
            let outcomes = [(*peer, outcome)];
            publish_bootstrap_admission_outcomes(bootstrap_state, &outcomes, &live_discovered)
                .await;
        }
    }
}

/// Admit hints and queue them for verification, returning newly-discovered keys.
///
/// Shared by neighbor-sync request handling, response handling, and bootstrap
/// sync so that admission + queueing logic lives in one place.
#[allow(clippy::too_many_arguments)]
/// Outcome of [`admit_and_queue_hints`].
///
/// `capacity_rejected_count` tracks incoming hints for which no fair slot was
/// available; that source is kept outstanding until it re-hints. `displaced`
/// tracks older borrowed hints reclaimed for another sender; only the key is
/// dropped from bootstrap tracking — the former owner's standing is untouched
/// (see [`publish_bootstrap_admission_outcomes`]).
struct AdmissionOutcome {
    discovered: HashSet<XorName>,
    capacity_rejected_count: usize,
    displaced: Vec<CapacityDisplacement>,
}

/// Publish one atomic admission unit into bootstrap drain accounting.
///
/// Only sources that were themselves capacity-rejected are kept outstanding.
/// A fairness displacement forfeits the displaced *key* but never stamps its
/// former owner: reclaiming a borrowed slot is this node's own fairness
/// decision, so charging it to the victim would let a sustained flooder wedge
/// our bootstrap drain open through an unrelated honest peer. Clean sources
/// are cleared only after every outcome in the unit is known, so peer
/// iteration order cannot falsely clear a rejection recorded later in the same
/// bootstrap batch.
async fn publish_bootstrap_admission_outcomes(
    bootstrap_state: &Arc<RwLock<BootstrapState>>,
    outcomes: &[(PeerId, AdmissionOutcome)],
    live_discovered: &HashSet<XorName>,
) {
    let mut rejected_sources = HashSet::new();
    let mut clean_sources = HashSet::new();
    let mut displaced_keys = HashSet::new();

    for (source, outcome) in outcomes {
        if outcome.capacity_rejected_count > 0 {
            rejected_sources.insert(*source);
        } else {
            clean_sources.insert(*source);
        }
        // Only the *key* of a fairness displacement is forfeited here, never
        // its owner's standing. Reclaiming a borrowed slot is our own fairness
        // decision, not a failure by the peer that held it: stamping the victim
        // would let a sustained flooder block our bootstrap drain through an
        // unrelated honest peer that never overflowed us. The displaced key
        // falls to the same post-bootstrap neighbor-sync and audit/repair
        // recovery path that TTL expiry already relies on.
        for displacement in &outcome.displaced {
            displaced_keys.insert(displacement.key);
        }
    }
    clean_sources.retain(|source| !rejected_sources.contains(source));

    let now = Instant::now();
    let mut state = bootstrap_state.write().await;
    for key in displaced_keys {
        state.remove_key(&key);
    }
    state.pending_keys.extend(live_discovered);
    for source in clean_sources {
        state.capacity_rejected_sources.remove(&source);
    }
    for source in rejected_sources {
        // First-seen: a source that keeps overflowing us must not keep its own
        // record fresh, or bootstrap never drains and auditing stays off.
        state.note_capacity_rejected(source, now);
    }
}

#[allow(clippy::too_many_arguments)]
async fn admit_and_queue_hints(
    self_id: &PeerId,
    source_peer: &PeerId,
    replica_hints: &[XorName],
    paid_hints: &[XorName],
    p2p_node: &Arc<P2PNode>,
    config: &ReplicationConfig,
    storage: &Arc<ChunkStore>,
    paid_list: &Arc<PaidList>,
    queues: &Arc<RwLock<ReplicationQueues>>,
) -> AdmissionOutcome {
    let pending_keys: HashSet<XorName> = {
        let q = queues.read().await;
        q.pending_keys().into_iter().collect()
    };

    let admitted = admission::admit_hints(
        self_id,
        replica_hints,
        paid_hints,
        p2p_node,
        config,
        storage,
        paid_list,
        &pending_keys,
    )
    .await;

    let mut q = queues.write().await;
    queue_admitted_hints(source_peer, admitted, storage, &mut q)
}

fn queue_admitted_hints(
    source_peer: &PeerId,
    admitted: admission::AdmissionResult,
    storage: &ChunkStore,
    q: &mut ReplicationQueues,
) -> AdmissionOutcome {
    let mut discovered = HashSet::new();
    let mut capacity_rejected_count: usize = 0;
    let mut displaced = Vec::new();
    let now = Instant::now();

    for key in admitted.replica_keys {
        if !storage.exists(&key).unwrap_or(false) {
            let result = q.add_pending_verify(
                key,
                VerificationEntry {
                    state: VerificationState::PendingVerify,
                    verified_sources: Vec::new(),
                    tried_sources: HashSet::new(),
                    created_at: now,
                    next_verify_at: now,
                    hint_sources: HashSet::from([*source_peer]),
                    // Non-empty: this peer claimed possession, so it is a
                    // fetch-source candidate. Derives HintPipeline::Replica.
                    replica_hint_sources: HashSet::from([*source_peer]),
                    unresolved_retries: 0,
                    no_holder_reported: false,
                },
            );
            match result {
                crate::replication::scheduling::AdmissionResult::Admitted => {
                    discovered.insert(key);
                }
                crate::replication::scheduling::AdmissionResult::AlreadyPresent => {}
                crate::replication::scheduling::AdmissionResult::CapacityRejected => {
                    capacity_rejected_count += 1;
                }
            }
        }
    }

    for key in admitted.paid_only_keys {
        let result = q.add_pending_verify(
            key,
            VerificationEntry {
                state: VerificationState::PendingVerify,
                verified_sources: Vec::new(),
                tried_sources: HashSet::new(),
                created_at: now,
                next_verify_at: now,
                hint_sources: HashSet::from([*source_peer]),
                // Empty: a paid hint makes no possession claim, so this peer is
                // not a fetch source. Derives HintPipeline::PaidOnly.
                replica_hint_sources: HashSet::new(),
                unresolved_retries: 0,
                no_holder_reported: false,
            },
        );
        match result {
            crate::replication::scheduling::AdmissionResult::Admitted => {
                discovered.insert(key);
            }
            crate::replication::scheduling::AdmissionResult::AlreadyPresent => {}
            crate::replication::scheduling::AdmissionResult::CapacityRejected => {
                capacity_rejected_count += 1;
            }
        }
    }

    if capacity_rejected_count > 0 {
        debug!(
            "admit_and_queue_hints from {source_peer}: {capacity_rejected_count} hints \
             rejected at queue capacity; source will need to re-hint after pending_verify drains"
        );
    }

    displaced.extend(q.take_capacity_displacements());

    AdmissionOutcome {
        discovered,
        capacity_rejected_count,
        displaced,
    }
}

// ---------------------------------------------------------------------------
// Verification cycle
// ---------------------------------------------------------------------------

/// Run one verification cycle: process pending keys through quorum checks.
#[allow(clippy::too_many_lines)]
async fn run_verification_cycle(ctx: VerificationCycleContext<'_>) {
    let cycle_started = Instant::now();
    let VerificationCycleContext {
        p2p_node,
        paid_list,
        storage,
        queues,
        config,
        bootstrap_state,
        is_bootstrapping,
        bootstrap_complete_notify,
        last_commitment_by_peer,
        ever_capable_peers,
        recent_provers,
    } = ctx;

    // Self-heal the bootstrap drain before any early-return below: abandoned
    // capacity-rejection records must expire, and a drain condition that
    // became true without a triggering event must still be observed. Pending
    // peer requests legitimately block the drain check itself, but not this.
    expire_and_recheck_bootstrap_drain(
        bootstrap_state,
        queues,
        is_bootstrapping,
        bootstrap_complete_notify,
        config.capacity_rejected_max_age(),
    )
    .await;

    // Bootstrap admits one concurrent neighbor batch as an atomic source
    // aggregation unit. Do not select newly queued keys until that batch's
    // hints and drain accounting have both been published.
    if bootstrap_state.read().await.pending_peer_requests > 0 {
        return;
    }

    // Evict stale entries that have been pending too long (e.g. unreachable
    // verification targets during a network partition).
    let stale_pending_keys = {
        let mut q = queues.write().await;
        q.evict_stale(config::PENDING_VERIFY_MAX_AGE)
    };
    if !stale_pending_keys.is_empty() {
        update_bootstrap_after_verification(
            &stale_pending_keys,
            bootstrap_state,
            queues,
            is_bootstrapping,
            bootstrap_complete_notify,
        )
        .await;
    }

    let pending_keys = {
        let mut q = queues.write().await;
        // Re-check while holding the queue write lock used by fair selection.
        // This closes the race
        // where a bootstrap batch starts after the early check: the batch
        // cannot publish its hints under the queue write lock until this
        // selection either returns or declines to run.
        if bootstrap_state.read().await.pending_peer_requests > 0 {
            return;
        }
        q.select_ready_pending_keys(Instant::now(), MAX_VERIFICATION_KEYS_PER_CYCLE)
    };

    if pending_keys.is_empty() {
        return;
    }
    let initial_pending_count = pending_keys.len();

    // Capacity verdict for this cycle — the same pre-check the PUT handler and
    // the fresh-offer path already run (V2-411), read once and reused below.
    //
    // It gates only the two steps that exist to enable a fetch: the presence
    // probe that discovers holders, and the promotion that queues the download.
    // Everything a cycle does that does not need a local write still runs on a
    // full node — the local paid-list fast path, `PaidForList` convergence
    // through the quorum round, and the terminal checks that retire a key this
    // node already holds or is no longer admitted for. Gating earlier than this
    // would stop a full node learning which of its keys were paid for, and
    // would strand keys that should have retired, which keeps bootstrap drain
    // pending and audits disabled until stale eviction.
    // Only a *full* disk is a standing condition worth minutes of backoff. A
    // failed `statvfs` says nothing about available space and may have cleared
    // by the next cycle, so it is treated as writable here and left to the
    // pre-check at the dial, which queries again and may well permit the write.
    let write_blocked = storage.capacity_verdict() == CapacityVerdict::Full;
    // Counted per gate rather than combined. `local_paid_probe` below counts
    // probes actually sent, so it reads zero once the first gate fires; keeping
    // the two deferral counts apart is what lets an operator see which branch a
    // full node's keys are taking without adding a code path to find out.
    let mut capacity_deferred_probe = 0usize;
    let mut capacity_deferred_promote = 0usize;

    let self_id = *p2p_node.peer_id();

    // Step 1: Check local PaidForList for fast-path authorization (Section 9,
    // step 4). Paid-list membership settles *validity* — the key is known-paid,
    // so no quorum round is needed. It says nothing about whether we must hold
    // the bytes; that is decided below.
    let mut local_paid_keys = Vec::new();
    let mut keys_needing_network = Vec::new();
    let mut terminal_keys: Vec<XorName> = Vec::new();
    {
        let mut q = queues.write().await;
        for key in &pending_keys {
            if paid_list.contains(key).unwrap_or(false) {
                if q.set_pending_state(key, VerificationState::PaidListVerified) {
                    local_paid_keys.push(*key);
                }
            } else {
                keys_needing_network.push(*key);
            }
        }
    }

    // Storage responsibility is a live routing question, decided identically
    // for every known-paid key regardless of how the advertising peer labelled
    // its hint — a replica hint is a possession *claim* by the sender, never
    // permission for us to store. Held outside the queue lock: `is_responsible`
    // awaits into the DHT manager.
    let mut local_paid_presence_probe_keys = Vec::new();
    if !local_paid_keys.is_empty() {
        let mut terminal_paid = Vec::new();
        for key in local_paid_keys {
            if storage.exists(&key).unwrap_or(false) {
                terminal_paid.push(key);
            } else if is_storage_admitted(&self_id, &key, p2p_node, config).await {
                // We carry storage responsibility and lack the bytes. The
                // presence probe below discovers holders to fetch from; it is
                // source discovery, not re-verification.
                local_paid_presence_probe_keys.push(key);
            } else {
                terminal_paid.push(key);
            }
        }

        if !terminal_paid.is_empty() {
            let mut q = queues.write().await;
            for key in terminal_paid {
                q.remove_pending(&key);
                terminal_keys.push(key);
            }
        }

        // Capacity gate, first of two. Everything above this point has already
        // run: authorization succeeded via the local `PaidForList` hit, and the
        // keys that should retire (already held, or no longer storage-admitted)
        // have retired. What is left is a probe whose only purpose is finding a
        // holder to download from, and this node cannot write what it would
        // download.
        if write_blocked && !local_paid_presence_probe_keys.is_empty() {
            let mut q = queues.write().await;
            for key in std::mem::take(&mut local_paid_presence_probe_keys) {
                if q.defer_pending(&key, config::CAPACITY_BLOCKED_RETRY) {
                    capacity_deferred_probe += 1;
                }
            }
        }
    }

    let local_paid_probe_count = local_paid_presence_probe_keys.len();
    let keys_needing_network_count = keys_needing_network.len();
    // Keys this cycle put back because no peer claimed possession. Reported in
    // aggregate below; the per-key line is warned once per entry, not per retry.
    let mut no_holder_deferrals = 0usize;

    // Step 1b: Local paid-list hit for fetch-eligible keys. Per Section 9
    // step 4, authorization succeeds immediately; run a presence-only probe
    // to find any holder we can fetch from.
    if !local_paid_presence_probe_keys.is_empty() {
        let targets = quorum::compute_presence_targets(
            &local_paid_presence_probe_keys,
            p2p_node,
            config,
            &self_id,
        )
        .await;
        let evidence = quorum::run_verification_round(
            &local_paid_presence_probe_keys,
            &targets,
            p2p_node,
            config,
        )
        .await;

        let mut q = queues.write().await;
        for key in local_paid_presence_probe_keys {
            if storage.exists(&key).unwrap_or(false) {
                q.remove_pending(&key);
                terminal_keys.push(key);
                continue;
            }
            let mut sources = evidence.get(&key).map_or_else(Vec::new, |ev| {
                quorum::present_sources_for_key(&key, ev, &targets)
            });
            if let Some(entry) = q.get_pending(&key) {
                add_replica_hint_sources(&mut sources, &entry.replica_hint_sources);
            }
            if sources.is_empty() {
                no_holder_deferrals += 1;
                let outcome = q.defer_unresolved(&key, config.verification_request_timeout);
                let first_report = q.claim_no_holder_report(&key);
                report_unresolved_deferral("Locally paid key", &key, outcome, first_report);
            } else {
                // A holder answered, so whatever came before is not a run of
                // consecutive failures any more. Clear before promoting: if the
                // fetch queue is full the entry stays pending, and it must not
                // carry the old backoff.
                q.clear_unresolved(&key);
                let distance = crate::client::xor_distance(&key, p2p_node.peer_id().as_bytes());
                // Atomic remove+enqueue: if fetch_queue is at capacity, the
                // pending entry is preserved and retried next cycle (no
                // silent drop of verified replica-repair work).
                let _ = q.promote_pending_to_fetch(key, distance, sources);
            }
        }
    }

    // Steps 2-5: Network verification (skipped if all keys resolved locally).
    if !keys_needing_network.is_empty() {
        // Step 2: Compute targets and run network verification round.
        let targets =
            quorum::compute_verification_targets(&keys_needing_network, p2p_node, config, &self_id)
                .await;

        let evidence =
            quorum::run_verification_round(&keys_needing_network, &targets, p2p_node, config).await;

        // Step 3: Evaluate results — collect outcomes without holding the write
        // lock across paid-list I/O.
        //
        // v12 §6 holder-eligibility: snapshot the per-peer last-commitment
        // table and recent_provers cache up front so the synchronous
        // evaluate_key_evidence_with_holder_check predicate can consult
        // them without awaiting. The predicate downgrades a Present
        // claim to Unresolved unless the peer is credited for that key.
        // Snapshot per-peer commitment data. We need two views:
        //   - `commitment_by_peer_snapshot`: peers that currently have
        //     a verified commitment record on file (used to look up
        //     their current hash).
        //   - `capable_peer_snapshot`: the sticky "ever v12-capable"
        //     set. Sourced from a separate set rather than the
        //     commitment map so eviction (PeerRemoved cleanup, sybil
        //     cap at `MAX_LAST_COMMITMENT_BY_PEER`) does NOT downgrade
        //     a previously-v12 peer to "legacy" credit-unconditionally.
        //     Legacy / pre-v12 peers that have never sent a commitment
        //     remain absent from the set and are credited via the
        //     legacy path so mixed-version networks stay live.
        let commitment_by_peer_snapshot: HashMap<PeerId, [u8; 32]> = {
            let map = last_commitment_by_peer.read().await;
            map.iter()
                // Read the CACHED hash (§13) — no per-cycle re-serialize/re-hash
                // of every peer's ~5 KiB commitment.
                .filter_map(|(p, rec)| rec.commitment_hash().map(|h| (*p, h)))
                .collect()
        };
        let capable_peer_snapshot: HashSet<PeerId> = ever_capable_peers.read().await.clone();
        // Take a full snapshot of recent_provers under the read lock,
        // then release. The cache is bounded (16/key × keys), so the
        // clone is cheap.
        let provers_snapshot = recent_provers.read().await.clone();
        // For the replica-fetch path, we need to know whether THIS
        // node already holds the key being verified. The v12 §6
        // holder-credit gate is meant to prevent uncredited Present
        // claims from contributing to paid-list / reward quorum for
        // keys we DO hold (and could audit ourselves). For keys we
        // are trying to FETCH (i.e. not in local storage), there is
        // no possible local audit credit, and gating the presence
        // quorum on credit would deadlock replica-repair in a
        // fully v12-capable close group.
        let mut locally_held: HashSet<XorName> = HashSet::new();
        for key in &keys_needing_network {
            if storage.exists(key).unwrap_or(false) {
                locally_held.insert(*key);
            }
        }
        let holder_credit = |peer: &PeerId, key: &XorName| -> bool {
            if !locally_held.contains(key) {
                // Replica-fetch path: we don't hold this key, so we
                // cannot have collected audit credit for it. Trust
                // Present claims to drive fetch-source promotion;
                // chunk-PUT payment_verifier is the security backstop
                // when the bytes actually arrive.
                return true;
            }
            if !capable_peer_snapshot.contains(peer) {
                // Pre-v12 / legacy peer that has never gossiped a
                // commitment. The v12 §6 holder-eligibility check
                // doesn't apply: their Present evidence comes through
                // the legacy path and we credit it unconditionally
                // so a mixed-version network stays live during
                // transition.
                return true;
            }
            let Some(hash) = commitment_by_peer_snapshot.get(peer) else {
                // Peer is commitment_capable (sticky) but currently
                // has no live commitment record on file (e.g. their
                // last gossip was evicted from the LRU cache, or it
                // failed verification). Withhold credit until they
                // re-prove storage under a fresh commitment.
                return false;
            };
            provers_snapshot.is_credited_holder(key, peer, hash)
        };

        let mut evaluated: Vec<(XorName, KeyVerificationOutcome)> = Vec::new();
        {
            let q = queues.read().await;
            for key in &keys_needing_network {
                let Some(ev) = evidence.get(key) else {
                    continue;
                };
                if q.get_pending(key).is_none() {
                    continue;
                }
                let outcome = quorum::evaluate_key_evidence_with_holder_check(
                    key,
                    ev,
                    &targets,
                    config,
                    holder_credit,
                );
                evaluated.push((*key, outcome));
            }
        } // read lock released

        // Step 4: Insert verified keys into PaidForList (no lock held).
        let mut paid_insert_keys: Vec<XorName> = Vec::new();
        for (key, outcome) in &evaluated {
            if matches!(
                outcome,
                KeyVerificationOutcome::QuorumVerified { .. }
                    | KeyVerificationOutcome::PaidListVerified { .. }
            ) {
                paid_insert_keys.push(*key);
            }
        }
        for key in &paid_insert_keys {
            if let Err(e) = paid_list.insert(key).await {
                warn!("Failed to add verified key to PaidForList: {e}");
            }
        }

        // Verification established validity; the paid-list insert above records
        // it. Downloading the bytes is a separate duty, owed only by the
        // storage-admission group, asked on the same terms for every verified
        // key — the advertising peer's replica/paid labelling is a claim about
        // itself and carries no authority over what we store. This check is a
        // cheap pre-filter that keeps never-responsible keys out of the fetch
        // queue entirely; the authoritative gate is the per-attempt recheck in
        // `execute_single_fetch`, because a key can wait in the nearest-first
        // fetch queue long enough for this promotion-time answer to go stale.
        let mut fetch_allowed_keys: HashSet<XorName> = HashSet::new();
        for (key, outcome) in &evaluated {
            if matches!(
                outcome,
                KeyVerificationOutcome::QuorumVerified { .. }
                    | KeyVerificationOutcome::PaidListVerified { .. }
            ) && !storage.exists(key).unwrap_or(false)
                && is_storage_admitted(&self_id, key, p2p_node, config).await
            {
                fetch_allowed_keys.insert(*key);
            }
        }

        // Step 5: Update queues with the evaluated outcomes.
        let mut bad_singleton_hints: HashMap<PeerId, usize> = HashMap::new();
        let mut q = queues.write().await;
        for (key, outcome) in evaluated {
            let replica_hint_sources = q
                .get_pending(&key)
                .map(|entry| entry.replica_hint_sources.clone())
                .unwrap_or_default();
            if let Some(ev) = evidence.get(&key) {
                if let Some(source) =
                    punishable_singleton_replica_hint_source(&replica_hint_sources, &outcome, ev)
                {
                    *bad_singleton_hints.entry(source).or_insert(0) += 1;
                }
            }
            match outcome {
                KeyVerificationOutcome::QuorumVerified { sources }
                | KeyVerificationOutcome::PaidListVerified { sources } => {
                    let mut fetch_sources = sources;
                    add_replica_hint_sources(&mut fetch_sources, &replica_hint_sources);
                    let fetch_eligible = fetch_allowed_keys.contains(&key);
                    if fetch_eligible && write_blocked {
                        // Capacity gate, second of two, and deliberately after
                        // Step 4: the key is now recorded in `PaidForList` and
                        // its verification stands. Only the download is held,
                        // because `execute_single_fetch` would refuse it and
                        // hand the key straight back here.
                        if q.defer_pending(&key, config::CAPACITY_BLOCKED_RETRY) {
                            capacity_deferred_promote += 1;
                        }
                    } else if fetch_eligible && !fetch_sources.is_empty() {
                        // A holder answered; see the matching clear on the
                        // local-paid path.
                        q.clear_unresolved(&key);
                        let distance =
                            crate::client::xor_distance(&key, p2p_node.peer_id().as_bytes());
                        // Atomic remove+enqueue: on fetch_queue capacity miss
                        // the pending entry is preserved so this verified key
                        // is retried on the next cycle (no silent drop).
                        let _ = q.promote_pending_to_fetch(key, distance, fetch_sources);
                        // Not terminal — either moved to fetch queue, or
                        // retained as pending until queue drains.
                    } else if fetch_eligible && fetch_sources.is_empty() {
                        no_holder_deferrals += 1;
                        let outcome = q.defer_unresolved(&key, config.verification_request_timeout);
                        let first_report = q.claim_no_holder_report(&key);
                        report_unresolved_deferral(
                            "Verified storage-admitted key",
                            &key,
                            outcome,
                            first_report,
                        );
                    } else {
                        q.remove_pending(&key);
                        terminal_keys.push(key);
                    }
                }
                KeyVerificationOutcome::QuorumFailed => {
                    q.remove_pending(&key);
                    terminal_keys.push(key);
                }
                KeyVerificationOutcome::QuorumInconclusive => {
                    q.set_pending_state(&key, VerificationState::QuorumInconclusive);
                    // Backed off like any other unresolved round, but it does
                    // NOT claim the no-holder report: nothing yet says this key
                    // has no holder, so the first round that does say so must
                    // still be the one that warns.
                    let _ = q.defer_unresolved(&key, config.verification_request_timeout);
                }
            }
        }
        drop(q);

        for (peer, bad_hint_count) in bad_singleton_hints {
            let reports = bad_hint_count.min(MAX_BAD_HINT_TRUST_REPORTS_PER_PEER_PER_CYCLE);
            warn!(
                "Peer {peer} submitted {bad_hint_count} rejected or self-contradicting \
                 sole-source replica hints; \
                 reporting {reports} bounded trust failure(s)"
            );
            for _ in 0..reports {
                p2p_node
                    .report_trust_event(
                        &peer,
                        TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                    )
                    .await;
            }
        }
    }

    // Step 6: Remove terminal keys from bootstrap pending set and re-check
    // the drain condition.
    update_bootstrap_after_verification(
        &terminal_keys,
        bootstrap_state,
        queues,
        is_bootstrapping,
        bootstrap_complete_notify,
    )
    .await;

    let (pending_after, fetch_after, in_flight_after) = {
        let q = queues.read().await;
        (
            q.pending_count(),
            q.fetch_queue_count(),
            q.in_flight_count(),
        )
    };
    let terminal_key_count = terminal_keys.len();
    let elapsed_ms = cycle_started.elapsed().as_millis();

    if elapsed_ms >= VERIFICATION_CYCLE_SLOW_LOG_MS {
        info!(
            target: "ant_node::replication::verification",
            "Slow replication verification cycle: pending_start={initial_pending_count}, capacity_deferred_probe={capacity_deferred_probe}, capacity_deferred_promote={capacity_deferred_promote}, local_paid_probe={local_paid_probe_count}, network_verify={keys_needing_network_count}, terminal={terminal_key_count}, no_holders={no_holder_deferrals}, pending_after={pending_after}, fetch_after={fetch_after}, in_flight_after={in_flight_after}, elapsed_ms={elapsed_ms}",
        );
    } else {
        debug!(
            target: "ant_node::replication::verification",
            "Replication verification cycle: pending_start={initial_pending_count}, capacity_deferred_probe={capacity_deferred_probe}, capacity_deferred_promote={capacity_deferred_promote}, local_paid_probe={local_paid_probe_count}, network_verify={keys_needing_network_count}, terminal={terminal_key_count}, no_holders={no_holder_deferrals}, pending_after={pending_after}, fetch_after={fetch_after}, in_flight_after={in_flight_after}, elapsed_ms={elapsed_ms}",
        );
    }
}

/// Report a verification round that left a key without a usable holder.
///
/// Warns once per entry — the first failure is news, and by the hundredth the
/// node is re-asking peers it has already exhausted. Later failures stay at
/// `debug`, and the per-cycle count is carried in the cycle summary so the
/// scale of a backlog is still visible without a line per key per retry.
///
/// `first_report` comes from `claim_no_holder_report`, not from the attempt
/// number: a round may advance the count without producing a no-holder result,
/// and such a round must not consume the warning.
///
/// Eviction at `PENDING_VERIFY_MAX_AGE` drops the entry, and a round that finds
/// a holder clears it, so a later relapse warns again: "once per episode", not
/// "once ever".
fn report_unresolved_deferral(
    what: &str,
    key: &XorName,
    outcome: Option<DeferralOutcome>,
    first_report: bool,
) {
    let Some(outcome) = outcome else {
        // The entry left `pending_verify` under the same lock; nothing deferred.
        return;
    };
    if first_report {
        warn!(
            "{what} {} has no responding holders yet; deferring retry",
            hex::encode(key)
        );
    } else {
        debug!(
            "{what} {} still has no responding holders after {} attempts; \
             retrying in {}s",
            hex::encode(key),
            outcome.attempt,
            outcome.retry_after.as_secs()
        );
    }
}

/// Add peers that claimed possession as fallback fetch sources.
///
/// Paid-only advertisers make no possession claim and are absent from
/// `replica_hint_sources` by construction, so they are never added.
fn add_replica_hint_sources(sources: &mut Vec<PeerId>, replica_hint_sources: &HashSet<PeerId>) {
    for source in replica_hint_sources {
        if !sources.contains(source) {
            sources.push(*source);
        }
    }
}

/// Return the sole replica advertiser when either the close group definitively
/// rejects the key or the advertiser explicitly denies possessing it.
/// Paid-only advertisements, corroborated replica hints, and inconclusive
/// rounds without that direct contradiction are deliberately non-penalizing.
fn punishable_singleton_replica_hint_source(
    replica_hint_sources: &HashSet<PeerId>,
    outcome: &KeyVerificationOutcome,
    evidence: &crate::replication::types::KeyVerificationEvidence,
) -> Option<PeerId> {
    // A paid-only advertiser leaves this set empty, so the sole-source lane is
    // reserved for peers that actually claimed possession.
    if replica_hint_sources.len() != 1 {
        return None;
    }
    let source = *replica_hint_sources.iter().next()?;
    let rejected_by_close_group = matches!(outcome, KeyVerificationOutcome::QuorumFailed);
    let denied_possession = evidence.presence.get(&source) == Some(&PresenceEvidence::Absent);

    (rejected_by_close_group || denied_possession).then_some(source)
}

/// Post-verification bootstrap bookkeeping: remove terminal keys from the
/// bootstrap pending set and transition out of bootstrapping when drained.
async fn update_bootstrap_after_verification(
    terminal_keys: &[XorName],
    bootstrap_state: &Arc<RwLock<BootstrapState>>,
    queues: &Arc<RwLock<ReplicationQueues>>,
    is_bootstrapping: &Arc<RwLock<bool>>,
    bootstrap_complete_notify: &Arc<Notify>,
) {
    if terminal_keys.is_empty() || bootstrap_state.read().await.is_drained() {
        return;
    }
    {
        let mut bs = bootstrap_state.write().await;
        for key in terminal_keys {
            bs.remove_key(key);
        }
    }
    let q = queues.read().await;
    if bootstrap::check_bootstrap_drained(bootstrap_state, &q).await {
        complete_bootstrap(is_bootstrapping, bootstrap_complete_notify).await;
    }
}

/// Retire bootstrap work owed by a peer that permanently left the routing
/// table, then immediately re-check drain so this removal can complete
/// bootstrap without waiting for an unrelated pipeline event.
async fn update_bootstrap_after_peer_removed(
    peer: &PeerId,
    bootstrap_state: &Arc<RwLock<BootstrapState>>,
    queues: &Arc<RwLock<ReplicationQueues>>,
    is_bootstrapping: &Arc<RwLock<bool>>,
    bootstrap_complete_notify: &Arc<Notify>,
) {
    let orphaned_keys = queues.write().await.remove_hint_source(peer);
    let cleared_rejection = bootstrap::clear_capacity_rejected(bootstrap_state, peer).await;

    if !orphaned_keys.is_empty() {
        let mut state = bootstrap_state.write().await;
        for key in &orphaned_keys {
            state.remove_key(key);
        }
    }

    if orphaned_keys.is_empty() && !cleared_rejection {
        return;
    }
    let q = queues.read().await;
    if bootstrap::check_bootstrap_drained(bootstrap_state, &q).await {
        complete_bootstrap(is_bootstrapping, bootstrap_complete_notify).await;
    }
}

/// Periodic bootstrap-drain self-healing, run on every verification worker
/// tick until bootstrap drains.
///
/// Two liveness gaps make this necessary. A `PeerRemoved` that races the
/// recording of a capacity rejection leaves an entry no future event can
/// clear — `max_age` expiry is its only exit. And a clean-cycle
/// `clear_capacity_rejected` never triggers its own drain re-check, so on a
/// quiet node the drain condition can become true with nothing left to
/// observe it. Expiry must run even while `pending_peer_requests` blocks the
/// drain itself, so this is invoked before `run_verification_cycle`'s
/// early-returns.
async fn expire_and_recheck_bootstrap_drain(
    bootstrap_state: &Arc<RwLock<BootstrapState>>,
    queues: &Arc<RwLock<ReplicationQueues>>,
    is_bootstrapping: &Arc<RwLock<bool>>,
    bootstrap_complete_notify: &Arc<Notify>,
    max_age: Duration,
) {
    if bootstrap_state.read().await.is_drained() {
        return;
    }
    bootstrap::expire_capacity_rejected(bootstrap_state, max_age).await;
    let q = queues.read().await;
    if bootstrap::check_bootstrap_drained(bootstrap_state, &q).await {
        complete_bootstrap(is_bootstrapping, bootstrap_complete_notify).await;
    }
}

/// Set `is_bootstrapping` to `false` and wake all waiters.
async fn complete_bootstrap(
    is_bootstrapping: &Arc<RwLock<bool>>,
    bootstrap_complete_notify: &Arc<Notify>,
) {
    *is_bootstrapping.write().await = false;
    bootstrap_complete_notify.notify_waiters();
    info!("Replication bootstrap complete");
}

// ---------------------------------------------------------------------------
// Fetch types and single-fetch executor
// ---------------------------------------------------------------------------

/// Result classification for a single fetch attempt.
enum FetchResult {
    /// Data fetched, integrity-checked, and stored successfully.
    Stored,
    /// Content-address integrity check failed — do not retry.
    IntegrityFailed,
    /// Source failed (network error or non-success response) — retryable.
    SourceFailed,
    /// This node could not write the chunk locally: the capacity pre-check
    /// refused before the dial, or `put` failed after the bytes arrived.
    ///
    /// Distinct from [`Self::SourceFailed`] because nothing about the source
    /// is implicated, and because the remedy is the opposite one. A source
    /// failure should move on to the next holder; a local write failure must
    /// not — every remaining source would send the same chunk into the same
    /// full disk. The key returns to verification instead, so a later cycle
    /// retries it once local capacity allows. That is a retry path, not a
    /// liveness guarantee: `evict_stale` still expires the entry at
    /// `PENDING_VERIFY_MAX_AGE` measured from when it was first queued, after
    /// which re-acquisition depends on a fresh hint.
    LocalWriteFailed,
    /// This node already holds the key, so there is nothing to fetch —
    /// terminal, exactly like [`Self::Stored`], because the duty is already
    /// discharged.
    ///
    /// Responsibility is rechecked at download time; possession has to be too.
    /// The earlier gates already drop held keys out of the fetch pipeline —
    /// `queue_admitted_hints` skips them, and the promotion-time
    /// `fetch_allowed_keys` filter and the local-paid probe both terminate
    /// them. (`is_relevant` is not one of them: possession there makes a key
    /// *relevant*, which is what keeps us tracking it.) So a held key reaches
    /// the dial only when it arrived *after* promotion — a fresh-offer push, a
    /// client PUT, or another source landing first. The nearest-first fetch
    /// queue is deep enough for that window to be real.
    ///
    /// This check must also precede the capacity pre-check below, because
    /// `ChunkStore::put` tests `exists` *before* it tests disk space: without
    /// it, a full node would decline a key it already holds, which `put` would
    /// have accepted as a duplicate.
    AlreadyHeld,
    /// Live routing state no longer places this node in the storage-admission
    /// group for the key — terminal, exactly like [`Self::Stored`]. The duty
    /// the fetch was serving has lapsed, so no alternate source is tried and
    /// no trust event is reported: the source did nothing wrong.
    NoLongerResponsible,
}

/// Outcome produced by [`execute_single_fetch`] and consumed by the fetch
/// worker loop to update queue state.
struct FetchOutcome {
    key: XorName,
    result: FetchResult,
}

/// What the fetch worker must do next for a key whose fetch attempt resolved.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FetchFollowUp {
    /// The key terminally left the fetch pipeline (its in-flight entry is
    /// removed and any verification retry-slot reservation released); the
    /// caller must run bootstrap accounting for it.
    Terminal,
    /// The key returned to `pending_verify` for a later verification round.
    RequeuedForVerification,
    /// Retry immediately from this next untried verified source.
    RetryFrom(PeerId),
}

/// Apply a resolved [`FetchResult`] to the queues and report the follow-up.
///
/// Split out of the fetch-worker loop so the per-variant queue transitions
/// are unit-testable without a live network. The caller holds the queues
/// write lock. [`FetchResult::NoLongerResponsible`] deliberately shares
/// [`FetchResult::Stored`]'s terminal path: `complete_fetch` releases the
/// retry-slot reservation, and the caller's terminal handling shrinks the
/// bootstrap pending set — dropping the key without that accounting would
/// stall bootstrap drain forever.
///
/// The nursery `option_if_let_else` rewrite is impossible here: both
/// `map_or_else` closures would need `&mut *q` simultaneously.
#[allow(clippy::option_if_let_else)]
fn apply_fetch_result(
    q: &mut ReplicationQueues,
    key: &XorName,
    result: &FetchResult,
    verification_retry_after: Duration,
) -> FetchFollowUp {
    match result {
        FetchResult::Stored | FetchResult::NoLongerResponsible | FetchResult::AlreadyHeld => {
            q.complete_fetch(key);
            FetchFollowUp::Terminal
        }
        FetchResult::IntegrityFailed | FetchResult::SourceFailed => {
            if let Some(next_peer) = q.retry_fetch(key) {
                FetchFollowUp::RetryFrom(next_peer)
            } else if q.requeue_fetch_for_verification(key, verification_retry_after) {
                FetchFollowUp::RequeuedForVerification
            } else {
                FetchFollowUp::Terminal
            }
        }
        // No `retry_fetch`: the local write is what failed, so every other
        // source would be asked to send the same chunk into the same full
        // disk. Requeueing for verification is what keeps the key from being
        // stranded — it comes back once capacity allows — and the fallthrough
        // to `Terminal` when there is no retry metadata is what keeps
        // bootstrap drain accounting correct, exactly as for a source failure.
        //
        // The ordinary requeue delay, deliberately. A standing capacity block
        // does not need a longer one here: the key returns to pending, and the
        // gate at the head of the next cycle defers it for minutes. A separate
        // backoff on this path would duplicate that to save one 15 s round, on
        // a race the gate already makes rare.
        FetchResult::LocalWriteFailed => {
            if q.requeue_fetch_for_verification(key, verification_retry_after) {
                FetchFollowUp::RequeuedForVerification
            } else {
                FetchFollowUp::Terminal
            }
        }
    }
}

/// Whether this node currently sits inside the storage-admission group for
/// `key`, per live local routing state.
///
/// This is the one question every storage decision in this module asks; see
/// [`admission::is_responsible`]. A purely local routing-table lookup — no
/// network I/O — but it awaits into the DHT manager, so callers must not
/// hold the queues lock across it.
async fn is_storage_admitted(
    self_id: &PeerId,
    key: &XorName,
    p2p_node: &Arc<P2PNode>,
    config: &ReplicationConfig,
) -> bool {
    admission::is_responsible(
        self_id,
        key,
        p2p_node,
        storage_admission_width(config.close_group_size),
    )
    .await
}

#[allow(clippy::too_many_lines)]
/// Execute a single fetch request against `source` for `key`.
///
/// Handles encoding, network I/O, integrity checking, storage, and trust
/// event reporting.  Returns a [`FetchOutcome`] so the caller can update
/// queue state without holding any locks during the network round-trip.
///
/// This is the authoritative storage-responsibility gate: each attempt —
/// including every per-source retry, which re-enters here — rechecks live
/// routing state before spending bandwidth, and once more before writing
/// bytes that arrived after a round-trip. The verification-time check that
/// promoted the key into the fetch queue is only a pre-filter; the queue is
/// nearest-first and deep, so a promotion decision can go stale under
/// topology churn before the key is ever dequeued.
async fn execute_single_fetch(
    p2p_node: Arc<P2PNode>,
    storage: Arc<ChunkStore>,
    config: Arc<ReplicationConfig>,
    key: XorName,
    source: PeerId,
) -> FetchOutcome {
    let self_id = *p2p_node.peer_id();
    if !is_storage_admitted(&self_id, &key, &p2p_node, &config).await {
        debug!(
            "Skipping fetch for {}: no longer in the storage-admission group",
            hex::encode(key)
        );
        return FetchOutcome {
            key,
            result: FetchResult::NoLongerResponsible,
        };
    }

    // Possession, then capacity — both before the dial, and in that order.
    //
    // `ChunkStore::put` tests `exists` before it tests disk space, so a full
    // node still accepts a key it already holds. Checking possession first is
    // what keeps this pair of gates from declining work `put` would have
    // taken.
    if storage.exists(&key).unwrap_or(false) {
        debug!(
            "Skipping fetch for {}: already held locally",
            hex::encode(key)
        );
        return FetchOutcome {
            key,
            result: FetchResult::AlreadyHeld,
        };
    }

    // Capacity is checked before the dial rather than after the bytes arrive.
    // `put` runs this same check on every write of a key it does not already
    // hold, so this reaches the same verdict `put` would reach *at this
    // instant*. It is not a guarantee about the later write: both answers can
    // change across the round trip. Space freed in between would have let a
    // post-download `put` succeed, so a refusal here can cost a fleeting
    // acquisition and one retry cycle of delay; space lost in between is why
    // the check inside `put` still has to stay.
    //
    // What it buys is that the refusal costs no bandwidth. Without it a
    // disk-full node pulls the whole chunk over the network first, and the
    // failure is then classified as the source's — no trust event, but the
    // worker walks to every remaining holder, and each re-uploads the same
    // chunk into the same full disk.
    if let Err(e) = storage.check_capacity() {
        debug!(
            "Skipping fetch for {}: local storage cannot accept a write: {e}",
            hex::encode(key)
        );
        return FetchOutcome {
            key,
            result: FetchResult::LocalWriteFailed,
        };
    }

    let request = protocol::FetchRequest { key };
    let msg = ReplicationMessage {
        request_id: rand::thread_rng().gen::<u64>(),
        body: ReplicationMessageBody::FetchRequest(request),
    };

    let encoded = match msg.encode() {
        Ok(data) => data,
        Err(e) => {
            warn!("Failed to encode fetch request: {e}");
            return FetchOutcome {
                key,
                result: FetchResult::SourceFailed,
            };
        }
    };

    let result = p2p_node
        .send_request(
            &source,
            REPLICATION_PROTOCOL_ID,
            encoded,
            config.fetch_request_timeout,
        )
        .await;

    match result {
        Ok(response) => {
            let Ok(resp_msg) = ReplicationMessage::decode(&response.data) else {
                p2p_node
                    .report_trust_event(
                        &source,
                        TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                    )
                    .await;
                return FetchOutcome {
                    key,
                    result: FetchResult::SourceFailed,
                };
            };

            match resp_msg.body {
                ReplicationMessageBody::FetchResponse(protocol::FetchResponse::Success {
                    key: resp_key,
                    data,
                }) => {
                    // Validate the response key matches the requested key.
                    // A malicious peer could serve valid data for a different
                    // key, passing integrity checks while the requested key
                    // is falsely marked as fetched.
                    if resp_key != key {
                        warn!(
                            "Fetch response key mismatch: requested {}, got {}",
                            hex::encode(key),
                            hex::encode(resp_key)
                        );
                        p2p_node
                            .report_trust_event(
                                &source,
                                TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                            )
                            .await;
                        return FetchOutcome {
                            key,
                            result: FetchResult::IntegrityFailed,
                        };
                    }

                    // Enforce chunk size invariant on fetched data.
                    // Checked before the content-address hash to avoid
                    // hashing up to 10 MiB of oversized junk data.
                    if data.len() > crate::ant_protocol::MAX_CHUNK_SIZE {
                        warn!(
                            "Fetched record {} exceeds MAX_CHUNK_SIZE ({} > {})",
                            hex::encode(resp_key),
                            data.len(),
                            crate::ant_protocol::MAX_CHUNK_SIZE,
                        );
                        p2p_node
                            .report_trust_event(
                                &source,
                                TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                            )
                            .await;
                        return FetchOutcome {
                            key,
                            result: FetchResult::IntegrityFailed,
                        };
                    }

                    // Content-address integrity check.
                    let computed = crate::client::compute_address(&data);
                    if computed != resp_key {
                        warn!(
                            "Fetched record integrity check failed: expected {}, got {}",
                            hex::encode(resp_key),
                            hex::encode(computed)
                        );
                        p2p_node
                            .report_trust_event(
                                &source,
                                TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                            )
                            .await;
                        return FetchOutcome {
                            key,
                            result: FetchResult::IntegrityFailed,
                        };
                    }

                    // Responsibility can lapse during the network round-trip.
                    // The bandwidth is already spent; declining the write is
                    // what still avoids the disk write and the later
                    // fetch→store→prune churn. Edge flapping is dampened by
                    // the margin `storage_admission_width` adds over
                    // `close_group_size`.
                    if !is_storage_admitted(&self_id, &key, &p2p_node, &config).await {
                        debug!(
                            "Fetched {} but responsibility lapsed in transit; not storing",
                            hex::encode(key)
                        );
                        return FetchOutcome {
                            key,
                            result: FetchResult::NoLongerResponsible,
                        };
                    }

                    if let Err(e) = storage.put(&resp_key, &data).await {
                        // The bytes arrived and passed the content-address
                        // check, so the source did its job; the failure is
                        // entirely local (disk-full, or a storage error). Any
                        // valid source must serve identical content, so trying
                        // the next one cannot cure a local error — it only
                        // re-downloads the same chunk into the same store.
                        warn!(
                            "Failed to store fetched record {}: {e}",
                            hex::encode(resp_key)
                        );
                        return FetchOutcome {
                            key,
                            result: FetchResult::LocalWriteFailed,
                        };
                    }

                    FetchOutcome {
                        key,
                        result: FetchResult::Stored,
                    }
                }
                ReplicationMessageBody::FetchResponse(
                    ref response @ (protocol::FetchResponse::NotFound { .. }
                    | protocol::FetchResponse::Error { .. }),
                ) => {
                    // This peer was selected as a fetch source because it recently
                    // answered `Present` during verification, so either answer means it
                    // could not serve a chunk it had just claimed.
                    if let protocol::FetchResponse::Error { reason, .. } = response {
                        warn!(
                            "Fetch: peer {source} returned error for {}: {reason}",
                            hex::encode(key)
                        );
                    } else {
                        warn!(
                            "Fetch: verified source {source} returned NotFound for {}",
                            hex::encode(key)
                        );
                    }
                    p2p_node
                        .report_trust_event(
                            &source,
                            TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                        )
                        .await;
                    FetchOutcome {
                        key,
                        result: FetchResult::SourceFailed,
                    }
                }
                _ => {
                    // Unexpected message type — treat as malformed.
                    p2p_node
                        .report_trust_event(
                            &source,
                            TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                        )
                        .await;
                    FetchOutcome {
                        key,
                        result: FetchResult::SourceFailed,
                    }
                }
            }
        }
        Err(e) => {
            debug!("Fetch request to {source} failed: {e}");
            // No ApplicationFailure here — P2PNode::send_request() already
            // reports ConnectionTimeout / ConnectionFailed to the TrustEngine.
            FetchOutcome {
                key,
                result: FetchResult::SourceFailed,
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Audit result handler
// ---------------------------------------------------------------------------

/// Format the first confirmed-failed key as a 16-hex-char label.
///
/// Pairs with `challenged_peer` to form a stable cross-host correlation
/// handle in the audit-failure log line, e.g.
///
/// ```text
/// Audit failure for <peer>: …, `first_failed_key=0x18878f1d2d9e0612`
/// ```
///
/// Falls back to `"0x"` when the list is empty so the log line never
/// contains a misleading default.
fn first_failed_key_label(confirmed_failed_keys: &[XorName]) -> String {
    confirmed_failed_keys.first().map_or_else(
        || "0x".to_string(),
        |k| format!("0x{}", hex::encode(&k[..8])),
    )
}

/// Execute the side effects for a subtree storage-commitment audit failure.
///
/// Subtree timeouts are fully graced: the multi-round, multi-chunk challenge can
/// legitimately time out on slow or loaded honest peers, so it never touches the
/// responsible-chunk audit path or its timeout accounting. Confirmed subtree
/// failures still penalise immediately and revoke holder credit.
async fn handle_subtree_failed_audit(
    challenged_peer: &PeerId,
    confirmed_failed_key_count: usize,
    reason: &AuditFailureReason,
    p2p_node: &Arc<P2PNode>,
    sync_state: &Arc<RwLock<NeighborSyncState>>,
    recent_provers: &Arc<RwLock<RecentProvers>>,
) {
    if matches!(reason, AuditFailureReason::Timeout) {
        debug!(
            "Audit timeout for {challenged_peer} fully graced \
             (subtree audit does not evict on timeout)"
        );
        return;
    }

    // The caller already logged the rich failure line with reason + per-category
    // summary; avoid a redundant second error log here.
    let _ = confirmed_failed_key_count;
    {
        let mut state = sync_state.write().await;
        state.clear_active_bootstrap_claim(challenged_peer);
    }
    {
        let mut provers_guard = recent_provers.write().await;
        apply_audit_failure_credit_revocation(&mut provers_guard, challenged_peer, reason);
    }
    p2p_node
        .report_trust_event(
            challenged_peer,
            TrustEvent::ApplicationFailure(config::AUDIT_FAILURE_TRUST_WEIGHT),
        )
        .await;
}

/// Handle audit result: log findings and emit trust events.
async fn handle_subtree_audit_result(
    result: &AuditTickResult,
    p2p_node: &Arc<P2PNode>,
    sync_state: &Arc<RwLock<NeighborSyncState>>,
    recent_provers: &Arc<RwLock<RecentProvers>>,
    config: &ReplicationConfig,
) {
    match result {
        AuditTickResult::Passed {
            challenged_peer,
            keys_checked,
        } => {
            protocol::record_audit_pass(protocol::AuditOutcomeKind::Subtree);
            debug!("Audit passed for {challenged_peer} ({keys_checked} keys)");
            // Peer responded normally — clear the active bootstrap claim while
            // retaining history so a later claim is treated as repeated abuse.
            {
                let mut state = sync_state.write().await;
                state.clear_active_bootstrap_claim(challenged_peer);
            }
            p2p_node
                .report_trust_event(
                    challenged_peer,
                    TrustEvent::ApplicationSuccess(REPLICATION_TRUST_WEIGHT),
                )
                .await;
        }
        AuditTickResult::Failed {
            evidence,
            no_response_class,
        } => {
            if let FailureEvidence::AuditFailure {
                challenged_peer,
                confirmed_failed_keys,
                summary,
                reason,
                ..
            } = evidence
            {
                protocol::record_audit_fail(protocol::AuditOutcomeKind::Subtree, reason);
                // Rich diagnostics (from main's audit-failure logging) + the
                // first-failed-key correlation handle.
                let first_failed_key = first_failed_key_label(confirmed_failed_keys);
                let audit_failure_class = no_response_class.unwrap_or("confirmed");
                error!(
                    "Audit failure for {challenged_peer}: reason={reason:?}, audit_failure_class={}, confirmed_failed_keys={}, challenged_keys={}, absent_keys={}, digest_mismatch_keys={}, first_failed_key={first_failed_key}",
                    audit_failure_class,
                    confirmed_failed_keys.len(),
                    summary.challenged_keys,
                    summary.absent_keys,
                    summary.digest_mismatch_keys,
                );
                // Route the side effects through the subtree-only failure path.
                // Responsible-chunk `AuditChallenge` handling intentionally uses
                // its own old immediate-penalty handler below.
                handle_subtree_failed_audit(
                    challenged_peer,
                    confirmed_failed_keys.len(),
                    reason,
                    p2p_node,
                    sync_state,
                    recent_provers,
                )
                .await;
            }
        }
        AuditTickResult::BootstrapClaim { peer } => {
            // Gap 6: BootstrapClaimAbuse grace period in audit path.
            // Separate state mutation from network I/O to avoid holding the
            // write lock across report_trust_event.
            let should_report = {
                let now = Instant::now();
                let mut state = sync_state.write().await;
                match state.observe_bootstrap_claim(*peer, now, config.bootstrap_claim_grace_period)
                {
                    BootstrapClaimObservation::WithinGrace { .. } => {
                        debug!("Audit: peer {peer} claims bootstrapping (within grace period)");
                        false
                    }
                    BootstrapClaimObservation::PastGrace { first_seen } => {
                        warn!(
                            "Audit: peer {peer} claiming bootstrap past grace period \
                             ({:?} > {:?}), reporting abuse",
                            now.duration_since(first_seen),
                            config.bootstrap_claim_grace_period,
                        );
                        true
                    }
                    BootstrapClaimObservation::Repeated { first_seen } => {
                        warn!(
                            "Audit: peer {peer} repeated bootstrap claim after previously \
                             stopping; first claim was {:?} ago, reporting abuse",
                            now.duration_since(first_seen),
                        );
                        true
                    }
                }
            };
            if should_report {
                p2p_node
                    .report_trust_event(
                        peer,
                        TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                    )
                    .await;
            }
        }
        AuditTickResult::Idle | AuditTickResult::InsufficientKeys => {}
    }
}

/// Whether a confirmed audit failure with this reason clears the peer's active
/// bootstrap claim. A `Timeout` does not (the peer may still be legitimately
/// bootstrapping); every confirmed storage-integrity reason does.
///
/// Responsible-chunk `AuditChallenge` failures use this directly: timeouts keep
/// the bootstrap claim, matching the pre-ADR-0002 behaviour, while every failure
/// still reports the normal audit trust penalty.
fn audit_failure_clears_bootstrap_claim(reason: &AuditFailureReason) -> bool {
    !matches!(reason, AuditFailureReason::Timeout)
}

/// Handle the result of a responsible-chunk audit tick (audit #2): emit trust
/// events and manage bootstrap-claim state.
///
/// This is intentionally separate from the subtree audit result handler. A
/// responsible-chunk `AuditChallenge` `Failed` result reports
/// `ApplicationFailure` immediately for every reason, including `Timeout`,
/// restoring the pre-ADR-0002 behaviour.
#[allow(clippy::too_many_lines)]
async fn handle_audit_result(
    result: &AuditTickResult,
    p2p_node: &Arc<P2PNode>,
    sync_state: &Arc<RwLock<NeighborSyncState>>,
    config: &ReplicationConfig,
) {
    match result {
        AuditTickResult::Passed {
            challenged_peer,
            keys_checked,
        } => {
            protocol::record_audit_pass(protocol::AuditOutcomeKind::Responsible);
            debug!("Audit passed for {challenged_peer} ({keys_checked} keys)");
            {
                let mut state = sync_state.write().await;
                state.clear_active_bootstrap_claim(challenged_peer);
            }
            p2p_node
                .report_trust_event(
                    challenged_peer,
                    TrustEvent::ApplicationSuccess(REPLICATION_TRUST_WEIGHT),
                )
                .await;
        }
        AuditTickResult::Failed {
            evidence,
            no_response_class,
        } => {
            if let FailureEvidence::AuditFailure {
                challenged_peer,
                confirmed_failed_keys,
                summary,
                reason,
                ..
            } = evidence
            {
                protocol::record_audit_fail(protocol::AuditOutcomeKind::Responsible, reason);
                let first_failed_key = first_failed_key_label(confirmed_failed_keys);
                let audit_failure_class = no_response_class.unwrap_or("confirmed");
                error!(
                    "Audit failure for {challenged_peer}: reason={reason:?}, audit_failure_class={}, confirmed_failed_keys={}, challenged_keys={}, absent_keys={}, digest_mismatch_keys={}, first_failed_key={first_failed_key}",
                    audit_failure_class,
                    confirmed_failed_keys.len(),
                    summary.challenged_keys,
                    summary.absent_keys,
                    summary.digest_mismatch_keys,
                );
                if audit_failure_clears_bootstrap_claim(reason) {
                    let mut state = sync_state.write().await;
                    state.clear_active_bootstrap_claim(challenged_peer);
                } else {
                    debug!("Audit timeout for {challenged_peer}; retaining active bootstrap claim");
                }
                p2p_node
                    .report_trust_event(
                        challenged_peer,
                        TrustEvent::ApplicationFailure(config::AUDIT_FAILURE_TRUST_WEIGHT),
                    )
                    .await;
            }
        }
        AuditTickResult::BootstrapClaim { peer } => {
            let should_report = {
                let now = Instant::now();
                let mut state = sync_state.write().await;
                match state.observe_bootstrap_claim(*peer, now, config.bootstrap_claim_grace_period)
                {
                    BootstrapClaimObservation::WithinGrace { .. } => {
                        debug!("Audit: peer {peer} claims bootstrapping (within grace period)");
                        false
                    }
                    BootstrapClaimObservation::PastGrace { first_seen } => {
                        warn!(
                            "Audit: peer {peer} claiming bootstrap past grace period \
                             ({:?} > {:?}), reporting abuse",
                            now.duration_since(first_seen),
                            config.bootstrap_claim_grace_period,
                        );
                        true
                    }
                    BootstrapClaimObservation::Repeated { first_seen } => {
                        warn!(
                            "Audit: peer {peer} repeated bootstrap claim after previously \
                             stopping; first claim was {:?} ago, reporting abuse",
                            now.duration_since(first_seen),
                        );
                        true
                    }
                }
            };
            if should_report {
                p2p_node
                    .report_trust_event(
                        peer,
                        TrustEvent::ApplicationFailure(REPLICATION_TRUST_WEIGHT),
                    )
                    .await;
            }
        }
        AuditTickResult::Idle | AuditTickResult::InsufficientKeys => {}
    }
}

/// Whether a confirmed audit failure with this reason should revoke the
/// peer's `recent_provers` holder credit immediately (v12 §6).
///
/// `true` for any reason where the peer actually answered (or admitted
/// it cannot): `DigestMismatch`, `KeyAbsent`, `Rejected` ("missing
/// bytes for committed key"), `MalformedResponse` — these prove the
/// peer no longer holds what it committed to, so it must not keep
/// holder credit for the proof TTL. `false` for `Timeout`: a single
/// dropped packet must not strip an honest peer; the 40-min TTL is the
/// deliberate liveness cushion there.
fn audit_failure_revokes_holder_credit(reason: &AuditFailureReason) -> bool {
    !matches!(reason, AuditFailureReason::Timeout)
}

/// Apply the holder-credit revocation decision for a confirmed audit
/// failure. Pure over `RecentProvers` so the handler wiring is unit-
/// testable without a live `P2PNode`: the production `Failed` arm of
/// `handle_subtree_audit_result` calls exactly this.
fn apply_audit_failure_credit_revocation(
    provers: &mut RecentProvers,
    challenged_peer: &PeerId,
    reason: &AuditFailureReason,
) {
    if audit_failure_revokes_holder_credit(reason) {
        provers.forget_peer(challenged_peer);
    }
}

// `admit_bootstrap_hints` was consolidated into `admit_and_queue_hints`.

// ---------------------------------------------------------------------------
// Storage-bound audit (ADR-0002) — gossip trigger + auditor-side ingestion
// ---------------------------------------------------------------------------

/// State the gossip-audit trigger needs to spawn an audit. Bundled so the
/// message handler passes one value instead of a long argument list; all
/// fields are cheap `Arc` clones.
#[derive(Clone)]
struct GossipAuditTrigger {
    p2p_node: Arc<P2PNode>,
    config: Arc<ReplicationConfig>,
    recent_provers: Arc<RwLock<RecentProvers>>,
    sync_state: Arc<RwLock<NeighborSyncState>>,
    /// Shared "an audit actually launched" cooldown, consulted by BOTH the
    /// gossip-lottery path and the monetized first-audit scheduler. Stamped
    /// only when a real audit is about to be sent — never by a losing lottery
    /// ticket — so gossip traffic alone can never suppress a paid first audit.
    cooldown: Arc<RwLock<HashMap<PeerId, Instant>>>,
    detached_task_tracker: TaskTracker,
    /// Gossip-private lottery attempt window: stamped on every roll (win or
    /// lose) so a gossip flood cannot re-roll the lottery within the window.
    /// The first-audit scheduler never reads this map.
    lottery_attempts: Arc<RwLock<HashMap<PeerId, Instant>>>,
}

/// What a gossip ingest yields for the audit trigger: the commitment hash to
/// pin and the `key_count` needed to size the response deadline from the actual
/// `ceil(sqrt(N))` subtree (ADR-0002). Returned on every VALID gossip (changed
/// or not) so a stable-keyset node stays auditable — not just on its first
/// commitment.
#[derive(Debug, Clone, Copy)]
struct AuditTarget {
    pin_hash: [u8; 32],
    key_count: u32,
}

/// ADR-0004: a commitment that backed a payment, surfaced by the payment
/// verifier's cross-check so it can receive a **deterministic first audit**.
///
/// Sent from the verifier to the replication engine's first-audit drainer. The
/// drainer dedups by `pin` (a pin gets one deterministic first audit; later
/// audits of the same peer revert to the gossip lottery), orders most-recently-
/// monetized first, and runs the same `run_subtree_audit` under the same
/// per-peer cooldown and concurrency caps — only the lottery is bypassed.
#[derive(Debug, Clone, Copy)]
pub struct MonetizedPinEvent {
    /// The peer whose commitment backed the payment.
    pub peer: PeerId,
    /// The pinned commitment hash.
    pub pin: [u8; 32],
    /// The committed key count (sizes the audit deadline).
    pub key_count: u32,
    /// The accused's own SIGNED quote timestamp. The first-audit drainer skips a
    /// pin whose quote is older than the answerability window (ADR-0004 A1
    /// guardrail A): with grace removed, challenging an aged-out pin would
    /// false-convict, so a stale client-forwarded quote must not trigger an audit.
    pub quote_ts: SystemTime,
}

/// Per-peer audit cooldown check-and-stamp (ADR-0002 "occasional surprise
/// exams, keeps load low"). Returns `true` if `peer` may be audited now (and
/// stamps `now`), `false` if it was audited within
/// `AUDIT_ON_GOSSIP_COOLDOWN_SECS`. Bounds the map under a flood of distinct
/// peers. Pure over the passed map so the flood/cooldown behaviour is testable
/// without a live node: a burst of gossips from one peer yields at most one
/// `true` per cooldown window.
fn cooldown_allows_audit(map: &mut HashMap<PeerId, Instant>, peer: &PeerId, now: Instant) -> bool {
    let cooldown = Duration::from_secs(config::AUDIT_ON_GOSSIP_COOLDOWN_SECS);
    let known = match map.get(peer) {
        Some(&last) => {
            if now.saturating_duration_since(last) < cooldown {
                return false;
            }
            true
        }
        None => false,
    };
    // Bound the map under churn like its siblings (drop the oldest stamp) before
    // admitting a brand-new peer.
    if !known && map.len() >= MAX_LAST_COMMITMENT_BY_PEER {
        if let Some(victim) = map.iter().min_by_key(|(_, &ts)| ts).map(|(p, _)| *p) {
            map.remove(&victim);
        }
    }
    map.insert(*peer, now);
    true
}

/// Read-only companion to [`cooldown_allows_audit`]: whether `peer` is OUTSIDE
/// its cooldown at `now`, WITHOUT stamping. Used by the first-audit reserve gate
/// (ADR-0004 Amendment 2 E′) to avoid reserving a peer that a recent audit
/// already covered; the authoritative check-and-stamp still runs at promotion,
/// so this is only an optimization and never the security boundary.
fn cooldown_would_allow(map: &HashMap<PeerId, Instant>, peer: &PeerId, now: Instant) -> bool {
    let cooldown = Duration::from_secs(config::AUDIT_ON_GOSSIP_COOLDOWN_SECS);
    map.get(peer)
        .is_none_or(|&last| now.saturating_duration_since(last) >= cooldown)
}

/// The gossip-audit launch decision in ONE place so the ordering is shared
/// between production and its test (ADR-0002 "occasional surprise exams").
///
/// Order matters and is the security-relevant property. Gate 1 checks-and-stamps
/// the gossip-PRIVATE `attempts` window, win or lose: if the lottery were
/// sampled first, a gossip flood would re-roll it on every message until one
/// won, multiplying audits, so each peer gets at most one lottery roll per
/// window regardless of how often it gossips. Gate 3 checks-and-stamps the
/// SHARED `launched` cooldown — the map the monetized first-audit scheduler
/// also consults — only after a WIN, i.e. only when a real audit is about to be
/// sent. A losing ticket must never stamp `launched`: no challenge went on the
/// wire, so it must not defer a paid first audit (repeatable losses could
/// otherwise hold a monetized pin past its answerability window). Production
/// calls this with `lottery_wins = gen_bool(AUDIT_ON_GOSSIP_PROBABILITY)`; the
/// test calls it with a deterministic `lottery_wins`, so a reorder regression
/// here fails the test.
fn audit_launch_decision(
    attempts: &mut HashMap<PeerId, Instant>,
    launched: &mut HashMap<PeerId, Instant>,
    peer: &PeerId,
    now: Instant,
    lottery_wins: bool,
) -> bool {
    // Gate 1: lottery-attempt window check-and-stamp (consumes the attempt
    // window even on a loss; private to the gossip path).
    if !cooldown_allows_audit(attempts, peer, now) {
        return false;
    }
    // Gate 2: the probability lottery. A loss stops here and stamps nothing
    // shared.
    if !lottery_wins {
        return false;
    }
    // Gate 3: the shared actual-audit cooldown (a recent real audit from
    // either path still suppresses this launch), stamped only on launch.
    cooldown_allows_audit(launched, peer, now)
}

/// On a peer's *changed* gossiped commitment, maybe launch a subtree audit
/// (ADR-0002): fire with probability `AUDIT_ON_GOSSIP_PROBABILITY`, subject to a
/// per-peer cooldown, pinned to the just-ingested root. Detached so gossip
/// handling is never blocked on a network round-trip.
async fn maybe_trigger_gossip_audit(
    trigger: &GossipAuditTrigger,
    peer: &PeerId,
    target: AuditTarget,
) {
    // The launch decision (attempt-window, lottery, shared-cooldown ordering)
    // lives in the pure `audit_launch_decision` so the ordering is shared with
    // its test. Sample the lottery here, then let the helper apply the gates.
    let now = Instant::now();
    let lottery_wins = rand::thread_rng().gen_bool(config::AUDIT_ON_GOSSIP_PROBABILITY);
    {
        // Lock order: attempts before the shared cooldown; this is the only
        // place both are held together.
        let mut attempts = trigger.lottery_attempts.write().await;
        let mut launched = trigger.cooldown.write().await;
        if !audit_launch_decision(&mut attempts, &mut launched, peer, now, lottery_wins) {
            return;
        }
    }

    let detached_task_tracker = trigger.detached_task_tracker.clone();
    let trigger = trigger.clone();
    let peer = *peer;
    detached_task_tracker.spawn(async move {
        let credit = storage_commitment_audit::AuditCredit {
            recent_provers: &trigger.recent_provers,
        };
        let result = storage_commitment_audit::run_subtree_audit_with_origin(
            &trigger.p2p_node,
            &trigger.config,
            &peer,
            target.pin_hash,
            target.key_count,
            Some(&credit),
            storage_commitment_audit::SubtreeAuditOrigin::Gossip,
        )
        .await;
        handle_subtree_audit_result(
            &result,
            &trigger.p2p_node,
            &trigger.sync_state,
            &trigger.recent_provers,
            &trigger.config,
        )
        .await;
    });
}

/// Atomic check-and-stamp of the per-peer commitment sig-verify rate limit.
///
/// Returns `true` if a signature verify is allowed now (and stamps the attempt
/// time), `false` if the peer is within [`COMMITMENT_SIG_VERIFY_MIN_INTERVAL`]
/// of its last attempt. Holds one write lock across the decision so two
/// concurrent ingests from the same peer cannot both pass. Stamps BEFORE the
/// caller's expensive verify so a slow/failed verify still rate-limits the next
/// message. Bounds the map under a flood of distinct peer ids.
async fn sig_verify_rate_limit_ok(
    sig_verify_attempts: &Arc<RwLock<HashMap<PeerId, Instant>>>,
    source: &PeerId,
    now: Instant,
) -> bool {
    let mut attempts = sig_verify_attempts.write().await;
    if let Some(&last) = attempts.get(source) {
        if now.saturating_duration_since(last) < COMMITMENT_SIG_VERIFY_MIN_INTERVAL {
            return false;
        }
    }
    if attempts.len() >= MAX_LAST_COMMITMENT_BY_PEER && !attempts.contains_key(source) {
        if let Some(victim) = attempts.iter().min_by_key(|(_, &ts)| ts).map(|(p, _)| *p) {
            attempts.remove(&victim);
        }
    }
    attempts.insert(*source, now);
    true
}

/// Verify + store an inbound commitment from a gossip peer.
///
/// Called from the inbound `NeighborSyncRequest`/`Response` handlers and
/// the bootstrap-sync loop. Drops the commitment unless all five gates
/// pass:
///   1. `source` is in our DHT routing table (sybil/churn cap).
///   2. `commitment.sender_peer_id == source.as_bytes()` (peer-id
///      binding to the authenticated transport peer).
///   3. `BLAKE3(commitment.sender_public_key) == commitment.sender_peer_id`
///      (the embedded pubkey actually belongs to the claimed identity —
///      saorsa-core derives `PeerId = BLAKE3(pubkey)`).
///   4. `verify_commitment_signature(commitment)` succeeds against the
///      embedded public key. The signed payload binds the pubkey, so an
///      adversary cannot swap the key while keeping the body.
///   5. The cache has room or this is an update for an existing entry
///      (sybil cap, `MAX_LAST_COMMITMENT_BY_PEER`).
///
/// On all-pass, the commitment is stored as the auditor's per-peer
/// "last known commitment" for use as `expected_commitment_hash` in
/// future audits.
///
/// Failures (no commitment / mismatched peer id / bad signature) are
/// silent drops — gossip is best-effort and a malformed commitment from
/// one peer should not affect anything else.
///
/// Returns `Some(AuditTarget)` whenever a VALID commitment was stored (whether
/// or not its root changed), so the caller can run a probabilistic,
/// cooldown-gated subtree audit. Returning on *every* valid gossip — not only
/// changed ones — is deliberate (ADR-0002): a node with a stable key set keeps
/// being auditable, so it cannot pass one audit and then delete data while
/// re-gossiping the same root forever. The cooldown + probability bound the
/// audit frequency. Returns `None` only if the commitment was dropped (failed a
/// gate) or there is nothing to pin.
///
/// Handle a capable peer gossiping `None` (a commitment downgrade).
///
/// A capable peer that previously gossiped a commitment but now gossips `None`
/// is trying to drop off the audit path. Within the answerability window we keep
/// the cached commitment pinned AND return it as an audit target so this gossip
/// still schedules a subtree audit against the peer's last known commitment — if
/// it genuinely dropped the data, the audit fails (there is no periodic tick, so
/// the trigger MUST fire here or the downgrade is never re-challenged).
///
/// But this only holds within the SAME `GOSSIP_ANSWERABILITY_TTL` the responder
/// honours for its own retired commitment: once that elapses since we last
/// received the peer's commitment, an honest peer has legitimately retired that
/// root (its responder side `retire_current`s and lets it age out) and can no
/// longer answer a pin on it. Auditing it past the TTL would manufacture a false
/// failure, so we then forget the cached commitment (keeping the sticky
/// `commitment_capable` bit) and stop pinning it.
async fn handle_commitment_downgrade(
    source: &PeerId,
    last_commitment_by_peer: &Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>>,
) -> Option<AuditTarget> {
    let now = Instant::now();
    let cached = {
        let map = last_commitment_by_peer.read().await;
        map.get(source).and_then(|rec| {
            if !rec.commitment_capable {
                return None;
            }
            let last = rec.last_commitment()?;
            let pin = rec.commitment_hash()?;
            let fresh = now.saturating_duration_since(rec.received_at) < GOSSIP_ANSWERABILITY_TTL;
            Some((pin, last.key_count, fresh))
        })
    };
    match cached {
        Some((pin, key_count, true)) => {
            warn!(
                "ingest_peer_commitment: commitment-capable peer {source} sent None \
                 (downgrade attempt); auditing against its last cached commitment"
            );
            Some(AuditTarget {
                pin_hash: pin,
                key_count,
            })
        }
        Some((_, _, false)) => {
            // Cached commitment has aged past the answerability window — forget
            // it so we stop pinning a root the peer is no longer obliged to
            // answer. Keep `commitment_capable` (sticky). Re-check freshness
            // UNDER the write lock (compare-and-clear): a concurrent valid gossip
            // from this peer may have refreshed `received_at` in the gap between
            // our read and write locks; if so, leave its fresh commitment intact.
            if let Some(rec) = last_commitment_by_peer.write().await.get_mut(source) {
                let still_stale =
                    now.saturating_duration_since(rec.received_at) >= GOSSIP_ANSWERABILITY_TTL;
                if still_stale {
                    rec.clear_commitment();
                    debug!(
                        "ingest_peer_commitment: capable peer {source} sent None and its cached \
                         commitment aged past the answerability TTL; forgetting it"
                    );
                }
            }
            None
        }
        None => None,
    }
}

async fn ingest_peer_commitment(
    source: &PeerId,
    commitment: Option<&StorageCommitment>,
    p2p_node: &Arc<P2PNode>,
    last_commitment_by_peer: &Arc<RwLock<HashMap<PeerId, PeerCommitmentRecord>>>,
    ever_capable_peers: &Arc<RwLock<HashSet<PeerId>>>,
    sig_verify_attempts: &Arc<RwLock<HashMap<PeerId, Instant>>>,
) -> Option<AuditTarget> {
    let Some(c) = commitment else {
        return handle_commitment_downgrade(source, last_commitment_by_peer).await;
    };
    // RT-membership gate: only accept commitments from peers in our
    // routing table. Off-RT senders (sybils, drive-by relays) cannot
    // populate the cache, which closes the hole where a flood of
    // off-RT identities could fill the cap and evict honest
    // peers. The neighbor-sync request handler applies the same gate
    // before admitting inbound replication hints (see neighbor_sync.rs
    // `sender_in_rt`); we mirror that policy here for the commitment
    // piggyback.
    if !p2p_node.dht_manager().is_in_routing_table(source).await {
        debug!("ingest_peer_commitment: source {source} not in routing table (dropped)");
        return None;
    }
    // Peer-id binding: the commitment's claimed sender must match the
    // authenticated transport peer (`source`). Defeats relay/replay
    // and also pins which embedded public key we are about to verify
    // against — the verify itself trusts the embedded key, so the
    // peer-id binding is the link to a real identity.
    if &c.sender_peer_id != source.as_bytes() {
        warn!(
            "ingest_peer_commitment: sender_peer_id mismatch from {source} \
             (dropped, possible relay attempt)"
        );
        return None;
    }
    // Peer-id to embedded-pubkey binding: saorsa-core derives PeerId as
    // BLAKE3(pubkey_bytes). Without this check, a responder could sign
    // with a throwaway key they own and lie about which identity it
    // belongs to (the embedded-key signature would verify trivially).
    let derived_peer_id = *blake3::hash(&c.sender_public_key).as_bytes();
    if derived_peer_id != c.sender_peer_id {
        warn!(
            "ingest_peer_commitment: embedded pubkey does not hash to claimed peer_id for \
             {source} (dropped, throwaway-key attack)"
        );
        return None;
    }
    // §2 step 3 + §11 DoS: rate-limit per-peer to at most one ML-DSA
    // signature verify per `COMMITMENT_SIG_VERIFY_MIN_INTERVAL`. A
    // sybil/RT-membership-bypassing peer that flooded valid-looking
    // gossip would otherwise burn CPU on every message. The rate
    // limit is checked AFTER cheap structural gates (RT, peer-id
    // binding, pubkey-binding) and BEFORE the expensive sig verify.
    //
    // Tracked in `sig_verify_attempts` (separate from
    // last_commitment_by_peer) so EVERY attempt — successful or not —
    // bumps the rate-limit clock. Reading only from PeerCommitmentRecord
    // would skip the cap for peers we've never successfully verified,
    // letting a flood of invalid-but-structurally-plausible gossips
    // burn CPU.
    let now = Instant::now();
    if !sig_verify_rate_limit_ok(sig_verify_attempts, source, now).await {
        debug!(
            "ingest_peer_commitment: rate-limited sig verify from {source} \
             (< {COMMITMENT_SIG_VERIFY_MIN_INTERVAL:?} since last attempt); dropped"
        );
        return None;
    }
    // Signature verify, using the public key embedded in the commitment
    // itself. The pubkey is bound by the signature payload (see
    // commitment_signed_payload) so an adversary cannot keep the body
    // and swap the key to one they hold the secret for.
    if !crate::replication::commitment::verify_commitment_signature(c) {
        warn!(
            "ingest_peer_commitment: signature did not verify under embedded key for {source} \
             (dropped, forged commitment)"
        );
        return None;
    }
    // The new commitment's hash, used to store and to pin for the audit target.
    let new_hash = commitment_hash(c);
    let mut map = last_commitment_by_peer.write().await;
    // Sybil/churn cap: if we're at the hard cap AND this is a new peer,
    // evict an arbitrary existing entry to make room. Updates for peers
    // already in the map are always accepted (they replace, not grow).
    if map.len() >= MAX_LAST_COMMITMENT_BY_PEER && !map.contains_key(source) {
        // Drop one arbitrary entry. HashMap iter order is random which
        // is fine — over time PeerRemoved cleanup keeps the working set
        // anchored on the real RT membership; this cap only fires under
        // active flooding attempts.
        if let Some(victim) = map.keys().next().copied() {
            map.remove(&victim);
            warn!(
                "ingest_peer_commitment: cache full ({MAX_LAST_COMMITMENT_BY_PEER}); \
                 evicted {victim} to admit {source}"
            );
        }
    }
    // Preserve sticky commitment_capable across updates — once true,
    // always true. New entries start with capable = true (we just
    // verified a valid commitment from this peer).
    map.entry(*source)
        .and_modify(|r| {
            // set_commitment refreshes the cached hash (§13) alongside the
            // commitment + received_at so they never drift.
            r.set_commitment(c.clone(), now);
            r.last_sig_verify_at = now;
            r.commitment_capable = true; // sticky-redundant but explicit
        })
        .or_insert_with(|| PeerCommitmentRecord::from_verified(c.clone(), now));
    drop(map);
    // Record the sticky "ever v12-capable" bit in a set independent of
    // `last_commitment_by_peer` (whose entries can be evicted by
    // `PeerRemoved` and the sybil cap). This is what the §3 audit
    // shield and the §6 holder-eligibility closure consult to decide
    // whether the peer is expected to speak v12.
    //
    // Capped at `MAX_EVER_CAPABLE_PEERS` to bound memory under
    // identity-rotation attacks: once full, new entries are refused.
    // Refusal degrades over-cap peers to the behaviour before this set
    // existed (treated as legacy on rejoin), which is not a security
    // regression and preserves the historic set stable.
    {
        let mut set = ever_capable_peers.write().await;
        if set.contains(source) || set.len() < MAX_EVER_CAPABLE_PEERS {
            set.insert(*source);
        } else {
            warn!(
                "ingest_peer_commitment: ever_capable_peers at cap \
                 ({MAX_EVER_CAPABLE_PEERS}); refusing to record {source} as sticky-capable"
            );
        }
    }
    // Return an audit target for EVERY valid stored commitment (changed or
    // not), so the caller's cooldown+probability-gated trigger keeps a
    // stable-keyset peer auditable over time (ADR-0002). Only a serialization
    // failure (new_hash == None, unreachable for a real commitment) yields None.
    new_hash.map(|pin_hash| AuditTarget {
        pin_hash,
        key_count: c.key_count,
    })
}

// ---------------------------------------------------------------------------
// Storage-bound audit (v12) — responder commitment rotation
// ---------------------------------------------------------------------------

/// Reload persisted responder retention at startup (ADR-0004 A1). A missing file
/// is a normal fresh start; a corrupt snapshot is logged and skipped (fail-open
/// LOCALLY — the node re-gossips a fresh root — which never grants a remote grace).
async fn load_commitment_retention(state: &ResponderCommitmentState, path: &Path) {
    let bytes = match tokio::fs::read(path).await {
        Ok(b) => b,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            debug!(
                "Commitment retention: no snapshot at {} (fresh start)",
                path.display()
            );
            return;
        }
        Err(e) => {
            warn!(
                "Commitment retention: failed to read {}: {e}",
                path.display()
            );
            return;
        }
    };
    if let Some(mut persisted) = PersistedRetention::from_bytes(&bytes) {
        let sidecar = pointer_leaves_path(path);
        match tokio::fs::read(&sidecar).await {
            Ok(leaves) => {
                if !persisted.attach_pointer_leaves(&leaves) {
                    warn!(
                        "Commitment retention: corrupt pointer leaves at {}; \
                         commitments holding pointers will not be restored",
                        sidecar.display()
                    );
                }
            }
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => warn!(
                "Commitment retention: failed to read {}: {e}",
                sidecar.display()
            ),
        }
        state.restore(&persisted);
        info!(
            "Commitment retention: reloaded {} slot(s) from {}",
            state.retained_slot_count(),
            path.display()
        );
    } else {
        warn!(
            "Commitment retention: corrupt snapshot at {}; starting with empty retention",
            path.display()
        );
    }
}

/// Persist the responder retention snapshot IF it changed since `last` (ADR-0004
/// A1). Write-on-change keeps frequent gossip-stamp refreshes durable without
/// needless disk writes on idle nodes. On success updates `last` to the bytes
/// written; on a serialization/write error the existing on-disk snapshot is left
/// intact (never truncated).
///
/// The pointer leaves go to a sidecar written first (ADR-0016), so the
/// retention file never names a commitment whose pointer leaves are not yet
/// on disk. A crash between the two leaves a sidecar ahead of the file, which
/// is harmless: it is keyed by commitment hash.
async fn persist_retention_if_changed(
    state: &ResponderCommitmentState,
    path: &Path,
    last: &mut Option<Vec<u8>>,
) {
    let snapshot = state.snapshot();
    let (Some(bytes), Some(leaves)) = (snapshot.to_bytes(), snapshot.pointer_leaves_bytes()) else {
        warn!("Commitment retention: serialization failed; keeping previous snapshot");
        return;
    };
    let mut combined = leaves.clone();
    combined.extend_from_slice(&bytes);
    if last.as_deref() == Some(combined.as_slice()) {
        return;
    }
    if write_retention_atomic(&pointer_leaves_path(path), leaves).await
        && write_retention_atomic(path, bytes).await
    {
        *last = Some(combined);
    }
}

/// Where the pointer leaves of the retention at `path` are kept.
fn pointer_leaves_path(path: &Path) -> PathBuf {
    path.with_file_name("commitment_retention_pointers.bin")
}

/// Durably write `bytes` to `path`: temp file → fsync temp → atomic rename →
/// fsync parent dir (so the rename itself survives a crash). Returns `true` on
/// success. Only the retention-persist loop writes this path, so a fixed temp
/// name is safe.
async fn write_retention_atomic(path: &Path, bytes: Vec<u8>) -> bool {
    let path = path.to_path_buf();
    let res = tokio::task::spawn_blocking(move || -> std::io::Result<()> {
        let tmp = path.with_extension("tmp");
        std::fs::write(&tmp, &bytes)?;
        std::fs::File::open(&tmp)?.sync_all()?;
        std::fs::rename(&tmp, &path)?;
        // Fsync the directory so the rename (the durable-commit point) is not
        // lost on a crash right after it. An empty parent (relative filename)
        // means the current directory.
        let dir = path
            .parent()
            .filter(|p| !p.as_os_str().is_empty())
            .unwrap_or_else(|| Path::new("."));
        std::fs::File::open(dir)?.sync_all()?;
        Ok(())
    })
    .await;
    match res {
        Ok(Ok(())) => true,
        Ok(Err(e)) => {
            warn!("Commitment retention: persist failed: {e}");
            false
        }
        Err(e) => {
            warn!("Commitment retention: persist task join failed: {e}");
            false
        }
    }
}

/// Read the current key set, build + sign a fresh
/// `StorageCommitment`, and rotate it into `state` as the new `current`.
/// The prior `current` is demoted to `previous`; the prior `previous` is
/// dropped (per `ResponderCommitmentState::rotate`).
///
/// For content-addressed chunks (Autonomi's chunk store), `address ==
/// BLAKE3(content)`, so `bytes_hash := key` and we don't have to
/// re-read each chunk's bytes to compute the leaf hash.
///
/// The pointers this node is responsible for are committed alongside, each as
/// `(address, pointer_leaf_hash(address))` (ADR-0016), so they are audited and
/// priced exactly as chunks are.
///
/// Skips (returns `Ok(())`) if the key set is empty — no commitment to
/// rotate. The auditor side handles "no commitment for this peer" by
/// falling back to the legacy plain-digest audit path.
async fn rebuild_and_rotate_commitment(
    storage: &Arc<ChunkStore>,
    pointers: Option<&PointerStore>,
    identity: &Arc<NodeIdentity>,
    state: &Arc<ResponderCommitmentState>,
    p2p: &Arc<P2PNode>,
    config: &Arc<ReplicationConfig>,
) -> Result<()> {
    // `all_keys()` is what the store can answer for: it already drops a file marked suspect or
    // known-wrong, so a name that cannot be read is not offered here. It is NOT the commitment
    // set — that is narrowed to the keys this node is still responsible for, by the filter a
    // few lines below, and not by this call. The comment that used to be here said "not
    // `all_keys()`" immediately above the call to it, which is the sort of thing that sends
    // somebody to change the wrong layer.
    let stored_keys = storage
        .all_keys()
        .await
        .map_err(|e| Error::Storage(format!("commitment build: read keys: {e}")))?;

    // Commit only to keys we are still RESPONSIBLE for ("want-to-hold"), not
    // everything currently on disk ("hold"). This is the half of the retention
    // contract that lets out-of-range chunks age out: a key that has left our
    // close group is excluded from the NEXT commitment, so once its last gossip
    // ages past GOSSIP_ANSWERABILITY_TTL it falls out of the in-window retained
    // set, `ResponderCommitmentState::is_held` goes false,
    // and the pruner (which until then vetoes its deletion) reclaims it. Without
    // this filter the pruner's reprieve would keep re-committing stale keys
    // forever (the rebuild reads all_keys, so a retained-on-disk key would be
    // re-committed and re-gossiped every rotation — a permanent pin).
    let self_id = *p2p.peer_id();
    let mut keys = Vec::with_capacity(stored_keys.len());
    for k in stored_keys {
        if admission::is_responsible(&self_id, &k, p2p, config.close_group_size).await {
            keys.push(k);
        }
    }
    // The same rule for pointers: an out-of-range pointer leaves the next
    // commitment, and the pruner reclaims it once no retained slot holds it.
    let mut pointer_keys = Vec::new();
    for state in pointers.map(PointerStore::held_states).unwrap_or_default() {
        if admission::is_responsible(&self_id, &state.address, p2p, config.close_group_size).await {
            pointer_keys.push(state.address);
        }
    }

    if keys.is_empty() && pointer_keys.is_empty() {
        // There used to be a second branch here that dropped every retained root outright
        // when the node looked empty. It is gone, and the reason is worth keeping.
        //
        // "Empty" was decided from key counts, and every version of that test was wrong in
        // the same direction: a node can hold bytes the counts do not show, because a file is
        // published before it is indexed. Each fix closed one window and left another.
        //
        // The asymmetry is what settles it. Clearing wrongly repudiates a root a peer is
        // pinning, and `UnknownCommitment` is a confirmed failure on the commitment-bound
        // lane, so a node that still holds the bytes is penalised for holding them. Retiring
        // wrongly costs a root that stops being advertised now and ages out by its gossip TTL
        // instead of vanishing now. Both set `has_current = false`; they differ only in
        // whether the node goes on being answerable in the meantime. A genuinely empty node
        // cannot answer either way, so retiring costs it nothing it had.
        //
        // So there is one branch, and no emptiness question to get wrong.
        //
        // A peer may still be pinning a root we gossiped
        // moments ago and could demand its bytes in a round-2 challenge, which
        // we can still answer (the bytes are present). But we must STOP
        // advertising the stale commitment: retire it so `current()` returns
        // `None` and the gossip-emit sites stop re-emitting and re-stamping it.
        // The retired slot then ages out by its gossip-answerability TTL while
        // remaining answerable for in-flight pins until then. Once it ages out,
        // `is_held` flips false and the pruner reclaims the now-uncommitted,
        // out-of-range chunks. (Calling `age_out` alone would leave `current()`
        // pointing at the stale root, which the gossip loop would keep
        // re-stamping — pinning its keys forever.)
        debug!(
            "Commitment rotation: no responsible keys to commit to; retiring current commitment \
             (stays answerable until its gossip TTL lapses, bytes still on disk)"
        );
        state.retire_current();
        return Ok(());
    }

    // Cap to MAX_COMMITMENT_KEY_COUNT for v12 (responder must not commit
    // to more than the protocol limit; auditor would reject the
    // commitment otherwise).
    let cap = commitment::MAX_COMMITMENT_KEY_COUNT as usize;
    let total = keys.len().saturating_add(pointer_keys.len());
    if total > cap {
        warn!(
            "Commitment rotation: key set ({total}) exceeds MAX_COMMITMENT_KEY_COUNT ({cap}); \
             truncating — investigate as this likely means a misconfiguration"
        );
    }

    // INVARIANT: this module is only used with CONTENT-ADDRESSED chunks,
    // where `key == BLAKE3(content)`, so `bytes_hash := key` and we skip a
    // full chunk re-read per rotation.
    //
    // Consequence to be precise about: because the leaf is `(key, key)`,
    // the Merkle root commits to the SET OF KEYS, not to the bytes. The
    // commitment therefore binds "which keys I claim to hold"; it does NOT
    // by itself prove byte possession. Byte possession is enforced by the
    // round-2 slice audit: a Bao verified slice decoded against the chunk
    // ADDRESS plus a keyed nonced block-tree opening under a fresh per-audit
    // nonce, so a responder that holds the key list but dropped the bytes
    // cannot answer. This is sound ONLY while keys are content addresses;
    // the round-1 verifier enforces `bytes_hash == key` on every audited leaf
    // (`evaluate_subtree_structure`), so a non-content-addressed
    // `(key, bytes_hash)` leaf is rejected rather than letting a byte-less node
    // earn credit for `key`. If this module is ever reused for
    // non-content-addressed records, that `(k, k)` shortcut AND the verifier
    // gate must be replaced with `(key, BLAKE3(bytes))` computed from real bytes.
    //
    // Pointers are that case, and are handled by being told apart rather than
    // hashed: a pointer leaf is `(address, pointer_leaf_hash(address))`, which
    // the verifier accepts only for a leaf of exactly a pointer's size, and
    // round 2 then demands the whole signed record in place of a Bao slice.
    // The root binds which pointers are held, never their current state, so an
    // update does not move it.
    //
    // A pointer address that is also a chunk key would need a BLAKE3 preimage;
    // the dedup only keeps such a collision from failing the whole build.
    let mut entries: Vec<_> = keys
        .into_iter()
        .map(|k| (k, k))
        .chain(
            pointer_keys
                .into_iter()
                .map(|address| (address, commitment::pointer_leaf_hash(&address))),
        )
        .collect();
    entries.sort_by_key(|(k, _)| *k);
    entries.dedup_by_key(|(k, _)| *k);
    entries.truncate(cap);

    // No-op-rotation guard: compute just the Merkle root from `entries`
    // and compare against the currently-advertised commitment's root.
    // If they match, the key set is unchanged and a new rotation would
    // only swap a randomized ML-DSA signature for a fresh one — same
    // content, different commitment_hash. That invalidates every
    // outstanding `recent_provers` credit on this node across the
    // close group with no security benefit, breaking steady-state
    // quorum liveness on large nodes that can't re-audit every key
    // every rotation interval. Skip the rotation entirely when the
    // tree is unchanged.
    // Build the tree ONCE here (moving `entries`): it serves both the no-op
    // root check below and, if we proceed, the signed commitment via
    // `build_from_tree` (§11 — previously the tree was built here and AGAIN
    // inside `BuiltCommitment::build`).
    let candidate_tree = commitment::MerkleTree::build(entries)
        .map_err(|e| Error::Crypto(format!("commitment tree build: {e}")))?;
    let candidate_root = candidate_tree.root();
    if let Some(current) = state.current() {
        if current.commitment().root == candidate_root {
            debug!(
                "Commitment rotation: key set unchanged (root={}); skipping no-op re-sign",
                hex::encode(candidate_root)
            );
            // Even though we skip re-signing (to avoid invalidating holder
            // credit), retention must still advance on the wall clock: a
            // previously-gossiped commitment that holds a now-out-of-range key
            // must be able to age out of the answerability window even when the
            // committed key set is frozen here for many rotations. Without this,
            // the no-op guard would pin a stale slot — and its key — forever.
            state.age_out();
            return Ok(());
        }
    }

    let sk_bytes = identity.secret_key_bytes().to_vec();
    let sk = MlDsaSecretKey::from_bytes(MlDsaVariant::MlDsa65, &sk_bytes)
        .map_err(|e| Error::Crypto(format!("commitment build: load sk: {e}")))?;
    let pk_bytes = identity.public_key().as_bytes().to_vec();
    let peer_id_bytes = *p2p.peer_id().as_bytes();

    let built = commitment_state::BuiltCommitment::build_from_tree(
        candidate_tree,
        &peer_id_bytes,
        &sk,
        &pk_bytes,
    )
    .map_err(|e| Error::Crypto(format!("commitment build: {e}")))?;

    let hash = hex::encode(built.hash());
    let key_count = built.commitment().key_count;
    let pointer_count = built.tree().pointer_count();
    state.rotate(built);
    info!("Storage commitment rotated: hash={hash} key_count={key_count} pointers={pointer_count}");
    Ok(())
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {

    /// The responder's answer says whether it lacks the chunk or failed to read it.
    ///
    /// A key the peer does not hold reads as `Ok(None)`. A read that fails, whether from
    /// an I/O fault or a failed integrity check, reads as `Err`. Nothing turns the first
    /// into the second.
    #[tokio::test]
    async fn a_missing_key_reads_as_a_plain_miss_and_a_failed_read_as_a_fault() {
        let dir = tempfile::tempdir().expect("temp dir");
        let storage = crate::storage::ChunkStore::new(crate::storage::ChunkStoreConfig {
            root_dir: dir.path().to_path_buf(),
            verify_on_read: true,
            disk_reserve: 0,
        })
        .await
        .expect("open store");

        let absent = [9u8; 32];
        assert!(
            matches!(storage.get(&absent).await, Ok(None)),
            "a chunk this node does not hold must read as a plain miss, not a fault"
        );

        // And the answer each read produces: a miss is `NotFound`, a failed read is `Error`.
        // Both are charged to a verified source that gives them, but they say different
        // things to whoever reads the logs.
        assert!(matches!(
            fetch_response_for(absent, Ok(None)),
            protocol::FetchResponse::NotFound { .. }
        ));
        assert!(matches!(
            fetch_response_for(absent, Ok(Some(vec![1, 2, 3]))),
            protocol::FetchResponse::Success { .. }
        ));
        assert!(matches!(
            fetch_response_for(
                absent,
                Err(crate::error::Error::Storage("read failed".into()))
            ),
            protocol::FetchResponse::Error { .. }
        ));
    }
    use super::*;
    use super::{
        apply_audit_failure_credit_revocation, audit_failure_clears_bootstrap_claim,
        audit_failure_revokes_holder_credit, audit_launch_decision, coalesce_first_audit_event,
        config, cooldown_allows_audit, first_audit_count_jump, first_audit_terminal_outcome,
        first_failed_key_label, fresh_offer_payment_context, paid_notify_payment_context,
        quote_answerable_through_nominal_jitter, quote_within_audit_window, FirstAuditLimiter,
        FirstAuditObservability, FirstAuditQueueOutcome, FirstAuditScheduler,
        FirstAuditTerminalOutcome, LimiterVerdict, MonetizedPinEvent,
        FIRST_AUDIT_SEND_LATENCY_SLACK, MONETIZED_AUDIT_SKEW_MARGIN,
    };
    use crate::payment::VerificationContext;
    use crate::replication::audit::AuditTickResult;
    use crate::replication::commitment_state::GOSSIP_ANSWERABILITY_TTL;
    use crate::replication::recent_provers::RecentProvers;
    use crate::replication::types::{AuditFailureReason, FailureEvidence};
    use lru::LruCache;
    use saorsa_core::identity::PeerId;
    use std::collections::HashMap;
    use std::num::NonZeroUsize;
    use std::sync::atomic::Ordering;
    use std::sync::Arc;
    use std::time::Duration;
    use std::time::Instant;
    use std::time::SystemTime;

    #[test]
    fn match_replication_protocol_accepts_core_and_subtree_bare_and_rr() {
        for id in [REPLICATION_PROTOCOL_ID, SUBTREE_AUDIT_PROTOCOL_ID] {
            assert_eq!(match_replication_protocol(id), Some((id, false)));
            assert_eq!(
                match_replication_protocol(&format!("{RR_PREFIX}{id}")),
                Some((id, true))
            );
        }
        assert_eq!(
            match_replication_protocol("autonomi.ant.replication.possession-audit.v2"),
            None
        );
        assert_eq!(match_replication_protocol("autonomi.ant.dht.v1"), None);
        assert_eq!(match_replication_protocol(RR_PREFIX), None);
    }

    #[test]
    fn body_matches_protocol_is_symmetric_over_core_and_subtree_bodies() {
        use crate::replication::protocol::{
            AuditChallenge, AuditResponse, FreshReplicationOffer, ReplicationMessageBody,
            SubtreeSliceChallenge,
        };

        let subtree = ReplicationMessageBody::SubtreeSliceChallenge(SubtreeSliceChallenge {
            challenge_id: 1,
            nonce: [0u8; 32],
            challenged_peer_id: [0u8; 32],
            expected_commitment_hash: [0u8; 32],
            openings: vec![],
        });
        let digest_challenge = ReplicationMessageBody::AuditChallenge(AuditChallenge {
            challenge_id: 1,
            nonce: [0u8; 32],
            challenged_peer_id: [0u8; 32],
            keys: vec![[0u8; 32]],
        });
        let digest_response =
            ReplicationMessageBody::AuditResponse(AuditResponse::Bootstrapping { challenge_id: 1 });
        let core = ReplicationMessageBody::FreshReplicationOffer(FreshReplicationOffer {
            key: [0u8; 32],
            data: vec![],
            proof_of_payment: vec![],
        });

        assert!(body_matches_protocol(&subtree, SUBTREE_AUDIT_PROTOCOL_ID));
        assert!(!body_matches_protocol(&subtree, REPLICATION_PROTOCOL_ID));

        for body in [&digest_challenge, &digest_response, &core] {
            assert!(body_matches_protocol(body, REPLICATION_PROTOCOL_ID));
            assert!(!body_matches_protocol(body, SUBTREE_AUDIT_PROTOCOL_ID));
        }

        for body in [&subtree, &digest_challenge, &digest_response, &core] {
            assert!(body_matches_protocol(body, response_protocol_for(body)));
        }
    }

    fn test_peer(b: u8) -> PeerId {
        let mut bytes = [0u8; 32];
        bytes[0] = b;
        PeerId::from_bytes(bytes)
    }

    /// A structurally valid offer for `data`: real content address, a proof of
    /// the minimum accepted size, distinguished by `proof_marker` so a test can
    /// tell whose proof came back out of the queue.
    fn test_fresh_offer(data: Vec<u8>, proof_marker: u8) -> protocol::FreshReplicationOffer {
        protocol::FreshReplicationOffer {
            key: crate::client::compute_address(&data),
            data,
            proof_of_payment: vec![proof_marker; MIN_PAYMENT_PROOF_SIZE_BYTES],
        }
    }

    fn admit_test_offer(
        in_flight: &FreshOfferInFlight,
        offer: protocol::FreshReplicationOffer,
        source: PeerId,
    ) -> FreshOfferAdmission {
        FreshOfferEntryGuard::admit(in_flight, offer, source, 0, None, Instant::now())
    }

    #[test]
    fn fresh_offer_entry_queues_one_proof_per_source_then_frees_the_key_on_drop() {
        let in_flight: FreshOfferInFlight = Arc::new(Mutex::new(HashMap::new()));
        let data = vec![0x11u8; 64];

        let opened = admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 1), test_peer(1));
        let FreshOfferAdmission::Opened(entry) = opened else {
            panic!("first offer for a key should open the entry")
        };

        // A second sender contributes its proof instead of being turned away:
        // its bytes are provably the same, so only the proof is worth keeping.
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 2), test_peer(2)),
                FreshOfferAdmission::Joined
            ),
            "a duplicate from a new source should queue its proof"
        );
        // The same peer twice is not extra evidence, just extra work.
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 3), test_peer(1)),
                FreshOfferAdmission::DuplicateSource
            ),
            "one source should hold at most one queued proof per key"
        );

        // An entry that outlived its handler would bar the key forever.
        drop(entry);
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(data, 4), test_peer(3)),
                FreshOfferAdmission::Opened(_)
            ),
            "a released key should be openable again"
        );
    }

    #[test]
    fn fresh_offer_entries_are_independent_across_keys_sharing_a_prefix() {
        let in_flight: FreshOfferInFlight = Arc::new(Mutex::new(HashMap::new()));

        // A node only receives offers for keys close to its own ID, so accepted
        // keys share a long prefix. Any prefix-derived shard index would have
        // funnelled these two onto one lock and serialized them.
        let _first = admit_test_offer(&in_flight, test_fresh_offer(vec![1u8; 8], 1), test_peer(1));
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(vec![2u8; 8], 1), test_peer(1)),
                FreshOfferAdmission::Opened(_)
            ),
            "distinct keys should never block each other"
        );
    }

    /// The regression that motivated queueing proofs at all.
    ///
    /// A sender whose proof does not verify must cost the network its own
    /// attempt and nothing more. Refusing every later offer outright made the
    /// first arrival the only arrival, so one bad proof lost the record — and
    /// the delayed possession check then charged that absence to this node.
    #[test]
    fn a_failing_proof_hands_the_key_to_the_next_sender() {
        let in_flight: FreshOfferInFlight = Arc::new(Mutex::new(HashMap::new()));
        let data = vec![0x5Au8; 128];
        let forger = test_peer(0xF0);
        let genuine = test_peer(0x0F);

        let FreshOfferAdmission::Opened(mut entry) =
            admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 0xBA), forger)
        else {
            panic!("the first offer should open the entry");
        };
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 0x60), genuine),
                FreshOfferAdmission::Joined
            ),
            "the genuine sender must not be turned away by the forger"
        );

        let first = entry
            .next_attempt()
            .expect("the opener's proof comes first");
        assert_eq!(first.source, forger);

        // Treat the forger's proof as failed: the next one is still reachable,
        // and it is tried against the bytes already held rather than a refetch.
        let second = entry
            .next_attempt()
            .expect("a failed proof must not consume the key");
        assert_eq!(second.source, genuine);
        assert_eq!(
            second.proof_of_payment,
            vec![0x60u8; MIN_PAYMENT_PROOF_SIZE_BYTES]
        );
        assert_eq!(
            entry.data(),
            data.as_slice(),
            "every proof is tried against the one copy of the bytes"
        );

        // Draining the queue releases the key rather than stranding it.
        assert!(entry.next_attempt().is_none());
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(data, 0x70), test_peer(9)),
                FreshOfferAdmission::Opened(_)
            ),
            "an exhausted entry should release its key"
        );
    }

    /// The cap has to be a lifetime budget, not a queue depth.
    ///
    /// The handler pops a proof before verifying it, so gating admission on the
    /// *instantaneous* queue length lets a fresh source refill the slot that pop
    /// just freed. Each refill is another on-chain payment verification, run
    /// sequentially while the entry holds its admission permit and a worker
    /// slot, so a stream of distinct sources could keep one key's handler — and
    /// one of only four workers — busy indefinitely.
    #[test]
    fn refilling_a_popped_slot_cannot_extend_a_keys_attempt_budget() {
        let in_flight: FreshOfferInFlight = Arc::new(Mutex::new(HashMap::new()));
        let data = vec![0x3Du8; 32];

        let FreshOfferAdmission::Opened(mut entry) =
            admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 0), test_peer(0))
        else {
            panic!("the first offer should open the entry")
        };

        // Drive pop-then-admit with a fresh source every time, exactly as a
        // sybil stream would. The budget must be spent by admissions, not
        // returned by pops.
        let mut admitted = 1;
        for i in 1..u8::try_from(MAX_FRESH_OFFER_ATTEMPTS_PER_KEY * 4).unwrap_or(u8::MAX) {
            let _ = entry.next_attempt();
            if matches!(
                admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 0), test_peer(i)),
                FreshOfferAdmission::Joined
            ) {
                admitted += 1;
            }
        }

        assert!(
            admitted <= MAX_FRESH_OFFER_ATTEMPTS_PER_KEY,
            "a key accepted {admitted} proofs over its lifetime, but the budget is \
             {MAX_FRESH_OFFER_ATTEMPTS_PER_KEY}: popping a proof must not return its slot"
        );
    }

    #[test]
    fn a_fresh_offer_entry_queues_no_more_proofs_than_the_close_group_can_send() {
        let in_flight: FreshOfferInFlight = Arc::new(Mutex::new(HashMap::new()));
        let data = vec![0x2Cu8; 32];

        // The opener occupies the first slot, so the cap is reached after
        // MAX_FRESH_OFFER_ATTEMPTS_PER_KEY distinct sources in total.
        let _entry = admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 0), test_peer(0));
        for i in 1..MAX_FRESH_OFFER_ATTEMPTS_PER_KEY {
            let source = test_peer(u8::try_from(i).unwrap_or(u8::MAX));
            assert!(
                matches!(
                    admit_test_offer(&in_flight, test_fresh_offer(data.clone(), 0), source),
                    FreshOfferAdmission::Joined
                ),
                "sender {i} is within the legitimate close-group fan-out"
            );
        }

        let surplus = test_peer(0xEE);
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(data, 0), surplus),
                FreshOfferAdmission::Full
            ),
            "past the cap a proof is surplus: enough are already queued to place the record"
        );
    }

    #[test]
    fn verification_receiver_accepts_a_full_cycle_and_rejects_more() {
        assert!(!verification_request_exceeds_limit(
            config::MAX_VERIFICATION_KEYS_PER_CYCLE
        ));
        assert!(verification_request_exceeds_limit(
            config::MAX_VERIFICATION_KEYS_PER_CYCLE + 1
        ));
    }

    // The heavy round-1 limiter enforces the per-peer rate cooldown
    // and single-use round-1 → round-2 sessions.
    #[tokio::test]
    async fn subtree_round1_limiter_cooldown_and_single_use_session() {
        let limiter = SubtreeRound1Limiter::new(
            Duration::from_hours(1),
            config::MAX_CONCURRENT_SUBTREE_ROUND1,
        );
        let peer = test_peer(1);

        // First round-1 is admitted; drop the guard so concurrency is free again.
        let guard = limiter.admit(&peer).await;
        assert!(guard.is_some(), "first round-1 admitted");
        drop(guard);
        // A second round-1 within the cooldown is dropped even though the heavy
        // pool now has a free slot — the rate cooldown, not concurrency, blocks it.
        assert!(
            limiter.admit(&peer).await.is_none(),
            "second round-1 within cooldown is rate-dropped"
        );
        // A different peer has its own cooldown.
        assert!(limiter.admit(&test_peer(2)).await.is_some());

        // Session: opened by round 1, consumed exactly once by the matching round 2.
        let hash = [7u8; 32];
        let nonce = [9u8; 32];
        assert!(
            limiter
                .open_session(peer, 42, hash, nonce, PointerBindings::new())
                .await
        );
        // Wrong nonce / commitment does not match.
        assert!(limiter
            .consume_session(&peer, 42, &hash, &[0u8; 32])
            .await
            .is_none());
        assert!(limiter
            .consume_session(&peer, 42, &[0u8; 32], &nonce)
            .await
            .is_none());
        // A round 2 with no prior round 1 (wrong challenge_id) misses.
        assert!(limiter
            .consume_session(&peer, 99, &hash, &nonce)
            .await
            .is_none());
        // The matching round 2 consumes it — and only once (single-use).
        assert!(limiter
            .consume_session(&peer, 42, &hash, &nonce)
            .await
            .is_some());
        assert!(limiter
            .consume_session(&peer, 42, &hash, &nonce)
            .await
            .is_none());
    }

    // A session carries what its round 1 bound for each pointer to round 2,
    // and every live session together stays under the binding budget. A
    // session that does not fit is refused, and no session already opened
    // gives up its bindings or its place for it.
    #[tokio::test]
    async fn subtree_session_carries_pointer_bindings_within_the_budget() {
        let limiter = SubtreeRound1Limiter::new(Duration::ZERO, 1);
        let (hash, nonce) = ([1u8; 32], [2u8; 32]);
        let bindings = |from: u64, count: usize| -> PointerBindings {
            (from..)
                .take(count)
                .map(|i| {
                    let mut key = [0u8; 32];
                    key[..8].copy_from_slice(&i.to_le_bytes());
                    (key, [0xAB; 32])
                })
                .collect()
        };

        let half = MAX_SESSION_POINTER_BINDINGS / 2;
        let first = bindings(0, half);
        assert!(
            limiter
                .open_session(test_peer(1), 1, hash, nonce, first.clone())
                .await
        );
        assert!(
            limiter
                .open_session(test_peer(2), 2, hash, nonce, bindings(1 << 40, half))
                .await
        );
        // The budget is full: one more binding is refused, but a round 1 with
        // no pointers in it still opens.
        assert!(
            !limiter
                .open_session(test_peer(3), 3, hash, nonce, bindings(1 << 41, 1))
                .await
        );
        assert!(
            limiter
                .open_session(test_peer(4), 4, hash, nonce, PointerBindings::new())
                .await
        );

        assert_eq!(
            limiter
                .consume_session(&test_peer(1), 1, &hash, &nonce)
                .await,
            Some(first.clone()),
            "an opened session keeps every binding it was given"
        );
        assert!(
            limiter
                .consume_session(&test_peer(3), 3, &hash, &nonce)
                .await
                .is_none(),
            "a refused session was never opened"
        );
        assert_eq!(
            limiter
                .consume_session(&test_peer(2), 2, &hash, &nonce)
                .await
                .map(|b| b.len()),
            Some(half)
        );

        // Consumed sessions free their room.
        assert!(
            limiter
                .open_session(test_peer(5), 5, hash, nonce, bindings(1 << 42, half))
                .await
        );
    }

    // A session refused for the binding budget is refused before the session
    // cap evicts anything: with both full, the refusal costs no session its
    // place.
    #[tokio::test]
    async fn a_session_refused_for_bindings_evicts_no_other_session() {
        let limiter = SubtreeRound1Limiter::new(Duration::ZERO, 1);
        let (hash, nonce) = ([1u8; 32], [2u8; 32]);
        let full: PointerBindings = (0u64..)
            .take(MAX_SESSION_POINTER_BINDINGS)
            .map(|i| {
                let mut key = [0u8; 32];
                key[..8].copy_from_slice(&i.to_le_bytes());
                (key, [0xCD; 32])
            })
            .collect();
        assert!(
            limiter
                .open_session(test_peer(0), 0, hash, nonce, full)
                .await
        );
        for id in 1..MAX_SUBTREE_SESSIONS as u64 {
            assert!(
                limiter
                    .open_session(test_peer(1), id, hash, nonce, PointerBindings::new())
                    .await
            );
        }

        let one: PointerBindings = std::iter::once(([0xEE; 32], [0xEE; 32])).collect();
        assert!(
            !limiter
                .open_session(test_peer(2), u64::MAX, hash, nonce, one)
                .await
        );
        assert_eq!(
            limiter.sessions.read().await.len(),
            MAX_SUBTREE_SESSIONS,
            "every session is still there"
        );
        assert!(
            limiter
                .consume_session(&test_peer(0), 0, &hash, &nonce)
                .await
                .is_some(),
            "including the oldest"
        );
    }

    // The concurrency pool and the per-peer cooldown are both keyed by peer id,
    // so a party holding several identities refills its allowance by rotating
    // between them and can keep the heavy pool busy indefinitely. The work
    // budget is keyed by nothing: it is charged for bytes proved, whoever asked,
    // so a fresh identity is refused exactly like a repeat caller once it is
    // spent. That is the difference between bounding concurrency and bounding
    // sustained work.
    #[tokio::test]
    async fn round1_work_budget_is_not_refilled_by_a_fresh_identity() {
        // Cooldown disabled so only the work budget can refuse anything.
        let limiter =
            SubtreeRound1Limiter::new(Duration::ZERO, config::MAX_CONCURRENT_SUBTREE_ROUND1);
        assert!(
            limiter.admit(&test_peer(1)).await.is_some(),
            "a node starts with budget in hand so it can serve audits at once"
        );

        // Serve enough proof work to run the balance into debt. Charging exactly
        // the burst would leave it at zero, which the next nanosecond of refill
        // lifts back above the line — the debt is the point.
        limiter
            .charge_work(2 * SUBTREE_ROUND1_WORK_BURST_BYTES)
            .await;

        for id in 2..8u8 {
            assert!(
                limiter.admit(&test_peer(id)).await.is_none(),
                "a never-seen peer must still be refused while the budget is spent"
            );
        }
    }

    // The budget is a rate, not a quota: it comes back on its own, so an honest
    // auditor blocked by a flood is only delayed. Carrying the debt is what
    // prices an expensive proof above a cheap one — without it, a proof reading
    // a maximal subtree would cost no more of the next caller's wait than a
    // one-leaf proof.
    #[test]
    fn round1_work_budget_carries_debt_and_refills_over_time() {
        let now = Instant::now();
        let at = |secs: u64| {
            now.checked_add(Duration::from_secs(secs))
                .unwrap_or_else(Instant::now)
        };
        let mut budget = Round1WorkBudget {
            balance: 0,
            last_refill: now,
        };
        assert!(!budget.has_budget(now), "empty means empty");

        // A proof costing four seconds' worth of refill leaves four seconds of
        // debt, so the wait scales with what was actually served.
        budget.charge(4 * SUBTREE_ROUND1_WORK_REFILL_BYTES_PER_SEC, now);
        assert!(
            !budget.has_budget(at(3)),
            "still in debt three seconds after an over-large proof"
        );
        assert!(
            budget.has_budget(at(5)),
            "the debt is worked off at the refill rate"
        );

        // Idle time does not bank unbounded credit for a later flood.
        budget.refill(at(24 * 60 * 60));
        assert_eq!(
            budget.balance, SUBTREE_ROUND1_WORK_BURST_BYTES,
            "refill is capped at the burst size"
        );
    }

    // The heavy round-1 pool size is the tightest bound the responder applies and
    // the one least backed by fleet measurement, so it is read from config rather
    // than baked into the binary: a fleet that lands on the wrong number retunes
    // instead of waiting for a release. These pin that the configured value is
    // what actually gates admission, and that a 0 degrades to slow, not to off —
    // a wedged-shut responder would read on every auditor as a graced timeout,
    // i.e. a silent audit outage with nobody penalised and nothing in the logs.
    #[tokio::test]
    async fn round1_pool_size_comes_from_config() {
        // Cooldown disabled so only concurrency can refuse anything.
        let limiter = SubtreeRound1Limiter::new(Duration::ZERO, 1);
        let held = limiter.admit(&test_peer(1)).await;
        assert!(held.is_some(), "the single configured slot is admitted");
        assert!(
            limiter.admit(&test_peer(2)).await.is_none(),
            "a second concurrent round 1 is refused at a configured pool of 1, \
             proving admission follows config rather than the compiled-in default"
        );
        drop(held);
        assert!(
            limiter.admit(&test_peer(2)).await.is_some(),
            "the slot is reusable once the first proof completes"
        );
    }

    #[tokio::test]
    async fn round1_pool_size_of_zero_is_clamped_to_one() {
        let limiter = SubtreeRound1Limiter::new(Duration::ZERO, 0);
        assert!(
            limiter.admit(&test_peer(1)).await.is_some(),
            "a mis-set pool of 0 must still serve audits, not silently refuse every one"
        );
    }

    fn test_key(b: u8) -> crate::ant_protocol::XorName {
        let mut k = [0u8; 32];
        k[0] = b;
        k
    }

    #[test]
    fn bad_hint_penalty_rejects_or_directly_contradicts_sole_replica_source() {
        let source = test_peer(0x91);
        let corroborator = test_peer(0x92);
        let mut evidence = types::KeyVerificationEvidence {
            presence: HashMap::from([(source, PresenceEvidence::Absent)]),
            paid_list: HashMap::new(),
        };
        let failed = KeyVerificationOutcome::QuorumFailed;

        assert_eq!(
            punishable_singleton_replica_hint_source(&HashSet::from([source]), &failed, &evidence),
            Some(source),
            "a close-group rejection of a sole-source hint is punishable"
        );
        assert_eq!(
            punishable_singleton_replica_hint_source(
                &HashSet::from([source, corroborator]),
                &failed,
                &evidence,
            ),
            None,
            "corroborated hints must not use the sole-source penalty lane"
        );
        assert_eq!(
            punishable_singleton_replica_hint_source(&HashSet::new(), &failed, &evidence),
            None,
            "paid-list advertisements do not claim possession, so they leave the \
             replica-hint source set empty and cannot be penalized"
        );

        evidence
            .presence
            .insert(source, PresenceEvidence::Unresolved);
        assert_eq!(
            punishable_singleton_replica_hint_source(&HashSet::from([source]), &failed, &evidence),
            Some(source),
            "definitive close-group rejection is punishable without direct contradiction"
        );
        assert_eq!(
            punishable_singleton_replica_hint_source(
                &HashSet::from([source]),
                &KeyVerificationOutcome::QuorumInconclusive,
                &evidence,
            ),
            None,
            "timeouts and inconclusive evidence are neutral"
        );

        evidence.presence.insert(source, PresenceEvidence::Absent);
        assert_eq!(
            punishable_singleton_replica_hint_source(
                &HashSet::from([source]),
                &KeyVerificationOutcome::QuorumVerified {
                    sources: vec![corroborator],
                },
                &evidence,
            ),
            Some(source),
            "an explicit denial is punishable regardless of the overall outcome"
        );
    }

    /// Build a round-2 slice challenge matching an open session. `openings` is
    /// empty: these tests exercise admission and session handling only, which
    /// run before any block is opened.
    fn slice_challenge(
        challenge_id: u64,
        commitment_hash: [u8; 32],
        nonce: [u8; 32],
    ) -> protocol::SubtreeSliceChallenge {
        protocol::SubtreeSliceChallenge {
            challenge_id,
            nonce,
            challenged_peer_id: [0u8; 32],
            expected_commitment_hash: commitment_hash,
            openings: Vec::new(),
        }
    }

    // Regression (Copilot, PR #181): a round-2 slice challenge refused at the
    // responder caps MUST NOT burn the single-use round-1 session.
    //
    // The handler used to consume the session first and admit second, so a
    // transient local capacity drop permanently destroyed that audit exchange —
    // every retry hit the `no live round-1 session` path and got `Transient`,
    // even after load cleared. Turning momentary local load into a deterministic
    // round-2 miss costs the responder its whole-slice credit for that round.
    #[tokio::test]
    async fn capacity_refused_slice_challenge_preserves_round1_session() {
        let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_AUDIT_RESPONSES));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let round1 =
            SubtreeRound1Limiter::new(Duration::ZERO, config::MAX_CONCURRENT_SUBTREE_ROUND1);
        let peer = test_peer(0xB1);
        let (id, hash, nonce) = (77u64, [3u8; 32], [4u8; 32]);
        let challenge = slice_challenge(id, hash, nonce);

        assert!(
            round1
                .open_session(peer, id, hash, nonce, PointerBindings::new())
                .await
        );

        // Saturate this peer's share so the next admission must be refused.
        let mut hold = Vec::new();
        for _ in 0..MAX_AUDIT_RESPONSES_PER_PEER {
            match admit_audit_responder(&semaphore, &inflight, &peer, AuditResponderClass::Byte)
                .await
            {
                Ok(guard) => hold.push(guard),
                Err(err) => panic!("unexpected admission failure below the cap: {err:?}"),
            }
        }

        let refused =
            admit_slice_challenge(&semaphore, &inflight, &round1, &peer, &challenge).await;
        assert!(
            matches!(refused, SliceAdmission::Capacity(_)),
            "a saturated peer share must refuse on capacity, not on session"
        );

        // Load clears. The session must have survived the refusal.
        drop(hold);
        let retried =
            admit_slice_challenge(&semaphore, &inflight, &round1, &peer, &challenge).await;
        assert!(
            matches!(retried, SliceAdmission::Admitted(..)),
            "the round-1 session must survive a capacity refusal so the retry succeeds"
        );

        // Still single-use: the successful admission consumed it exactly once.
        drop(retried);
        let replayed =
            admit_slice_challenge(&semaphore, &inflight, &round1, &peer, &challenge).await;
        assert!(
            matches!(replayed, SliceAdmission::NoSession),
            "a consumed session must not be replayable"
        );
    }

    // The permit taken for the session probe is released when the probe misses,
    // so an unsessioned flood cannot pin the responder pool shut.
    #[tokio::test]
    async fn unsessioned_slice_challenge_releases_its_admission_slot() {
        let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_AUDIT_RESPONSES));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let round1 =
            SubtreeRound1Limiter::new(Duration::ZERO, config::MAX_CONCURRENT_SUBTREE_ROUND1);
        let peer = test_peer(0xB2);
        let challenge = slice_challenge(1, [0u8; 32], [0u8; 32]);

        // Far more unsessioned challenges than the per-peer cap would allow if
        // the slot leaked on the miss path.
        for _ in 0..(MAX_AUDIT_RESPONSES_PER_PEER * 4) {
            let outcome =
                admit_slice_challenge(&semaphore, &inflight, &round1, &peer, &challenge).await;
            assert!(
                matches!(outcome, SliceAdmission::NoSession),
                "no session was ever opened, so every attempt must miss"
            );
        }

        assert_eq!(
            semaphore.available_permits(),
            MAX_CONCURRENT_AUDIT_RESPONSES,
            "every global permit must be returned"
        );
        assert!(
            inflight.read().await.get(&peer).copied().unwrap_or(0) == 0,
            "no per-peer slot may be left occupied"
        );
    }

    #[test]
    fn holder_credit_revocation_distinguishes_timeout_from_confirmed_failure() {
        assert!(!audit_failure_revokes_holder_credit(
            &AuditFailureReason::Timeout
        ));
        assert!(audit_failure_revokes_holder_credit(
            &AuditFailureReason::DigestMismatch
        ));
    }

    #[tokio::test]
    async fn audit_responder_admission_reports_per_peer_cap_full() {
        let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_AUDIT_RESPONSES));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let peer = test_peer(0xA1);

        let mut guards = Vec::new();
        for _ in 0..MAX_AUDIT_RESPONSES_PER_PEER {
            match admit_audit_responder(&semaphore, &inflight, &peer, AuditResponderClass::Subtree)
                .await
            {
                Ok(guard) => guards.push(guard),
                Err(err) => panic!("unexpected admission failure before peer cap: {err:?}"),
            }
        }

        let Err(err) =
            admit_audit_responder(&semaphore, &inflight, &peer, AuditResponderClass::Subtree).await
        else {
            panic!("admission should fail once per-peer cap is full");
        };
        assert_eq!(err.reason, ResponderRejectReason::PerPeerCapFull);
        assert_eq!(err.peer_inflight, MAX_AUDIT_RESPONSES_PER_PEER);
        assert_eq!(err.peer_limit, MAX_AUDIT_RESPONSES_PER_PEER);
        assert_eq!(err.global_limit, MAX_CONCURRENT_AUDIT_RESPONSES);

        drop(guards);
    }

    #[tokio::test]
    async fn audit_responder_admission_reports_global_pool_full() {
        let semaphore = Arc::new(Semaphore::new(MAX_CONCURRENT_AUDIT_RESPONSES));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let peer = test_peer(0xA2);

        let mut held_global_permits = Vec::new();
        for _ in 0..MAX_CONCURRENT_AUDIT_RESPONSES {
            held_global_permits.push(
                Arc::clone(&semaphore)
                    .try_acquire_owned()
                    .expect("test should be able to exhaust the global pool"),
            );
        }

        let Err(err) =
            admit_audit_responder(&semaphore, &inflight, &peer, AuditResponderClass::Subtree).await
        else {
            panic!("admission should fail once global pool is full");
        };
        assert_eq!(err.reason, ResponderRejectReason::GlobalPoolFull);
        assert_eq!(err.global_inflight, MAX_CONCURRENT_AUDIT_RESPONSES);
        assert_eq!(err.global_limit, MAX_CONCURRENT_AUDIT_RESPONSES);
        assert_eq!(err.peer_inflight, 0);
        assert_eq!(err.peer_limit, MAX_AUDIT_RESPONSES_PER_PEER);

        drop(held_global_permits);
    }

    #[tokio::test]
    async fn bounded_responder_guard_releases_global_and_peer_slots_on_drop() {
        let semaphore = Arc::new(Semaphore::new(1));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let peer = test_peer(0xA3);

        let guard = admit_bounded_responder(&semaphore, &inflight, &peer, 1, 1)
            .await
            .expect("first responder should be admitted");
        assert_eq!(semaphore.available_permits(), 0);
        assert_eq!(inflight.read().await.get(&peer), Some(&1));

        drop(guard);
        assert_eq!(semaphore.available_permits(), 1);
        assert!(!inflight.read().await.contains_key(&peer));
    }

    #[tokio::test]
    async fn cancelled_bounded_responder_wait_does_not_leak_a_peer_slot() {
        let semaphore = Arc::new(Semaphore::new(1));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let peer = test_peer(0xA4);
        let held_map = inflight.write().await;

        let waiting = admit_bounded_responder(&semaphore, &inflight, &peer, 1, 1);
        assert!(
            tokio::time::timeout(Duration::from_millis(10), waiting)
                .await
                .is_err(),
            "admission should still be waiting for the peer map"
        );

        drop(held_map);
        assert_eq!(semaphore.available_permits(), 1);
        assert!(!inflight.read().await.contains_key(&peer));
    }

    #[test]
    fn responder_staleness_sheds_expired_requests_but_serves_fresh_ones() {
        let timeout = Duration::from_secs(1);
        let old_received_at = Instant::now()
            .checked_sub(timeout)
            .unwrap_or_else(Instant::now);

        assert!(request_is_stale(old_received_at, timeout));
        assert!(!request_is_stale(Instant::now(), timeout));
    }

    #[tokio::test]
    async fn neighbor_sync_admission_serializes_each_peer_but_allows_other_peers() {
        let semaphore = Arc::new(Semaphore::new(NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let first_peer = test_peer(0xB3);
        let second_peer = test_peer(0xB4);

        let first_guard = admit_bounded_responder(
            &semaphore,
            &inflight,
            &first_peer,
            NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING,
            NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING_PER_PEER,
        )
        .await
        .expect("first sync from a peer should be admitted");
        let duplicate = admit_bounded_responder(
            &semaphore,
            &inflight,
            &first_peer,
            NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING,
            NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING_PER_PEER,
        )
        .await;
        let other_peer = admit_bounded_responder(
            &semaphore,
            &inflight,
            &second_peer,
            NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING,
            NEIGHBOR_SYNC_RESPONDER_MAX_OUTSTANDING_PER_PEER,
        )
        .await;

        assert!(matches!(
            duplicate,
            Err(ResponderAdmissionFailure {
                reason: ResponderRejectReason::PerPeerCapFull,
                ..
            })
        ));
        assert!(other_peer.is_ok());
        drop(first_guard);
        drop(other_peer);
    }

    /// One flooding peer must not be able to hold every fresh-offer slot.
    ///
    /// This is the eviction primitive the per-peer share closes: with only a
    /// global bound, a single source occupies the pool, every honest offer in
    /// that window is refused, and because a refusal reads as absence to the
    /// sender's delayed possession check those refusals land as
    /// audit-severity penalties on *this* node.
    #[tokio::test]
    async fn fresh_offer_admission_reserves_capacity_for_other_peers() {
        let semaphore = Arc::new(Semaphore::new(FRESH_OFFER_MAX_OUTSTANDING));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let flooder = test_peer(0xC1);
        let honest = test_peer(0xC2);

        let mut held = Vec::new();
        for _ in 0..FRESH_OFFER_MAX_OUTSTANDING_PER_PEER {
            held.push(
                admit_bounded_responder(
                    &semaphore,
                    &inflight,
                    &flooder,
                    FRESH_OFFER_MAX_OUTSTANDING,
                    FRESH_OFFER_MAX_OUTSTANDING_PER_PEER,
                )
                .await
                .expect("offers within the per-peer share are admitted"),
            );
        }

        let flooder_excess = admit_bounded_responder(
            &semaphore,
            &inflight,
            &flooder,
            FRESH_OFFER_MAX_OUTSTANDING,
            FRESH_OFFER_MAX_OUTSTANDING_PER_PEER,
        )
        .await;
        assert!(
            matches!(
                flooder_excess,
                Err(ResponderAdmissionFailure {
                    reason: ResponderRejectReason::PerPeerCapFull,
                    ..
                })
            ),
            "one source must not exceed its fresh-offer share"
        );

        let honest_offer = admit_bounded_responder(
            &semaphore,
            &inflight,
            &honest,
            FRESH_OFFER_MAX_OUTSTANDING,
            FRESH_OFFER_MAX_OUTSTANDING_PER_PEER,
        )
        .await;
        assert!(
            honest_offer.is_ok(),
            "an honest sender must still be admitted while another peer floods"
        );

        assert!(
            usize::try_from(FRESH_OFFER_MAX_OUTSTANDING_PER_PEER).unwrap_or(usize::MAX)
                < FRESH_OFFER_MAX_OUTSTANDING,
            "the per-peer share must leave room for other sources"
        );

        drop(held);
        drop(honest_offer);
    }

    /// Paid notifies get the same per-source treatment as every other class.
    #[tokio::test]
    async fn paid_notify_admission_reserves_capacity_for_other_peers() {
        let semaphore = Arc::new(Semaphore::new(PAID_NOTIFY_MAX_OUTSTANDING));
        let inflight = Arc::new(RwLock::new(HashMap::new()));
        let flooder = test_peer(0xC3);
        let honest = test_peer(0xC4);

        let mut held = Vec::new();
        for _ in 0..PAID_NOTIFY_MAX_OUTSTANDING_PER_PEER {
            held.push(
                admit_bounded_responder(
                    &semaphore,
                    &inflight,
                    &flooder,
                    PAID_NOTIFY_MAX_OUTSTANDING,
                    PAID_NOTIFY_MAX_OUTSTANDING_PER_PEER,
                )
                .await
                .expect("notifies within the per-peer share are admitted"),
            );
        }

        assert!(
            matches!(
                admit_bounded_responder(
                    &semaphore,
                    &inflight,
                    &flooder,
                    PAID_NOTIFY_MAX_OUTSTANDING,
                    PAID_NOTIFY_MAX_OUTSTANDING_PER_PEER,
                )
                .await,
                Err(ResponderAdmissionFailure {
                    reason: ResponderRejectReason::PerPeerCapFull,
                    ..
                })
            ),
            "one source must not exceed its paid-notify share"
        );
        assert!(
            admit_bounded_responder(
                &semaphore,
                &inflight,
                &honest,
                PAID_NOTIFY_MAX_OUTSTANDING,
                PAID_NOTIFY_MAX_OUTSTANDING_PER_PEER,
            )
            .await
            .is_ok(),
            "an honest sender must still be admitted while another peer floods"
        );

        drop(held);
    }

    /// The fresh-offer per-source bound must guarantee headroom for a peer
    /// holding nothing, without throttling a single legitimate fan-out.
    ///
    /// Sized as a quota rather than a reserve, this bound refused offers during
    /// an ordinary upload (see `tests/e2e/fresh_offer_capacity.rs`), and every
    /// refusal is charged to THIS node at audit severity because the sender
    /// never reads it.
    #[test]
    fn fresh_offer_share_reserves_headroom_without_throttling_one_sender() {
        let share = usize::try_from(FRESH_OFFER_MAX_OUTSTANDING_PER_PEER).unwrap_or(usize::MAX);

        assert_eq!(
            FRESH_OFFER_MAX_OUTSTANDING - share,
            FRESH_OFFER_RESERVED_FOR_OTHER_SOURCES,
            "a source holding none must always find a free slot"
        );
        assert!(
            share < FRESH_OFFER_MAX_OUTSTANDING,
            "one peer must not be able to take the whole pool"
        );
        // The failure this pins: a share sized like the request/response
        // classes (fetch 2, verification 1, neighbor sync 1) binds on ordinary
        // one-way bulk traffic.
        assert!(
            share > usize::try_from(FETCH_RESPONDER_MAX_OUTSTANDING_PER_PEER).unwrap_or(usize::MAX),
            "fresh offers are one-way bulk, not request/response — their share \
             must not be sized like the request classes'"
        );
    }

    /// A malformed offer must be refused before it can occupy an admission
    /// slot, so junk cannot hold capacity for the length of a verification.
    #[test]
    fn structurally_invalid_fresh_offers_are_rejected_before_admission() {
        let key = test_key(0xC5);

        let valid_proof = vec![1u8; MIN_PAYMENT_PROOF_SIZE_BYTES];

        let rejection = fresh_offer_structural_rejection(&key, &[0u8; 1], &[])
            .expect("an offer with no proof must be refused");
        assert!(
            rejection.penalise,
            "an honest sender always attaches a proof"
        );

        // A proof is retained per queued sender, so its size has to be bounded
        // here rather than at the verifier, which only sees it on a worker.
        let undersized = vec![1u8; MIN_PAYMENT_PROOF_SIZE_BYTES - 1];
        let rejection = fresh_offer_structural_rejection(&key, &[0u8; 1], &undersized)
            .expect("an undersized proof must be refused");
        assert!(
            rejection.penalise,
            "no honest sender constructs an undersized proof"
        );
        let oversized_proof = vec![1u8; MAX_PAYMENT_PROOF_SIZE_BYTES + 1];
        let rejection = fresh_offer_structural_rejection(&key, &[0u8; 1], &oversized_proof)
            .expect("an oversized proof must be refused");
        assert!(
            rejection.penalise,
            "no honest sender constructs an oversized proof"
        );

        let rejection = fresh_offer_structural_rejection(
            &key,
            &vec![0u8; crate::ant_protocol::MAX_CHUNK_SIZE + 1],
            &valid_proof,
        )
        .expect("an oversized offer must be refused");
        assert!(
            rejection.penalise,
            "no honest sender constructs an oversized offer"
        );

        let data = vec![0u8; 1];
        assert!(
            fresh_offer_structural_rejection(
                &crate::client::compute_address(&data),
                &data,
                &valid_proof
            )
            .is_none(),
            "a well-formed offer must reach admission"
        );
    }

    /// Knowing a key must not be enough to enter its entry.
    ///
    /// Opening an entry on an unverified key/bytes association would let a peer
    /// that has only *learned* the key — every recipient of its `PaidNotify`,
    /// not just the close group that gets the chunk — seize it with junk.
    /// Joining one would be worse: the handler tries each queued proof against
    /// the *opener's* bytes, so an unchecked joiner could have its proof spent
    /// on bytes it never offered.
    #[test]
    fn a_fresh_offer_cannot_enter_an_entry_for_bytes_it_lacks() {
        let genuine_data = vec![0x5Au8; 128];
        let key = crate::client::compute_address(&genuine_data);
        let valid_proof = vec![1u8; MIN_PAYMENT_PROOF_SIZE_BYTES];

        let rejection = fresh_offer_structural_rejection(&key, &[0xFFu8; 1], &valid_proof)
            .expect("bytes that do not hash to the advertised key must be refused");
        assert!(
            rejection.penalise,
            "no honest sender offers bytes that do not hash to the advertised key"
        );

        // Dispatch reaches the entry only past that rejection, so the forged
        // offer never touched the key and the genuine one still opens it.
        assert!(
            fresh_offer_structural_rejection(&key, &genuine_data, &valid_proof).is_none(),
            "the offer that actually carries the key's bytes must reach the entry"
        );

        let in_flight: FreshOfferInFlight = Arc::new(Mutex::new(HashMap::new()));
        assert!(
            matches!(
                admit_test_offer(&in_flight, test_fresh_offer(genuine_data, 1), test_peer(1)),
                FreshOfferAdmission::Opened(_)
            ),
            "an offer refused on payload shape must leave the key openable"
        );
    }

    #[tokio::test]
    async fn peer_removed_clears_capacity_rejection_and_completes_bootstrap() {
        let peer = test_peer(0xA5);
        let key = test_key(0xA5);
        let bootstrap_state = Arc::new(RwLock::new(BootstrapState::new()));
        let queues = Arc::new(RwLock::new(ReplicationQueues::new()));
        let is_bootstrapping = Arc::new(RwLock::new(true));
        let bootstrap_complete_notify = Arc::new(Notify::new());

        let now = Instant::now();
        queues.write().await.add_pending_verify(
            key,
            VerificationEntry {
                state: VerificationState::PendingVerify,
                verified_sources: Vec::new(),
                tried_sources: HashSet::new(),
                created_at: now,
                next_verify_at: now,
                hint_sources: HashSet::from([peer]),
                replica_hint_sources: HashSet::from([peer]),
                unresolved_retries: 0,
                no_holder_reported: false,
            },
        );
        super::bootstrap::track_discovered_keys(&bootstrap_state, &HashSet::from([key])).await;
        super::bootstrap::note_capacity_rejected(&bootstrap_state, peer).await;
        {
            let q = queues.read().await;
            assert!(
                !super::bootstrap::check_bootstrap_drained(&bootstrap_state, &q).await,
                "capacity rejection should initially block bootstrap drain"
            );
        }

        update_bootstrap_after_peer_removed(
            &peer,
            &bootstrap_state,
            &queues,
            &is_bootstrapping,
            &bootstrap_complete_notify,
        )
        .await;

        let state = bootstrap_state.read().await;
        assert!(state.capacity_rejected_sources.is_empty());
        assert!(state.pending_keys.is_empty());
        assert!(state.is_drained());
        drop(state);
        assert!(!*is_bootstrapping.read().await);
    }

    /// A fairness displacement forfeits the key but must not stamp its former
    /// owner: the owner did nothing wrong, and stamping it lets a flooding
    /// source block bootstrap drain through an unrelated honest peer.
    #[tokio::test]
    async fn fairness_displacement_does_not_stamp_its_victim() {
        let displaced_owner = test_peer(0xB1);
        let incoming_source = test_peer(0xB2);
        let displaced_key = test_key(0xB1);
        let admitted_key = test_key(0xB2);
        let bootstrap_state = Arc::new(RwLock::new(BootstrapState::new()));
        {
            let mut state = bootstrap_state.write().await;
            state.pending_keys.insert(displaced_key);
            state
                .capacity_rejected_sources
                .insert(incoming_source, Instant::now());
        }
        let outcomes = [(
            incoming_source,
            AdmissionOutcome {
                discovered: HashSet::from([admitted_key]),
                capacity_rejected_count: 0,
                displaced: vec![CapacityDisplacement {
                    key: displaced_key,
                    owner: displaced_owner,
                }],
            },
        )];

        publish_bootstrap_admission_outcomes(
            &bootstrap_state,
            &outcomes,
            &HashSet::from([admitted_key]),
        )
        .await;

        let state = bootstrap_state.read().await;
        assert!(!state.pending_keys.contains(&displaced_key));
        assert!(state.pending_keys.contains(&admitted_key));
        assert!(
            !state
                .capacity_rejected_sources
                .contains_key(&displaced_owner),
            "the displaced owner must not be charged for our own fairness reclaim"
        );
        assert!(!state
            .capacity_rejected_sources
            .contains_key(&incoming_source));
    }

    /// A source that keeps overflowing us must not keep its own record fresh.
    ///
    /// This is the wedge the first-seen stamp closes: with refresh-on-every-
    /// rejection the debt could never age out, so `check_bootstrap_drained`
    /// stayed false forever and auditing never started (Invariant 19).
    #[tokio::test]
    async fn repeat_capacity_rejection_keeps_the_first_seen_stamp() {
        const REJECTION_ROUNDS: usize = 5;

        let source = test_peer(0xB3);
        let key = test_key(0xB3);
        let bootstrap_state = Arc::new(RwLock::new(BootstrapState::new()));

        let first_seen = {
            let outcomes = [(
                source,
                AdmissionOutcome {
                    discovered: HashSet::new(),
                    capacity_rejected_count: 1,
                    displaced: Vec::new(),
                },
            )];
            publish_bootstrap_admission_outcomes(&bootstrap_state, &outcomes, &HashSet::new())
                .await;
            bootstrap_state
                .read()
                .await
                .capacity_rejected_sources
                .get(&source)
                .copied()
                .expect("first rejection recorded")
        };

        for _ in 0..REJECTION_ROUNDS {
            let outcomes = [(
                source,
                AdmissionOutcome {
                    discovered: HashSet::from([key]),
                    capacity_rejected_count: 1,
                    displaced: Vec::new(),
                },
            )];
            publish_bootstrap_admission_outcomes(
                &bootstrap_state,
                &outcomes,
                &HashSet::from([key]),
            )
            .await;
        }

        let recorded = bootstrap_state
            .read()
            .await
            .capacity_rejected_sources
            .get(&source)
            .copied()
            .expect("debt still outstanding");
        assert_eq!(
            recorded, first_seen,
            "repeat rejections must not restart the expiry clock"
        );
    }

    /// The verification-worker tick's self-heal path: a capacity rejection
    /// recorded after the peer's `PeerRemoved` cleanup (the TOCTOU orphan)
    /// blocks drain until the TTL expires it, at which point the same tick
    /// completes bootstrap without any external event.
    #[tokio::test]
    async fn drain_self_heal_expires_orphaned_rejection_and_completes_bootstrap() {
        let peer = test_peer(0xA6);
        let bootstrap_state = Arc::new(RwLock::new(BootstrapState::new()));
        let queues = Arc::new(RwLock::new(ReplicationQueues::new()));
        let is_bootstrapping = Arc::new(RwLock::new(true));
        let bootstrap_complete_notify = Arc::new(Notify::new());

        // PeerRemoved was fully processed before the rejection landed, so no
        // future event will ever clear this entry.
        super::bootstrap::note_capacity_rejected(&bootstrap_state, peer).await;

        // Within the TTL the tick keeps waiting for re-delivery.
        expire_and_recheck_bootstrap_drain(
            &bootstrap_state,
            &queues,
            &is_bootstrapping,
            &bootstrap_complete_notify,
            ReplicationConfig::default().capacity_rejected_max_age(),
        )
        .await;
        assert!(!bootstrap_state.read().await.is_drained());
        assert!(*is_bootstrapping.read().await);

        // Past the TTL the tick expires the orphan and completes bootstrap.
        expire_and_recheck_bootstrap_drain(
            &bootstrap_state,
            &queues,
            &is_bootstrapping,
            &bootstrap_complete_notify,
            Duration::ZERO,
        )
        .await;
        assert!(bootstrap_state.read().await.is_drained());
        assert!(!*is_bootstrapping.read().await);
    }

    #[test]
    fn first_audit_terminal_outcomes_are_stable() {
        let peer = test_peer(1);
        let passed = AuditTickResult::Passed {
            challenged_peer: peer,
            keys_checked: 1,
        };
        let timed_out = AuditTickResult::Failed {
            evidence: FailureEvidence::AuditFailure {
                challenge_id: 1,
                challenged_peer: peer,
                confirmed_failed_keys: vec![test_key(1)],
                summary: crate::replication::types::AuditFailureSummary::default(),
                reason: AuditFailureReason::Timeout,
            },
            no_response_class: Some("timeout"),
        };

        assert_eq!(
            first_audit_terminal_outcome(&passed),
            FirstAuditTerminalOutcome::Passed
        );
        assert_eq!(
            first_audit_terminal_outcome(&timed_out),
            FirstAuditTerminalOutcome::Timeout
        );
        assert_eq!(
            first_audit_terminal_outcome(&AuditTickResult::Idle),
            FirstAuditTerminalOutcome::Idle
        );
        assert_eq!(FirstAuditTerminalOutcome::Passed.as_str(), "passed");
        assert_eq!(FirstAuditTerminalOutcome::Timeout.as_str(), "timeout");
        assert_eq!(FirstAuditTerminalOutcome::Failed.as_str(), "failed");
        assert_eq!(FirstAuditTerminalOutcome::Idle.as_str(), "idle");
        assert_eq!(
            FirstAuditTerminalOutcome::InsufficientKeys.as_str(),
            "insufficient_keys"
        );
        assert_eq!(
            FirstAuditTerminalOutcome::BootstrapClaim.as_str(),
            "bootstrap_claim"
        );
    }

    #[test]
    fn first_audit_coalescing_keeps_highest_count_and_exposes_eviction() {
        let mut pending = LruCache::new(NonZeroUsize::new(1).unwrap());
        let mut rng = StdRng::seed_from_u64(7);
        let peer = test_peer(1);
        let base = MonetizedPinEvent {
            peer,
            pin: [1; 32],
            key_count: 100,
            quote_ts: SystemTime::now(),
        };

        // First insert into an empty slot: Queued.
        assert_eq!(
            coalesce_first_audit_event(&mut pending, base, true, &mut rng),
            FirstAuditQueueOutcome::Queued
        );

        // A strictly LOWER-count same-peer nomination must NOT displace it.
        let lower = MonetizedPinEvent {
            pin: [2; 32],
            key_count: 50,
            ..base
        };
        assert_eq!(
            coalesce_first_audit_event(&mut pending, lower, true, &mut rng),
            FirstAuditQueueOutcome::SuppressedLower
        );
        assert_eq!(
            pending.peek(&peer).map(|e| (e.pin, e.key_count)),
            Some(([1; 32], 100)),
            "the higher-count pin is retained"
        );

        // A HIGHER-count same-peer nomination wins (the inflated pin to audit).
        let higher = MonetizedPinEvent {
            pin: [3; 32],
            key_count: 400,
            ..base
        };
        assert_eq!(
            coalesce_first_audit_event(&mut pending, higher, true, &mut rng),
            FirstAuditQueueOutcome::Coalesced
        );
        assert_eq!(
            pending.peek(&peer).map(|e| (e.pin, e.key_count)),
            Some(([3; 32], 400))
        );

        // EQUAL count: an ordinary (newer) enqueue replaces for freshness...
        let equal_newer = MonetizedPinEvent {
            pin: [4; 32],
            key_count: 400,
            ..base
        };
        assert_eq!(
            coalesce_first_audit_event(&mut pending, equal_newer, true, &mut rng),
            FirstAuditQueueOutcome::Coalesced
        );
        assert_eq!(pending.peek(&peer).map(|e| e.pin), Some([4; 32]));
        // ...but an equal-count OLDER requeue (incoming_is_newer=false) does not.
        let equal_older = MonetizedPinEvent {
            pin: [5; 32],
            key_count: 400,
            ..base
        };
        assert_eq!(
            coalesce_first_audit_event(&mut pending, equal_older, false, &mut rng),
            FirstAuditQueueOutcome::RetainedOnTie
        );
        assert_eq!(pending.peek(&peer).map(|e| e.pin), Some([4; 32]));

        // A different peer at capacity 1 evicts the LRU (a DIFFERENT peer).
        let other_peer = MonetizedPinEvent {
            peer: test_peer(2),
            pin: [6; 32],
            key_count: 100,
            ..base
        };
        assert_eq!(
            coalesce_first_audit_event(&mut pending, other_peer, true, &mut rng),
            FirstAuditQueueOutcome::CapacityEvicted { peer, pin: [4; 32] }
        );
        assert_eq!(pending.len(), 1);
        assert_eq!(pending.peek(&other_peer.peer).map(|e| e.pin), Some([6; 32]));
    }

    // -- ADR-0004 Amendment 2: first-audit launch limiter --------------------

    #[test]
    fn first_audit_limiter_enforces_burst_then_refills() {
        let base = Instant::now();
        let mut limiter = FirstAuditLimiter::new(base);
        let interval = config::FIRST_AUDIT_LAUNCH_INTERVAL;

        // The full burst is admitted back-to-back (distinct peers).
        for i in 0..config::FIRST_AUDIT_BUDGET_BURST {
            let peer = test_peer(u8::try_from(i).expect("small burst"));
            assert_eq!(limiter.assess(&peer, 10, base, 0), LimiterVerdict::Admit);
            limiter.commit_launch(peer, 10, base);
        }
        // Bucket empty: the next distinct peer is deferred, never dropped.
        let extra = test_peer(0xEE);
        assert_eq!(
            limiter.assess(&extra, 10, base, 0),
            LimiterVerdict::RateDeferred
        );

        // One full interval later exactly one token is available again.
        let later = base + interval;
        assert_eq!(limiter.assess(&extra, 10, later, 0), LimiterVerdict::Admit);
        limiter.commit_launch(extra, 10, later);
        let extra2 = test_peer(0xEF);
        assert_eq!(
            limiter.assess(&extra2, 10, later, 0),
            LimiterVerdict::RateDeferred
        );
    }

    #[test]
    fn first_audit_limiter_refill_keeps_fractional_remainder() {
        let base = Instant::now();
        let mut limiter = FirstAuditLimiter::new(base);
        let interval = config::FIRST_AUDIT_LAUNCH_INTERVAL;
        for i in 0..config::FIRST_AUDIT_BUDGET_BURST {
            let peer = test_peer(u8::try_from(i).expect("small burst"));
            assert_eq!(limiter.assess(&peer, 1, base, 0), LimiterVerdict::Admit);
            limiter.commit_launch(peer, 1, base);
        }
        // 1.5 intervals later one token is earned and the half interval is
        // NOT lost to drift...
        let at_1_5 = base + interval + interval / 2;
        let p = test_peer(0xAA);
        assert_eq!(limiter.assess(&p, 1, at_1_5, 0), LimiterVerdict::Admit);
        limiter.commit_launch(p, 1, at_1_5);
        // ...so the next token arrives at 2.0 intervals, not 2.5.
        let at_2_0 = base + interval * 2;
        let q = test_peer(0xAB);
        assert_eq!(limiter.assess(&q, 1, at_2_0, 0), LimiterVerdict::Admit);
    }

    #[test]
    fn first_audit_limiter_inflight_cap_defers_until_slot_frees() {
        let base = Instant::now();
        let mut limiter = FirstAuditLimiter::new(base);
        let peer = test_peer(1);
        assert_eq!(
            limiter.assess(&peer, 1, base, config::FIRST_AUDIT_MAX_INFLIGHT),
            LimiterVerdict::RateDeferred
        );
        // A freed slot admits without any clock movement.
        assert_eq!(
            limiter.assess(&peer, 1, base, config::FIRST_AUDIT_MAX_INFLIGHT - 1),
            LimiterVerdict::Admit
        );
    }

    #[test]
    fn first_audit_limiter_assess_consumes_nothing() {
        let base = Instant::now();
        let mut limiter = FirstAuditLimiter::new(base);
        let peer = test_peer(3);
        // Repeated assessment must not burn budget or stamp the window: only
        // `commit_launch` consumes (the cooldown gate between assess and
        // commit can defer, and that deferral must be free).
        for _ in 0..10 {
            assert_eq!(limiter.assess(&peer, 5, base, 0), LimiterVerdict::Admit);
        }
        for i in 0..config::FIRST_AUDIT_BUDGET_BURST {
            let p = test_peer(0x20 + u8::try_from(i).expect("small burst"));
            assert_eq!(limiter.assess(&p, 5, base, 0), LimiterVerdict::Admit);
            limiter.commit_launch(p, 5, base);
        }
    }

    #[test]
    fn first_audit_limiter_window_dedups_rotated_pins_and_count_jump_overrides() {
        let base = Instant::now();
        let mut limiter = FirstAuditLimiter::new(base);
        let peer = test_peer(7);
        assert_eq!(limiter.assess(&peer, 100, base, 0), LimiterVerdict::Admit);
        limiter.commit_launch(peer, 100, base);

        // A rotated pin with a similar count inside the window is dropped...
        let soon = base + Duration::from_mins(1);
        assert_eq!(
            limiter.assess(&peer, 100, soon, 0),
            LimiterVerdict::WindowDeduped
        );
        // ...even at the exact jump boundary (new*DEN == old*NUM is no jump)...
        assert_eq!(
            limiter.assess(&peer, 150, soon, 0),
            LimiterVerdict::WindowDeduped
        );
        // ...but a >1.5x committed-count jump re-nominates immediately (an
        // inflated sidecar-only pin is invisible to the gossip lottery, so
        // the window must not shield it).
        assert_eq!(limiter.assess(&peer, 151, soon, 0), LimiterVerdict::Admit);

        // Window expiry re-admits an unchanged count.
        let expired = base + config::FIRST_AUDIT_PEER_REAUDIT_INTERVAL;
        assert_eq!(
            limiter.assess(&peer, 100, expired, 0),
            LimiterVerdict::Admit
        );
    }

    #[test]
    fn first_audit_limiter_window_verdict_outranks_empty_budget() {
        // A window-deduped nomination must be DROPPED, not kept pending as
        // rate-deferred, even when the bucket is also empty: re-queuing a
        // suppressed rotation would hold a pending slot for two hours.
        let base = Instant::now();
        let mut limiter = FirstAuditLimiter::new(base);
        let peer = test_peer(9);
        assert_eq!(limiter.assess(&peer, 10, base, 0), LimiterVerdict::Admit);
        limiter.commit_launch(peer, 10, base);
        let other = test_peer(10);
        assert_eq!(limiter.assess(&other, 10, base, 0), LimiterVerdict::Admit);
        limiter.commit_launch(other, 10, base);
        assert_eq!(
            limiter.assess(&peer, 10, base, 0),
            LimiterVerdict::WindowDeduped
        );
    }

    /// ADR-0004 Amendment 2 (E'): the B horizon prefilter rejects a quote that
    /// is answerable now but would age out of the answerability window during
    /// the launch jitter, so scheduling state is only ever committed for a pin
    /// that can still be challenged when it actually sends.
    #[test]
    fn first_audit_horizon_prefilter_boundary() {
        let now = SystemTime::now();
        // C = the too-old cutoff; H = the worst-case send horizon.
        let c = GOSSIP_ANSWERABILITY_TTL.saturating_sub(MONETIZED_AUDIT_SKEW_MARGIN);
        let h = config::FIRST_AUDIT_LAUNCH_JITTER_MAX + FIRST_AUDIT_SEND_LATENCY_SLACK;
        // A quote whose age is exactly C at `now + H` (in the past, since C > H).
        let boundary = now
            .checked_add(h)
            .and_then(|t| t.checked_sub(c))
            .expect("boundary time");

        // In window at `now` (age = C - H < C)...
        assert!(quote_within_audit_window(boundary, now));
        // ...but the horizon prefilter rejects it (age == C at now + H).
        assert!(!quote_answerable_through_nominal_jitter(boundary, now));

        // A hair newer stays answerable through the horizon; a hair older does
        // not. Use 1µs (not 1ns): Windows `SystemTime` has 100ns granularity, so
        // a nanosecond step would round to the same instant there.
        let newer = boundary
            .checked_add(Duration::from_micros(1))
            .expect("newer");
        let older = boundary
            .checked_sub(Duration::from_micros(1))
            .expect("older");
        assert!(quote_answerable_through_nominal_jitter(newer, now));
        assert!(!quote_answerable_through_nominal_jitter(older, now));

        // A quote too far in the FUTURE is rejected at `now`, independent of the
        // horizon.
        let future = now
            .checked_add(MONETIZED_AUDIT_SKEW_MARGIN)
            .and_then(|t| t.checked_add(Duration::from_mins(1)))
            .expect("future");
        assert!(!quote_answerable_through_nominal_jitter(future, now));
    }

    /// ADR-0004 Amendment 2 (E'): a reservation whose AUTHORITATIVE post-jitter
    /// answerability check fails at promotion is fully state-neutral — it stamps
    /// no `first_audited`, no per-peer window, refunds its token, releases its
    /// in-flight slot, does not flip the lane, and does not count a launch — and
    /// a same-peer, same-count successor enqueued DURING the reservation is
    /// retained and becomes the next reservation after the cancel. This is the
    /// exact hole the reviewer flagged: suppression must never outlive a launch
    /// that did not send.
    /// A pending pin that has aged past the answerability horizon must not
    /// suppress a live lower-count nomination for the same peer: under token
    /// starvation no reserve scan ever collects the dead entry, so without the
    /// enqueue-time check the peer would stay unauditable through this path
    /// indefinitely (e.g. after a prune legitimately lowered its key count).
    #[test]
    fn first_audit_stale_incumbent_does_not_suppress_live_lower_count() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mut scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        let peer = test_peer(1);

        let dead_quote = SystemTime::now()
            .checked_sub(GOSSIP_ANSWERABILITY_TTL)
            .and_then(|t| t.checked_sub(Duration::from_mins(1)))
            .expect("past wall time");
        let stale_high = MonetizedPinEvent {
            peer,
            pin: [1; 32],
            key_count: 400,
            quote_ts: dead_quote,
        };
        scheduler.enqueue(stale_high, &obs);
        assert_eq!(scheduler.pending_len(), 1);

        // A fresh, lower-count nomination (a post-prune commitment).
        let fresh_lower = MonetizedPinEvent {
            peer,
            pin: [2; 32],
            key_count: 100,
            quote_ts: SystemTime::now(),
        };
        scheduler.enqueue(fresh_lower, &obs);

        assert_eq!(scheduler.pending_len(), 1);
        assert_eq!(
            scheduler.pending.peek(&peer).map(|e| e.pin),
            Some([2; 32]),
            "the live nomination replaced the dead incumbent"
        );
        assert_eq!(
            obs.suppressed_lower.load(Ordering::Relaxed),
            0,
            "no self-erasure signal for displacing a dead pin"
        );
        assert_eq!(obs.queued.load(Ordering::Relaxed), 2);
        assert_eq!(
            obs.outside_answerability_window.load(Ordering::Relaxed),
            1,
            "the dead incumbent is accounted as an expiry"
        );
    }

    /// The self-erasure defence is untouched for LIVE incumbents: a lower-count
    /// nomination still loses to an answerable higher-count pending pin.
    #[test]
    fn first_audit_live_incumbent_still_suppresses_lower_count() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mut scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        let peer = test_peer(1);

        let live_high = MonetizedPinEvent {
            peer,
            pin: [1; 32],
            key_count: 400,
            quote_ts: SystemTime::now(),
        };
        scheduler.enqueue(live_high, &obs);
        let cheaper = MonetizedPinEvent {
            peer,
            pin: [2; 32],
            key_count: 100,
            quote_ts: SystemTime::now(),
        };
        scheduler.enqueue(cheaper, &obs);

        assert_eq!(scheduler.pending_len(), 1);
        assert_eq!(
            scheduler.pending.peek(&peer).map(|e| e.pin),
            Some([1; 32]),
            "the higher-count live pin is retained"
        );
        assert_eq!(obs.suppressed_lower.load(Ordering::Relaxed), 1);
        assert_eq!(obs.outside_answerability_window.load(Ordering::Relaxed), 0);
    }

    /// A fresh nomination for peer `b` (distinct pin per peer).
    fn live_nomination(b: u8) -> MonetizedPinEvent {
        MonetizedPinEvent {
            peer: test_peer(b),
            pin: [b; 32],
            key_count: 100,
            quote_ts: SystemTime::now(),
        }
    }

    /// The pending admission cap must equal the number of usable launches the
    /// token budget can perform inside one answerability window, counted by
    /// SIMULATING the shipped reserve-time predicate instant-by-instant (burst
    /// tokens at age zero, one refill per launch interval), not by repeating
    /// the derivation formula. Any larger and admitted work is guaranteed to
    /// expire unlaunched; any smaller and the budget idles while nominations
    /// are displaced.
    #[test]
    fn first_audit_pending_cap_matches_strict_launch_horizon() {
        let quote_ts = SystemTime::now();
        // Burst launches fire at age zero and must pass the reserve predicate.
        assert!(quote_answerable_through_nominal_jitter(quote_ts, quote_ts));
        let mut usable = usize::try_from(config::FIRST_AUDIT_BUDGET_BURST).expect("burst fits");
        let mut refill = 1u32;
        loop {
            let age = config::FIRST_AUDIT_LAUNCH_INTERVAL * refill;
            let at = quote_ts + age;
            if !quote_answerable_through_nominal_jitter(quote_ts, at) {
                break;
            }
            usable += 1;
            refill += 1;
            assert!(refill < 10_000, "runaway horizon simulation");
        }
        assert_eq!(
            FIRST_AUDIT_PENDING_CAP, usable,
            "cap must match the simulated strict launch horizon"
        );
    }

    /// The pending queue is sized to the launch budget while the dedup set
    /// keeps the commitment-cache bound: shrinking `first_audited` would
    /// forget audited pins and re-admit duplicates.
    #[test]
    fn first_audit_pending_cap_independent_of_dedup_cap() {
        let scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        assert_eq!(scheduler.pending.cap().get(), FIRST_AUDIT_PENDING_CAP);
        assert_eq!(
            scheduler.first_audited.cap().get(),
            MAX_LAST_COMMITMENT_BY_PEER
        );
    }

    /// Admission beyond the cap displaces exactly one incumbent per overflow
    /// and every displacement is accounted as `capacity_evicted` — the steady
    /// overload signal — never silent loss, and never an admission refusal.
    #[test]
    fn first_audit_admission_beyond_cap_displaces_and_counts() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mut scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        let overflow: usize = 5;
        let total = FIRST_AUDIT_PENDING_CAP + overflow;

        for i in 0..total {
            let b = u8::try_from(i + 1).expect("test peer count fits u8");
            scheduler.enqueue(live_nomination(b), &obs);
        }

        assert_eq!(scheduler.pending_len(), FIRST_AUDIT_PENDING_CAP);
        assert_eq!(
            obs.capacity_evicted.load(Ordering::Relaxed),
            u64::try_from(overflow).expect("overflow fits u64"),
            "each admission past the cap displaces exactly one entry"
        );
        assert_eq!(
            obs.queued.load(Ordering::Relaxed),
            u64::try_from(total).expect("total fits u64"),
            "displacement is not an admission refusal"
        );
        // Every arrival was admitted (the newest always enters the sample).
        let last = u8::try_from(total).expect("test peer count fits u8");
        assert!(
            scheduler.pending.peek(&test_peer(last)).is_some(),
            "the newest arrival always enters the sample"
        );
    }

    /// The reviewer-demonstrated suppression route: a target followed by an
    /// ordered batch of distinct-peer nominations, all inside one ingress
    /// drain batch (`pending_cap < batch <= FIRST_AUDIT_DRAIN_BATCH`), with
    /// the token bucket EMPTY so no reservation can intervene. Under the old
    /// keep-newest LRU the target was evicted with certainty; under random
    /// displacement its per-overflow eviction probability is `1/cap`, so
    /// across repeated trials the target must survive some runs and be
    /// displaced in others — never deterministically flushed, and every
    /// displacement accounted.
    #[test]
    fn first_audit_ordered_flood_cannot_deterministically_evict_target() {
        let flood: usize = 60; // pending_cap < 60 <= FIRST_AUDIT_DRAIN_BATCH
        assert!(FIRST_AUDIT_PENDING_CAP < flood);
        assert!(flood <= config::FIRST_AUDIT_DRAIN_BATCH);

        let trials = 100u64;
        let mut survived = 0u32;
        let mut evicted = 0u32;
        for trial in 0..trials {
            let obs = Arc::new(FirstAuditObservability::default());
            let mut scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
            // Deterministic per-trial seed: the run is fully reproducible (no
            // flake budget at all) while still exercising 100 distinct
            // eviction sequences.
            scheduler.rng = StdRng::seed_from_u64(trial);
            // Drain the burst tokens so the pre-overflow reservation
            // opportunity cannot fire: this isolates pure retention.
            scheduler.limiter.reserve_token();
            scheduler.limiter.reserve_token();

            let target = test_peer(200);
            scheduler.enqueue(live_nomination(200), &obs);
            for i in 0..flood {
                let b = u8::try_from(i + 1).expect("flood peer fits u8");
                scheduler.enqueue(live_nomination(b), &obs);
            }

            assert_eq!(scheduler.pending_len(), FIRST_AUDIT_PENDING_CAP);
            let overflows = u64::try_from(1 + flood - FIRST_AUDIT_PENDING_CAP)
                .expect("overflow count fits u64");
            assert_eq!(
                obs.capacity_evicted.load(Ordering::Relaxed),
                overflows,
                "every overflow past the cap is accounted, none silent"
            );
            if scheduler.pending.peek(&target).is_some() {
                survived += 1;
            } else {
                evicted += 1;
            }
        }
        // With eviction probability 1/cap per overflow, P(target survives one
        // trial) ~= (1 - 1/31)^30 ~= 0.37, so both outcomes appear across the
        // 100 seeded trials — and the seeds make the split exactly
        // reproducible rather than a (vanishingly small) flake budget.
        assert!(
            survived > 0,
            "target must survive some ordered floods — deterministic eviction \
             would mean keep-newest retention regressed"
        );
        assert!(
            evicted > 0,
            "target must also be displaceable — otherwise overflow is refusing \
             admissions instead of sampling"
        );
    }

    /// With a token AVAILABLE, an overflowing arrival must first give pending
    /// work a launch opportunity: the drainer's pre-overflow reservation pulls
    /// one entry out of the queue, so admission proceeds without any eviction.
    #[test]
    fn first_audit_overflow_reserves_before_destructive_eviction() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mut scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        let cooldown: HashMap<PeerId, Instant> = HashMap::new();

        for i in 0..FIRST_AUDIT_PENDING_CAP {
            let b = u8::try_from(i + 1).expect("test peer count fits u8");
            scheduler.enqueue(live_nomination(b), &obs);
        }
        let overflowing = live_nomination(250);
        assert!(scheduler.would_displace(&overflowing));

        // The drainer's pre-overflow sequence: reserve, then enqueue.
        assert!(scheduler.try_reserve(Instant::now(), 0, Duration::ZERO, &cooldown, &obs));
        assert!(
            !scheduler.would_displace(&overflowing),
            "a successful reservation frees the slot"
        );
        scheduler.enqueue(overflowing, &obs);

        assert_eq!(scheduler.pending_len(), FIRST_AUDIT_PENDING_CAP);
        assert_eq!(
            obs.capacity_evicted.load(Ordering::Relaxed),
            0,
            "no destructive eviction when a launch opportunity existed"
        );
    }

    /// Displacement stamps no suppression state: a displaced peer's next
    /// nomination re-enters like any newcomer (it is NOT window-deduped or
    /// treated as a duplicate), so under overload every peer keeps an
    /// unpredictable chance of prompt first audit.
    #[test]
    fn first_audit_displaced_peer_may_be_renominated() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mut scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        let total = FIRST_AUDIT_PENDING_CAP + 1;

        for i in 0..total {
            let b = u8::try_from(i + 1).expect("test peer count fits u8");
            scheduler.enqueue(live_nomination(b), &obs);
        }
        assert_eq!(obs.capacity_evicted.load(Ordering::Relaxed), 1);
        // Find whichever peer the random displacement removed.
        let displaced = (1..=total)
            .map(|i| u8::try_from(i).expect("test peer count fits u8"))
            .find(|b| scheduler.pending.peek(&test_peer(*b)).is_none())
            .expect("exactly one admitted peer was displaced");

        scheduler.enqueue(live_nomination(displaced), &obs);

        assert!(
            scheduler.pending.peek(&test_peer(displaced)).is_some(),
            "the displaced peer re-enters the sample"
        );
        assert_eq!(scheduler.pending_len(), FIRST_AUDIT_PENDING_CAP);
        assert_eq!(obs.capacity_evicted.load(Ordering::Relaxed), 2);
        assert_eq!(obs.duplicates.load(Ordering::Relaxed), 0);
        assert_eq!(obs.window_deduped.load(Ordering::Relaxed), 0);
    }

    /// The periodic sweep collects expired pending entries even when the token
    /// bucket is empty — the reserve path returns at the budget gate and never
    /// scans — keeping the capped pending queue and the pending/oldest-age telemetry
    /// honest under fleet-wide starvation.
    #[test]
    fn first_audit_sweep_expired_collects_dead_entries_without_tokens() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mono = Instant::now();
        let mut scheduler = FirstAuditScheduler::new(mono, test_peer(99));

        let dead_quote = SystemTime::now()
            .checked_sub(GOSSIP_ANSWERABILITY_TTL)
            .and_then(|t| t.checked_sub(Duration::from_mins(1)))
            .expect("past wall time");
        scheduler.enqueue(
            MonetizedPinEvent {
                peer: test_peer(1),
                pin: [1; 32],
                key_count: 100,
                quote_ts: dead_quote,
            },
            &obs,
        );
        scheduler.enqueue(
            MonetizedPinEvent {
                peer: test_peer(2),
                pin: [2; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );
        assert_eq!(scheduler.pending_len(), 2);

        // Empty the bucket so the reserve path is budget-gated (fleet-wide
        // starvation) and cannot collect the dead entry itself.
        scheduler.limiter.tokens = 0;
        let cooldown: HashMap<PeerId, Instant> = HashMap::new();
        assert!(!scheduler.try_reserve(mono, 0, Duration::ZERO, &cooldown, &obs));
        assert_eq!(
            scheduler.pending_len(),
            2,
            "budget-gated reserve scans nothing"
        );

        let wall_now = SystemTime::now();
        assert_eq!(scheduler.sweep_expired(wall_now, &obs), 1);
        assert_eq!(scheduler.pending_len(), 1);
        assert!(
            scheduler.pending.peek(&test_peer(2)).is_some(),
            "the live entry survives the sweep"
        );
        assert_eq!(obs.outside_answerability_window.load(Ordering::Relaxed), 1);
        let age_ms = scheduler.oldest_pending_quote_age_ms(wall_now);
        assert!(
            Duration::from_millis(age_ms) < GOSSIP_ANSWERABILITY_TTL,
            "the age gauge reflects only live work after the sweep"
        );
    }

    #[test]
    #[allow(clippy::too_many_lines)]
    fn first_audit_answerability_cancel_is_state_neutral_and_retains_successor() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mono = Instant::now();
        let mut scheduler = FirstAuditScheduler::new(mono, test_peer(99));

        // A pre-existing window sentinel for an UNRELATED peer must survive the
        // cancel byte-for-byte (cancel never touches `recent`).
        let sentinel_peer = test_peer(0xAA);
        scheduler.limiter.promote(sentinel_peer, 500, mono);
        let sentinel_before = scheduler
            .limiter
            .recent
            .peek(&sentinel_peer)
            .copied()
            .expect("sentinel present");

        let peer = test_peer(1);
        let a = MonetizedPinEvent {
            peer,
            pin: [1; 32],
            key_count: 100,
            quote_ts: SystemTime::now(), // fresh: passes the horizon prefilter now
        };
        scheduler.enqueue(a, &obs);

        let tokens_before = scheduler.tokens();
        let lane_before = scheduler.oldest_first_lane;
        let launched_before = obs.launched.load(Ordering::Relaxed);
        let mut cooldown: HashMap<PeerId, Instant> = HashMap::new();

        // Reserve A (jitter 0 so it is immediately due).
        let inflight0 = obs.inflight.load(Ordering::Relaxed);
        assert!(scheduler.try_reserve(mono, inflight0, Duration::ZERO, &cooldown, &obs));
        assert_eq!(scheduler.reserved_peer(), Some(peer));
        assert_eq!(
            scheduler.tokens(),
            tokens_before - 1,
            "reserve consumes a token"
        );
        assert_eq!(
            obs.inflight.load(Ordering::Relaxed),
            1,
            "reserve holds a slot"
        );

        // A same-peer, same-count successor arrives DURING the reservation. It
        // must be retained (bypasses the window for the reserved peer) and must
        // NOT create a second reservation.
        let b = MonetizedPinEvent {
            peer,
            pin: [2; 32],
            key_count: 100,
            quote_ts: SystemTime::now(),
        };
        scheduler.enqueue(b, &obs);
        assert_eq!(scheduler.pending_len(), 1, "successor retained in pending");
        assert!(
            !scheduler.try_reserve(
                mono,
                obs.inflight.load(Ordering::Relaxed),
                Duration::ZERO,
                &cooldown,
                &obs
            ),
            "no second reservation while one is outstanding"
        );

        // Resolve A with an injected wall time PAST A's answerability cutoff.
        let reservation = scheduler
            .take_due_reservation(mono)
            .expect("A is due at jitter 0");
        let wall_fail = a
            .quote_ts
            .checked_add(GOSSIP_ANSWERABILITY_TTL)
            .and_then(|t| t.checked_add(Duration::from_secs(1)))
            .expect("past-cutoff wall time");
        let promoted = scheduler.resolve(reservation, wall_fail, mono, &mut cooldown, &obs);
        assert!(
            promoted.is_none(),
            "answerability lapsed -> cancelled, not promoted"
        );

        // State-neutral cancel.
        assert!(scheduler.first_audited.is_empty(), "no pin marked audited");
        assert!(
            scheduler.limiter.recent.peek(&peer).is_none(),
            "cancel stamps no per-peer window"
        );
        assert_eq!(
            scheduler.limiter.recent.peek(&sentinel_peer).copied(),
            Some(sentinel_before),
            "unrelated window sentinel untouched"
        );
        assert!(!cooldown.contains_key(&peer), "cancel stamps no cooldown");
        assert_eq!(
            scheduler.tokens(),
            tokens_before,
            "token refunded on cancel"
        );
        assert_eq!(
            obs.inflight.load(Ordering::Relaxed),
            0,
            "in-flight slot released"
        );
        assert_eq!(scheduler.oldest_first_lane, lane_before, "lane not flipped");
        assert_eq!(
            obs.launched.load(Ordering::Relaxed),
            launched_before,
            "no launch counted"
        );
        assert_eq!(
            obs.outside_answerability_window.load(Ordering::Relaxed),
            1,
            "the cancel reason is recorded"
        );

        // The successor is still pending and is now fully schedulable: a fresh
        // reserve makes B the next reservation (proving eligibility, not merely
        // that the limiter would admit it).
        assert_eq!(
            scheduler.pending_len(),
            1,
            "successor still pending after cancel"
        );
        assert!(scheduler.reserved.is_none());
        assert!(scheduler.try_reserve(
            mono,
            obs.inflight.load(Ordering::Relaxed),
            Duration::ZERO,
            &cooldown,
            &obs
        ));
        assert_eq!(
            scheduler.reserved_peer(),
            Some(peer),
            "the retained successor becomes the next reservation"
        );
    }

    /// ADR-0004 Amendment 2 (E'): when a promotion loses the shared-cooldown
    /// race, the reserved event is requeued ONLY if no same-peer successor is
    /// already pending. A successor arrived after the reservation, so it is the
    /// newer nomination (e.g. a count jump) and must not be overwritten by the
    /// older reserved event.
    #[test]
    fn first_audit_cooldown_race_requeue_preserves_newer_successor() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mono = Instant::now();
        let mut scheduler = FirstAuditScheduler::new(mono, test_peer(99));
        let peer = test_peer(1);

        // Reserve A.
        let a = MonetizedPinEvent {
            peer,
            pin: [1; 32],
            key_count: 100,
            quote_ts: SystemTime::now(),
        };
        scheduler.enqueue(a, &obs);
        let cooldown_reserve: HashMap<PeerId, Instant> = HashMap::new();
        assert!(scheduler.try_reserve(mono, 0, Duration::ZERO, &cooldown_reserve, &obs));

        // A newer same-peer successor B (a count jump) arrives during the
        // reservation and is retained.
        let b = MonetizedPinEvent {
            peer,
            pin: [2; 32],
            key_count: 400,
            quote_ts: SystemTime::now(),
        };
        scheduler.enqueue(b, &obs);
        assert_eq!(scheduler.pending_len(), 1);

        // Resolve A: answerability PASSES (fresh quote) but the shared cooldown
        // is already stamped for the peer, so promotion loses the race.
        let reservation = scheduler.take_due_reservation(mono).expect("due");
        let mut cooldown: HashMap<PeerId, Instant> = HashMap::new();
        cooldown.insert(peer, mono); // freshly on cooldown
        let promoted = scheduler.resolve(reservation, SystemTime::now(), mono, &mut cooldown, &obs);
        assert!(promoted.is_none(), "cooldown race -> not promoted");

        // B (newer) is preserved; A did NOT overwrite it.
        assert_eq!(scheduler.pending_len(), 1, "still exactly one pending");
        assert_eq!(
            scheduler.pending.peek(&peer).map(|e| e.pin),
            Some([2; 32]),
            "the newer successor B is retained, not the older reserved A"
        );
        assert!(scheduler.first_audited.is_empty());
        assert!(scheduler.limiter.recent.peek(&peer).is_none());
    }

    /// The oldest-pending-quote-age gauge reports the age of the OLDEST quote
    /// still awaiting a first audit (not the newest), is `0` on an empty queue,
    /// and saturates to `0` for a future-dated quote (clock skew) without
    /// panicking. A climbing value is the pending-work-aging-out signal.
    #[test]
    fn first_audit_oldest_pending_quote_age_tracks_the_oldest() {
        let now = SystemTime::now();
        let mut scheduler = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        assert_eq!(
            scheduler.oldest_pending_quote_age_ms(now),
            0,
            "empty pending -> zero"
        );

        // Two peers: quotes 10s and 2s old. The gauge must track the older.
        for (peer_id, pin, secs) in [(1u8, [1; 32], 10u64), (2, [2; 32], 2)] {
            let _ = coalesce_first_audit_event(
                &mut scheduler.pending,
                MonetizedPinEvent {
                    peer: test_peer(peer_id),
                    pin,
                    key_count: 100,
                    quote_ts: now - Duration::from_secs(secs),
                },
                true,
                &mut scheduler.rng,
            );
        }
        assert_eq!(
            scheduler.oldest_pending_quote_age_ms(now),
            10_000,
            "tracks the oldest (10s), not the newest (2s)"
        );

        // A future-dated quote (clock skew) saturates to zero, never panics.
        let mut skewed = FirstAuditScheduler::new(Instant::now(), test_peer(99));
        let _ = coalesce_first_audit_event(
            &mut skewed.pending,
            MonetizedPinEvent {
                peer: test_peer(3),
                pin: [3; 32],
                key_count: 100,
                quote_ts: now + Duration::from_secs(5),
            },
            true,
            &mut skewed.rng,
        );
        assert_eq!(
            skewed.oldest_pending_quote_age_ms(now),
            0,
            "future quote_ts saturates to zero"
        );
    }

    /// A flood of strictly-lower-count same-peer nominations must neither
    /// displace the retained higher pin NOR disturb its recency position (each
    /// is suppressed via `peek`, no `push`), and each must be counted as
    /// `suppressed_lower` — the attempted cheaper-pin self-erasure signal.
    /// Recency is asserted DIRECTLY via the queue's MRU-to-LRU iteration order
    /// (the documented `lru` contract that also drives the reserve scan's lane
    /// ordering), deliberately independent of the overflow policy: eviction is
    /// random-victim, so no assertion may require a particular peer to be
    /// displaced.
    #[test]
    fn first_audit_suppressed_lower_flood_leaves_recency_and_counts() {
        let mut pending: LruCache<PeerId, MonetizedPinEvent> =
            LruCache::new(NonZeroUsize::new(2).unwrap());
        let mut rng = StdRng::seed_from_u64(11);
        let victim = test_peer(1);
        let other = test_peer(2);
        // Victim (high count) inserted first (older), then `other` (newer/MRU).
        let _ = coalesce_first_audit_event(
            &mut pending,
            MonetizedPinEvent {
                peer: victim,
                pin: [1; 32],
                key_count: 400,
                quote_ts: SystemTime::now(),
            },
            true,
            &mut rng,
        );
        let _ = coalesce_first_audit_event(
            &mut pending,
            MonetizedPinEvent {
                peer: other,
                pin: [9; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            true,
            &mut rng,
        );
        let order_before: Vec<PeerId> = pending.iter().map(|(p, _)| *p).collect();
        assert_eq!(
            order_before,
            vec![other, victim],
            "sanity: `other` is MRU, the victim is LRU"
        );

        // Flood the victim with cheaper nominations.
        let mut suppressed = 0u64;
        for i in 0..8u8 {
            let out = coalesce_first_audit_event(
                &mut pending,
                MonetizedPinEvent {
                    peer: victim,
                    pin: [i; 32],
                    key_count: 50,
                    quote_ts: SystemTime::now(),
                },
                true,
                &mut rng,
            );
            assert_eq!(out, FirstAuditQueueOutcome::SuppressedLower);
            suppressed += 1;
        }
        assert_eq!(suppressed, 8);
        // Victim pin/count unchanged.
        assert_eq!(
            pending.peek(&victim).map(|e| (e.pin, e.key_count)),
            Some(([1; 32], 400))
        );
        // Recency untouched: the iteration order (which the reserve lanes
        // consume) is byte-for-byte what it was before the flood.
        let order_after: Vec<PeerId> = pending.iter().map(|(p, _)| *p).collect();
        assert_eq!(
            order_after, order_before,
            "the suppressed-lower flood must not have changed any recency position"
        );
    }

    /// The cooldown-race requeue counts a genuine different-peer capacity
    /// eviction (the ADR promises capacity loss is observable).
    #[test]
    fn first_audit_cooldown_race_requeue_counts_capacity_eviction() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mono = Instant::now();
        let mut scheduler = FirstAuditScheduler::new(mono, test_peer(99));
        // Pending capacity 1 so a requeue of a different peer must evict.
        scheduler.pending = LruCache::new(NonZeroUsize::new(1).unwrap());
        let reserved_peer = test_peer(1);
        let other_peer = test_peer(2);

        // Reserve peer 1.
        scheduler.enqueue(
            MonetizedPinEvent {
                peer: reserved_peer,
                pin: [1; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );
        let cooldown_read: HashMap<PeerId, Instant> = HashMap::new();
        assert!(scheduler.try_reserve(mono, 0, Duration::ZERO, &cooldown_read, &obs));

        // A DIFFERENT peer fills the single pending slot during the reservation.
        scheduler.enqueue(
            MonetizedPinEvent {
                peer: other_peer,
                pin: [2; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );
        assert_eq!(scheduler.pending_len(), 1);

        // Resolve peer 1: cooldown race -> requeue peer 1, evicting peer 2.
        let reservation = scheduler.take_due_reservation(mono).expect("due");
        let mut cooldown: HashMap<PeerId, Instant> = HashMap::new();
        cooldown.insert(reserved_peer, mono);
        let cap_before = obs.capacity_evicted.load(Ordering::Relaxed);
        assert!(scheduler
            .resolve(reservation, SystemTime::now(), mono, &mut cooldown, &obs)
            .is_none());
        assert_eq!(
            obs.capacity_evicted.load(Ordering::Relaxed),
            cap_before + 1,
            "the requeue eviction of a different peer is counted"
        );
        assert_eq!(
            scheduler.pending.peek(&reserved_peer).map(|e| e.pin),
            Some([1; 32])
        );
    }

    /// ADR-0004 Amendment 2 (reviewer blocker): a strictly-lower-count same-peer
    /// nomination arriving while an inflated pin is PENDING must not displace it.
    /// The inflated pin stays and is the one launched.
    #[test]
    fn first_audit_pending_lower_count_does_not_replace_higher() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mono = Instant::now();
        let mut scheduler = FirstAuditScheduler::new(mono, test_peer(99));
        let peer = test_peer(1);

        // Inflated (high-count) sidecar pin lands in pending.
        scheduler.enqueue(
            MonetizedPinEvent {
                peer,
                pin: [1; 32],
                key_count: 400,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );
        // A cheaper same-peer settlement arrives right after.
        scheduler.enqueue(
            MonetizedPinEvent {
                peer,
                pin: [2; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );

        assert_eq!(scheduler.pending_len(), 1);
        assert_eq!(
            scheduler.pending.peek(&peer).map(|e| (e.pin, e.key_count)),
            Some(([1; 32], 400)),
            "the inflated pin must not be erased by the cheaper successor"
        );
        // The dropped cheaper nomination is counted through the enqueue path.
        assert_eq!(
            obs.suppressed_lower.load(Ordering::Relaxed),
            1,
            "the attempted cheaper-pin self-erasure is observable"
        );

        // It reserves and promotes as the inflated pin/count.
        let cooldown_read: HashMap<PeerId, Instant> = HashMap::new();
        assert!(scheduler.try_reserve(mono, 0, Duration::ZERO, &cooldown_read, &obs));
        let reservation = scheduler.take_due_reservation(mono).expect("due");
        let mut cooldown: HashMap<PeerId, Instant> = HashMap::new();
        let (event, _slot) = scheduler
            .resolve(reservation, SystemTime::now(), mono, &mut cooldown, &obs)
            .expect("promotes");
        assert_eq!((event.pin, event.key_count), ([1; 32], 400));
    }

    /// ADR-0004 Amendment 2 (reviewer blocker): a RESERVED inflated pin that
    /// loses the cooldown race must be requeued OVER a lower-count same-peer
    /// successor that arrived during its jitter, and must remain launchable once
    /// the shared cooldown expires — the cheaper successor cannot suppress it.
    #[test]
    fn first_audit_cooldown_race_requeues_reserved_higher_over_lower_successor() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mono = Instant::now();
        let mut scheduler = FirstAuditScheduler::new(mono, test_peer(99));
        let peer = test_peer(1);

        // Reserve the inflated pin.
        scheduler.enqueue(
            MonetizedPinEvent {
                peer,
                pin: [1; 32],
                key_count: 400,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );
        let cooldown_read: HashMap<PeerId, Instant> = HashMap::new();
        assert!(scheduler.try_reserve(mono, 0, Duration::ZERO, &cooldown_read, &obs));

        // A cheaper successor arrives during the reservation (bypasses the
        // window as the reserved peer) and sits in pending.
        scheduler.enqueue(
            MonetizedPinEvent {
                peer,
                pin: [2; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );
        assert_eq!(scheduler.pending_len(), 1);

        // Resolve: answerability passes but the shared cooldown is already
        // stamped, so the reservation loses the race and requeues.
        let reservation = scheduler.take_due_reservation(mono).expect("due");
        let mut cooldown: HashMap<PeerId, Instant> = HashMap::new();
        cooldown.insert(peer, mono);
        assert!(scheduler
            .resolve(reservation, SystemTime::now(), mono, &mut cooldown, &obs)
            .is_none());

        // The inflated pin (400) replaced the cheaper successor (100).
        assert_eq!(scheduler.pending_len(), 1);
        assert_eq!(
            scheduler.pending.peek(&peer).map(|e| (e.pin, e.key_count)),
            Some(([1; 32], 400)),
            "the inflated reserved pin must survive the requeue over the cheaper successor"
        );
        assert!(scheduler.first_audited.is_empty());
        assert!(scheduler.limiter.recent.peek(&peer).is_none());

        // Once the shared cooldown expires, the inflated pin reserves and
        // promotes with its intended pin/count.
        let later = mono
            .checked_add(Duration::from_secs(
                config::AUDIT_ON_GOSSIP_COOLDOWN_SECS + 1,
            ))
            .expect("later");
        let cooldown_read_later: HashMap<PeerId, Instant> = HashMap::new();
        assert!(scheduler.try_reserve(later, 0, Duration::ZERO, &cooldown_read_later, &obs));
        let reservation = scheduler.take_due_reservation(later).expect("due");
        let mut cooldown_later: HashMap<PeerId, Instant> = HashMap::new();
        let (event, _slot) = scheduler
            .resolve(
                reservation,
                SystemTime::now(),
                later,
                &mut cooldown_later,
                &obs,
            )
            .expect("promotes after cooldown");
        assert_eq!((event.pin, event.key_count), ([1; 32], 400));
    }

    /// ADR-0004 Amendment 2 (E'): consecutive PROMOTIONS strictly alternate the
    /// launch lane, driven through the real scheduler (reserve -> resolve ->
    /// promote), so a stream of fresh nominations cannot keep every launch on
    /// the newest lane and starve the oldest.
    #[test]
    fn first_audit_lane_alternates_across_promotions() {
        let obs = Arc::new(FirstAuditObservability::default());
        let mono = Instant::now();
        let mut scheduler = FirstAuditScheduler::new(mono, test_peer(99));
        let mut cooldown: HashMap<PeerId, Instant> = HashMap::new();

        // Two distinct peers; the newest-inserted is the MRU (newest lane end).
        let oldest_peer = test_peer(1);
        let newest_peer = test_peer(2);
        scheduler.enqueue(
            MonetizedPinEvent {
                peer: oldest_peer,
                pin: [1; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );
        scheduler.enqueue(
            MonetizedPinEvent {
                peer: newest_peer,
                pin: [2; 32],
                key_count: 100,
                quote_ts: SystemTime::now(),
            },
            &obs,
        );

        let mut launched_peers = Vec::new();
        for _ in 0..2 {
            assert!(scheduler.try_reserve(
                mono,
                obs.inflight.load(Ordering::Relaxed),
                Duration::ZERO,
                &cooldown,
                &obs
            ));
            let reservation = scheduler.take_due_reservation(mono).expect("due");
            // Answerable wall time == the quote's own time (age 0).
            let (event, _slot) = scheduler
                .resolve(reservation, SystemTime::now(), mono, &mut cooldown, &obs)
                .expect("fresh in-window quote promotes");
            launched_peers.push(event.peer);
        }

        // First launch takes the newest lane (lane starts false = newest), the
        // second takes the oldest lane: strict alternation.
        assert_eq!(
            launched_peers,
            vec![newest_peer, oldest_peer],
            "consecutive promotions alternate newest-then-oldest lane"
        );
    }

    #[test]
    fn first_audit_count_jump_boundaries() {
        // Exactly 1.5x is NOT a jump; strictly above is.
        assert!(!first_audit_count_jump(100, 150));
        assert!(first_audit_count_jump(100, 151));
        // Anything beats an audited zero; zero never jumps.
        assert!(first_audit_count_jump(0, 1));
        assert!(!first_audit_count_jump(0, 0));
        // Equal max counts must not jump (and must not overflow).
        assert!(!first_audit_count_jump(u32::MAX, u32::MAX));
    }

    /// A verified payment's quote list includes the local node's own quote, so
    /// the verifier emits a monetized-pin event for the local peer on every
    /// payment it verifies. The node cannot network-audit itself, so the
    /// scheduler must drop such an event at ingress: never queued, and hence
    /// never launched nor marked first-audited.
    #[test]
    fn first_audit_queue_drops_self_targeting_events() {
        let obs = Arc::new(FirstAuditObservability::default());
        let self_peer = test_peer(1);
        let mut scheduler = FirstAuditScheduler::new(Instant::now(), self_peer);
        let self_event = MonetizedPinEvent {
            peer: self_peer,
            pin: [7; 32],
            key_count: 1,
            quote_ts: SystemTime::now(),
        };

        scheduler.enqueue(self_event, &obs);
        assert_eq!(obs.self_target_skipped.load(Ordering::Relaxed), 1);
        assert_eq!(obs.queued.load(Ordering::Relaxed), 0);
        assert_eq!(scheduler.pending_len(), 0, "self-target must never queue");

        // A remote peer's event still queues normally under the same filter.
        let remote_event = MonetizedPinEvent {
            peer: test_peer(2),
            pin: [8; 32],
            ..self_event
        };
        scheduler.enqueue(remote_event, &obs);
        assert_eq!(obs.queued.load(Ordering::Relaxed), 1);
        assert_eq!(scheduler.pending_len(), 1);
        assert_eq!(
            obs.self_target_skipped.load(Ordering::Relaxed),
            1,
            "remote event must not count as a self-target skip"
        );
    }

    #[test]
    fn fresh_offer_runs_store_admission_payment_checks() {
        let context = fresh_offer_payment_context();
        assert_eq!(context, VerificationContext::FreshReplication);
        // Fresh replication must keep verifying exactly like a direct client
        // PUT (store-strength cache, same live checks); the variant only
        // exists so price-floor telemetry can tell the two paths apart.
        assert!(context.is_store_admission());
    }

    #[test]
    fn paid_notify_uses_paid_list_admission_payment_checks() {
        assert_eq!(
            paid_notify_payment_context(),
            VerificationContext::PaidListAdmission
        );
    }

    /// ADR-0004 A1 (guardrail A): the monetized first-audit only fires for a
    /// signed quote inside the answerability window, fail-closed on BOTH ends so a
    /// stale or future/skewed client-forwarded quote cannot frame an honest node.
    #[test]
    fn monetized_quote_audit_window_fails_closed_both_ends() {
        let now = SystemTime::now();
        // Fresh (just quoted) and small future/past skew -> audited.
        assert!(quote_within_audit_window(now, now));
        assert!(quote_within_audit_window(now + Duration::from_mins(1), now));
        assert!(quote_within_audit_window(
            now - Duration::from_hours(1),
            now
        ));
        // Far future (badly-skewed / replayed) -> skipped.
        assert!(!quote_within_audit_window(
            now + MONETIZED_AUDIT_SKEW_MARGIN + Duration::from_mins(1),
            now
        ));
        // Older than the window -> skipped (pin may have aged out).
        assert!(!quote_within_audit_window(
            now - commitment_state::GOSSIP_ANSWERABILITY_TTL,
            now
        ));
    }

    #[tokio::test]
    async fn replication_branch_lagged_events_are_counted() {
        let before = audit_metrics::replication_event_lagged_total();
        let flow = handle_replication_event_recv_error(
            &tokio::sync::broadcast::error::RecvError::Lagged(3),
        );
        assert_eq!(flow, std::ops::ControlFlow::Continue(()));
        let after = audit_metrics::replication_event_lagged_total();
        assert_eq!(after.saturating_sub(before), 3);
    }

    #[tokio::test]
    async fn replication_branch_closed_events_stop_the_loop() {
        let flow =
            handle_replication_event_recv_error(&tokio::sync::broadcast::error::RecvError::Closed);
        assert_eq!(flow, std::ops::ControlFlow::Break(()));
    }

    #[tokio::test]
    async fn full_serial_queue_drops_instead_of_running_the_message_inline() {
        let (sender, mut receiver) = mpsc::channel(1);
        let dropped_body =
            ReplicationMessageBody::VerificationRequest(protocol::VerificationRequest {
                keys: Vec::new(),
                paid_list_check_indices: Vec::new(),
            });
        let overflow_before = audit_metrics::serial_queue_overflow_drops_total(&dropped_body);
        let first = InboundReplicationMessage {
            source: test_peer(0xC1),
            msg: ReplicationMessage {
                request_id: 1,
                body: ReplicationMessageBody::FetchRequest(protocol::FetchRequest {
                    key: test_key(1),
                }),
            },
            rr_message_id: None,
            received_at: Instant::now(),
        };
        let second = InboundReplicationMessage {
            source: test_peer(0xC2),
            msg: ReplicationMessage {
                request_id: 2,
                body: dropped_body.clone(),
            },
            rr_message_id: None,
            received_at: Instant::now(),
        };

        assert!(try_enqueue_serial_message(&sender, first).is_ok());
        let dropped = try_enqueue_serial_message(&sender, second)
            .expect_err("a full serial queue must refuse the second message");
        assert_eq!(dropped.reason, SerialQueueDropReason::Full);
        assert_eq!(dropped.message_class, "verification_request");
        assert_eq!(dropped.queue_depth, 1);
        let overflow_after = audit_metrics::serial_queue_overflow_drops_total(&dropped_body);
        assert_eq!(overflow_after.saturating_sub(overflow_before), 1);

        let queued = receiver
            .recv()
            .await
            .expect("first message should remain queued");
        assert_eq!(queued.msg.request_id, 1);
        assert!(receiver.try_recv().is_err());
    }

    #[tokio::test]
    async fn digest_admission_gets_higher_per_peer_cap_subtree_stays_at_two() {
        let peer = test_peer(0x44);
        let semaphore = Arc::new(Semaphore::new(config::MAX_CONCURRENT_AUDIT_RESPONSES));

        let digest_inflight = Arc::new(RwLock::new(HashMap::new()));
        let mut digest_guards = Vec::new();
        for _ in 0..config::MAX_DIGEST_AUDIT_RESPONSES_PER_PEER {
            let guard = admit_audit_responder(
                &semaphore,
                &digest_inflight,
                &peer,
                AuditResponderClass::Digest,
            )
            .await;
            assert!(guard.is_ok());
            digest_guards.push(guard);
        }
        assert!(
            admit_audit_responder(
                &semaphore,
                &digest_inflight,
                &peer,
                AuditResponderClass::Digest,
            )
            .await
            .is_err(),
            "digest class must stop at its documented per-source cap"
        );
        drop(digest_guards);

        let subtree_inflight = Arc::new(RwLock::new(HashMap::new()));
        let mut subtree_guards = Vec::new();
        for _ in 0..config::MAX_AUDIT_RESPONSES_PER_PEER {
            let guard = admit_audit_responder(
                &semaphore,
                &subtree_inflight,
                &peer,
                AuditResponderClass::Subtree,
            )
            .await;
            assert!(guard.is_ok());
            subtree_guards.push(guard);
        }
        assert!(
            admit_audit_responder(
                &semaphore,
                &subtree_inflight,
                &peer,
                AuditResponderClass::Subtree,
            )
            .await
            .is_err(),
            "subtree class must retain the deployed cap of two"
        );
        drop(subtree_guards);
    }

    #[test]
    fn in_scope_audit_deadlines_share_one_formula() {
        let config = config::ReplicationConfig::default();
        for key_count in [1, 4, 16] {
            assert_eq!(
                audit::responsible_audit_response_timeout(&config, key_count),
                config.audit_response_timeout(key_count)
            );
            assert_eq!(
                pruning::prune_audit_response_timeout(&config, key_count),
                config.audit_response_timeout(key_count)
            );
        }
        assert_eq!(
            possession::possession_probe_response_timeout(&config),
            config.audit_response_timeout(1)
        );
    }

    #[test]
    fn replica_hint_sources_are_added_as_fallback_fetch_sources() {
        const EXISTING_SOURCE_ID: u8 = 1;
        const HINT_SENDER_ID: u8 = 2;

        let existing_source = test_peer(EXISTING_SOURCE_ID);
        let hint_sender = test_peer(HINT_SENDER_ID);
        let mut sources = vec![existing_source];

        let hint_sources = HashSet::from([hint_sender]);
        add_replica_hint_sources(&mut sources, &hint_sources);
        add_replica_hint_sources(&mut sources, &hint_sources);

        // A paid-only advertiser leaves the claim set empty (see
        // `queue_admitted_hints`), so it contributes no fetch source.
        add_replica_hint_sources(&mut sources, &HashSet::new());

        assert_eq!(sources, vec![existing_source, hint_sender]);
    }

    #[test]
    fn audit_timeout_preserves_active_bootstrap_claim() {
        assert!(!audit_failure_clears_bootstrap_claim(
            &AuditFailureReason::Timeout
        ));
    }

    fn strike_peer(b: u8) -> PeerId {
        let mut bytes = [0u8; 32];
        bytes[0] = b;
        PeerId::from_bytes(bytes)
    }

    // ADR-0002: "occasional surprise exams, keeps load low" — the per-peer
    // cooldown must collapse a gossip flood into at most one audit per window.

    #[test]
    fn gossip_flood_yields_at_most_one_audit_per_cooldown_window() {
        let peer = strike_peer(1);
        let mut map: HashMap<PeerId, Instant> = HashMap::new();
        let t0 = Instant::now();
        // First gossip in the window passes; a burst of further gossips at the
        // same instant are all suppressed.
        assert!(cooldown_allows_audit(&mut map, &peer, t0));
        let mut passed = 1;
        for _ in 0..100 {
            if cooldown_allows_audit(&mut map, &peer, t0) {
                passed += 1;
            }
        }
        assert_eq!(
            passed, 1,
            "a flood at one instant must trigger exactly one audit"
        );
    }

    // ADR-0002 ordering invariant: `maybe_trigger_gossip_audit` stamps the
    // per-peer cooldown BEFORE the probability lottery, so a LOSING ticket still
    // consumes the window. This is the property the isolated cooldown tests above
    // cannot see: they never sample the lottery, so a regression that reordered
    // the gates (sample probability first, only stamp the cooldown on a win)
    // would still pass them while breaking flood-resistance: a flood would then
    // re-roll the lottery on EVERY message until one won, multiplying audits.
    //
    // We model the exact production gate order (attempt-window, lottery,
    // shared cooldown) with a lottery driven by a fixed outcome instead of
    // `gen_bool(..)`. The first message LOSES the lottery; the remaining flood
    // messages all WIN. With the production order, the losing first ticket
    // burns the ATTEMPT window and every later winner in the same window is
    // blocked, so there are 0 audits this window. If the gates were flipped,
    // the second message's winning ticket would slip through. The window only
    // reopens after it elapses.
    //
    // FLIPS IF: the lottery is sampled before the attempt-window
    // check-and-stamp (a losing ticket no longer consumes the window),
    // re-enabling a flood-amplified audit storm.
    #[test]
    fn losing_lottery_still_consumes_attempt_window() {
        // Calls the SHIPPED `audit_launch_decision` (the same function
        // `maybe_trigger_gossip_audit` uses), so a reorder of the gates in
        // production fails this test — not a local reimplementation.
        let peer = strike_peer(3);
        let mut attempts: HashMap<PeerId, Instant> = HashMap::new();
        let mut launched: HashMap<PeerId, Instant> = HashMap::new();
        let t0 = Instant::now();

        // First flooded message at t0 LOSES the lottery, but the attempt window
        // is stamped BEFORE the lottery is consulted, so the window is consumed.
        assert!(
            !audit_launch_decision(&mut attempts, &mut launched, &peer, t0, false),
            "a losing ticket launches no audit"
        );

        // 99 more flooded messages at the same instant would all WIN the lottery,
        // yet every one must be blocked by the attempt window the loser stamped.
        // (If production sampled the lottery FIRST, these would each get a fresh
        // roll and audits would multiply — this assertion catches that reorder.)
        let mut audits = 0;
        for _ in 0..99 {
            if audit_launch_decision(&mut attempts, &mut launched, &peer, t0, true) {
                audits += 1;
            }
        }
        assert_eq!(
            audits, 0,
            "a losing first ticket must consume the attempt window so no later \
             flooded message in the same window can audit"
        );

        // The window only reopens after it elapses; the next winning ticket
        // then launches exactly one audit and stamps the SHARED cooldown.
        let after = t0 + Duration::from_secs(config::AUDIT_ON_GOSSIP_COOLDOWN_SECS + 1);
        assert!(
            audit_launch_decision(&mut attempts, &mut launched, &peer, after, true),
            "after the window a winning ticket audits again"
        );
        assert!(
            launched.contains_key(&peer),
            "a real launch stamps the shared cooldown"
        );
    }

    /// The reviewer-flagged suppression route: a LOSING gossip lottery must not
    /// stamp the SHARED audit cooldown, otherwise repeated losses (one per
    /// 30-minute window, no challenge ever sent) keep a paid monetized pin's
    /// first audit deferred until its answerability window expires.
    ///
    /// FLIPS IF: `audit_launch_decision` stamps the shared `launched` map on a
    /// loss (the pre-split behavior, where both paths shared one map).
    #[test]
    fn losing_lottery_does_not_suppress_monetized_first_audit() {
        let peer = strike_peer(4);
        let mut attempts: HashMap<PeerId, Instant> = HashMap::new();
        let mut launched: HashMap<PeerId, Instant> = HashMap::new();
        let t0 = Instant::now();

        // A losing ticket consumes the gossip attempt window...
        assert!(!audit_launch_decision(
            &mut attempts,
            &mut launched,
            &peer,
            t0,
            false
        ));
        // ...but leaves the shared map untouched, so the first-audit reserve
        // gate (read-only) and the authoritative promotion check-and-stamp both
        // still allow the paid audit to launch immediately.
        assert!(
            cooldown_would_allow(&launched, &peer, t0),
            "reserve gate must not see a losing ticket as audit coverage"
        );
        assert!(
            cooldown_allows_audit(&mut launched, &peer, t0),
            "promotion must not be deferred by a losing ticket"
        );

        // Conversely a WINNING ticket (real audit sent) does suppress the
        // first audit for the window, which is the intended shared semantics.
        let peer_won = strike_peer(5);
        assert!(audit_launch_decision(
            &mut attempts,
            &mut launched,
            &peer_won,
            t0,
            true
        ));
        assert!(
            !cooldown_would_allow(&launched, &peer_won, t0),
            "a real gossip audit still covers the peer for the window"
        );
    }

    #[test]
    fn cooldown_lets_audit_through_after_the_window() {
        let peer = strike_peer(2);
        let mut map: HashMap<PeerId, Instant> = HashMap::new();
        let t0 = Instant::now();
        assert!(cooldown_allows_audit(&mut map, &peer, t0));
        // Within the window: suppressed.
        let within = t0 + Duration::from_secs(config::AUDIT_ON_GOSSIP_COOLDOWN_SECS - 1);
        assert!(!cooldown_allows_audit(&mut map, &peer, within));
        // Past the window: allowed again.
        let after = t0 + Duration::from_secs(config::AUDIT_ON_GOSSIP_COOLDOWN_SECS + 1);
        assert!(cooldown_allows_audit(&mut map, &peer, after));
    }

    #[test]
    fn cooldown_is_per_peer_independent() {
        let mut map: HashMap<PeerId, Instant> = HashMap::new();
        let t0 = Instant::now();
        // Different peers each get their own first-audit pass at the same instant.
        for i in 0..20u8 {
            assert!(
                cooldown_allows_audit(&mut map, &strike_peer(i), t0),
                "peer {i} should be auditable independently"
            );
        }
    }

    #[test]
    fn audit_on_gossip_constants_match_adr() {
        // Tripwire on the ADR-locked tunables. The spot-check count sits at the
        // top of the auditor's 3..=5 band (the auditor clamps to that band, so
        // values above 5 would silently never be requested).
        assert_eq!(config::AUDIT_SPOTCHECK_COUNT, 5);
        assert!((config::AUDIT_ON_GOSSIP_PROBABILITY - 0.2).abs() < f64::EPSILON);
        assert_eq!(config::AUDIT_ON_GOSSIP_COOLDOWN_SECS, 30 * 60);
    }

    // (d) A confirmed storage-integrity failure penalizes immediately and
    // revokes credit; it is not a timeout.
    #[test]
    fn digest_mismatch_is_not_a_timeout_and_penalizes_immediately() {
        assert!(audit_failure_clears_bootstrap_claim(
            &AuditFailureReason::DigestMismatch
        ));
        assert!(audit_failure_revokes_holder_credit(
            &AuditFailureReason::DigestMismatch
        ));
    }

    /// The exact decision the `Failed` arm of `handle_subtree_audit_result`
    /// uses: confirmed failures revoke credit, `Timeout` does not.
    #[test]
    fn confirmed_failures_revoke_credit_timeout_does_not() {
        for reason in [
            AuditFailureReason::MalformedResponse,
            AuditFailureReason::DigestMismatch,
            AuditFailureReason::KeyAbsent,
            AuditFailureReason::Rejected,
        ] {
            assert!(
                audit_failure_revokes_holder_credit(&reason),
                "confirmed failure {reason:?} must revoke holder credit"
            );
        }
        assert!(
            !audit_failure_revokes_holder_credit(&AuditFailureReason::Timeout),
            "Timeout must NOT revoke credit (single dropped packet != storage loss)"
        );
    }

    /// Wiring test for the security fix: the helper the handler calls
    /// actually strips a credited peer on a confirmed failure
    /// (`DigestMismatch`), and actually RETAINS credit on `Timeout`.
    /// Records genuine credit first so neither assertion is vacuous;
    /// this fails if `forget_peer` stops being called, or if the
    /// `Timeout` exclusion is dropped (both verified by mutation).
    #[test]
    fn apply_revocation_strips_on_digest_mismatch_retains_on_timeout() {
        let peer = test_peer(0xAB);
        let key = test_key(1);
        let hash = [0xCD; 32];

        // Confirmed failure -> credit revoked.
        let mut provers = RecentProvers::new();
        provers.record_proof(key, peer, hash, Instant::now());
        assert!(
            provers.is_credited_holder(&key, &peer, &hash),
            "precondition: peer credited before failure"
        );
        apply_audit_failure_credit_revocation(
            &mut provers,
            &peer,
            &AuditFailureReason::DigestMismatch,
        );
        assert!(
            !provers.is_credited_holder(&key, &peer, &hash),
            "DigestMismatch must strip the peer's holder credit"
        );

        // Timeout -> credit retained.
        let mut provers_timeout = RecentProvers::new();
        provers_timeout.record_proof(key, peer, hash, Instant::now());
        apply_audit_failure_credit_revocation(
            &mut provers_timeout,
            &peer,
            &AuditFailureReason::Timeout,
        );
        assert!(
            provers_timeout.is_credited_holder(&key, &peer, &hash),
            "Timeout must retain holder credit (deliberate liveness cushion)"
        );
    }

    #[test]
    fn decoded_audit_failures_clear_active_bootstrap_claim() {
        for reason in [
            AuditFailureReason::MalformedResponse,
            AuditFailureReason::DigestMismatch,
            AuditFailureReason::KeyAbsent,
            AuditFailureReason::Rejected,
        ] {
            assert!(
                audit_failure_clears_bootstrap_claim(&reason),
                "decoded non-bootstrap failure {reason:?} should clear active claim"
            );
        }
    }

    #[test]
    fn first_failed_key_label_truncates_to_16_hex_chars() {
        // The high-order 8 bytes of the XorName determine the label so an
        // operator can group audit-failures on the same chunk prefix.
        let mut key = [0u8; 32];
        key[0] = 0x18;
        key[7] = 0xff;
        // Low-order bytes (positions 8..32) are deliberately set to 0xAA
        // to verify they are NOT included in the label.
        for byte in &mut key[8..] {
            *byte = 0xAA;
        }
        let label = first_failed_key_label(&[key]);
        // Only the first 8 bytes are encoded, low-order bytes are dropped.
        assert_eq!(label, "0x18000000000000ff");
        assert_eq!(label.len(), "0x".len() + 16);
    }

    #[test]
    fn first_failed_key_label_falls_back_when_empty() {
        // Should never happen in production (audit failure handling rejects
        // empty sets), but the formatter must still produce a valid label
        // so the log line doesn't contain a misleading default.
        assert_eq!(first_failed_key_label(&[]), "0x");
    }

    #[test]
    fn first_failed_key_label_uses_first_key_only() {
        let first = [0x11u8; 32];
        let second = [0x22u8; 32];
        assert_eq!(
            first_failed_key_label(&[first, second]),
            format!("0x{}", hex::encode(&first[..8]))
        );
    }

    // -- apply_fetch_result --------------------------------------------------
    //
    // The worker's disposition of a fetch outcome. Note there is no trust
    // handle in `apply_fetch_result`'s signature: the worker cannot report a
    // trust event for ANY outcome, and `execute_single_fetch` returns
    // `NoLongerResponsible` before its trust-reporting paths — the source did
    // nothing wrong when this node's own responsibility lapsed.

    /// Route `key` through the real pipeline stages (pending → promoted →
    /// dequeued → in-flight) so it carries a verification retry-slot
    /// reservation, exactly as a worker-dequeued key does. Returns the
    /// in-flight source.
    fn drive_key_in_flight(
        q: &mut ReplicationQueues,
        key: XorName,
        sources: Vec<PeerId>,
    ) -> PeerId {
        let hinter = sources.first().copied().unwrap_or_else(|| test_peer(0x01));
        let now = Instant::now();
        let entry = VerificationEntry {
            state: VerificationState::PendingVerify,
            verified_sources: Vec::new(),
            tried_sources: HashSet::new(),
            created_at: now,
            next_verify_at: now,
            hint_sources: HashSet::from([hinter]),
            replica_hint_sources: HashSet::from([hinter]),
            unresolved_retries: 0,
            no_holder_reported: false,
        };
        assert!(q.add_pending_verify(key, entry).admitted());
        assert!(q.promote_pending_to_fetch(key, key, sources));
        let candidate = q.dequeue_fetch().expect("promoted key must dequeue");
        let source = *candidate.sources.first().expect("candidate has a source");
        q.start_dequeued_fetch(candidate, source);
        source
    }

    #[test]
    fn no_longer_responsible_is_terminal_and_releases_the_retry_slot() {
        const RETRY_AFTER: Duration = Duration::from_mins(1);

        let mut q = ReplicationQueues::new();
        let key = test_key(0xAB);
        drive_key_in_flight(&mut q, key, vec![test_peer(0x01), test_peer(0x02)]);
        assert_eq!(q.retry_reserved_slot_count(), 1);

        let follow_up =
            apply_fetch_result(&mut q, &key, &FetchResult::NoLongerResponsible, RETRY_AFTER);

        assert_eq!(
            follow_up,
            FetchFollowUp::Terminal,
            "lapsed responsibility must run the caller's terminal bootstrap accounting"
        );
        assert!(
            !q.contains_key(&key),
            "the key must leave every pipeline stage — alternate sources included"
        );
        assert_eq!(
            q.pending_count(),
            0,
            "a lapsed-responsibility key must not be requeued for verification"
        );
        assert_eq!(
            q.retry_reserved_slot_count(),
            0,
            "terminal exit must release the verification retry-slot reservation"
        );
    }

    #[test]
    fn no_longer_responsible_shares_the_stored_terminal_path() {
        const RETRY_AFTER: Duration = Duration::from_mins(1);

        // Both variants must walk the identical terminal gate so the
        // battle-tested Stored accounting (retry-slot release + bootstrap
        // pending-set shrink in the caller) covers lapsed responsibility too.
        for result in [FetchResult::Stored, FetchResult::NoLongerResponsible] {
            let mut q = ReplicationQueues::new();
            let key = test_key(0xCD);
            drive_key_in_flight(&mut q, key, vec![test_peer(0x03)]);

            let follow_up = apply_fetch_result(&mut q, &key, &result, RETRY_AFTER);

            assert_eq!(follow_up, FetchFollowUp::Terminal);
            assert!(!q.contains_key(&key));
            assert_eq!(q.retry_reserved_slot_count(), 0);
        }
    }

    #[test]
    fn source_failure_walks_alternate_sources_then_requeues_for_verification() {
        const RETRY_AFTER: Duration = Duration::from_mins(1);

        let mut q = ReplicationQueues::new();
        let key = test_key(0xEF);
        let first = test_peer(0x04);
        let second = test_peer(0x05);
        drive_key_in_flight(&mut q, key, vec![first, second]);

        let follow_up = apply_fetch_result(&mut q, &key, &FetchResult::SourceFailed, RETRY_AFTER);
        assert_eq!(
            follow_up,
            FetchFollowUp::RetryFrom(second),
            "a failed source must not abandon the remaining verified sources"
        );
        assert_eq!(q.in_flight_count(), 1, "retry keeps the key in flight");

        let follow_up = apply_fetch_result(&mut q, &key, &FetchResult::SourceFailed, RETRY_AFTER);
        assert_eq!(
            follow_up,
            FetchFollowUp::RequeuedForVerification,
            "exhausted sources must restore the reserved verification entry"
        );
        assert_eq!(q.pending_count(), 1);
        assert_eq!(
            q.retry_reserved_slot_count(),
            0,
            "the reservation converts back into the pending entry itself"
        );
    }

    #[test]
    fn source_failure_without_retry_metadata_is_terminal() {
        const RETRY_AFTER: Duration = Duration::from_mins(1);

        // Direct enqueue (no pending entry) models a fetch with no
        // verification retry reservation to restore.
        let mut q = ReplicationQueues::new();
        let key = test_key(0x1F);
        let source = test_peer(0x06);
        assert!(q.enqueue_fetch(key, key, vec![source]));
        let candidate = q.dequeue_fetch().expect("enqueued key must dequeue");
        q.start_dequeued_fetch(candidate, source);

        let follow_up = apply_fetch_result(&mut q, &key, &FetchResult::SourceFailed, RETRY_AFTER);

        assert_eq!(follow_up, FetchFollowUp::Terminal);
        assert!(!q.contains_key(&key));
    }

    #[test]
    fn already_held_key_leaves_the_pipeline_terminally() {
        const RETRY_AFTER: Duration = Duration::from_mins(1);

        let mut q = ReplicationQueues::new();
        let key = test_key(0x4C);
        let source = test_peer(0x0B);
        drive_key_in_flight(&mut q, key, vec![source, test_peer(0x0C)]);

        let follow_up = apply_fetch_result(&mut q, &key, &FetchResult::AlreadyHeld, RETRY_AFTER);

        assert_eq!(
            follow_up,
            FetchFollowUp::Terminal,
            "holding the key already discharges the duty, so the key must leave \
             the pipeline exactly as a completed fetch does — requeueing it \
             would re-verify a key that needs nothing"
        );
        assert!(!q.contains_key(&key));
        assert_eq!(
            q.retry_reserved_slot_count(),
            0,
            "the terminal path must release the retry-slot reservation, or the \
             owner's verification budget leaks"
        );
    }

    #[test]
    fn local_write_failure_does_not_conscript_the_remaining_sources() {
        const RETRY_AFTER: Duration = Duration::from_mins(1);

        let mut q = ReplicationQueues::new();
        let key = test_key(0x2A);
        let first = test_peer(0x07);
        let second = test_peer(0x08);
        let third = test_peer(0x09);
        drive_key_in_flight(&mut q, key, vec![first, second, third]);

        let follow_up =
            apply_fetch_result(&mut q, &key, &FetchResult::LocalWriteFailed, RETRY_AFTER);

        assert_eq!(
            follow_up,
            FetchFollowUp::RequeuedForVerification,
            "a local write failure must go straight back to verification: the two \
             untried sources would each re-upload the same chunk into the same \
             full disk, and neither of them did anything wrong"
        );
        assert_eq!(q.in_flight_count(), 0);
        assert_eq!(
            q.pending_count(),
            1,
            "the key must come back — capacity frees up, and a stranded key is \
             a replica this node silently stops owing"
        );
        assert_eq!(q.retry_reserved_slot_count(), 0);
    }

    #[test]
    fn local_write_failure_without_retry_metadata_is_terminal() {
        const RETRY_AFTER: Duration = Duration::from_mins(1);

        // No pending entry to restore, so the only way not to strand the key
        // in `in_flight_fetch` forever — which would also stall bootstrap
        // drain — is the terminal path.
        let mut q = ReplicationQueues::new();
        let key = test_key(0x3B);
        let source = test_peer(0x0A);
        assert!(q.enqueue_fetch(key, key, vec![source]));
        let candidate = q.dequeue_fetch().expect("enqueued key must dequeue");
        q.start_dequeued_fetch(candidate, source);

        let follow_up =
            apply_fetch_result(&mut q, &key, &FetchResult::LocalWriteFailed, RETRY_AFTER);

        assert_eq!(follow_up, FetchFollowUp::Terminal);
        assert!(!q.contains_key(&key));
        assert_eq!(q.retry_reserved_slot_count(), 0);
    }
}