par2-rs 0.8.0

PAR2 parity verification and repair
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
//! High-level PAR2 verifier/repairer.
//!
//! This module mirrors the repairer shape used by traditional PAR2 tools:
//! load packets, build source blocks, scan job-local files for usable blocks,
//! stage/copy known-good blocks, run RS reconstruction for the missing blocks,
//! and verify repaired output before installing it.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
#[cfg(unix)]
use std::os::unix::fs::FileExt as _;
#[cfg(windows)]
use std::os::windows::fs::FileExt as _;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{Arc, LazyLock, Mutex};
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

#[cfg(test)]
use crate::DiskFileAccess;
use crate::checksum::{self, Crc32Hasher, Md5State};
use crate::error::{Par2Error, Result};
use crate::evidence::FileStatFingerprint;
use crate::md5_simd;
use crate::packet::budget::packet_retained_bytes;
use crate::packet::{
    Packet, PacketScanBudget, PacketScanLimits, PacketSink, scan_packets_from_path_bounded,
};
use crate::par2_set::{FileDescription, PacketAdmission, Par2FileSet, Par2FileSetBuilder};
use crate::path::is_generated_par2_artifact_name;
use crate::repair::{
    DEFAULT_REPAIR_MEMORY_LIMIT, RepairOptions, execute_repair_with_options,
    plan_repair_with_memory_limit, repair_matrix_resource_limit_reason,
};
use crate::types::{
    CancellationToken, FileId, MAX_SLICES_PER_FILE, ProgressCallback, RecoverySetId, SliceChecksum,
};
use crate::verify::{
    self, FileAccess, FileStatus, FileVerification, Repairability, VerificationResult,
};
use rayon::prelude::*;
use thiserror::Error;
use tracing::{debug, warn};

const ZERO_PAD_CHUNK: [u8; 8192] = [0u8; 8192];
const SCANNER_MD5_BATCH_MEMORY_BYTES: usize = 4 * 1024 * 1024;
const SCANNER_IO_TARGET_BYTES: usize = 4 * 1024 * 1024;
const SCANNER_MMAP_FALLBACK_SLICE_BYTES: usize = 8 * 1024 * 1024;
const SCANNER_PARALLEL_SEGMENT_TARGET_BYTES: usize = 8 * 1024 * 1024;
const ORDERED_SCAN_SERIAL_ENV: &str = "WEAVER_PAR2_SERIAL_SCAN";
const ORDERED_SCAN_PARALLEL_ENV: &str = "WEAVER_PAR2_PARALLEL_SCAN";
const CANONICAL_COMPLETE_HASH_SKIP_BYTES: u64 = 1024 * 1024;
const ORDERED_SCAN_DEFAULT_SKIP_LEEWAY: u64 = 64;
const SCANNER_SLOW_WARN_STEPS: u64 = 5_000_000;
const SCANNER_SLOW_WARN_DURATION: Duration = Duration::from_secs(5);

/// Read-only view over a whole file, used by the block scanner.
///
/// On native targets this is a real memory map (`memmap2`), preserving the
/// existing zero-copy scan behaviour and performance byte-for-byte. On wasm
/// targets — where `mmap` does not exist under wasip1 — it is a compile-time
/// fallback that reads the file into an owned `Vec<u8>`. Both variants
/// `Deref` to `&[u8]`, so every scan call site is identical across targets.
///
/// The selection is purely `#[cfg(target_family = "wasm")]`, so native
/// codegen is unchanged: the `Vec` variant does not exist in the native build
/// and the mmap variant does not exist in the wasm build.
struct MappedFile {
    #[cfg(not(target_family = "wasm"))]
    inner: memmap2::Mmap,
    #[cfg(target_family = "wasm")]
    inner: Vec<u8>,
}

impl MappedFile {
    /// Map (native) or fully read (wasm) an already-opened file.
    ///
    /// `#[inline]` so the native wrapper collapses into the call site, leaving
    /// the exact `MmapOptions::new().map(&file)` codegen the scanner had before.
    #[cfg(not(target_family = "wasm"))]
    #[inline]
    fn map(file: &File) -> io::Result<Self> {
        // SAFETY: identical to the prior inline `MmapOptions::new().map(&file)`
        // call. The scanner only reads through the returned slice and drops the
        // map before the file is truncated or rewritten.
        let inner = unsafe { memmap2::MmapOptions::new().map(file)? };
        Ok(Self { inner })
    }

    /// wasip1 has no `mmap`; buffer the whole file into memory instead. The
    /// scanner treats the result as an immutable `&[u8]`, so behaviour matches
    /// the native mmap path (only the backing storage differs).
    #[cfg(target_family = "wasm")]
    fn map(file: &File) -> io::Result<Self> {
        let mut inner = Vec::new();
        // Clone the handle so this read does not disturb any cursor the caller
        // holds, mirroring mmap's independence from the file position.
        (&mut &*file).read_to_end(&mut inner)?;
        Ok(Self { inner })
    }
}

impl std::ops::Deref for MappedFile {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        &self.inner
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Par2RepairStatus {
    Verified,
    RepairPossible,
    Repaired,
    Insufficient,
    ResourceLimited,
}

#[derive(Debug, Clone, Default)]
pub struct PacketDiagnostics {
    pub packets_loaded: u32,
    pub corrupt_packets: u32,
    pub duplicate_packets: u32,
    pub discarded_recovery_blocks: u32,
    pub inconsistent_packets: u32,
    pub conflicting_packets: u32,
}

#[derive(Debug, Clone, Default)]
// A pass learns to report more about itself over time; a new counter should
// not cost every consumer a major version.
#[non_exhaustive]
pub struct ScanDiagnostics {
    pub files_scanned: u32,
    pub bytes_scanned: u64,
    pub blocks_found: u32,
    pub duplicate_blocks: u32,
    pub files_skipped: u32,
    /// Candidates the deferred short-block relocation search re-read. Zero is
    /// the healthy shape: it means the merged scan state already placed every
    /// short block, so nothing had to be hunted for.
    pub short_relocation_candidates_scanned: u32,
    /// Candidates the relocation search declined to re-read because the merged
    /// scan state already accounts for every byte of them.
    pub short_relocation_candidates_skipped: u32,
    /// Rolling windows the relocation search stepped, summed over candidates.
    /// This is the counter that makes an exhaustive relocation search visible;
    /// the ordinary per-file scan counters never see its work.
    pub short_relocation_windows_stepped: u64,
    /// Bytes the relocation search re-read from candidate files.
    pub short_relocation_bytes_read: u64,
    /// Short blocks the relocation search matched and placed. A block whose
    /// held location the search may not displace is skipped before the match
    /// check, so a match counted here is never a futile offer.
    pub short_relocation_blocks_placed: u32,
    /// True when this pass installed a prior pass's scan instead of running
    /// its own. Every counter above then describes that earlier scan, and this
    /// pass read no source bytes to analyse the set.
    pub carried: bool,
    /// Source slices this pass declined to read because evidence had already
    /// located them and the file still matched the stat fingerprint that
    /// evidence was admitted against. Zero unless the host opted in with
    /// [`crate::Par2RepairSessionOptions::trust_seeded_evidence_for_scan`], or
    /// supplied a carry built from its own verification
    /// ([`ScanCarry::from_verification`]), where every located slice is one
    /// this crate never read.
    pub slices_settled_by_evidence: u32,
    /// Source bytes covered by [`Self::slices_settled_by_evidence`], and
    /// therefore neither read nor hashed. [`Self::bytes_scanned`] excludes
    /// them, so an outcome reached without reading its sources in full is
    /// distinguishable from one that was: this counter is non-zero.
    pub bytes_skipped_by_evidence: u64,
}

/// How a pass treated the scan state it was handed.
///
/// A host that wants to know whether a repair cost one scan or two reads three
/// fields together: `carry_applied` says this pass installed carried state
/// instead of scanning, `carry_retried_fresh` says a second pass ran from a
/// real scan anyway, and `carry_consumed_for_repair` says the mutation itself
/// ran on the carried analysis. `carry_applied && carry_consumed_for_repair &&
/// !carry_retried_fresh` is the single-scan shape; the accompanying
/// [`ScanDiagnostics::carried`] flag says the same thing about the scan
/// counters in the same outcome.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct CarryDiagnostics {
    pub carry_attempted: bool,
    pub carry_applied: bool,
    pub carry_retried_fresh: bool,
    pub carry_retry_reason: Option<CarryRetryReason>,
    /// The mutating repair ran on the carried analysis, with no second scan.
    /// Set only after every source the repair would read was re-stat'd
    /// immediately before mutation and still matched the fingerprint the
    /// carried scan captured.
    pub carry_consumed_for_repair: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
// New reasons are diagnostic detail, not a contract change for matchers.
#[non_exhaustive]
pub enum CarryRetryReason {
    TerminalStatus(Par2RepairStatus),
    RepairRequested,
    PostRepairVerificationFailed,
    /// A source the repair would have read no longer matches the stat
    /// fingerprint the carried scan captured for it — it changed, was
    /// replaced, or is gone. Also reported when validated reads found the
    /// bytes themselves changed under a fingerprint that did not move.
    RepairInputChanged,
    /// A source the repair would have read carries no fingerprint the carry
    /// can be checked against — an access-backed source, whose validity is a
    /// property of the serving handle rather than of the filesystem.
    RepairInputNotFingerprinted,
}

/// Scan state carried from an analyze pass to a later execute pass over the
/// same set, letting later scheduling avoid re-scanning every source file.
/// Application re-stats every file the scan observed (including recording
/// nonexistence) and refuses on visible drift.
///
/// A repair consumes an applied carry only after a second, narrower check
/// immediately before it mutates anything: every source the repair will read
/// must still match the fingerprint the scan captured for it. That repair then
/// reads through the validated path, so a change too subtle for `stat` is
/// still caught on the bytes rather than written into the output.
///
/// Every carried result that does *not* mutate stays speculative and is
/// re-established from a fresh content scan before it is reported.
///
/// A carry never discovers source files that appeared after the analyze
/// pass — callers that allow drop-ins between passes should not supply one.
///
/// A carry can also come from outside this crate:
/// [`ScanCarry::from_verification`] turns a host's own verification pass into
/// one, so a host that already read the payload does not pay for the
/// repairer's scan to read it again. Both origins meet the same gates — the
/// set match, the per-path stat snapshot, the pre-mutation re-stat of every
/// repair input, and the validated read during repair — and nothing
/// downstream can tell them apart.
#[derive(Debug)]
pub struct ScanCarry {
    /// Identity of the set this carry describes. Checked before the carried
    /// files and blocks are installed, because those vectors are swapped in
    /// wholesale: their `first_block` offsets, expected lengths and slice
    /// checksums are only meaningful for the set they were laid out from.
    /// File IDs alone nearly settle this (a `FileId` hashes the file's first
    /// 16 KiB, length and name), but they say nothing about the slice size
    /// that turns a local slice index into a byte offset.
    recovery_set_id: RecoverySetId,
    slice_size: u64,
    set_file_ids: Vec<FileId>,
    snapshot: Vec<CarriedFileStat>,
    files: Vec<SourceFileEntry>,
    blocks: Vec<SourceBlock>,
    diagnostics: ScanDiagnostics,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct CarriedFileStat {
    path: PathBuf,
    /// The stat fingerprint when the path existed as a regular file, and
    /// `None` when it did not. Recording nonexistence is load-bearing: a file
    /// that appears between two passes is drift just as much as one that
    /// changes.
    state: Option<FileStatFingerprint>,
}

/// Fingerprint `path` as the carry gate sees it.
///
/// Symlinks are not followed and only regular files fingerprint, so a path
/// that becomes a directory, a symlink, or a device reads as absent rather
/// than as an unchanged file. What the fingerprint covers — length, mtime,
/// and on Unix device and inode — is what `stat` can prove; a same-length
/// rewrite that also restores the original mtime in place is invisible to it,
/// which is why the repair that consumes a carry re-checks the bytes
/// themselves against their slice checksums as it reads them.
fn stat_for_carry(path: &Path) -> CarriedFileStat {
    CarriedFileStat {
        path: path.to_path_buf(),
        state: stat_fingerprint(path),
    }
}

/// Fingerprint `path` as every stat gate in this crate compares it: symlinks
/// are not followed and only regular files fingerprint, so a path that became
/// a directory, a symlink or a device reads as absent rather than unchanged.
///
/// This is a one-line forward to [`FileStatFingerprint::capture_path`], the
/// public capture a host uses when it builds a carry from its own verification
/// ([`ScanCarry::from_verification`]). Having exactly one implementation is the
/// point: a host-captured fingerprint and the gate that re-checks it must
/// agree about what "the same file" means, and two copies of this rule would
/// eventually stop agreeing.
pub(crate) fn stat_fingerprint(path: &Path) -> Option<FileStatFingerprint> {
    FileStatFingerprint::capture_path(path)
}

/// Why a host's verification could not be turned into a [`ScanCarry`].
///
/// Every variant names a disagreement between the attestation and the set it
/// claims to describe, not a property of the files on disk. They are caller
/// bugs — a mismatched set, an attestation that contradicts itself — and are
/// reported rather than absorbed, because a carry built from an attestation
/// this crate could not make sense of is exactly the thing that must never
/// reach a repair.
#[derive(Debug, Error)]
// New ways for an attestation to be inconsistent are detail, not a contract
// change for matchers.
#[non_exhaustive]
pub enum ExternalCarryError {
    /// The verification covers a file the set does not describe.
    #[error("verification names file {file_id}, which is not in the recovery set")]
    UnknownFile { file_id: FileId },
    /// Two entries in the verification claim the same file.
    #[error("verification names file {file_id} more than once")]
    DuplicateFile { file_id: FileId },
    /// A recoverable file in the set has no entry in the verification. A carry
    /// must describe the whole set: what it does not mention would otherwise
    /// be silently taken as unrecoverable.
    #[error("verification does not cover recovery-set file {file_id}")]
    UncoveredFile { file_id: FileId },
    /// The per-slice validity vector is not the length the set's slice layout
    /// requires for that file.
    #[error(
        "file {file_id} has {expected} slices in the set but the verification supplied {supplied}"
    )]
    SliceCountMismatch {
        file_id: FileId,
        expected: usize,
        supplied: usize,
    },
    /// `missing_slice_count` disagrees with the count of invalid slices, or a
    /// `Damaged(n)` status disagrees with either.
    #[error(
        "file {file_id} declares {declared} damaged slices but its validity vector shows {actual}"
    )]
    DamagedCountMismatch {
        file_id: FileId,
        declared: u32,
        actual: u32,
    },
    /// A file claimed `Complete` whose validity vector is not all-valid.
    #[error("file {file_id} is reported complete but its validity vector has invalid slices")]
    IncompleteCompleteFile { file_id: FileId },
    /// A file claimed `Missing` for which a stat fingerprint was supplied, or
    /// whose validity vector claims a valid slice. A missing file has neither.
    #[error("file {file_id} is reported missing but the verification also claims content for it")]
    MissingFileWithContent { file_id: FileId },
    /// A file that is not `Missing` but carries no stat fingerprint. Without
    /// one there is nothing for the carry gate to re-check, so the carry would
    /// be refused at repair time anyway; refusing to build it says so up front.
    #[error("file {file_id} is present in the verification but carries no stat fingerprint")]
    UnfingerprintedFile { file_id: FileId },
    /// A `Renamed` status. A carry built from a host verification describes
    /// files at their canonical paths only (see [`ScanCarry::from_verification`]).
    #[error(
        "file {file_id} is reported at a non-canonical path, which an external carry cannot describe"
    )]
    RelocatedFile { file_id: FileId },
    /// The set could not be laid out into source files and blocks at all.
    #[error("PAR2 set cannot be laid out for a carry: {0}")]
    Set(#[from] Par2Error),
}

impl ScanCarry {
    /// Build a carry from a verification pass this crate did not run.
    ///
    /// # What this is for
    ///
    /// A host that verifies a set itself — a full strict read through
    /// [`crate::verify`] — and then decides to repair would otherwise watch
    /// [`Par2Repairer`] scan and hash the very bytes it just read. Handing the
    /// repairer a carry built from that verification skips the repairer's
    /// scan: the payload is read once, by the host, and the repair proceeds on
    /// what the host found.
    ///
    /// # The trust contract
    ///
    /// The caller attests that it read the bytes it claims: that
    /// `verification`'s per-slice validity is what a real read of each file
    /// produced, and that each `fingerprints` entry was captured
    /// ([`FileStatFingerprint::capture_path`]) at the moment of that read. This
    /// crate cannot check the first claim — that is what "attest" means — and
    /// it does not try to.
    ///
    /// What it does instead is refuse to let a false attestation reach the
    /// output, through the same three gates a natively-produced carry passes:
    ///
    /// 1. **The snapshot gate.** Before the carried analysis is installed,
    ///    every path the carry names is re-stat'd and must still match the
    ///    fingerprint the caller captured. A file that changed after the
    ///    caller read it — including one whose length is unchanged but whose
    ///    mtime moved — refuses the carry, and the pass scans for real.
    /// 2. **The pre-mutation gate.** Immediately before a repair mutates
    ///    anything, every source it will read is re-stat'd again against the
    ///    same fingerprints. Anything else sends the pass back to a full scan
    ///    before a byte is written.
    /// 3. **The validated read.** A repair that consumes a carry — whatever
    ///    produced it — reads every source slice through the validated path,
    ///    checking it against its IFSC checksum on the way into staging and
    ///    into the Reed-Solomon input stream. This is unconditional, and it is
    ///    what covers the one drift `stat` cannot see: a same-length rewrite
    ///    that also restored the original mtime.
    ///
    /// So a false attestation degrades to a rescan (gates 1 and 2) or to a
    /// caught checksum mismatch that retries from a fresh scan before
    /// installing anything (gate 3). It never produces corrupt output. The
    /// cost of being wrong is the scan the carry was meant to save, which is
    /// the honest price.
    ///
    /// # What a carry built this way describes
    ///
    /// Only canonical placement. A host verification reads each file at its
    /// recorded path, so this constructor can only say "this file's slice *i*
    /// is intact, at its canonical offset, at its canonical path". It cannot
    /// describe a file found under a different name, a copy in an extra search
    /// path, or a block relocated within a file — the three things the
    /// repairer's own scanner exists to find. Those are not lost, only
    /// unclaimed: a slice this carry does not locate is a slice the repair
    /// reconstructs from parity, which costs Reed-Solomon work but produces
    /// the same bytes. A [`FileStatus::Renamed`] entry is refused outright
    /// rather than silently downgraded, because a host that found a renamed
    /// file is describing a placement this form has no way to carry.
    ///
    /// Non-recovery files (those the set describes but does not protect) are
    /// neither required in `verification` nor recorded here: they are never
    /// repair inputs and never repair targets.
    ///
    /// # Consistency the constructor does check
    ///
    /// The attestation must agree with itself and with the set: one entry per
    /// recoverable file, validity vectors of the length the set's slice layout
    /// requires, damage counts that match their validity vectors, a
    /// fingerprint for every file not reported [`FileStatus::Missing`] and
    /// none for one that is. These are caller bugs, and each returns an
    /// [`ExternalCarryError`] instead of a carry.
    ///
    /// A slice claimed valid whose bytes would fall past the end of the file
    /// the fingerprint describes is dropped rather than carried: the caller
    /// cannot have read bytes that are not there. Dropping it makes the
    /// repair reconstruct that slice, which is the conservative direction.
    ///
    /// # Cost
    ///
    /// `set` is cloned to lay out the source files and blocks through exactly
    /// the same code path [`Par2Repairer`] uses, so the two layouts cannot
    /// drift. The clone is of the set's metadata; recovery slice payloads are
    /// reference-counted and are not copied. No file is opened or read here,
    /// and nothing is stat'd — the fingerprints are the caller's, by design.
    pub fn from_verification(
        base_dir: &Path,
        set: &Par2FileSet,
        verification: &VerificationResult,
        fingerprints: &HashMap<FileId, FileStatFingerprint>,
    ) -> std::result::Result<Self, ExternalCarryError> {
        // Laid out by the repairer's own constructor, not by a parallel
        // reimplementation: `try_apply_carry` swaps these vectors in wholesale,
        // so a layout that differed by one block offset would be undetectable
        // and catastrophic.
        let mut state = RepairState::from_set(base_dir, set.clone())?;
        let slice_size = state.set.slice_size;

        let mut attested: HashMap<FileId, &FileVerification> =
            HashMap::with_capacity(verification.files.len());
        for file in &verification.files {
            if attested.insert(file.file_id, file).is_some() {
                return Err(ExternalCarryError::DuplicateFile {
                    file_id: file.file_id,
                });
            }
        }

        let mut located_blocks = 0u32;
        let mut located_bytes = 0u64;

        for file_index in 0..state.files.len() {
            if !state.files[file_index].recoverable {
                continue;
            }
            let file_id = state.files[file_index].file_id;
            let attestation = attested
                .remove(&file_id)
                .ok_or(ExternalCarryError::UncoveredFile { file_id })?;
            let fingerprint = fingerprints.get(&file_id);
            check_attestation(&state.files[file_index], attestation, fingerprint)?;

            let Some(fingerprint) = fingerprint else {
                // Reported missing, and checked as such above: no target, no
                // locations. `verification_result` reads that back as
                // `FileStatus::Missing`, which is what a scan of an absent
                // file produces, so repair treats the file as a target rather
                // than as an input.
                state.files[file_index].target_exists = false;
                continue;
            };
            state.files[file_index].target_exists = true;

            let first_block = state.files[file_index].first_block;
            let block_count = state.files[file_index].block_count;
            let safe_path = state.files[file_index].safe_path.clone();
            for local in 0..block_count {
                if !attestation.valid_slices[local] {
                    continue;
                }
                let block_index = first_block + local;
                let expected_len = state.blocks[block_index].expected_len;
                let offset = local as u64 * slice_size;
                if offset.saturating_add(expected_len) > fingerprint.length() {
                    // The caller claims a slice that does not fit in the file
                    // it fingerprinted. It cannot have read those bytes, so
                    // the block stays unlocated and repair rebuilds it.
                    continue;
                }
                state.blocks[block_index].location = Some(BlockLocation {
                    source: SourceLocation::Path(safe_path.clone()),
                    offset,
                    len: expected_len,
                    kind: BlockLocationKind::Canonical,
                });
                located_blocks = located_blocks.saturating_add(1);
                located_bytes = located_bytes.saturating_add(expected_len);
            }

            if external_carry_layout_is_complete(&state, file_index, fingerprint.length()) {
                let file = &state.files[file_index];
                let complete = BlockLocation {
                    source: SourceLocation::Path(file.safe_path.clone()),
                    offset: 0,
                    len: file.length,
                    kind: BlockLocationKind::Canonical,
                };
                state.files[file_index].complete_location = Some(complete);
            }
        }

        if let Some(file_id) = attested.keys().next().copied() {
            return Err(ExternalCarryError::UnknownFile { file_id });
        }

        // The snapshot is the caller's fingerprints verbatim. Re-statting the
        // paths here would defeat the whole gate: it would record the file as
        // it is *now* rather than as it was when the caller read it, and any
        // change in between would become invisible.
        let present_files = state
            .files
            .iter()
            .filter(|file| file.recoverable && file.target_exists)
            .count() as u32;
        let absent_files = state
            .files
            .iter()
            .filter(|file| file.recoverable && !file.target_exists)
            .count() as u32;
        let snapshot: Vec<CarriedFileStat> = state
            .files
            .iter()
            .filter(|file| file.recoverable)
            .map(|file| CarriedFileStat {
                path: file.safe_path.clone(),
                state: fingerprints.get(&file.file_id).cloned(),
            })
            .collect();

        Ok(ScanCarry {
            recovery_set_id: state.set.recovery_set_id,
            slice_size,
            set_file_ids: state.files.iter().map(|file| file.file_id).collect(),
            snapshot,
            files: state.files.clone(),
            blocks: state.blocks.clone(),
            diagnostics: ScanDiagnostics {
                files_scanned: present_files,
                // This pass read nothing. The bytes behind the carried
                // verdicts are counted as skipped-by-evidence below, which is
                // the counter that exists to disclose an analysis reached
                // without reading its sources.
                bytes_scanned: 0,
                blocks_found: located_blocks,
                files_skipped: absent_files,
                slices_settled_by_evidence: located_blocks,
                bytes_skipped_by_evidence: located_bytes,
                ..ScanDiagnostics::default()
            },
        })
    }
}

/// Check one host attestation against the set entry it claims to describe.
///
/// Only self-consistency is checked — whether the vector, the counts and the
/// status agree with each other and with the set's slice layout. Whether the
/// validity bits are *true* is the caller's attestation and is not checkable
/// here; see [`ScanCarry::from_verification`] for what defends against a false
/// one.
fn check_attestation(
    file: &SourceFileEntry,
    attestation: &FileVerification,
    fingerprint: Option<&FileStatFingerprint>,
) -> std::result::Result<(), ExternalCarryError> {
    let file_id = file.file_id;
    if attestation.valid_slices.len() != file.expected_block_count {
        return Err(ExternalCarryError::SliceCountMismatch {
            file_id,
            expected: file.expected_block_count,
            supplied: attestation.valid_slices.len(),
        });
    }

    let invalid = attestation
        .valid_slices
        .iter()
        .filter(|valid| !**valid)
        .count() as u32;
    if attestation.missing_slice_count != invalid {
        return Err(ExternalCarryError::DamagedCountMismatch {
            file_id,
            declared: attestation.missing_slice_count,
            actual: invalid,
        });
    }

    match &attestation.status {
        FileStatus::Renamed(_) => return Err(ExternalCarryError::RelocatedFile { file_id }),
        FileStatus::Missing => {
            // A missing file has no bytes to have read and no file to have
            // fingerprinted. Either claim contradicts the status.
            if fingerprint.is_some() || attestation.valid_slices.iter().any(|valid| *valid) {
                return Err(ExternalCarryError::MissingFileWithContent { file_id });
            }
            return Ok(());
        }
        FileStatus::Complete => {
            if invalid != 0 {
                return Err(ExternalCarryError::IncompleteCompleteFile { file_id });
            }
        }
        FileStatus::Damaged(declared) => {
            if *declared != invalid {
                return Err(ExternalCarryError::DamagedCountMismatch {
                    file_id,
                    declared: *declared,
                    actual: invalid,
                });
            }
        }
    }

    if fingerprint.is_none() {
        return Err(ExternalCarryError::UnfingerprintedFile { file_id });
    }
    Ok(())
}

/// Whether this file's carried block locations amount to a whole-file
/// canonical source, using the caller's fingerprinted length in place of the
/// `stat` a scan would do.
///
/// This mirrors `RepairState::file_has_canonical_block_layout` exactly, with
/// the one substitution the external form requires: the length comes from the
/// fingerprint the caller captured when it read the file, not from a fresh
/// `stat`, for the same reason the snapshot does. A length read now would
/// describe a file that may already have moved on.
fn external_carry_layout_is_complete(
    state: &RepairState,
    file_index: usize,
    fingerprinted_length: u64,
) -> bool {
    let file = &state.files[file_index];
    if !file.target_exists {
        return false;
    }
    if file.block_count == 0 {
        return file.length == 0 && fingerprinted_length == 0;
    }
    if fingerprinted_length != file.length {
        return false;
    }
    (0..file.block_count).all(|local| {
        let block = &state.blocks[file.first_block + local];
        block.location.as_ref().is_some_and(|location| {
            location.kind == BlockLocationKind::Canonical
                && location.source.is_path(&file.safe_path)
                && location.offset == local as u64 * state.set.slice_size
                && location.len == block.expected_len
        })
    })
}

/// Which seeded slice verdicts an analysis pass is permitted to take on trust
/// instead of re-reading, and the proof each one must still carry.
///
/// This is empty unless the host opted in with
/// [`crate::Par2RepairSessionOptions::trust_seeded_evidence_for_scan`], and an
/// empty plan leaves the scan byte-for-byte what it always was. Only path-keyed
/// evidence can appear here: an access-backed session never scans a directory,
/// so it has no reads to skip, and committed whole-file evidence removes its
/// file from the candidate list outright.
///
/// The fingerprint is captured per slice verdict rather than per file, because
/// verdicts admitted at different moments describe different states of the same
/// path. A host that feeds verdicts while the file is still growing will find
/// most of them refused at scan time; that is the honest answer, not a defect.
#[derive(Debug, Default)]
pub(crate) struct EvidenceScanTrust {
    files: HashMap<FileId, EvidenceTrustEntry>,
}

#[derive(Debug)]
struct EvidenceTrustEntry {
    /// The path the evidence named. A skip is offered only when the candidate
    /// being scanned is this exact path.
    path: PathBuf,
    /// Set once a second path is named for the same PAR2 file, and never
    /// cleared. That is not a picture this can reason about, so the whole
    /// entry stops offering anything — latched rather than recomputed, because
    /// verdicts arrive in map order and a rule that only cleared what it had
    /// seen so far would depend on that order.
    conflicted: bool,
    /// Local slice index and the fingerprint the path carried when that
    /// verdict was admitted.
    slices: Vec<(u32, FileStatFingerprint)>,
}

impl EvidenceScanTrust {
    pub(crate) fn record(
        &mut self,
        file_id: FileId,
        path: &Path,
        slice_index: u32,
        fingerprint: FileStatFingerprint,
    ) {
        let entry = self
            .files
            .entry(file_id)
            .or_insert_with(|| EvidenceTrustEntry {
                path: path.to_path_buf(),
                conflicted: false,
                slices: Vec::new(),
            });
        if entry.conflicted {
            return;
        }
        if entry.path != path {
            entry.conflicted = true;
            entry.slices.clear();
            return;
        }
        entry.slices.push((slice_index, fingerprint));
    }

    /// Slice verdicts admitted for `path` under `file_id`, or `None` when this
    /// plan says nothing about that pair.
    fn slices_for(&self, file_id: &FileId, path: &Path) -> Option<&[(u32, FileStatFingerprint)]> {
        let entry = self.files.get(file_id)?;
        (!entry.conflicted && entry.path == path).then_some(entry.slices.as_slice())
    }
}

/// Per-local-slice bitmap of what one candidate's scan may skip.
///
/// A local index is set only when *all* of the following hold: seeded evidence
/// named it for exactly this path, the path still carries the fingerprint that
/// verdict was admitted against, the block is a full slice, and the scan state
/// already holds a location for it at this path and this offset. The last
/// condition is what makes a skip incapable of losing a block — the scan
/// declines to look for a block that is already placed.
///
/// The converse is deliberate and is the whole meaning of opting in: if the
/// host's verdict was wrong about bytes that never moved, the fingerprint still
/// matches, the range is still skipped, and the wrong verdict stands. Nothing
/// here re-derives it. That is why the evidence admission bar
/// ([`crate::SliceEvidence::may_seed_repair_input`]) exists on the way in.
fn evidence_settled_slices(
    trust: &EvidenceScanTrust,
    target_file: &SourceFileEntry,
    path: &Path,
    blocks: &ScanBlockState<'_>,
    slice_size: u64,
) -> Vec<bool> {
    let Some(slices) = trust.slices_for(&target_file.file_id, path) else {
        return Vec::new();
    };
    if slices.is_empty() || slice_size == 0 {
        return Vec::new();
    }
    // The fresh stat happens here, immediately before this candidate is read,
    // and nowhere earlier: a fingerprint checked at plan-build time would leave
    // a window in which the file could change before the scan reached it.
    let Some(current) = stat_fingerprint(path) else {
        return Vec::new();
    };

    let mut settled = vec![false; target_file.block_count];
    for (local_index, fingerprint) in slices {
        if *fingerprint != current {
            continue;
        }
        let local = *local_index as usize;
        if local >= target_file.block_count {
            continue;
        }
        let block_index = target_file.first_block + local;
        let block = blocks.block(block_index);
        if block.file_id != target_file.file_id || block.expected_len != slice_size {
            continue;
        }
        let offset = local as u64 * slice_size;
        if blocks.location(block_index).is_some_and(|location| {
            location.offset == offset
                && location.len == block.expected_len
                && location.source.is_path(path)
        }) {
            settled[local] = true;
        }
    }
    settled
}

/// Coalesce settled local slice indices into byte ranges.
///
/// Consecutive settled slices become one range, so a file whose damage is a
/// single burst costs one seek rather than one per intact slice. A slice whose
/// range would run past the file on disk is dropped: the file is shorter than
/// the set describes, which is a discrepancy for the scan to find, not one to
/// seek over.
fn settled_byte_runs(settled: &[bool], slice_size: usize, len: usize) -> Vec<(usize, usize)> {
    let mut runs: Vec<(usize, usize)> = Vec::new();
    if slice_size == 0 {
        return runs;
    }
    for (local, _) in settled.iter().enumerate().filter(|(_, set)| **set) {
        let start = local * slice_size;
        let end = start + slice_size;
        if end > len {
            continue;
        }
        match runs.last_mut() {
            Some(last) if last.1 == start => last.1 = end,
            _ => runs.push((start, end)),
        }
    }
    runs
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FileScanMode {
    Complete,
    OrderedCanonical,
    OrderedCanonicalParallel,
    RollingGeneric,
}

impl FileScanMode {
    fn as_str(self) -> &'static str {
        match self {
            Self::Complete => "complete",
            Self::OrderedCanonical => "ordered_canonical",
            Self::OrderedCanonicalParallel => "ordered_canonical_parallel",
            Self::RollingGeneric => "rolling_generic",
        }
    }
}

#[derive(Debug, Clone, Copy)]
struct FileScanStats {
    mode: FileScanMode,
    bytes_scanned: u64,
    windows_stepped: u64,
    jumps_taken: u64,
    max_consecutive_steps: u64,
    /// Bytes this file's scan seeked past instead of reading, because seeded
    /// evidence already accounted for them. Always zero without the opt-in.
    bytes_skipped_by_evidence: u64,
    slices_settled_by_evidence: u32,
}

impl FileScanStats {
    fn new(mode: FileScanMode, bytes_scanned: u64) -> Self {
        Self {
            mode,
            bytes_scanned,
            windows_stepped: 0,
            jumps_taken: 0,
            max_consecutive_steps: 0,
            bytes_skipped_by_evidence: 0,
            slices_settled_by_evidence: 0,
        }
    }
}

/// Accounting for the exhaustive short-block relocation search.
///
/// That search is the one scan phase whose cost is not proportional to the
/// candidate it was asked about: it re-reads a whole candidate once per
/// distinct still-open short length. It used to update no counter at all, so
/// a quadratic blow-up surfaced in the logs as a slow file scan reporting zero
/// windows stepped. These fields exist so it can never hide again.
#[derive(Debug, Default, Clone, Copy)]
struct ShortRelocationStats {
    windows_stepped: u64,
    bytes_read: u64,
    blocks_placed: u64,
}

impl ShortRelocationStats {
    fn accumulate(&mut self, other: &Self) {
        self.windows_stepped = self.windows_stepped.saturating_add(other.windows_stepped);
        self.bytes_read = self.bytes_read.saturating_add(other.bytes_read);
        self.blocks_placed = self.blocks_placed.saturating_add(other.blocks_placed);
    }
}

#[derive(Debug, Clone)]
struct ScanCandidate {
    path: PathBuf,
    kind: BlockLocationKind,
}

/// One candidate the deferred relocation search may re-read: a candidate that
/// reached the block-scan phase, so its bytes were never claimed wholesale by
/// a complete-file match.
#[derive(Debug, Clone)]
struct ShortRelocationTarget {
    path: PathBuf,
    kind: BlockLocationKind,
    len: u64,
}

#[derive(Debug, Clone)]
struct CompleteFileMatch {
    file_index: usize,
    location: BlockLocation,
}

type CompleteScanMatches = (Vec<CompleteFileMatch>, Vec<(usize, BlockLocation)>);

struct ScanBlockState<'a> {
    blocks: &'a [SourceBlock],
    locations: Vec<Option<BlockLocation>>,
}

impl<'a> ScanBlockState<'a> {
    fn new(blocks: &'a [SourceBlock]) -> Self {
        Self {
            blocks,
            locations: blocks
                .iter()
                .map(|block| block.location.clone())
                .collect::<Vec<_>>(),
        }
    }

    fn block(&self, block_index: usize) -> &SourceBlock {
        &self.blocks[block_index]
    }

    fn baseline(&self) -> &'a [SourceBlock] {
        self.blocks
    }

    fn location(&self, block_index: usize) -> Option<&BlockLocation> {
        self.locations[block_index].as_ref()
    }

    fn record_location(&mut self, block_index: usize, location: BlockLocation) {
        let replace = self.locations[block_index].as_ref().is_none_or(|existing| {
            location.kind < existing.kind
                || (location.kind == existing.kind && location.source < existing.source)
        });
        if replace {
            self.locations[block_index] = Some(location);
        }
    }

    fn changed_locations(&self) -> Vec<(usize, BlockLocation)> {
        self.locations
            .iter()
            .zip(self.blocks.iter())
            .enumerate()
            .filter_map(|(idx, (location, block))| {
                (location != &block.location)
                    .then(|| location.clone().map(|location| (idx, location)))
                    .flatten()
            })
            .collect()
    }

    #[cfg(test)]
    fn apply_to_blocks(self, blocks: &mut [SourceBlock]) {
        for (block, location) in blocks.iter_mut().zip(self.locations) {
            block.location = location;
        }
    }
}

#[derive(Debug)]
struct CandidateScanResult {
    path: PathBuf,
    kind: BlockLocationKind,
    files_scanned: u32,
    files_skipped: u32,
    /// The candidate's length — the bytes this scan was asked to account for.
    /// What it actually read is this minus [`Self::bytes_skipped_by_evidence`];
    /// the two are separate because the deferred short-block relocation search
    /// needs the file length, not the read total.
    bytes_scanned: u64,
    bytes_skipped_by_evidence: u64,
    slices_settled_by_evidence: u32,
    stats: Option<FileScanStats>,
    elapsed: Duration,
    complete_files: Vec<CompleteFileMatch>,
    block_locations: Vec<(usize, BlockLocation)>,
}

impl CandidateScanResult {
    fn ignored(path: &Path, kind: BlockLocationKind) -> Self {
        Self {
            path: path.to_path_buf(),
            kind,
            files_scanned: 0,
            files_skipped: 0,
            bytes_scanned: 0,
            bytes_skipped_by_evidence: 0,
            slices_settled_by_evidence: 0,
            stats: None,
            elapsed: Duration::ZERO,
            complete_files: Vec::new(),
            block_locations: Vec::new(),
        }
    }

    fn skipped(path: &Path, kind: BlockLocationKind) -> Self {
        Self {
            files_skipped: 1,
            ..Self::ignored(path, kind)
        }
    }

    /// The relocation target this candidate offers, if any.
    ///
    /// Only a candidate that actually reached the block-scan phase qualifies.
    /// [`FileScanMode::Complete`] marks the early exits — a whole-file hash
    /// match, or a rename-only pass — which never looked at short blocks
    /// before this change either.
    fn short_relocation_target(&self) -> Option<ShortRelocationTarget> {
        let stats = self.stats?;
        (stats.mode != FileScanMode::Complete && self.bytes_scanned > 0).then(|| {
            ShortRelocationTarget {
                path: self.path.clone(),
                kind: self.kind,
                len: self.bytes_scanned,
            }
        })
    }
}

#[derive(Debug, Clone)]
pub struct Par2RepairOutcome {
    pub status: Par2RepairStatus,
    pub files_complete: u32,
    pub files_renamed: u32,
    pub files_damaged: u32,
    pub files_missing: u32,
    pub available_blocks: u32,
    pub missing_blocks: u32,
    pub recovery_blocks_available: u32,
    pub recovery_blocks_used: u32,
    pub bytes_copied: u64,
    pub bytes_reconstructed: u64,
    pub packets: PacketDiagnostics,
    pub scan: ScanDiagnostics,
    pub carry: CarryDiagnostics,
    pub verification: VerificationResult,
}

#[derive(Clone)]
pub struct Par2RepairerOptions {
    pub base_dir: PathBuf,
    pub file_set: Option<Par2FileSet>,
    pub par2_paths: Vec<PathBuf>,
    pub recovery_paths: Vec<PathBuf>,
    pub extra_paths: Vec<PathBuf>,
    pub repair: bool,
    /// Working-memory budget applied to scanning and repair. Parallel ordered
    /// scans fall back to the bounded serial scanner when their fixed
    /// bookkeeping cannot fit; `None` uses the crate default.
    pub memory_limit: Option<usize>,
    /// Resource bounds for the packet-inventory load, shared across every
    /// `.par2` input of the pass. The defaults come from what the PAR2 format
    /// can describe; see [`PacketScanLimits`].
    pub packet_scan_limits: PacketScanLimits,
    pub rename_only: bool,
    pub purge: bool,
    pub scan_skip_data: bool,
    pub scan_skip_leeway: u64,
    pub cancel: Option<CancellationToken>,
    pub progress: Option<ProgressCallback>,
    /// Scan state from a prior pass over the same set (see
    /// [`Par2Repairer::verify_or_repair_carrying`]). Applied only when the
    /// set matches and every observed file's stat snapshot still matches;
    /// otherwise this pass scans normally. A mutating repair additionally
    /// re-checks every source it will read immediately before mutating, and
    /// consumes the carry only when all of them still match; every other
    /// accepted-carry result is retried from a fresh content scan before
    /// reporting. Must come from a pass with the same `base_dir`/
    /// `extra_paths`/`scan_skip_*` configuration.
    pub scan_carry: Option<Arc<ScanCarry>>,
}

impl Par2RepairerOptions {
    pub fn new(base_dir: PathBuf, par2_paths: Vec<PathBuf>) -> Self {
        Self {
            base_dir,
            file_set: None,
            par2_paths,
            recovery_paths: Vec::new(),
            extra_paths: Vec::new(),
            repair: true,
            memory_limit: Some(DEFAULT_REPAIR_MEMORY_LIMIT),
            packet_scan_limits: PacketScanLimits::default(),
            rename_only: false,
            purge: false,
            scan_skip_data: false,
            scan_skip_leeway: ORDERED_SCAN_DEFAULT_SKIP_LEEWAY,
            cancel: None,
            progress: None,
            scan_carry: None,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum BlockLocationKind {
    Canonical,
    Renamed,
    Extra,
}

/// Where the bytes behind a verified source block actually live.
///
/// PAR2 sources are not always files. A [`SourceLocation::Path`] is read with
/// `std::fs`; a [`SourceLocation::Access`] names its source only by PAR2
/// [`FileId`] and is read exclusively through the session's
/// [`FileAccess`] handle. The distinction is a type,
/// not a convention: an access-backed source carries no path, so no code path
/// can accidentally open it from disk.
///
/// ```
/// use par2_rs::{BlockLocation, BlockLocationKind, FileId, SourceLocation};
/// use std::path::PathBuf;
///
/// let on_disk = BlockLocation {
///     source: SourceLocation::Path(PathBuf::from("/downloads/release.r00")),
///     offset: 0,
///     len: 4096,
///     kind: BlockLocationKind::Canonical,
/// };
/// assert!(on_disk.path().is_some());
/// assert!(on_disk.file_id().is_none());
///
/// let virtual_volume = BlockLocation {
///     source: SourceLocation::Access(FileId::from_bytes([7; 16])),
///     offset: 0,
///     len: 4096,
///     kind: BlockLocationKind::Canonical,
/// };
/// assert!(virtual_volume.path().is_none());
/// assert_eq!(virtual_volume.file_id(), Some(FileId::from_bytes([7; 16])));
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SourceLocation {
    /// A real file on disk, addressed by path.
    Path(PathBuf),
    /// A source served by a [`FileAccess`] handle,
    /// addressed only by its PAR2 file identifier.
    Access(FileId),
}

impl SourceLocation {
    /// The backing path, or `None` for an access-backed source.
    pub fn path(&self) -> Option<&Path> {
        match self {
            Self::Path(path) => Some(path.as_path()),
            Self::Access(_) => None,
        }
    }

    /// The backing PAR2 file identifier, or `None` for a path-backed source.
    pub fn file_id(&self) -> Option<FileId> {
        match self {
            Self::Path(_) => None,
            Self::Access(file_id) => Some(*file_id),
        }
    }

    /// Whether this source is the file at `path`. Access-backed sources are
    /// never at any path, so this is always `false` for them.
    pub fn is_path(&self, path: &Path) -> bool {
        matches!(self, Self::Path(owned) if owned == path)
    }

    /// Whether this source is served through a
    /// [`FileAccess`] handle.
    pub fn is_access(&self) -> bool {
        matches!(self, Self::Access(_))
    }

    /// Whether this source *is* the described file rather than a copy of it
    /// found elsewhere. For a path that means the file sits at its canonical
    /// location; for an access-backed source it means the handle was asked for
    /// this very file identifier, which is the only thing it can be asked for.
    fn is_canonical_for(&self, file: &SourceFileEntry) -> bool {
        match self {
            Self::Path(path) => *path == file.safe_path,
            Self::Access(file_id) => *file_id == file.file_id,
        }
    }

    /// Heap bytes owned by this location. A [`FileId`] lives inline in the
    /// enum, so an access-backed source owns nothing on the heap; a path owns
    /// its own bytes.
    pub(crate) fn heap_bytes(&self) -> usize {
        match self {
            Self::Path(path) => path.as_os_str().len(),
            Self::Access(_) => 0,
        }
    }
}

/// One resolved span of source data: where it lives, and how much of it
/// belongs to the block or file that resolved to it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockLocation {
    /// Where the bytes are. Scanning only ever produces
    /// [`SourceLocation::Path`]; evidence fed to a session over a
    /// [`FileAccess`] handle produces [`SourceLocation::Access`].
    pub source: SourceLocation,
    /// Byte offset of this span within `source`.
    pub offset: u64,
    /// Length of this span.
    pub len: u64,
    /// How this location was matched to the set.
    pub kind: BlockLocationKind,
}

impl BlockLocation {
    /// The backing path, or `None` for an access-backed source.
    pub fn path(&self) -> Option<&Path> {
        self.source.path()
    }

    /// The backing PAR2 file identifier, or `None` for a path-backed source.
    pub fn file_id(&self) -> Option<FileId> {
        self.source.file_id()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct BlockCopyRange {
    src: SourceLocation,
    src_offset: u64,
    dst: PathBuf,
    dst_offset: u64,
    len: u64,
}

/// Destination for an intact source block while reconstruction is active.
/// The source bytes are copied from the same buffer that is submitted to the
/// Reed-Solomon controller so copy and reconstruction observe identical data.
type ReconstructionCopyTargets = HashMap<(FileId, u32), BlockCopyRange>;

impl BlockCopyRange {
    fn can_extend(&self, next: &Self) -> bool {
        self.src == next.src
            && self.dst == next.dst
            && self.src_offset.checked_add(self.len) == Some(next.src_offset)
            && self.dst_offset.checked_add(self.len) == Some(next.dst_offset)
    }

    fn extend(&mut self, next: &Self) {
        self.len += next.len;
    }
}

#[derive(Debug, Clone)]
pub struct SourceBlock {
    pub global_index: usize,
    pub file_id: FileId,
    pub local_index: u32,
    pub expected_len: u64,
    pub checksum: SliceChecksum,
    pub location: Option<BlockLocation>,
}

#[derive(Debug, Clone)]
pub struct SourceFileEntry {
    pub file_id: FileId,
    pub par2_name: String,
    pub safe_path: PathBuf,
    pub safe_name: String,
    pub length: u64,
    pub hash_full: [u8; 16],
    pub hash_16k: [u8; 16],
    pub recoverable: bool,
    pub first_block: usize,
    pub expected_block_count: usize,
    pub block_count: usize,
    pub target_exists: bool,
    pub complete_location: Option<BlockLocation>,
    pub non_canonical_complete_source_count: u32,
}

#[derive(Debug, Clone)]
pub struct PacketInventory {
    pub set: Par2FileSet,
    pub diagnostics: PacketDiagnostics,
    pub purge_paths: Vec<PathBuf>,
}

#[derive(Debug, Clone)]
struct PacketInputPath {
    path: PathBuf,
    optional: bool,
    purgeable: bool,
}

pub struct Par2Repairer {
    options: Par2RepairerOptions,
}

struct RepairPassSuccess {
    outcome: Par2RepairOutcome,
    carry: Option<Arc<ScanCarry>>,
    /// Set when a carried pass refused, before installing anything, to mutate
    /// on the carried analysis. The reason names what the pre-mutation
    /// fingerprint gate — or a validated read — saw, and the caller retries
    /// the whole pass from a fresh scan.
    carry_gate_rejection: Option<CarryRetryReason>,
}

impl RepairPassSuccess {
    fn new(outcome: Par2RepairOutcome, carry: Option<Arc<ScanCarry>>) -> Self {
        Self {
            outcome,
            carry,
            carry_gate_rejection: None,
        }
    }
}

enum RepairPassResult {
    Success(RepairPassSuccess),
    PostRepairVerificationFailed {
        reason: String,
        carry: CarryDiagnostics,
    },
}

impl Par2Repairer {
    pub fn new(options: Par2RepairerOptions) -> Self {
        Self { options }
    }

    pub fn verify_or_repair(&self) -> Result<Par2RepairOutcome> {
        Ok(self.verify_or_repair_inner(false)?.0)
    }

    /// Like [`Self::verify_or_repair`], additionally returning this pass's
    /// scan state for reuse by a later pass over the same set
    /// ([`Par2RepairerOptions::scan_carry`]). A repair consumes such a carry
    /// when every source it will read still matches the fingerprint the scan
    /// captured; carried results that do not mutate stay speculative and are
    /// retried from a fresh content scan before they are reported.
    pub fn verify_or_repair_carrying(&self) -> Result<(Par2RepairOutcome, Option<Arc<ScanCarry>>)> {
        self.verify_or_repair_inner(true)
    }

    fn verify_or_repair_inner(
        &self,
        want_carry: bool,
    ) -> Result<(Par2RepairOutcome, Option<Arc<ScanCarry>>)> {
        // No-op on native. On `wasm32-wasip1-threads` this is what lets the
        // scan fan-out, the GF elimination, and the streamed repair
        // controller's `rayon::current_num_threads()` worker sizing see more
        // than one worker; the width comes from the same process-stable
        // embedder-supplied value creation uses.
        reedsolomon_rs::threading::ensure_pool(crate::create::configured_create_threads_for_pool);
        // This entry point carries repair intent: pages the scan verifies are
        // read again by staging and by copy-only repair, which never reaches
        // `execute_repair_with_options`' own deferral.
        let _cache_retention = crate::file_cache::CacheEvictionDeferral::acquire();
        match self.verify_or_repair_pass(want_carry)? {
            RepairPassResult::Success(success) => self.finish_or_retry(success, want_carry),
            RepairPassResult::PostRepairVerificationFailed { reason, carry } => {
                if carry.carry_applied {
                    debug!(
                        retry_reason = ?CarryRetryReason::PostRepairVerificationFailed,
                        "carried PAR2 repair failed post-verification; retrying from a fresh scan"
                    );
                    return self
                        .retry_fresh(want_carry, CarryRetryReason::PostRepairVerificationFailed);
                }
                Err(Par2Error::ReedSolomonError { reason })
            }
        }
    }

    fn finish_or_retry(
        &self,
        success: RepairPassSuccess,
        want_carry: bool,
    ) -> Result<(Par2RepairOutcome, Option<Arc<ScanCarry>>)> {
        let status = success.outcome.status;
        if let Some(reason) = success.carry_gate_rejection {
            debug!(
                ?status,
                retry_reason = ?reason,
                "carried PAR2 pass could not prove its repair inputs; retrying from a fresh scan before mutation"
            );
            return self.retry_fresh(want_carry, reason);
        }
        // A carried pass that reached a mutating repair and proved every
        // input still matches its scan-time fingerprint has already repaired
        // on the carried analysis; re-running it from a fresh scan would only
        // read the whole set a second time to reach the same place.
        if success.outcome.carry.carry_applied
            && self.options.repair
            && !success.outcome.carry.carry_consumed_for_repair
        {
            debug!(
                ?status,
                "carried PAR2 pass reached a repair request; retrying from a fresh scan before mutation"
            );
            return self.retry_fresh(want_carry, CarryRetryReason::RepairRequested);
        }
        if success.outcome.carry.carry_applied && Self::is_terminal_non_repair_status(status) {
            debug!(
                ?status,
                "carried PAR2 pass returned terminal non-repair status; retrying from a fresh scan"
            );
            return self.retry_fresh(want_carry, CarryRetryReason::TerminalStatus(status));
        }
        Ok((success.outcome, success.carry))
    }

    fn retry_fresh(
        &self,
        want_carry: bool,
        retry_reason: CarryRetryReason,
    ) -> Result<(Par2RepairOutcome, Option<Arc<ScanCarry>>)> {
        let mut options = self.options.clone();
        options.scan_carry = None;
        match Par2Repairer::new(options).verify_or_repair_pass(want_carry)? {
            RepairPassResult::Success(mut success) => {
                success.outcome.carry = CarryDiagnostics {
                    carry_attempted: true,
                    carry_applied: true,
                    carry_retried_fresh: true,
                    carry_retry_reason: Some(retry_reason),
                    // This outcome came from a fresh scan of the tree, not
                    // from the carried analysis, whatever the retried pass
                    // decided about its own (absent) carry.
                    carry_consumed_for_repair: false,
                };
                Ok((success.outcome, success.carry))
            }
            RepairPassResult::PostRepairVerificationFailed { reason, .. } => {
                Err(Par2Error::ReedSolomonError { reason })
            }
        }
    }

    fn is_terminal_non_repair_status(status: Par2RepairStatus) -> bool {
        matches!(
            status,
            Par2RepairStatus::Verified
                | Par2RepairStatus::RepairPossible
                | Par2RepairStatus::Insufficient
                | Par2RepairStatus::ResourceLimited
        )
    }

    fn with_carry_diagnostics(
        mut outcome: Par2RepairOutcome,
        carry: &CarryDiagnostics,
    ) -> Par2RepairOutcome {
        outcome.carry = carry.clone();
        outcome
    }

    fn verify_or_repair_pass(&self, want_carry: bool) -> Result<RepairPassResult> {
        let PacketInventory {
            set,
            diagnostics,
            purge_paths,
        } = self.load_inventory()?;
        let mut state = RepairState::from_set(&self.options.base_dir, set)?;
        let mut packet_diagnostics = diagnostics;

        packet_diagnostics.discarded_recovery_blocks = state.discarded_recovery_blocks;
        packet_diagnostics.inconsistent_packets = state.inconsistent_packets;

        let mut carry_diagnostics = CarryDiagnostics {
            carry_attempted: self.options.scan_carry.is_some(),
            ..CarryDiagnostics::default()
        };
        let scan = match self.options.scan_carry.as_deref().and_then(|carry| {
            let diagnostics = state.try_apply_carry(carry);
            if diagnostics.is_some() {
                carry_diagnostics.carry_applied = true;
            }
            diagnostics
        }) {
            Some(mut diagnostics) => {
                // The counters describe the pass that produced them, not this
                // one. Say so, so a host reading a single outcome can tell a
                // scan that happened here from one it inherited.
                diagnostics.carried = true;
                diagnostics
            }
            None => state.scan(&self.options)?,
        };
        let mut verification = state.verification_result();
        if let Some(reason) = repair_matrix_resource_limit_reason(
            &state.set,
            &verification,
            self.options.memory_limit,
        )? {
            verification.repairable = Repairability::ResourceLimited { reason };
        }
        let carry = want_carry.then(|| Arc::new(state.scan_carry(&scan)));

        if carry_diagnostics.carry_applied {
            let status =
                if verification.total_missing_blocks == 0 && state.files_are_canonical_complete() {
                    Par2RepairStatus::Verified
                } else {
                    match &verification.repairable {
                        Repairability::NotNeeded => Par2RepairStatus::Verified,
                        Repairability::Repairable { .. } => Par2RepairStatus::RepairPossible,
                        Repairability::Insufficient { .. } => Par2RepairStatus::Insufficient,
                        Repairability::ResourceLimited { .. } => Par2RepairStatus::ResourceLimited,
                    }
                };
            // Only a request that will actually rewrite the tree can consume
            // the carried analysis. Every other carried result — a clean
            // verify (which may still purge), an insufficient or
            // resource-limited verdict, and any verify-only pass — is still
            // speculative and is re-established from a real scan before it is
            // reported, exactly as before.
            let mutation_requested =
                self.options.repair && status == Par2RepairStatus::RepairPossible;
            let gate = mutation_requested
                .then(|| {
                    let applied = self
                        .options
                        .scan_carry
                        .as_deref()
                        .expect("carry_applied implies a supplied carry");
                    state.carry_repair_inputs_unchanged(applied)
                })
                .transpose();
            match gate {
                // Not a mutating request: report the carried result and let
                // the caller re-establish it from a fresh scan.
                Ok(None) => {
                    return Ok(RepairPassResult::Success(RepairPassSuccess::new(
                        Self::with_carry_diagnostics(
                            state.outcome(status, 0, 0, packet_diagnostics, scan, verification),
                            &carry_diagnostics,
                        ),
                        carry,
                    )));
                }
                // Every input the repair would read is still the file the scan
                // read. Fall through and repair on the carried analysis.
                Ok(Some(())) => {
                    carry_diagnostics.carry_consumed_for_repair = true;
                }
                Err(reason) => {
                    debug!(
                        ?status,
                        ?reason,
                        "carried PAR2 repair inputs no longer match their scan-time fingerprints"
                    );
                    return Ok(RepairPassResult::Success(RepairPassSuccess {
                        outcome: Self::with_carry_diagnostics(
                            state.outcome(status, 0, 0, packet_diagnostics, scan, verification),
                            &carry_diagnostics,
                        ),
                        carry,
                        carry_gate_rejection: Some(reason),
                    }));
                }
            }
        }

        if verification.total_missing_blocks == 0 && state.files_are_canonical_complete() {
            if self.options.purge {
                purge_files_best_effort(&purge_paths);
            }
            return Ok(RepairPassResult::Success(RepairPassSuccess::new(
                Self::with_carry_diagnostics(
                    state.outcome(
                        Par2RepairStatus::Verified,
                        0,
                        0,
                        packet_diagnostics,
                        scan,
                        verification,
                    ),
                    &carry_diagnostics,
                ),
                carry,
            )));
        }

        if !self.options.repair {
            let status = match &verification.repairable {
                Repairability::NotNeeded => Par2RepairStatus::Verified,
                Repairability::Repairable { .. } => Par2RepairStatus::RepairPossible,
                Repairability::Insufficient { .. } => Par2RepairStatus::Insufficient,
                Repairability::ResourceLimited { .. } => Par2RepairStatus::ResourceLimited,
            };
            return Ok(RepairPassResult::Success(RepairPassSuccess::new(
                Self::with_carry_diagnostics(
                    state.outcome(status, 0, 0, packet_diagnostics, scan, verification),
                    &carry_diagnostics,
                ),
                carry,
            )));
        }

        if matches!(
            &verification.repairable,
            Repairability::Insufficient { .. } | Repairability::ResourceLimited { .. }
        ) {
            let status = match &verification.repairable {
                Repairability::ResourceLimited { .. } => Par2RepairStatus::ResourceLimited,
                _ => Par2RepairStatus::Insufficient,
            };
            return Ok(RepairPassResult::Success(RepairPassSuccess::new(
                Self::with_carry_diagnostics(
                    state.outcome(status, 0, 0, packet_diagnostics, scan, verification),
                    &carry_diagnostics,
                ),
                carry,
            )));
        }

        let repair = if carry_diagnostics.carry_consumed_for_repair {
            // The analysis feeding this repair was taken before the caller's
            // own gap between passes, so the validated read path is used: each
            // source slice is checked against its IFSC checksum on the way
            // into staging and into the Reed-Solomon input stream, and every
            // path is re-stat'd as it is opened. That covers the one drift a
            // stat fingerprint cannot see — a same-length rewrite that also
            // restored the original mtime in place — by catching it on the
            // bytes instead of on the metadata. Nothing has been installed at
            // this point, so a caught change simply falls back to a fresh
            // scan.
            match state.repair_validated(&self.options, &verification) {
                Ok(repair) => repair,
                Err(error) if is_source_changed_error(&error) => {
                    debug!(
                        %error,
                        "carried PAR2 repair read a changed source; retrying from a fresh scan"
                    );
                    return Ok(RepairPassResult::Success(RepairPassSuccess {
                        outcome: Self::with_carry_diagnostics(
                            state.outcome(
                                Par2RepairStatus::RepairPossible,
                                0,
                                0,
                                packet_diagnostics,
                                scan,
                                verification,
                            ),
                            &carry_diagnostics,
                        ),
                        carry,
                        carry_gate_rejection: Some(CarryRetryReason::RepairInputChanged),
                    }));
                }
                Err(error) => return Err(error),
            }
        } else {
            state.repair(&self.options, &verification)?
        };
        let repaired_access = RepairVerificationAccess::new(
            &state.files,
            &repair.install_dir,
            &repair.staged_file_ids,
            state.source_access.clone(),
        );
        // Fresh repair passes read staged files back and verify them
        // slice-by-slice against IFSC before installation.
        let staged_ids: Vec<FileId> = state
            .set
            .recovery_file_ids
            .iter()
            .filter(|file_id| repair.staged_file_ids.contains(file_id))
            .copied()
            .collect();
        let post_staged =
            verify::verify_repaired_file_ids_parallel(&state.set, &repaired_access, &staged_ids);
        let post = verify::merge_verification_results(&state.set, &verification, post_staged);
        if post.total_missing_blocks > 0
            || !post
                .files
                .iter()
                .all(|file| matches!(file.status, FileStatus::Complete))
        {
            let _ = fs::remove_dir_all(&repair.install_dir);
            return Ok(RepairPassResult::PostRepairVerificationFailed {
                reason: format!(
                    "post-repair verification failed: {} blocks remain damaged",
                    post.total_missing_blocks
                ),
                carry: carry_diagnostics,
            });
        }

        if let Err(error) = state.install_repaired_files(&repair, &self.options) {
            let _ = fs::remove_dir_all(&repair.install_dir);
            return Err(error);
        }
        let _ = fs::remove_dir_all(&repair.install_dir);
        if self.options.purge {
            purge_files_best_effort(&purge_paths);
        }

        Ok(RepairPassResult::Success(RepairPassSuccess::new(
            Self::with_carry_diagnostics(
                state.outcome(
                    Par2RepairStatus::Repaired,
                    repair.bytes_copied,
                    repair.bytes_reconstructed,
                    packet_diagnostics,
                    scan,
                    post,
                ),
                &carry_diagnostics,
            ),
            carry,
        )))
    }

    pub(crate) fn load_inventory(&self) -> Result<PacketInventory> {
        self.load_inventory_with_adjacent_recovery(true)
    }

    pub(crate) fn load_inventory_without_adjacent_recovery(&self) -> Result<PacketInventory> {
        self.load_inventory_with_adjacent_recovery(false)
    }

    fn load_inventory_with_adjacent_recovery(
        &self,
        discover_adjacent_recovery: bool,
    ) -> Result<PacketInventory> {
        if let Some(set) = self.options.file_set.clone() {
            return Ok(PacketInventory {
                set,
                diagnostics: PacketDiagnostics::default(),
                purge_paths: Vec::new(),
            });
        }

        let mut paths = Vec::<PacketInputPath>::new();
        let mut seen = HashSet::<PathBuf>::new();
        let mut primary_par2_paths = Vec::new();

        for path in &self.options.par2_paths {
            if is_par2_path(path) {
                if seen.insert(path.clone()) {
                    paths.push(PacketInputPath {
                        path: path.clone(),
                        optional: false,
                        purgeable: true,
                    });
                }
                primary_par2_paths.push(path.clone());
                continue;
            }

            if let Some(primary) = discover_source_primary_par2_file(path)? {
                if seen.insert(primary.clone()) {
                    paths.push(PacketInputPath {
                        path: primary.clone(),
                        optional: false,
                        purgeable: true,
                    });
                }
                primary_par2_paths.push(primary);
                continue;
            }

            if seen.insert(path.clone()) {
                paths.push(PacketInputPath {
                    path: path.clone(),
                    optional: false,
                    purgeable: false,
                });
            }
        }

        for path in &self.options.recovery_paths {
            if is_par2_path(path) {
                if seen.insert(path.clone()) {
                    paths.push(PacketInputPath {
                        path: path.clone(),
                        optional: false,
                        purgeable: true,
                    });
                }
                continue;
            }

            if let Some(primary) = discover_source_primary_par2_file(path)? {
                if seen.insert(primary.clone()) {
                    paths.push(PacketInputPath {
                        path: primary,
                        optional: false,
                        purgeable: true,
                    });
                }
                continue;
            }

            if seen.insert(path.clone()) {
                paths.push(PacketInputPath {
                    path: path.clone(),
                    optional: false,
                    purgeable: false,
                });
            }
        }

        if discover_adjacent_recovery {
            for adjacent in discover_adjacent_par2_files(&primary_par2_paths)? {
                if seen.insert(adjacent.clone()) {
                    paths.push(PacketInputPath {
                        path: adjacent,
                        optional: true,
                        purgeable: true,
                    });
                }
            }
        }

        for path in self
            .options
            .extra_paths
            .iter()
            .filter(|path| has_par2_marker(path))
        {
            if seen.insert(path.clone()) {
                paths.push(PacketInputPath {
                    path: path.clone(),
                    optional: true,
                    purgeable: false,
                });
            }
        }

        let budget = PacketScanBudget::with_cancellation(
            self.options.packet_scan_limits,
            self.options.cancel.clone(),
        );
        let mut loader = InventoryLoader::new(&budget);

        for input in paths {
            budget.check_cancelled()?;
            loader.begin_file(input.path.clone(), input.purgeable);
            match scan_packets_from_path_bounded(&input.path, &budget, &mut loader) {
                Ok(()) => {}
                // A budget refusal or a cancellation is never softened into
                // "this optional input contributed nothing": that would hand
                // back a silently short inventory.
                Err(error @ (Par2Error::ResourceLimitExceeded { .. } | Par2Error::Cancelled)) => {
                    return Err(error);
                }
                Err(_) if input.optional => {}
                Err(error) => return Err(error),
            }
            loader.end_file(input.optional);
        }

        loader.finish()
    }
}

/// Streams scanned packets into one deduplicated inventory.
///
/// Replaces the scan-into-`Vec` / retain-per-file / move-into-`Vec<Packet>` /
/// build chain that used to hold the same packets in three places at once. A
/// packet is now filtered, deduplicated, and either absorbed or dropped at the
/// point it is parsed.
///
/// # Choosing the active recovery set
///
/// The active recovery-set ID is the one carried by the first Main packet seen
/// across the inputs, in input order — the same packet the previous two-pass
/// loader picked. Every later packet is filtered against it and a foreign
/// packet is discarded before its contents are retained.
///
/// Packets seen *before* that first Main cannot be filtered yet, so they are
/// staged, and the stage is flushed the moment the ID is known. Staging is
/// charged to the same budget as everything else, so a file that never yields a
/// Main cannot use the stage to escape the bound. Real volumes put their Main
/// packet within the first handful of packets, so the stage is normally short
/// lived and only ever holds part of the first input.
///
/// # Deliberate difference from the previous loader
///
/// The old loader dropped a whole file when the file's own first Main packet
/// disagreed with the active set. For a file whose packets all belong to that
/// foreign set — the case that actually occurs — the per-packet filter drops
/// exactly the same packets and reports exactly the same count. The two differ
/// only for a file that mixes packets from two recovery sets, where the
/// per-packet filter now keeps the packets that do belong to the active set
/// instead of discarding the file wholesale.
struct InventoryLoader<'a> {
    budget: &'a PacketScanBudget,
    builder: Par2FileSetBuilder,
    diagnostics: PacketDiagnostics,
    active_set_id: Option<RecoverySetId>,
    staged: Vec<StagedPacket>,
    files: Vec<InventoryFile>,
}

/// A packet held until the active recovery-set ID is known.
enum StagedPacket {
    /// Contents kept, charged to the budget's retained meters.
    Held {
        packet: Packet,
        set_id: RecoverySetId,
        bytes: usize,
        file: usize,
    },
    /// Contents dropped on arrival because the builder already holds this key.
    /// Only the record survives, so the packet can still be counted as work
    /// once the set filter has been applied.
    KnownDuplicate { set_id: RecoverySetId, file: usize },
}

struct InventoryFile {
    path: PathBuf,
    purgeable: bool,
    /// At least one packet from this file survived the recovery-set filter.
    contributed: bool,
    /// Packets this file yielded, whatever became of them.
    scanned: u32,
}

impl<'a> InventoryLoader<'a> {
    fn new(budget: &'a PacketScanBudget) -> Self {
        Self {
            budget,
            builder: Par2FileSetBuilder::new(),
            diagnostics: PacketDiagnostics::default(),
            active_set_id: None,
            staged: Vec::new(),
            files: Vec::new(),
        }
    }

    fn begin_file(&mut self, path: PathBuf, purgeable: bool) {
        self.files.push(InventoryFile {
            path,
            purgeable,
            contributed: false,
            scanned: 0,
        });
    }

    fn end_file(&mut self, optional: bool) {
        let file = self.files.last().expect("begin_file precedes end_file");
        if file.scanned == 0 && !optional {
            self.diagnostics.corrupt_packets += 1;
        }
    }

    /// Absorb a packet whose recovery set has already been checked.
    ///
    /// The budget is charged inside the builder, and only for what the builder
    /// actually keeps.
    fn commit(&mut self, packet: Packet, file: usize) -> Result<()> {
        self.diagnostics.packets_loaded += 1;
        self.files[file].contributed = true;
        if self.builder.add_packet_budgeted(packet, 0, self.budget)? == PacketAdmission::Duplicate {
            self.diagnostics.duplicate_packets += 1;
        }
        Ok(())
    }

    /// Move everything staged into the builder now that the active set is known.
    fn flush_staged(&mut self) -> Result<()> {
        for staged in std::mem::take(&mut self.staged) {
            self.budget.release_bytes(size_of::<StagedPacket>());
            match staged {
                StagedPacket::Held {
                    packet,
                    set_id,
                    bytes,
                    file,
                } => {
                    // Hand back the staging charge; `commit` re-charges for
                    // whatever the builder ends up keeping.
                    self.budget.release_retained(bytes);
                    if self.active_set_id.is_some_and(|active| active != set_id) {
                        self.diagnostics.conflicting_packets += 1;
                        continue;
                    }
                    self.commit(packet, file)?;
                }
                StagedPacket::KnownDuplicate { set_id, file } => {
                    if self.active_set_id.is_some_and(|active| active != set_id) {
                        self.diagnostics.conflicting_packets += 1;
                        continue;
                    }
                    self.diagnostics.packets_loaded += 1;
                    self.diagnostics.duplicate_packets += 1;
                    self.files[file].contributed = true;
                }
            }
        }
        Ok(())
    }

    fn finish(mut self) -> Result<PacketInventory> {
        // No Main packet anywhere leaves the stage unfiltered; flush it so the
        // builder can report the real reason rather than an empty set.
        self.flush_staged()?;
        self.budget.check_cancelled()?;

        let purge_paths = self
            .files
            .iter()
            .filter(|file| {
                file.purgeable
                    && (file.contributed || !file.path.exists() || is_par2_path(&file.path))
            })
            .map(|file| file.path.clone())
            .collect();

        self.budget.check_cancelled()?;
        let set = self.builder.build()?;
        Ok(PacketInventory {
            set,
            diagnostics: self.diagnostics,
            purge_paths,
        })
    }
}

impl PacketSink for InventoryLoader<'_> {
    fn accept(
        &mut self,
        packet: Packet,
        _offset: u64,
        recovery_set_id: RecoverySetId,
    ) -> Result<()> {
        let file = self.files.len() - 1;
        self.files[file].scanned += 1;

        let newly_active = match (&packet, self.active_set_id) {
            (Packet::Main(main), None) => {
                self.active_set_id = Some(main.recovery_set_id);
                true
            }
            _ => false,
        };

        if let Some(active) = self.active_set_id
            && recovery_set_id != active
        {
            self.diagnostics.conflicting_packets += 1;
            return Ok(());
        }

        if newly_active {
            // Everything staged behind this Main can now be filtered.
            self.flush_staged()?;
        }

        if self.active_set_id.is_some() {
            return self.commit(packet, file);
        }

        // Still waiting on the first Main packet. Stage the packet, unless the
        // builder already holds its key, in which case only the fact of it
        // needs to survive.
        self.budget.charge_bytes(size_of::<StagedPacket>())?;
        crate::packet::budget::reserve_fallible(&mut self.staged, 1)?;
        if self.builder.would_duplicate(&packet) {
            self.staged.push(StagedPacket::KnownDuplicate {
                set_id: recovery_set_id,
                file,
            });
            return Ok(());
        }
        let bytes = packet_retained_bytes(&packet);
        self.budget.charge_retained(bytes)?;
        self.staged.push(StagedPacket::Held {
            packet,
            set_id: recovery_set_id,
            bytes,
            file,
        });
        Ok(())
    }
}

pub(crate) struct RepairState {
    pub(crate) set: Par2FileSet,
    pub(crate) files: Vec<SourceFileEntry>,
    pub(crate) blocks: Vec<SourceBlock>,
    file_index_by_id: HashMap<FileId, usize>,
    block_index_by_file_slice: HashMap<(FileId, u32), usize>,
    hash_table: VerificationHashTable,
    /// Handle serving every [`SourceLocation::Access`] location this state
    /// holds. `None` is the ordinary filesystem-only state.
    pub(crate) source_access: Option<Arc<dyn FileAccess + Send + Sync>>,
    discarded_recovery_blocks: u32,
    inconsistent_packets: u32,
    discarded_recoverable_files: u32,
}

pub(crate) struct RepairInstall {
    pub(crate) install_dir: PathBuf,
    pub(crate) staged_file_ids: HashSet<FileId>,
    pub(crate) bytes_copied: u64,
    pub(crate) bytes_reconstructed: u64,
    pub(crate) validation_bytes: u64,
}

struct RepairStagingGuard {
    path: PathBuf,
    armed: bool,
}

impl RepairStagingGuard {
    fn new(path: PathBuf) -> Self {
        Self { path, armed: true }
    }

    fn disarm(&mut self) {
        self.armed = false;
    }
}

impl Drop for RepairStagingGuard {
    fn drop(&mut self) {
        if self.armed {
            let _ = fs::remove_dir_all(&self.path);
        }
    }
}

struct RepairExecutionAccess {
    slice_size: u64,
    repair_paths: HashMap<FileId, PathBuf>,
    source_locations: HashMap<(FileId, u32), BlockLocation>,
    source_blocks: HashMap<(FileId, u32), SourceBlock>,
    source_files: HashMap<PathBuf, File>,
    reconstruction_copy_targets: ReconstructionCopyTargets,
    staged_writers: Mutex<HashMap<FileId, File>>,
    /// Handle serving every [`SourceLocation::Access`] location. Absent when
    /// the state has no access-backed sources.
    source_access: Option<Arc<dyn FileAccess + Send + Sync>>,
    source_snapshots: Option<HashMap<PathBuf, CarriedFileStat>>,
    stream_validation: Mutex<HashMap<(FileId, u32), StreamSourceValidation>>,
    validation_bytes: AtomicU64,
}

#[derive(Default)]
struct RepairExecutionContext {
    source_access: Option<Arc<dyn FileAccess + Send + Sync>>,
    source_snapshots: Option<HashMap<PathBuf, CarriedFileStat>>,
    reconstruction_copy_targets: ReconstructionCopyTargets,
}

struct StreamSourceValidation {
    next_offset: u64,
    crc32: Option<Crc32Hasher>,
    last_stripe: Option<(u64, usize, u32)>,
    finalized: bool,
}

impl RepairExecutionAccess {
    fn new(
        install_dir: PathBuf,
        files: &[SourceFileEntry],
        blocks: &[SourceBlock],
        staged_file_ids: &HashSet<FileId>,
        slice_size: u64,
        context: RepairExecutionContext,
    ) -> io::Result<Self> {
        let RepairExecutionContext {
            source_access,
            source_snapshots,
            reconstruction_copy_targets,
        } = context;
        let repair_paths: HashMap<FileId, PathBuf> = files
            .iter()
            .filter(|file| staged_file_ids.contains(&file.file_id))
            .map(|file| (file.file_id, install_dir.join(&file.safe_name)))
            .collect();
        let source_locations: HashMap<(FileId, u32), BlockLocation> = blocks
            .iter()
            .filter_map(|block| {
                block
                    .location
                    .clone()
                    .map(|location| ((block.file_id, block.local_index), location))
            })
            .collect();
        let source_blocks = blocks
            .iter()
            .filter(|block| block.location.is_some())
            .map(|block| ((block.file_id, block.local_index), block.clone()))
            .collect();
        // Only path-backed sources get a file handle. Access-backed sources
        // are read through `source_access` and never opened from disk.
        let source_files = source_locations
            .values()
            .filter_map(|location| location.path().map(Path::to_path_buf))
            .collect::<HashSet<_>>()
            .into_iter()
            .map(|path| {
                File::open(&path)
                    .map(|file| (path.clone(), file))
                    .map_err(|_| source_changed_io(&path))
            })
            .collect::<io::Result<HashMap<_, _>>>()?;
        let staged_writers = repair_paths
            .iter()
            .map(|(file_id, path)| {
                OpenOptions::new()
                    .write(true)
                    .open(path)
                    .map(|file| (*file_id, file))
            })
            .collect::<io::Result<HashMap<_, _>>>()?;

        Ok(Self {
            slice_size,
            repair_paths,
            source_locations,
            source_blocks,
            source_files,
            reconstruction_copy_targets,
            staged_writers: Mutex::new(staged_writers),
            source_access,
            source_snapshots,
            stream_validation: Mutex::new(HashMap::new()),
            validation_bytes: AtomicU64::new(0),
        })
    }

    /// The handle serving access-backed sources. Reaching an access-backed
    /// location without one is a wiring defect, so it reports as a changed
    /// source rather than silently falling back to disk.
    fn access(&self, file_id: FileId) -> io::Result<&(dyn FileAccess + Send + Sync)> {
        self.source_access
            .as_deref()
            .ok_or_else(|| source_location_changed_io(&SourceLocation::Access(file_id)))
    }

    fn validation_bytes(&self) -> u64 {
        self.validation_bytes.load(Ordering::Relaxed)
    }

    fn ensure_source_unchanged(&self, path: &Path) -> io::Result<()> {
        let Some(snapshots) = self.source_snapshots.as_ref() else {
            return Ok(());
        };
        let Some(expected) = snapshots.get(path) else {
            return Ok(());
        };
        if stat_for_carry(path) == *expected {
            Ok(())
        } else {
            Err(source_changed_io(path))
        }
    }

    fn validate_source_chunk(
        &self,
        file_id: FileId,
        local_slice: u32,
        slice_offset: u64,
        data: &[u8],
    ) -> io::Result<()> {
        let Some(expected) = self.source_blocks.get(&(file_id, local_slice)) else {
            return Ok(());
        };
        let stripe_crc = checksum::crc32(data);
        let mut states = self
            .stream_validation
            .lock()
            .map_err(|_| io::Error::other("source validation state lock poisoned"))?;
        let state =
            states
                .entry((file_id, local_slice))
                .or_insert_with(|| StreamSourceValidation {
                    next_offset: 0,
                    crc32: Some(Crc32Hasher::new()),
                    last_stripe: None,
                    finalized: false,
                });

        if state.finalized {
            return match state.last_stripe {
                Some((start, len, crc))
                    if start == slice_offset && len == data.len() && crc == stripe_crc =>
                {
                    // GPU fallback replays only the current outer stripe.
                    // Do not advance checksum or accounting twice.
                    Ok(())
                }
                _ => Err(source_location_changed_io(
                    &self.source_locations[&(file_id, local_slice)].source,
                )),
            };
        }
        if slice_offset != state.next_offset {
            return match state.last_stripe {
                Some((start, len, crc))
                    if start == slice_offset && len == data.len() && crc == stripe_crc =>
                {
                    // GPU fallback replays only the current outer stripe.
                    // Do not advance checksum or accounting twice.
                    Ok(())
                }
                _ => Err(source_location_changed_io(
                    &self.source_locations[&(file_id, local_slice)].source,
                )),
            };
        }
        if state.next_offset.saturating_add(data.len() as u64) > expected.expected_len {
            return Err(source_location_changed_io(
                &self.source_locations[&(file_id, local_slice)].source,
            ));
        }
        state
            .crc32
            .as_mut()
            .expect("unfinalized source checksum")
            .update(data);
        self.validation_bytes
            .fetch_add(data.len() as u64, Ordering::Relaxed);
        state.last_stripe = Some((slice_offset, data.len(), stripe_crc));
        state.next_offset += data.len() as u64;
        if state.next_offset == expected.expected_len {
            let mut crc32 = state.crc32.take().expect("unfinalized source checksum");
            update_crc_zeros(
                &mut crc32,
                self.slice_size.saturating_sub(expected.expected_len),
            );
            if crc32.finalize() != expected.checksum.crc32 {
                return Err(source_location_changed_io(
                    &self.source_locations[&(file_id, local_slice)].source,
                ));
            }
            state.finalized = true;
        }
        Ok(())
    }

    fn repair_path_for(&self, file_id: &FileId) -> io::Result<&Path> {
        self.repair_paths
            .get(file_id)
            .map(PathBuf::as_path)
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "repair target not staged"))
    }

    /// Copy one source stripe into its staged destination after the source
    /// bytes have been read and, when requested, checksum-validated. Replays
    /// after a backend fallback write the same positional range again, which
    /// keeps the operation idempotent without rereading the source.
    fn copy_reconstruction_chunk(
        &self,
        file_id: FileId,
        local_slice: u32,
        slice_offset: u64,
        data: &[u8],
    ) -> io::Result<()> {
        let Some(target) = self
            .reconstruction_copy_targets
            .get(&(file_id, local_slice))
        else {
            return Ok(());
        };
        let Some(relative_end) = slice_offset.checked_add(data.len() as u64) else {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "reconstruction copy range overflow",
            ));
        };
        if relative_end > target.len {
            return Err(source_location_changed_io(&target.src));
        }
        let dst_offset = target
            .dst_offset
            .checked_add(slice_offset)
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "staged offset overflow"))?;
        let mut writers = self
            .staged_writers
            .lock()
            .map_err(|_| io::Error::other("staged writer lock poisoned"))?;
        let writer = writers.get_mut(&file_id).ok_or_else(|| {
            io::Error::new(io::ErrorKind::NotFound, "staged writer handle not cached")
        })?;
        write_all_file_at(writer, data, dst_offset)
    }
}

impl crate::verify::FileAccess for RepairExecutionAccess {
    fn read_file_range(&self, file_id: &FileId, offset: u64, len: u64) -> io::Result<Vec<u8>> {
        if offset.is_multiple_of(self.slice_size) {
            let local_slice = u32::try_from(offset / self.slice_size).ok();
            if let Some((location, expected)) = local_slice.and_then(|local_slice| {
                self.source_locations
                    .get(&(*file_id, local_slice))
                    .zip(self.source_blocks.get(&(*file_id, local_slice)))
            }) && len == expected.expected_len
                && location.len == expected.expected_len
            {
                let mut buf = vec![0u8; expected.expected_len as usize];
                match &location.source {
                    SourceLocation::Path(path) => {
                        self.ensure_source_unchanged(path)?;
                        let file = self.source_files.get(path).ok_or_else(|| {
                            io::Error::new(io::ErrorKind::NotFound, "source file handle not cached")
                        })?;
                        read_exact_file_at(file, &mut buf, location.offset)
                            .map_err(|_| source_changed_io(path))?;
                        let file_len = file
                            .metadata()
                            .ok()
                            .map_or(location.len, |metadata| metadata.len());
                        crate::file_cache::drop_touched_file_cache(
                            file,
                            path,
                            file_len,
                            location.offset,
                            buf.len() as u64,
                        );
                    }
                    SourceLocation::Access(source_id) => {
                        read_exact_from_access(
                            self.access(*source_id)?,
                            source_id,
                            location.offset,
                            &mut buf,
                        )
                        .map_err(|_| source_location_changed_io(&location.source))?;
                    }
                }
                let mut checksum = checksum::SliceChecksumState::new();
                checksum.update(&buf);
                let (crc32, md5) = checksum.finalize(Some(self.slice_size));
                if crc32 != expected.checksum.crc32 || md5 != expected.checksum.md5 {
                    return Err(source_location_changed_io(&location.source));
                }
                self.validation_bytes
                    .fetch_add(buf.len() as u64, Ordering::Relaxed);
                let local_slice = u32::try_from(offset / self.slice_size).map_err(|_| {
                    io::Error::new(io::ErrorKind::InvalidData, "source slice index overflow")
                })?;
                self.copy_reconstruction_chunk(*file_id, local_slice, 0, &buf)?;
                return Ok(buf);
            }
        }
        let requested = usize::try_from(len)
            .map_err(|_| io::Error::new(io::ErrorKind::InvalidInput, "read range too large"))?;
        let mut buf = Vec::with_capacity(requested);
        let mut current_offset = offset;
        while buf.len() < requested {
            let slice_offset = current_offset % self.slice_size;
            let chunk_len =
                (self.slice_size - slice_offset).min((requested - buf.len()) as u64) as usize;
            if chunk_len == 0 {
                break;
            }
            let start = buf.len();
            buf.resize(start + chunk_len, 0);
            let read_len = self.read_file_range_into(
                file_id,
                current_offset,
                &mut buf[start..start + chunk_len],
            )?;
            buf.truncate(start + read_len);
            if read_len == 0 {
                break;
            }
            current_offset += read_len as u64;
        }
        Ok(buf)
    }

    fn read_file_range_into(
        &self,
        file_id: &FileId,
        offset: u64,
        dst: &mut [u8],
    ) -> io::Result<usize> {
        if let Some(slice_index) = offset.checked_div(self.slice_size) {
            let local_slice = slice_index as u32;
            let slice_offset = offset % self.slice_size;
            if let Some(location) = self.source_locations.get(&(*file_id, local_slice)) {
                if slice_offset >= location.len {
                    if let SourceLocation::Path(path) = &location.source {
                        self.ensure_source_unchanged(path)?;
                    }
                    return Ok(0);
                }
                let len = (dst.len() as u64).min(location.len - slice_offset) as usize;
                let read_offset = location.offset + slice_offset;
                match &location.source {
                    SourceLocation::Path(path) => {
                        self.ensure_source_unchanged(path)?;
                        let file = self.source_files.get(path).ok_or_else(|| {
                            io::Error::new(io::ErrorKind::NotFound, "source file handle not cached")
                        })?;
                        // `read_exact_file_at`, not a bare `read_file_at` plus a
                        // length check: a positional read may legally come back
                        // short, and `fd_pread` under wasmtime always does above
                        // 64 KiB (on *both* wasm targets — see
                        // `disk::read_filled`). Treating that as a changed source
                        // would abort reconstruction on any set whose slice size
                        // exceeds the host's cap. The loop keeps the same
                        // "anything less than a full fill is a changed source"
                        // outcome for a genuinely truncated file.
                        read_exact_file_at(file, &mut dst[..len], read_offset)
                            .map_err(|_| source_changed_io(path))?;
                        let read = len;
                        let file_len = file
                            .metadata()
                            .ok()
                            .map_or(location.len, |metadata| metadata.len());
                        crate::file_cache::drop_touched_file_cache(
                            file,
                            path,
                            file_len,
                            read_offset,
                            read as u64,
                        );
                    }
                    SourceLocation::Access(source_id) => {
                        read_exact_from_access(
                            self.access(*source_id)?,
                            source_id,
                            read_offset,
                            &mut dst[..len],
                        )
                        .map_err(|_| source_location_changed_io(&location.source))?;
                    }
                }
                self.validate_source_chunk(*file_id, local_slice, slice_offset, &dst[..len])?;
                self.copy_reconstruction_chunk(*file_id, local_slice, slice_offset, &dst[..len])?;
                return Ok(len);
            }
        }

        let path = self.repair_path_for(file_id)?;
        let mut file = File::open(path)?;
        let file_len = file.metadata()?.len();
        file.seek(SeekFrom::Start(offset))?;
        let read = crate::disk::read_filled(&mut file, dst)?;
        crate::file_cache::drop_touched_file_cache(&file, path, file_len, offset, read as u64);
        Ok(read)
    }

    fn open_sequential_reader(
        &self,
        file_id: &FileId,
    ) -> io::Result<Option<Box<dyn std::io::Read>>> {
        if self
            .source_locations
            .keys()
            .any(|(source_file_id, _)| source_file_id == file_id)
        {
            return Ok(None);
        }

        Ok(Some(Box::new(crate::file_cache::CacheAdvisedReader::open(
            self.repair_path_for(file_id)?,
        )?)))
    }

    fn file_exists(&self, file_id: &FileId) -> bool {
        self.repair_paths
            .get(file_id)
            .is_some_and(|path| path.exists())
    }

    fn file_length(&self, file_id: &FileId) -> Option<u64> {
        self.repair_paths
            .get(file_id)
            .and_then(|path| fs::metadata(path).ok())
            .map(|metadata| metadata.len())
    }

    fn read_file(&self, file_id: &FileId) -> io::Result<Vec<u8>> {
        crate::file_cache::read_to_vec(self.repair_path_for(file_id)?)
    }

    fn write_file_range(&mut self, file_id: &FileId, offset: u64, data: &[u8]) -> io::Result<()> {
        let path = self.repair_path_for(file_id)?;
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let mut writers = self
            .staged_writers
            .lock()
            .map_err(|_| io::Error::other("staged writer lock poisoned"))?;
        let file = writers.get_mut(file_id).ok_or_else(|| {
            io::Error::new(io::ErrorKind::NotFound, "staged writer handle not cached")
        })?;
        write_all_file_at(file, data, offset)
    }
}

#[cfg(unix)]
fn read_file_at(file: &File, dst: &mut [u8], offset: u64) -> io::Result<usize> {
    file.read_at(dst, offset)
}

#[cfg(unix)]
fn write_file_at(file: &File, src: &[u8], offset: u64) -> io::Result<usize> {
    file.write_at(src, offset)
}

#[cfg(windows)]
fn write_file_at(file: &File, src: &[u8], offset: u64) -> io::Result<usize> {
    file.seek_write(src, offset)
}

/// Positional write on wasi, via `libc::pwrite`.
///
/// The previous portable fallback (`try_clone` + `seek` + `write`) could never
/// succeed here: `File::try_clone` is `Unsupported` on wasip1, so every staged
/// repair write failed at the first call. `pwrite` is both correct and a closer
/// match to the `unix` arm — it does not disturb the handle's seek cursor,
/// which is the property the shared-handle callers rely on.
#[cfg(target_os = "wasi")]
fn write_file_at(file: &File, src: &[u8], offset: u64) -> io::Result<usize> {
    use std::os::fd::AsRawFd;

    // SAFETY: `src` is a valid initialized slice of `src.len()` bytes and the
    // fd is owned by `file`, which outlives the call.
    let written = unsafe {
        libc::pwrite(
            file.as_raw_fd(),
            src.as_ptr().cast::<libc::c_void>(),
            src.len(),
            offset as libc::off_t,
        )
    };
    if written < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(written as usize)
}

#[cfg(not(any(unix, windows, target_os = "wasi")))]
fn write_file_at(file: &File, src: &[u8], offset: u64) -> io::Result<usize> {
    let mut cloned = file.try_clone()?;
    cloned.seek(SeekFrom::Start(offset))?;
    cloned.write(src)
}

fn write_all_file_at(file: &File, mut src: &[u8], mut offset: u64) -> io::Result<()> {
    while !src.is_empty() {
        let written = write_file_at(file, src, offset)?;
        if written == 0 {
            return Err(io::Error::new(
                io::ErrorKind::WriteZero,
                "failed to write the complete staged range",
            ));
        }
        src = &src[written..];
        offset += written as u64;
    }
    Ok(())
}

#[cfg(windows)]
fn read_file_at(file: &File, dst: &mut [u8], offset: u64) -> io::Result<usize> {
    file.seek_read(dst, offset)
}

/// Positional read on wasi, via `libc::pread`; see [`write_file_at`] for why
/// the `try_clone` fallback is unusable on this target.
#[cfg(target_os = "wasi")]
fn read_file_at(file: &File, dst: &mut [u8], offset: u64) -> io::Result<usize> {
    use std::os::fd::AsRawFd;

    // SAFETY: `dst` is a valid writable slice of `dst.len()` bytes and the fd is
    // owned by `file`, which outlives the call.
    let read = unsafe {
        libc::pread(
            file.as_raw_fd(),
            dst.as_mut_ptr().cast::<libc::c_void>(),
            dst.len(),
            offset as libc::off_t,
        )
    };
    if read < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(read as usize)
}

#[cfg(not(any(unix, windows, target_os = "wasi")))]
fn read_file_at(file: &File, dst: &mut [u8], offset: u64) -> io::Result<usize> {
    let mut cloned = file.try_clone()?;
    cloned.seek(SeekFrom::Start(offset))?;
    cloned.read(dst)
}

/// Positional `read_exact`: fills `dst` from `offset` without relying on the
/// handle's seek cursor, so parallel scan segments can share one handle.
fn read_exact_file_at(file: &File, mut dst: &mut [u8], mut offset: u64) -> io::Result<()> {
    while !dst.is_empty() {
        match read_file_at(file, dst, offset) {
            Ok(0) => {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "failed to fill whole buffer",
                ));
            }
            Ok(read) => {
                dst = &mut dst[read..];
                offset += read as u64;
            }
            Err(error) if error.kind() == io::ErrorKind::Interrupted => {}
            Err(error) => return Err(error),
        }
    }
    Ok(())
}

pub(crate) struct RepairVerificationAccess {
    paths: HashMap<FileId, PathBuf>,
    /// Handle serving files that were *not* staged for repair. Present only
    /// for access-backed states, where an unstaged file's bytes were never on
    /// disk and reading its `safe_path` would read whatever else lives there.
    unstaged_access: Option<Arc<dyn FileAccess + Send + Sync>>,
}

impl RepairVerificationAccess {
    pub(crate) fn new(
        files: &[SourceFileEntry],
        install_dir: &Path,
        staged_file_ids: &HashSet<FileId>,
        unstaged_access: Option<Arc<dyn FileAccess + Send + Sync>>,
    ) -> Self {
        let paths = files
            .iter()
            .filter(|file| unstaged_access.is_none() || staged_file_ids.contains(&file.file_id))
            .map(|file| {
                let path = if staged_file_ids.contains(&file.file_id) {
                    install_dir.join(&file.safe_name)
                } else {
                    file.safe_path.clone()
                };
                (file.file_id, path)
            })
            .collect();

        Self {
            paths,
            unstaged_access,
        }
    }

    fn path_for(&self, file_id: &FileId) -> io::Result<&Path> {
        self.paths
            .get(file_id)
            .map(PathBuf::as_path)
            .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, "unknown file ID"))
    }

    /// The serving handle for a file that has no staged output. Only an
    /// access-backed verification has one; otherwise the file is on disk.
    fn unstaged(&self, file_id: &FileId) -> Option<&(dyn FileAccess + Send + Sync)> {
        if self.paths.contains_key(file_id) {
            return None;
        }
        self.unstaged_access.as_deref()
    }
}

impl crate::verify::FileAccess for RepairVerificationAccess {
    fn read_file_range(&self, file_id: &FileId, offset: u64, len: u64) -> io::Result<Vec<u8>> {
        if let Some(access) = self.unstaged(file_id) {
            return access.read_file_range(file_id, offset, len);
        }
        let path = self.path_for(file_id)?;
        let mut file = File::open(path)?;
        let file_len = file.metadata()?.len();
        file.seek(SeekFrom::Start(offset))?;
        let mut buf = vec![0u8; len as usize];
        let read_len = crate::disk::read_filled(&mut file, &mut buf)?;
        crate::file_cache::drop_touched_file_cache(&file, path, file_len, offset, read_len as u64);
        buf.truncate(read_len);
        Ok(buf)
    }

    fn read_file_range_into(
        &self,
        file_id: &FileId,
        offset: u64,
        dst: &mut [u8],
    ) -> io::Result<usize> {
        if let Some(access) = self.unstaged(file_id) {
            return access.read_file_range_into(file_id, offset, dst);
        }
        let path = self.path_for(file_id)?;
        let mut file = File::open(path)?;
        let file_len = file.metadata()?.len();
        file.seek(SeekFrom::Start(offset))?;
        let read = crate::disk::read_filled(&mut file, dst)?;
        crate::file_cache::drop_touched_file_cache(&file, path, file_len, offset, read as u64);
        Ok(read)
    }

    fn open_sequential_reader(
        &self,
        file_id: &FileId,
    ) -> io::Result<Option<Box<dyn std::io::Read>>> {
        if self.unstaged(file_id).is_some() {
            return Ok(None);
        }
        Ok(Some(Box::new(crate::file_cache::CacheAdvisedReader::open(
            self.path_for(file_id)?,
        )?)))
    }

    fn file_exists(&self, file_id: &FileId) -> bool {
        if let Some(access) = self.unstaged(file_id) {
            return access.file_exists(file_id);
        }
        self.paths.get(file_id).is_some_and(|path| path.exists())
    }

    fn file_length(&self, file_id: &FileId) -> Option<u64> {
        if let Some(access) = self.unstaged(file_id) {
            return access.file_length(file_id);
        }
        self.paths
            .get(file_id)
            .and_then(|path| fs::metadata(path).ok())
            .map(|metadata| metadata.len())
    }

    fn read_file(&self, file_id: &FileId) -> io::Result<Vec<u8>> {
        if let Some(access) = self.unstaged(file_id) {
            return access.read_file(file_id);
        }
        crate::file_cache::read_to_vec(self.path_for(file_id)?)
    }

    fn write_file_range(
        &mut self,
        _file_id: &FileId,
        _offset: u64,
        _data: &[u8],
    ) -> io::Result<()> {
        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "verification access is read-only",
        ))
    }
}

impl RepairState {
    /// Conservative preflight estimate used before allocating the retained
    /// block map and verification hash table.
    pub(crate) fn estimated_retained_bytes_from_set(base_dir: &Path, set: &Par2FileSet) -> usize {
        let recoverable_blocks = set
            .recovery_file_ids
            .iter()
            .filter_map(|file_id| set.slice_checksums.get(file_id))
            .fold(0usize, |total, checksums| {
                total.saturating_add(checksums.len())
            });
        let recoverable_files = set.recovery_file_ids.len();
        let mut bytes = std::mem::size_of::<Self>()
            .saturating_add(std::mem::size_of::<Par2FileSet>())
            .saturating_add(
                recoverable_files.saturating_mul(
                    std::mem::size_of::<SourceFileEntry>()
                        .saturating_add(std::mem::size_of::<(FileId, usize)>() * 2),
                ),
            )
            .saturating_add(
                recoverable_blocks.saturating_mul(
                    std::mem::size_of::<SourceBlock>()
                        .saturating_add(std::mem::size_of::<((FileId, u32), usize)>() * 2)
                        .saturating_add(std::mem::size_of::<(u32, Vec<usize>)>() * 2)
                        .saturating_add(std::mem::size_of::<usize>()),
                ),
            )
            .saturating_add(
                set.recovery_file_ids
                    .len()
                    .saturating_mul(std::mem::size_of::<FileId>()),
            )
            .saturating_add(
                set.non_recovery_file_ids
                    .len()
                    .saturating_mul(std::mem::size_of::<FileId>()),
            )
            .saturating_add(
                set.files
                    .len()
                    .saturating_mul(std::mem::size_of::<(FileId, FileDescription)>() * 2),
            )
            .saturating_add(
                set.slice_checksums
                    .len()
                    .saturating_mul(std::mem::size_of::<(FileId, Vec<SliceChecksum>)>() * 2),
            );

        for description in set.files.values() {
            bytes = bytes
                .saturating_add(description.par2_name.len())
                .saturating_add(description.filename.len())
                .saturating_add(description.filename.len())
                .saturating_add(base_dir.as_os_str().len())
                .saturating_add(1);
        }
        for checksums in set.slice_checksums.values() {
            bytes = bytes.saturating_add(
                checksums
                    .len()
                    .saturating_mul(std::mem::size_of::<SliceChecksum>()),
            );
        }
        for recovery in set.recovery_slices.values() {
            bytes = bytes
                .saturating_add(std::mem::size_of_val(recovery).saturating_mul(2))
                .saturating_add(match recovery.data.as_bytes() {
                    Some(data) => data.len(),
                    None => recovery
                        .data
                        .file_span()
                        .map_or(0, |(path, _, _)| path.as_os_str().len()),
                });
        }
        if let Some(creator) = &set.creator {
            bytes = bytes.saturating_add(creator.len());
        }
        bytes
    }

    pub(crate) fn from_set(base_dir: &Path, set: Par2FileSet) -> Result<Self> {
        Self::from_set_with_access(base_dir, set, None)
    }

    /// Build a state whose sources are served by `source_access` instead of
    /// (or alongside) the filesystem. `base_dir` still names where repair
    /// *output* lands; only source reads change.
    pub(crate) fn from_set_with_access(
        base_dir: &Path,
        mut set: Par2FileSet,
        source_access: Option<Arc<dyn FileAccess + Send + Sync>>,
    ) -> Result<Self> {
        let mut discarded_recovery_blocks = 0;
        let slice_size = set.slice_size;
        set.recovery_slices.retain(|_, recovery| {
            let keep = recovery.data.len() as u64 == slice_size;
            if !keep {
                discarded_recovery_blocks += 1;
            }
            keep
        });

        let mut inconsistent_packets = 0;
        let mut discarded_recoverable_files = 0;
        let mut files = Vec::new();
        let mut blocks = Vec::new();
        let mut file_index_by_id = HashMap::new();
        let mut block_index_by_file_slice = HashMap::new();

        for file_id in set
            .recovery_file_ids
            .iter()
            .chain(set.non_recovery_file_ids.iter())
        {
            let recoverable = set.recovery_file_ids.contains(file_id);
            let Some(desc) = set.files.get(file_id) else {
                inconsistent_packets += 1;
                if recoverable {
                    discarded_recoverable_files += 1;
                }
                continue;
            };
            let safe_path = base_dir.join(&desc.filename);
            let first_block = blocks.len();
            let expected_blocks =
                usize::try_from(set.slice_count_for_file(desc.length)).map_err(|_| {
                    Par2Error::ResourceLimitExceeded {
                        reason: format!(
                            "file {} has more than {MAX_SLICES_PER_FILE} addressable PAR2 slices",
                            desc.filename
                        ),
                    }
                })?;
            if expected_blocks > MAX_SLICES_PER_FILE {
                return Err(Par2Error::ResourceLimitExceeded {
                    reason: format!(
                        "file {} has {expected_blocks} PAR2 slices; max is {MAX_SLICES_PER_FILE}",
                        desc.filename
                    ),
                });
            }
            let mut block_count = 0usize;

            if recoverable {
                if expected_blocks == 0 {
                    // Zero-length files have no IFSC entries but still need a
                    // source entry so repair can create/verify the target.
                } else if let Some(checksum_count) = set
                    .slice_checksums
                    .get(file_id)
                    .map(|checksums| checksums.len())
                {
                    if checksum_count != expected_blocks {
                        // A bad IFSC packet is unusable block metadata, not
                        // proof that the described file can be ignored.
                        set.slice_checksums.remove(file_id);
                        inconsistent_packets += 1;
                    } else if let Some(checksums) = set.slice_checksums.get(file_id) {
                        block_count = checksums.len();
                        for (local_index, checksum) in checksums.iter().enumerate() {
                            let offset = local_index as u64 * slice_size;
                            let expected_len = desc.length.saturating_sub(offset).min(slice_size);
                            let global_index = blocks.len();
                            block_index_by_file_slice
                                .insert((*file_id, local_index as u32), global_index);
                            blocks.push(SourceBlock {
                                global_index,
                                file_id: *file_id,
                                local_index: local_index as u32,
                                expected_len,
                                checksum: *checksum,
                                location: None,
                            });
                        }
                    }
                } else {
                    // A missing IFSC packet removes block-scanner evidence for
                    // this file, but FileDesc still permits exact full-hash
                    // verification and RS output.
                    inconsistent_packets += 1;
                }
            }

            let entry = SourceFileEntry {
                file_id: *file_id,
                par2_name: desc.par2_name.clone(),
                safe_path,
                safe_name: desc.filename.clone(),
                length: desc.length,
                hash_full: desc.hash_full,
                hash_16k: desc.hash_16k,
                recoverable,
                first_block,
                expected_block_count: if recoverable { expected_blocks } else { 0 },
                block_count: if recoverable { block_count } else { 0 },
                target_exists: false,
                complete_location: None,
                non_canonical_complete_source_count: 0,
            };
            file_index_by_id.insert(*file_id, files.len());
            files.push(entry);
        }

        let hash_table = VerificationHashTable::new(&blocks, slice_size);

        Ok(Self {
            set,
            files,
            blocks,
            file_index_by_id,
            block_index_by_file_slice,
            hash_table,
            source_access,
            discarded_recovery_blocks,
            inconsistent_packets,
            discarded_recoverable_files,
        })
    }

    /// Conservative heap bytes that may be added when a complete source
    /// location is cloned into the file entry and each of its block locations.
    /// An access-backed source owns no heap, so its budget is zero — which is
    /// the honest number, not a placeholder.
    pub(crate) fn complete_location_budget(
        &self,
        file_id: FileId,
        source: &SourceLocation,
    ) -> Option<usize> {
        let file = &self.files[*self.file_index_by_id.get(&file_id)?];
        file.recoverable.then(|| {
            source
                .heap_bytes()
                .saturating_mul(file.block_count.saturating_add(1))
        })
    }

    /// Conservative heap bytes that may be added for one slice location.
    pub(crate) fn block_location_budget(
        &self,
        file_id: FileId,
        local_index: u32,
        source: &SourceLocation,
    ) -> Option<usize> {
        self.block_index_by_file_slice
            .contains_key(&(file_id, local_index))
            .then_some(source.heap_bytes())
    }

    /// Seed a complete, independently committed source. The retained-session
    /// caller is responsible for checking its evidence before calling this;
    /// repair-time validation still checks every byte that is later consumed.
    pub(crate) fn seed_complete_location(
        &mut self,
        file_id: FileId,
        source: SourceLocation,
    ) -> bool {
        let Some(file_index) = self.file_index_by_id.get(&file_id).copied() else {
            return false;
        };
        let (recoverable, length, first_block, block_count, canonical) = {
            let file = &self.files[file_index];
            (
                file.recoverable,
                file.length,
                file.first_block,
                file.block_count,
                source.is_canonical_for(file),
            )
        };
        if !recoverable {
            return false;
        }
        let kind = if canonical {
            BlockLocationKind::Canonical
        } else {
            BlockLocationKind::Extra
        };
        self.files[file_index].complete_location = Some(BlockLocation {
            source: source.clone(),
            offset: 0,
            len: length,
            kind,
        });
        for block_index in first_block..first_block + block_count {
            let block = &self.blocks[block_index];
            self.record_block_location(
                block_index,
                BlockLocation {
                    source: source.clone(),
                    offset: block.local_index as u64 * self.set.slice_size,
                    len: block.expected_len,
                    kind,
                },
            );
        }
        true
    }

    /// Seed one IFSC-verified source slice. This never promotes a file to a
    /// whole-file match: only a full committed hash may do that.
    pub(crate) fn seed_block_location(
        &mut self,
        file_id: FileId,
        local_index: u32,
        source: SourceLocation,
    ) -> bool {
        let Some(block_index) = self
            .block_index_by_file_slice
            .get(&(file_id, local_index))
            .copied()
        else {
            return false;
        };
        let file = &self.files[*self
            .file_index_by_id
            .get(&file_id)
            .expect("block file exists")];
        let block = &self.blocks[block_index];
        let kind = if source.is_canonical_for(file) {
            BlockLocationKind::Canonical
        } else {
            BlockLocationKind::Extra
        };
        self.record_block_location(
            block_index,
            BlockLocation {
                source,
                offset: local_index as u64 * self.set.slice_size,
                len: block.expected_len,
                kind,
            },
        );
        true
    }

    /// Forget every retained location belonging to one PAR2 file, whichever
    /// kind of source backs it. Packet metadata and other files stay intact,
    /// so the next analysis re-resolves only what this dropped.
    pub(crate) fn invalidate_file(&mut self, file_id: FileId) -> bool {
        let Some(file_index) = self.file_index_by_id.get(&file_id).copied() else {
            return false;
        };
        let mut changed = false;
        let file = &mut self.files[file_index];
        if file.complete_location.take().is_some() {
            changed = true;
        }
        file.target_exists = false;
        file.non_canonical_complete_source_count = 0;
        let (first_block, block_count) = (file.first_block, file.block_count);
        for block in &mut self.blocks[first_block..first_block + block_count] {
            if block.location.take().is_some() {
                changed = true;
            }
        }
        changed
    }

    /// Forget every access-backed location, leaving physical ones untouched.
    /// Used when the handle's coverage generation moves on.
    pub(crate) fn invalidate_access_sources(&mut self) -> bool {
        let mut changed = false;
        for file in &mut self.files {
            if file
                .complete_location
                .as_ref()
                .is_some_and(|location| location.source.is_access())
            {
                file.complete_location = None;
                file.non_canonical_complete_source_count = 0;
                changed = true;
            }
        }
        for block in &mut self.blocks {
            if block
                .location
                .as_ref()
                .is_some_and(|location| location.source.is_access())
            {
                block.location = None;
                changed = true;
            }
        }
        changed
    }

    /// Promote every access-backed file whose slices are all seeded, and mark
    /// existence from the serving handle. This is the access counterpart of
    /// [`Self::refresh_file_states`] and touches no filesystem path: a
    /// directory walk over virtual sources would be meaningless.
    pub(crate) fn refresh_access_file_states(&mut self) {
        let Some(access) = self.source_access.clone() else {
            return;
        };
        for file_index in 0..self.files.len() {
            let file_id = self.files[file_index].file_id;
            self.files[file_index].target_exists = access.file_exists(&file_id);
            if !self.files[file_index].recoverable
                || self.files[file_index].complete_location.is_some()
            {
                continue;
            }
            let file = &self.files[file_index];
            if file.block_count == 0 || file.block_count != file.expected_block_count {
                continue;
            }
            let complete = (0..file.block_count).all(|local| {
                let block = &self.blocks[file.first_block + local];
                block.location.as_ref().is_some_and(|location| {
                    location.source == SourceLocation::Access(file_id)
                        && location.offset == local as u64 * self.set.slice_size
                        && location.len == block.expected_len
                })
            });
            if complete {
                let length = file.length;
                self.files[file_index].complete_location = Some(BlockLocation {
                    source: SourceLocation::Access(file_id),
                    offset: 0,
                    len: length,
                    kind: BlockLocationKind::Canonical,
                });
            }
        }
    }

    pub(crate) fn invalidate_path(&mut self, path: &Path) -> bool {
        let mut changed = false;
        for file in &mut self.files {
            if file
                .complete_location
                .as_ref()
                .is_some_and(|location| location.source.is_path(path))
            {
                file.complete_location = None;
                changed = true;
            }
            if file.safe_path == path {
                file.target_exists = false;
            }
        }
        for block in &mut self.blocks {
            if block
                .location
                .as_ref()
                .is_some_and(|location| location.source.is_path(path))
            {
                block.location = None;
                changed = true;
            }
        }
        changed
    }

    pub(crate) fn invalidate_all_sources(&mut self) {
        for file in &mut self.files {
            file.complete_location = None;
            file.target_exists = false;
            file.non_canonical_complete_source_count = 0;
        }
        for block in &mut self.blocks {
            block.location = None;
        }
    }

    /// Conservative accounting for memory retained by a stateful session.
    /// File-backed recovery packets count only their owned path metadata, not
    /// their on-disk payload; in-memory recovery packets count their bytes.
    pub(crate) fn estimated_retained_bytes(&self) -> usize {
        self.estimated_retained_bytes_with_set(&self.set)
    }

    pub(crate) fn estimated_retained_bytes_with_set(&self, set: &Par2FileSet) -> usize {
        let mut bytes = std::mem::size_of::<Self>()
            .saturating_add(
                self.files
                    .capacity()
                    .saturating_mul(std::mem::size_of::<SourceFileEntry>()),
            )
            .saturating_add(
                self.blocks
                    .capacity()
                    .saturating_mul(std::mem::size_of::<SourceBlock>()),
            )
            .saturating_add(
                self.file_index_by_id
                    .capacity()
                    .saturating_mul(std::mem::size_of::<(FileId, usize)>()),
            )
            .saturating_add(
                self.block_index_by_file_slice
                    .capacity()
                    .saturating_mul(std::mem::size_of::<((FileId, u32), usize)>()),
            )
            .saturating_add(
                set.recovery_file_ids
                    .capacity()
                    .saturating_mul(std::mem::size_of::<FileId>()),
            )
            .saturating_add(
                set.non_recovery_file_ids
                    .capacity()
                    .saturating_mul(std::mem::size_of::<FileId>()),
            )
            .saturating_add(
                set.files
                    .capacity()
                    .saturating_mul(std::mem::size_of::<(FileId, FileDescription)>()),
            )
            .saturating_add(
                set.slice_checksums
                    .capacity()
                    .saturating_mul(std::mem::size_of::<(FileId, Vec<SliceChecksum>)>()),
            )
            .saturating_add(self.hash_table.estimated_retained_bytes());
        for file in &self.files {
            bytes = bytes
                .saturating_add(file.par2_name.capacity())
                .saturating_add(file.safe_name.capacity())
                .saturating_add(file.safe_path.as_os_str().len())
                .saturating_add(
                    file.complete_location
                        .as_ref()
                        .map_or(0, |location| location.source.heap_bytes()),
                );
        }
        for block in &self.blocks {
            bytes = bytes.saturating_add(
                block
                    .location
                    .as_ref()
                    .map_or(0, |location| location.source.heap_bytes()),
            );
        }
        for recovery in set.recovery_slices.values() {
            bytes = bytes
                .saturating_add(std::mem::size_of_val(recovery).saturating_mul(2))
                .saturating_add(match recovery.data.as_bytes() {
                    Some(data) => data.len(),
                    None => recovery
                        .data
                        .file_span()
                        .map_or(0, |(path, _, _)| path.as_os_str().len()),
                });
        }
        for description in set.files.values() {
            bytes = bytes
                .saturating_add(std::mem::size_of_val(description).saturating_mul(2))
                .saturating_add(description.par2_name.capacity())
                .saturating_add(description.filename.capacity());
        }
        for checksums in set.slice_checksums.values() {
            bytes = bytes.saturating_add(
                checksums
                    .capacity()
                    .saturating_mul(std::mem::size_of::<SliceChecksum>()),
            );
        }
        if let Some(creator) = &set.creator {
            bytes = bytes.saturating_add(creator.capacity());
        }
        bytes
    }

    fn sources_resolved(&self) -> bool {
        self.files
            .iter()
            .filter(|file| file.recoverable)
            .all(|file| {
                file.complete_location.is_some()
                    || (file.block_count == file.expected_block_count
                        && (0..file.block_count)
                            .all(|local| self.blocks[file.first_block + local].location.is_some()))
            })
    }

    /// Scan only files that do not already have committed whole-file or
    /// per-slice evidence. This is deliberately separate from `scan`, which
    /// remains the one-shot scanner and preserves its existing behaviour.
    ///
    /// `trust` names the seeded slice verdicts whose byte ranges this pass may
    /// take on trust rather than re-read. It is empty unless the host opted in,
    /// and an empty plan makes this function read exactly what it read before
    /// the policy existed.
    pub(crate) fn scan_unresolved(
        &mut self,
        options: &Par2RepairerOptions,
        trust: &EvidenceScanTrust,
    ) -> Result<ScanDiagnostics> {
        let mut diagnostics = ScanDiagnostics::default();
        let mut canonical_candidates = self
            .files
            .iter()
            .filter(|file| file.recoverable && file.complete_location.is_none())
            .filter(|file| {
                file.block_count == 0
                    || (0..file.block_count)
                        .any(|local| self.blocks[file.first_block + local].location.is_none())
            })
            .map(|file| ScanCandidate {
                path: file.safe_path.clone(),
                kind: BlockLocationKind::Canonical,
            })
            .collect::<Vec<_>>();
        canonical_candidates.sort_by(|left, right| left.path.cmp(&right.path));
        canonical_candidates.dedup_by(|left, right| left.path == right.path);
        self.scan_candidates(options, &canonical_candidates, &mut diagnostics, trust)?;

        self.refresh_file_states();
        if self.sources_resolved() {
            return Ok(diagnostics);
        }

        let source_file_keys: HashSet<PathBuf> = self
            .files
            .iter()
            .map(|file| canonical_extra_path(&file.safe_path))
            .collect();
        let mut extra_candidates = BTreeMap::new();
        for path in discover_candidate_files(&options.base_dir)? {
            extra_candidates
                .entry(canonical_extra_path(&path))
                .or_insert(path);
        }
        for path in &options.extra_paths {
            if !has_par2_marker(path)
                && fs::symlink_metadata(path).is_ok_and(|metadata| metadata.file_type().is_file())
            {
                let canonical = canonical_extra_path(path);
                extra_candidates.insert(canonical.clone(), canonical);
            }
        }
        let extra_candidates = extra_candidates
            .into_iter()
            .filter_map(|(key, path)| {
                (!source_file_keys.contains(&key)).then_some(ScanCandidate {
                    path,
                    kind: BlockLocationKind::Extra,
                })
            })
            .collect::<Vec<_>>();
        // Extra candidates are, by construction, paths no source file claims,
        // so no seeded verdict can name one. The empty plan states that rather
        // than relying on the lookup to miss.
        self.scan_candidates(
            options,
            &extra_candidates,
            &mut diagnostics,
            &EvidenceScanTrust::default(),
        )?;
        self.refresh_file_states();
        Ok(diagnostics)
    }

    fn scan(&mut self, options: &Par2RepairerOptions) -> Result<ScanDiagnostics> {
        let mut diagnostics = ScanDiagnostics::default();
        let mut canonical_candidates = self
            .files
            .iter()
            .map(|file| ScanCandidate {
                path: file.safe_path.clone(),
                kind: BlockLocationKind::Canonical,
            })
            .collect::<Vec<_>>();
        canonical_candidates.sort_by(|left, right| left.path.cmp(&right.path));
        canonical_candidates.dedup_by(|left, right| left.path == right.path);
        // The one-shot repairer holds no seeded evidence: nothing is located
        // before it starts, so there is nothing for a skip policy to skip.
        self.scan_candidates(
            options,
            &canonical_candidates,
            &mut diagnostics,
            &EvidenceScanTrust::default(),
        )?;

        self.refresh_file_states();
        if self.files_are_canonical_complete() {
            return Ok(diagnostics);
        }

        let source_file_keys: HashSet<PathBuf> = self
            .files
            .iter()
            .map(|file| canonical_extra_path(&file.safe_path))
            .collect();
        let mut extra_candidates = BTreeMap::new();
        for path in discover_candidate_files(&options.base_dir)? {
            extra_candidates
                .entry(canonical_extra_path(&path))
                .or_insert(path);
        }
        for path in &options.extra_paths {
            if !has_par2_marker(path) {
                let Ok(metadata) = fs::symlink_metadata(path) else {
                    continue;
                };
                if !metadata.file_type().is_file() {
                    continue;
                }
                let canonical = canonical_extra_path(path);
                extra_candidates.insert(canonical.clone(), canonical);
            }
        }

        let extra_candidates = extra_candidates
            .into_iter()
            .filter_map(|(key, path)| {
                (!source_file_keys.contains(&key)).then_some(ScanCandidate {
                    path,
                    kind: BlockLocationKind::Extra,
                })
            })
            .collect::<Vec<_>>();
        self.scan_candidates(
            options,
            &extra_candidates,
            &mut diagnostics,
            &EvidenceScanTrust::default(),
        )?;

        self.refresh_file_states();
        Ok(diagnostics)
    }

    fn scan_candidates(
        &mut self,
        options: &Par2RepairerOptions,
        candidates: &[ScanCandidate],
        diagnostics: &mut ScanDiagnostics,
        trust: &EvidenceScanTrust,
    ) -> Result<()> {
        if candidates.is_empty() {
            return Ok(());
        }

        let baseline_blocks = &self.blocks;
        let files = &self.files;
        let file_index_by_id = &self.file_index_by_id;
        let block_index_by_file_slice = &self.block_index_by_file_slice;
        let hash_table = &self.hash_table;
        let slice_size = self.set.slice_size;

        // Parallelism runs on exactly one axis: across candidates here, or
        // inside a single candidate's scan — never both (nested fan-out
        // measured as an intermittent worker stack overflow via rayon's
        // steal-on-block recursion).
        //
        // `parallel_enabled()` const-folds to `true` on native (the guard is
        // unchanged). On wasm it is a cached runtime probe: `false` on
        // single-threaded `wasm32-wasip1`, so the candidate scan is sequential
        // there and `rayon::current_num_threads` is never called; `true` on
        // `wasm32-wasip1-threads`, where the candidate fan-out is real.
        let results = if reedsolomon_rs::threading::parallel_enabled()
            && candidates.len() > 1
            && rayon::current_num_threads() > 1
        {
            candidates
                .par_iter()
                .map(|candidate| {
                    Self::scan_candidate_snapshot(
                        options,
                        candidate,
                        files,
                        file_index_by_id,
                        block_index_by_file_slice,
                        baseline_blocks,
                        hash_table,
                        slice_size,
                        false,
                        trust,
                    )
                })
                .collect::<Result<Vec<_>>>()?
        } else {
            candidates
                .iter()
                .map(|candidate| {
                    Self::scan_candidate_snapshot(
                        options,
                        candidate,
                        files,
                        file_index_by_id,
                        block_index_by_file_slice,
                        baseline_blocks,
                        hash_table,
                        slice_size,
                        true,
                        trust,
                    )
                })
                .collect::<Result<Vec<_>>>()?
        };

        let mut relocation_targets = Vec::new();
        for result in results {
            if let Some(target) = result.short_relocation_target() {
                relocation_targets.push(target);
            }
            self.apply_scan_result(result, diagnostics);
        }
        self.relocate_open_short_blocks(options, &relocation_targets, diagnostics)?;

        Ok(())
    }

    /// Exhaustive short-block relocation, run once per candidate batch over the
    /// merged scan state.
    ///
    /// Every candidate above scans a private pre-merge snapshot, so a candidate
    /// cannot see that another candidate has already placed a short block.
    /// Searching for relocated short blocks inside that phase therefore made
    /// every candidate sweep its whole file once per distinct short length in
    /// the set — quadratic in set size, and reached in both passes, because a
    /// fully obfuscated download makes every file an extra candidate. Deferring
    /// the search to the merged state searches only for blocks that are still
    /// open, and only inside candidates the merged state cannot already account
    /// for byte-for-byte.
    ///
    /// The search itself is unchanged, and so is its reach into candidates
    /// that still hold unexplained bytes: a short block shifted inside,
    /// concatenated into, or otherwise relocated within one is still found.
    /// Only a block duplicated inside a candidate the merged state already
    /// explains in full goes unsalvaged, and that costs a recovery block, not
    /// the data.
    fn relocate_open_short_blocks(
        &mut self,
        options: &Par2RepairerOptions,
        targets: &[ShortRelocationTarget],
        diagnostics: &mut ScanDiagnostics,
    ) -> Result<()> {
        if targets.is_empty() || self.hash_table.short_blocks.is_empty() {
            return Ok(());
        }

        let started = Instant::now();
        let mut candidates_scanned = 0u32;
        let mut candidates_skipped = 0u32;
        let mut totals = ShortRelocationStats::default();
        let mut open_short_block_count;

        let changed = {
            let table = &self.hash_table;
            let slice_size = self.set.slice_size;
            // Built on demand: the healthy path breaks out below before any
            // candidate is considered, and never pays for the span map.
            let mut explained = None;
            let mut blocks = ScanBlockState::new(&self.blocks);
            let mut open = open_short_blocks(table, &blocks, slice_size);
            open_short_block_count = open.iter().filter(|open| **open).count();

            for target in targets {
                if open_short_block_count == 0 {
                    break;
                }
                check_cancel(options)?;
                if explained
                    .get_or_insert_with(|| self.explained_bytes_by_path())
                    .get_mut(&target.path)
                    .is_some_and(|spans| merged_span_bytes(spans) >= target.len)
                {
                    candidates_skipped = candidates_skipped.saturating_add(1);
                    continue;
                }

                candidates_scanned = candidates_scanned.saturating_add(1);
                let candidate_started = Instant::now();
                let mut stats = ShortRelocationStats::default();
                let attempted = {
                    let mut scan = ShortRelocationScan {
                        table,
                        path: &target.path,
                        kind: target.kind,
                        open: &open,
                        blocks: &mut blocks,
                        stats: &mut stats,
                    };
                    scan_shifted_short_blocks_from_file(&mut scan, target.len as usize)
                };
                let attempted = match attempted {
                    Ok(attempted) => attempted,
                    Err(error) => {
                        log_short_relocation(
                            &target.path,
                            target.kind,
                            &[],
                            &stats,
                            candidate_started.elapsed(),
                        );
                        return Err(error);
                    }
                };
                log_short_relocation(
                    &target.path,
                    target.kind,
                    &attempted,
                    &stats,
                    candidate_started.elapsed(),
                );
                totals.accumulate(&stats);
                if stats.blocks_placed > 0 {
                    open = open_short_blocks(table, &blocks, slice_size);
                    open_short_block_count = open.iter().filter(|open| **open).count();
                }
            }

            blocks.changed_locations()
        };

        let found_before = self
            .blocks
            .iter()
            .filter(|block| block.location.is_some())
            .count();
        for (block_index, location) in changed {
            self.record_block_location(block_index, location);
        }
        let found_after = self
            .blocks
            .iter()
            .filter(|block| block.location.is_some())
            .count();
        diagnostics.blocks_found = diagnostics
            .blocks_found
            .saturating_add(found_after.saturating_sub(found_before) as u32);
        diagnostics.short_relocation_candidates_scanned = diagnostics
            .short_relocation_candidates_scanned
            .saturating_add(candidates_scanned);
        diagnostics.short_relocation_candidates_skipped = diagnostics
            .short_relocation_candidates_skipped
            .saturating_add(candidates_skipped);
        diagnostics.short_relocation_windows_stepped = diagnostics
            .short_relocation_windows_stepped
            .saturating_add(totals.windows_stepped);
        diagnostics.short_relocation_bytes_read = diagnostics
            .short_relocation_bytes_read
            .saturating_add(totals.bytes_read);
        diagnostics.short_relocation_blocks_placed = diagnostics
            .short_relocation_blocks_placed
            .saturating_add(totals.blocks_placed.min(u64::from(u32::MAX)) as u32);

        log_short_relocation_pass(
            targets.len(),
            candidates_scanned,
            candidates_skipped,
            open_short_block_count,
            &totals,
            started.elapsed(),
        );

        Ok(())
    }

    /// Located byte spans of every path the merged state can account for,
    /// keyed by path. A candidate whose spans already cover its whole length
    /// holds no byte the merged state cannot already name, so the relocation
    /// search skips it. What that gives up is narrow and deliberate: a short
    /// block whose bytes are *duplicated* inside such a candidate is real but
    /// no longer salvaged from there, costing one recovery block instead of a
    /// whole-candidate sweep per still-open short length.
    fn explained_bytes_by_path(&self) -> HashMap<PathBuf, Vec<(u64, u64)>> {
        let mut spans: HashMap<PathBuf, Vec<(u64, u64)>> = HashMap::new();
        let mut push = |location: &BlockLocation| {
            if let Some(path) = location.path() {
                spans
                    .entry(path.to_path_buf())
                    .or_default()
                    .push((location.offset, location.len));
            }
        };
        for file in &self.files {
            if let Some(location) = file.complete_location.as_ref() {
                push(location);
            }
        }
        for block in &self.blocks {
            if let Some(location) = block.location.as_ref() {
                push(location);
            }
        }
        spans
    }

    #[allow(clippy::too_many_arguments)]
    fn scan_candidate_snapshot(
        options: &Par2RepairerOptions,
        candidate: &ScanCandidate,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        block_index_by_file_slice: &HashMap<(FileId, u32), usize>,
        baseline_blocks: &[SourceBlock],
        hash_table: &VerificationHashTable,
        slice_size: u64,
        inner_parallel: bool,
        trust: &EvidenceScanTrust,
    ) -> Result<CandidateScanResult> {
        let path = &candidate.path;
        let kind = candidate.kind;
        check_cancel(options)?;
        if should_skip_candidate(path) {
            return Ok(CandidateScanResult::skipped(path, kind));
        }
        let metadata = if kind == BlockLocationKind::Extra {
            let Ok(metadata) = fs::symlink_metadata(path) else {
                return Ok(CandidateScanResult::ignored(path, kind));
            };
            if !metadata.file_type().is_file() {
                return Ok(CandidateScanResult::ignored(path, kind));
            }
            metadata
        } else {
            if !path.is_file() {
                return Ok(CandidateScanResult::ignored(path, kind));
            }
            fs::metadata(path)?
        };

        let mut result = CandidateScanResult {
            path: path.clone(),
            kind,
            files_scanned: 1,
            files_skipped: 0,
            bytes_scanned: metadata.len(),
            bytes_skipped_by_evidence: 0,
            slices_settled_by_evidence: 0,
            stats: None,
            elapsed: Duration::ZERO,
            complete_files: Vec::new(),
            block_locations: Vec::new(),
        };
        if kind == BlockLocationKind::Extra && metadata.len() == 0 {
            return Ok(result);
        }

        let started = Instant::now();
        let (complete_files, block_locations) = Self::scan_complete_file_matches(
            path,
            kind,
            metadata.len(),
            files,
            block_index_by_file_slice,
            baseline_blocks,
            slice_size,
        )?;
        if !complete_files.is_empty() {
            result.complete_files = complete_files;
            result.block_locations = block_locations;
            result.stats = Some(FileScanStats::new(FileScanMode::Complete, metadata.len()));
            result.elapsed = started.elapsed();
            return Ok(result);
        }
        if options.rename_only && kind == BlockLocationKind::Extra {
            result.stats = Some(FileScanStats::new(FileScanMode::Complete, metadata.len()));
            result.elapsed = started.elapsed();
            return Ok(result);
        }

        let ordered_target = (kind == BlockLocationKind::Canonical)
            .then(|| {
                files
                    .iter()
                    .find(|file| {
                        file.safe_path == *path && file.recoverable && file.block_count > 0
                    })
                    .cloned()
            })
            .flatten();
        let scanner = RollingBlockScanner::new(hash_table, slice_size);
        let mut scan_blocks = ScanBlockState::new(baseline_blocks);
        let stats = if let Some(target_file) = ordered_target.as_ref() {
            // Seeded-evidence skipping reaches exactly here: the ordered
            // canonical scan is the path a damaged source file takes, and the
            // only one where a seeded verdict names the same path and offsets
            // the scanner is about to walk.
            let settled =
                evidence_settled_slices(trust, target_file, path, &scan_blocks, slice_size);
            scanner.scan_file_ordered_canonical_state(
                path,
                kind,
                SourceFileScanLookup {
                    files,
                    file_index_by_id,
                },
                target_file,
                &mut scan_blocks,
                ScanSkipOptions {
                    skip_data: options.scan_skip_data,
                    skip_leeway: options.scan_skip_leeway,
                },
                inner_parallel,
                options.memory_limit.unwrap_or(DEFAULT_REPAIR_MEMORY_LIMIT),
                options.cancel.as_ref(),
                &settled,
            )?
        } else {
            scanner.scan_file_with_state_options(
                path,
                kind,
                files,
                file_index_by_id,
                &mut scan_blocks,
                ScanSkipOptions {
                    skip_data: options.scan_skip_data,
                    skip_leeway: options.scan_skip_leeway,
                },
            )?
        };

        result.block_locations = scan_blocks.changed_locations();
        result.bytes_skipped_by_evidence = stats.bytes_skipped_by_evidence;
        result.slices_settled_by_evidence = stats.slices_settled_by_evidence;
        result.stats = Some(stats);
        result.elapsed = started.elapsed();

        Ok(result)
    }

    fn scan_complete_file_matches(
        path: &Path,
        kind: BlockLocationKind,
        len: u64,
        files: &[SourceFileEntry],
        block_index_by_file_slice: &HashMap<(FileId, u32), usize>,
        baseline_blocks: &[SourceBlock],
        slice_size: u64,
    ) -> Result<CompleteScanMatches> {
        let first = read_first_16k(path)?;
        let hash_16k = checksum::md5(&first);

        let candidates: Vec<usize> = files
            .iter()
            .enumerate()
            .filter_map(|(idx, file)| {
                (file.length == len && file.hash_16k == hash_16k).then_some(idx)
            })
            .collect();

        if candidates.is_empty() {
            return Ok((Vec::new(), Vec::new()));
        }

        let should_skip_full_hash = kind == BlockLocationKind::Canonical
            && len >= CANONICAL_COMPLETE_HASH_SKIP_BYTES.max(slice_size.saturating_mul(4))
            && candidates
                .iter()
                .copied()
                .any(|idx| files[idx].safe_path == path && files[idx].block_count > 0);
        if should_skip_full_hash {
            return Ok((Vec::new(), Vec::new()));
        }

        let full = hash_file(path)?;
        let mut complete_files = Vec::new();
        let mut block_locations = Vec::new();
        for idx in candidates {
            if files[idx].hash_full != full {
                continue;
            }

            let file = &files[idx];
            let file_id = file.file_id;
            let complete_kind = if file.safe_path == path {
                BlockLocationKind::Canonical
            } else {
                kind
            };
            complete_files.push(CompleteFileMatch {
                file_index: idx,
                location: BlockLocation {
                    source: SourceLocation::Path(path.to_path_buf()),
                    offset: 0,
                    len,
                    kind: complete_kind,
                },
            });

            for local_index in 0..file.block_count {
                let Some(block_index) = block_index_by_file_slice
                    .get(&(file_id, local_index as u32))
                    .copied()
                else {
                    continue;
                };
                let offset = local_index as u64 * slice_size;
                let expected_len = baseline_blocks[block_index].expected_len;
                block_locations.push((
                    block_index,
                    BlockLocation {
                        source: SourceLocation::Path(path.to_path_buf()),
                        offset,
                        len: expected_len,
                        kind: complete_kind,
                    },
                ));
            }
        }

        Ok((complete_files, block_locations))
    }

    fn apply_scan_result(
        &mut self,
        result: CandidateScanResult,
        diagnostics: &mut ScanDiagnostics,
    ) {
        diagnostics.files_scanned = diagnostics
            .files_scanned
            .saturating_add(result.files_scanned);
        diagnostics.files_skipped = diagnostics
            .files_skipped
            .saturating_add(result.files_skipped);
        // `bytes_scanned` is what this pass read. Ranges a seeded verdict
        // settled were seeked past, so they are subtracted here and reported
        // separately: the two counters together say both how big the candidate
        // was and how much of it the pass actually looked at.
        diagnostics.bytes_scanned = diagnostics.bytes_scanned.saturating_add(
            result
                .bytes_scanned
                .saturating_sub(result.bytes_skipped_by_evidence),
        );
        diagnostics.bytes_skipped_by_evidence = diagnostics
            .bytes_skipped_by_evidence
            .saturating_add(result.bytes_skipped_by_evidence);
        diagnostics.slices_settled_by_evidence = diagnostics
            .slices_settled_by_evidence
            .saturating_add(result.slices_settled_by_evidence);

        let found_before = self
            .blocks
            .iter()
            .filter(|block| block.location.is_some())
            .count();
        for complete in result.complete_files {
            if self.files[complete.file_index].safe_path != result.path {
                self.files[complete.file_index].non_canonical_complete_source_count = self.files
                    [complete.file_index]
                    .non_canonical_complete_source_count
                    .saturating_add(1);
            }
            self.files[complete.file_index].complete_location = Some(complete.location);
        }
        for (block_index, location) in result.block_locations {
            self.record_block_location(block_index, location);
        }
        let found_after = self
            .blocks
            .iter()
            .filter(|block| block.location.is_some())
            .count();
        let blocks_confirmed = found_after.saturating_sub(found_before) as u32;
        diagnostics.blocks_found = diagnostics.blocks_found.saturating_add(blocks_confirmed);

        if let Some(stats) = result.stats {
            log_file_scan(
                &result.path,
                result.kind,
                stats,
                blocks_confirmed,
                result.elapsed,
            );
        }
    }
    fn record_block_location(&mut self, block_index: usize, location: BlockLocation) {
        let replace = self.blocks[block_index]
            .location
            .as_ref()
            .is_none_or(|existing| {
                location.kind < existing.kind
                    || (location.kind == existing.kind && location.source < existing.source)
            });
        if replace {
            self.blocks[block_index].location = Some(location);
        }
    }

    fn refresh_file_states(&mut self) {
        for file_index in 0..self.files.len() {
            let target_exists = self.files[file_index].safe_path.exists();
            self.files[file_index].target_exists = target_exists;
            if !self.files[file_index].recoverable
                || self.files[file_index].complete_location.is_some()
            {
                continue;
            }
            if self.file_has_canonical_block_layout(file_index) {
                let file = &self.files[file_index];
                self.files[file_index].complete_location = Some(BlockLocation {
                    source: SourceLocation::Path(file.safe_path.clone()),
                    offset: 0,
                    len: file.length,
                    kind: BlockLocationKind::Canonical,
                });
            }
        }
    }

    fn file_has_canonical_block_layout(&self, file_index: usize) -> bool {
        let file = &self.files[file_index];
        if !file.target_exists {
            return false;
        }
        if file.block_count == 0 {
            return file.length == 0
                && fs::metadata(&file.safe_path)
                    .map(|metadata| metadata.len() == 0)
                    .unwrap_or(false);
        }
        if fs::metadata(&file.safe_path)
            .map(|metadata| metadata.len() != file.length)
            .unwrap_or(true)
        {
            return false;
        }

        (0..file.block_count).all(|local| {
            let block = &self.blocks[file.first_block + local];
            block.location.as_ref().is_some_and(|location| {
                location.kind == BlockLocationKind::Canonical
                    && location.source.is_path(&file.safe_path)
                    && location.offset == local as u64 * self.set.slice_size
                    && location.len == block.expected_len
            })
        })
    }

    /// Capture the scan's full effect (file states + block locations) plus
    /// a stat snapshot of every path it observed, for reuse by a later
    /// pass over the same set. Access-backed locations contribute no snapshot
    /// entry: their staleness is not a filesystem fact.
    fn scan_carry(&self, diagnostics: &ScanDiagnostics) -> ScanCarry {
        let mut paths: BTreeSet<PathBuf> = BTreeSet::new();
        for file in &self.files {
            paths.insert(file.safe_path.clone());
            if let Some(path) = file
                .complete_location
                .as_ref()
                .and_then(BlockLocation::path)
            {
                paths.insert(path.to_path_buf());
            }
        }
        for block in &self.blocks {
            if let Some(path) = block.location.as_ref().and_then(BlockLocation::path) {
                paths.insert(path.to_path_buf());
            }
        }
        ScanCarry {
            recovery_set_id: self.set.recovery_set_id,
            slice_size: self.set.slice_size,
            set_file_ids: self.files.iter().map(|file| file.file_id).collect(),
            snapshot: paths.iter().map(|path| stat_for_carry(path)).collect(),
            files: self.files.clone(),
            blocks: self.blocks.clone(),
            diagnostics: diagnostics.clone(),
        }
    }

    /// Install a carried scan if it matches this state's set and every
    /// observed path is unchanged on disk (length + mtime, including
    /// nonexistence). Returns the carried diagnostics on success; `None`
    /// means the caller must run a real scan.
    fn try_apply_carry(&mut self, carry: &ScanCarry) -> Option<ScanDiagnostics> {
        // The carried vectors replace this state's own, so they must have been
        // laid out from this same set: same recovery set, same slice size.
        if carry.recovery_set_id != self.set.recovery_set_id
            || carry.slice_size != self.set.slice_size
        {
            return None;
        }
        let ids_match = self.files.len() == carry.set_file_ids.len()
            && self
                .files
                .iter()
                .zip(carry.set_file_ids.iter())
                .all(|(file, id)| file.file_id == *id);
        if !ids_match || self.blocks.len() != carry.blocks.len() {
            return None;
        }
        for expected in &carry.snapshot {
            if stat_for_carry(&expected.path) != *expected {
                return None;
            }
        }
        self.files = carry.files.clone();
        self.blocks = carry.blocks.clone();
        Some(carry.diagnostics.clone())
    }

    pub(crate) fn verification_result(&self) -> VerificationResult {
        let mut files = Vec::new();
        let mut total_missing_blocks = 0u32;
        // Only files whose FileDesc packet is missing are truly unrepairable:
        // without a length the global slice layout (and thus every RS
        // constant) is unknown. A file that merely lost its IFSC packet still
        // repairs positionally — all of its slices count as missing and the
        // FileDesc full-file hash validates the reconstruction afterwards.
        let missing_unrepairable_block_metadata = self.discarded_recoverable_files > 0;

        for file in self.files.iter().filter(|file| file.recoverable) {
            let mut valid_slices = vec![false; file.expected_block_count];
            for (local, valid) in valid_slices.iter_mut().enumerate().take(file.block_count) {
                let block = &self.blocks[file.first_block + local];
                *valid = block.location.is_some();
            }
            if file.complete_location.is_some() {
                valid_slices.fill(true);
            }
            let missing = if file.complete_location.is_some() {
                0
            } else {
                valid_slices.iter().filter(|valid| !**valid).count() as u32
            };
            total_missing_blocks = total_missing_blocks.saturating_add(missing);

            let status = if self.is_canonical_complete(file) {
                FileStatus::Complete
            } else if let Some(path) = file
                .complete_location
                .as_ref()
                .and_then(BlockLocation::path)
            {
                // Only a physical source can be "the same content under a
                // different name". An access-backed complete source is always
                // the file's own bytes, so it is complete, never renamed.
                FileStatus::Renamed(path.to_path_buf())
            } else if !file.target_exists && file.complete_location.is_none() && missing > 0 {
                FileStatus::Missing
            } else {
                FileStatus::Damaged(missing)
            };

            files.push(FileVerification {
                file_id: file.file_id,
                filename: file.safe_name.clone(),
                status,
                valid_slices,
                missing_slice_count: missing,
            });
        }

        let recovery_blocks_available = self.set.recovery_block_count();
        let blocks_needed = total_missing_blocks.saturating_add(self.discarded_recoverable_files);
        let repairable = if total_missing_blocks == 0 && self.files_are_canonical_complete() {
            Repairability::NotNeeded
        } else if missing_unrepairable_block_metadata {
            Repairability::Insufficient {
                blocks_needed,
                blocks_available: recovery_blocks_available,
                deficit: blocks_needed
                    .saturating_sub(recovery_blocks_available)
                    .max(1),
            }
        } else if total_missing_blocks <= recovery_blocks_available {
            Repairability::Repairable {
                blocks_needed: total_missing_blocks,
                blocks_available: recovery_blocks_available,
            }
        } else {
            Repairability::Insufficient {
                blocks_needed: total_missing_blocks,
                blocks_available: recovery_blocks_available,
                deficit: total_missing_blocks - recovery_blocks_available,
            }
        };

        VerificationResult {
            files,
            recovery_blocks_available,
            total_missing_blocks,
            repairable,
        }
    }

    pub(crate) fn files_are_canonical_complete(&self) -> bool {
        if self.discarded_recoverable_files > 0 {
            return false;
        }
        self.files
            .iter()
            .filter(|file| file.recoverable)
            .all(|file| self.is_canonical_complete(file))
    }

    fn is_canonical_complete(&self, file: &SourceFileEntry) -> bool {
        file.complete_location.as_ref().is_some_and(|location| {
            location.kind == BlockLocationKind::Canonical && location.source.is_canonical_for(file)
        })
    }

    /// Every source a repair on this state would read: each recoverable file's
    /// whole-file source, plus the source behind every located block, which
    /// together cover the copy-only path, the staged block copies and the
    /// Reed-Solomon input stream.
    ///
    /// The pre-mutation carry gate and the mid-repair change check are both
    /// built from this one enumeration so they can never come to disagree
    /// about what "a repair input" is.
    fn for_each_repair_input_source<'a>(&'a self, mut visit: impl FnMut(&'a SourceLocation)) {
        for file in self.files.iter().filter(|file| file.recoverable) {
            if let Some(location) = file.complete_location.as_ref() {
                visit(&location.source);
            }
        }
        for block in &self.blocks {
            if let Some(location) = block.location.as_ref() {
                visit(&location.source);
            }
        }
    }

    /// Stat every physical repair input so a mid-repair change is caught.
    /// Access-backed sources carry no stat: their staleness is governed by the
    /// serving handle's own coverage, not by device/inode/mtime.
    fn snapshot_repair_input_sources(&self) -> HashMap<PathBuf, CarriedFileStat> {
        let mut snapshots = HashMap::new();
        self.for_each_repair_input_source(|source| {
            if let Some(path) = source.path() {
                snapshots
                    .entry(path.to_path_buf())
                    .or_insert_with(|| stat_for_carry(path));
            }
        });
        snapshots
    }

    /// Decide whether this carried analysis may be mutated on without a fresh
    /// scan.
    ///
    /// Every source the repair would read is re-stat'd and compared against
    /// the fingerprint the carried scan captured for it. All of them matching
    /// is what licenses the repair to skip its own scan; anything else — a
    /// changed, replaced, truncated, renamed or deleted file, or an input the
    /// carry holds no fingerprint for — sends the caller back to a full scan.
    ///
    /// This is deliberately narrower than [`Self::try_apply_carry`], which
    /// re-stats everything the scan ever looked at: what licenses a *mutation*
    /// is the state of the bytes the mutation will read, and this check runs
    /// immediately before that mutation rather than at the top of the pass.
    fn carry_repair_inputs_unchanged<'a>(
        &'a self,
        carry: &ScanCarry,
    ) -> std::result::Result<(), CarryRetryReason> {
        let expected: HashMap<&Path, &CarriedFileStat> = carry
            .snapshot
            .iter()
            .map(|stat| (stat.path.as_path(), stat))
            .collect();
        let mut rejection = None;
        let mut checked: HashSet<&'a Path> = HashSet::new();
        self.for_each_repair_input_source(|source| {
            if rejection.is_some() {
                return;
            }
            let Some(path) = source.path() else {
                // An access-backed source has no filesystem identity to
                // re-stat, and a carry records no serving-handle generation,
                // so nothing available here can honestly say the bytes behind
                // it are still the ones the scan read. Refuse rather than
                // guess.
                rejection = Some(CarryRetryReason::RepairInputNotFingerprinted);
                return;
            };
            if !checked.insert(path) {
                return;
            }
            match expected.get(path) {
                Some(expected) if stat_for_carry(path) == **expected => {}
                // A repair input the carry never fingerprinted cannot be
                // checked at all, so it is refused for the same reason an
                // access-backed one is.
                None => rejection = Some(CarryRetryReason::RepairInputNotFingerprinted),
                Some(_) => rejection = Some(CarryRetryReason::RepairInputChanged),
            }
        });
        match rejection {
            Some(reason) => Err(reason),
            None => Ok(()),
        }
    }

    pub(crate) fn repair(
        &self,
        options: &Par2RepairerOptions,
        verification: &VerificationResult,
    ) -> Result<RepairInstall> {
        self.repair_inner(options, verification, false)
    }

    /// Retained sessions call this path after analysis. Every source slice is
    /// checked against its IFSC checksum before it can enter staging or the
    /// Reed-Solomon input stream.
    pub(crate) fn repair_validated(
        &self,
        options: &Par2RepairerOptions,
        verification: &VerificationResult,
    ) -> Result<RepairInstall> {
        self.repair_inner(options, verification, true)
    }

    fn repair_inner(
        &self,
        options: &Par2RepairerOptions,
        verification: &VerificationResult,
        validate_sources: bool,
    ) -> Result<RepairInstall> {
        let install_dir = unique_repair_dir(&options.base_dir);
        fs::create_dir_all(&install_dir)?;
        let mut staging_guard = RepairStagingGuard::new(install_dir.clone());
        let mut bytes_copied = 0u64;
        let staged_file_ids: HashSet<FileId> = self
            .files
            .iter()
            .filter(|file| file.recoverable && !self.is_canonical_complete(file))
            .map(|file| file.file_id)
            .collect();
        let source_snapshots = validate_sources.then(|| self.snapshot_repair_input_sources());

        for file in self
            .files
            .iter()
            .filter(|file| staged_file_ids.contains(&file.file_id))
        {
            let target = install_dir.join(&file.safe_name);
            if let Some(parent) = target.parent() {
                fs::create_dir_all(parent)?;
            }
            let out = OpenOptions::new()
                .create(true)
                .write(true)
                .truncate(true)
                .open(&target)?;
            out.set_len(file.length)?;
        }

        let reconstruction_active = verification.total_missing_blocks > 0;
        let mut whole_file_copied_ids = HashSet::new();
        for file in self
            .files
            .iter()
            .filter(|file| staged_file_ids.contains(&file.file_id))
        {
            if reconstruction_active {
                continue;
            }
            let Some(location) = file.complete_location.as_ref() else {
                continue;
            };
            let target = install_dir.join(&file.safe_name);
            if validate_sources {
                copy_complete_file_validated(
                    file,
                    &self.blocks[file.first_block..file.first_block + file.block_count],
                    self.set.slice_size,
                    &location.source,
                    self.source_access.as_deref(),
                    &target,
                )?;
            } else {
                copy_source_range(
                    &location.source,
                    self.source_access.as_deref(),
                    0,
                    &target,
                    0,
                    file.length,
                )?;
            }
            bytes_copied += file.length;
            whole_file_copied_ids.insert(file.file_id);
        }

        let mut block_copy_ranges = Vec::new();
        let mut reconstruction_copy_targets = ReconstructionCopyTargets::new();
        let mut validated_block_copies = Vec::new();
        for block in &self.blocks {
            check_cancel(options)?;
            if !staged_file_ids.contains(&block.file_id)
                || whole_file_copied_ids.contains(&block.file_id)
            {
                continue;
            }
            let Some(location) = block.location.as_ref() else {
                continue;
            };
            let Some(file_idx) = self.file_index_by_id.get(&block.file_id).copied() else {
                continue;
            };
            let target = install_dir.join(&self.files[file_idx].safe_name);
            let range = BlockCopyRange {
                src: location.source.clone(),
                src_offset: location.offset,
                dst: target,
                dst_offset: block.local_index as u64 * self.set.slice_size,
                len: block.expected_len,
            };
            if reconstruction_active {
                reconstruction_copy_targets.insert((block.file_id, block.local_index), range);
            } else if validate_sources {
                validated_block_copies.push((block.clone(), range));
            } else {
                push_block_copy_range(&mut block_copy_ranges, range);
            }
            bytes_copied += block.expected_len;
        }
        let copy_block_ranges = |ranges: &[BlockCopyRange]| -> Result<()> {
            for range in ranges {
                check_cancel(options)?;
                copy_source_range(
                    &range.src,
                    self.source_access.as_deref(),
                    range.src_offset,
                    &range.dst,
                    range.dst_offset,
                    range.len,
                )?;
            }
            Ok(())
        };
        let copy_validated_blocks = || -> Result<()> {
            for (block, range) in &validated_block_copies {
                check_cancel(options)?;
                copy_block_range_validated(
                    block,
                    self.set.slice_size,
                    range,
                    self.source_access.as_deref(),
                )?;
            }
            Ok(())
        };

        let reconstruct = || -> Result<(u64, u64)> {
            let mut bytes_reconstructed = 0u64;
            let mut validation_bytes = 0u64;
            if verification.total_missing_blocks > 0 {
                let mut access = RepairExecutionAccess::new(
                    install_dir.clone(),
                    &self.files,
                    &self.blocks,
                    &staged_file_ids,
                    self.set.slice_size,
                    RepairExecutionContext {
                        source_access: self.source_access.clone(),
                        source_snapshots: source_snapshots.clone(),
                        reconstruction_copy_targets: reconstruction_copy_targets.clone(),
                    },
                )?;
                let plan =
                    plan_repair_with_memory_limit(&self.set, verification, options.memory_limit)?;
                bytes_reconstructed = plan
                    .missing_slices
                    .iter()
                    .filter_map(|(file_id, local)| {
                        if let Some(idx) = self.block_index_by_file_slice.get(&(*file_id, *local)) {
                            return Some(self.blocks[*idx].expected_len);
                        }
                        self.set.file_description(file_id).map(|desc| {
                            let offset = *local as u64 * self.set.slice_size;
                            desc.length.saturating_sub(offset).min(self.set.slice_size)
                        })
                    })
                    .sum();
                let repair_options = RepairOptions {
                    cancel: options.cancel.clone(),
                    progress: options.progress.clone(),
                    memory_limit: options.memory_limit,
                };
                execute_repair_with_options(&plan, &self.set, &mut access, &repair_options)?;
                validation_bytes = access.validation_bytes();
            }
            Ok((bytes_reconstructed, validation_bytes))
        };

        // Copy-only repairs retain the direct copy path. When reconstruction
        // is active, intact blocks are copied by RepairExecutionAccess from
        // the source buffer immediately before it enters the controller.
        // Overlapping the copy with compute adds page-cache and memory-bandwidth
        // contention without improving throughput, so keep the work ordered.
        copy_block_ranges(&block_copy_ranges)?;
        copy_validated_blocks()?;
        let (bytes_reconstructed, reconstruction_validation_bytes) = reconstruct()?;
        let validation_bytes = if validate_sources { bytes_copied } else { 0 }
            .saturating_add(reconstruction_validation_bytes);

        let repair = RepairInstall {
            install_dir,
            staged_file_ids,
            bytes_copied,
            bytes_reconstructed,
            validation_bytes,
        };
        staging_guard.disarm();
        Ok(repair)
    }

    pub(crate) fn install_repaired_files(
        &self,
        repair: &RepairInstall,
        options: &Par2RepairerOptions,
    ) -> Result<()> {
        let canonical_paths: HashSet<PathBuf> = self
            .files
            .iter()
            .filter(|file| file.recoverable)
            .map(|file| canonical_extra_path(&file.safe_path))
            .collect();
        let explicit_extra_paths: HashSet<PathBuf> = options
            .extra_paths
            .iter()
            .filter(|path| !has_par2_marker(path))
            .map(|path| canonical_extra_path(path))
            .collect();
        let consumed_complete_sources: HashSet<PathBuf> = self
            .files
            .iter()
            .filter(|file| repair.staged_file_ids.contains(&file.file_id))
            .filter_map(|file| {
                let location = file.complete_location.as_ref()?;
                let source = canonical_extra_path(location.path()?);
                (source != canonical_extra_path(&file.safe_path)
                    && file.non_canonical_complete_source_count == 1
                    && explicit_extra_paths.contains(&source)
                    && !canonical_paths.contains(&source))
                .then_some(source)
            })
            .collect();

        let mut installed_targets = Vec::new();
        let mut backups = Vec::new();
        let install_result = (|| -> Result<()> {
            for file in self
                .files
                .iter()
                .filter(|file| repair.staged_file_ids.contains(&file.file_id))
            {
                let src = repair.install_dir.join(&file.safe_name);
                let dst = &file.safe_path;
                match fs::symlink_metadata(dst) {
                    Ok(metadata) if metadata.file_type().is_symlink() => {
                        let target_metadata = fs::metadata(dst).map_err(|error| {
                            Par2Error::Io(io::Error::new(
                                io::ErrorKind::InvalidInput,
                                format!(
                                    "repair target is a dangling symbolic link: {} ({error})",
                                    dst.display()
                                ),
                            ))
                        })?;
                        if !target_metadata.file_type().is_file() {
                            return Err(Par2Error::Io(io::Error::new(
                                io::ErrorKind::InvalidInput,
                                format!(
                                    "repair target symbolic link does not point to a file: {}",
                                    dst.display()
                                ),
                            )));
                        }
                        let backup = unique_backup_path(dst)?;
                        crate::disk::rename_within_base(&options.base_dir, dst, &backup)?;
                        crate::file_cache::drop_path_cache(&backup);
                        backups.push((dst.clone(), backup));
                    }
                    Ok(metadata) if metadata.file_type().is_file() => {
                        let backup = unique_backup_path(dst)?;
                        crate::disk::rename_within_base(&options.base_dir, dst, &backup)?;
                        crate::file_cache::drop_path_cache(&backup);
                        backups.push((dst.clone(), backup));
                    }
                    Ok(_) => {
                        return Err(Par2Error::Io(io::Error::new(
                            io::ErrorKind::InvalidInput,
                            format!(
                                "repair target exists and is not a regular file: {}",
                                dst.display()
                            ),
                        )));
                    }
                    Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                    Err(error) => return Err(error.into()),
                }
                crate::disk::rename_within_base(&options.base_dir, &src, dst)?;
                crate::file_cache::drop_path_cache(dst);
                installed_targets.push(dst.clone());
            }

            Ok(())
        })();

        if install_result.is_err() {
            rollback_installed_files(&options.base_dir, &installed_targets, &backups);
        } else {
            // The repaired targets are now accepted. Removing duplicate extra
            // sources is cleanup, not part of the rollback transaction: a
            // cleanup failure must never discard the valid source and then
            // restore a damaged target.
            purge_files_best_effort(&consumed_complete_sources);
            if options.purge {
                purge_files_best_effort(backups.iter().map(|(_, backup)| backup));
            }
        }
        install_result
    }

    pub(crate) fn outcome(
        &self,
        status: Par2RepairStatus,
        bytes_copied: u64,
        bytes_reconstructed: u64,
        packets: PacketDiagnostics,
        scan: ScanDiagnostics,
        verification: VerificationResult,
    ) -> Par2RepairOutcome {
        let mut files_complete = 0u32;
        let mut files_renamed = 0u32;
        let mut files_damaged = 0u32;
        let mut files_missing = self.discarded_recoverable_files;

        for file in &verification.files {
            match file.status {
                FileStatus::Complete => {
                    files_complete += 1;
                }
                FileStatus::Renamed(_) => {
                    files_renamed += 1;
                }
                FileStatus::Damaged(_) => {
                    files_damaged += 1;
                }
                FileStatus::Missing => {
                    files_missing += 1;
                }
            }
        }

        let available_blocks = self
            .blocks
            .iter()
            .filter(|block| block.location.is_some())
            .count() as u32;
        let missing_blocks = verification.total_missing_blocks;
        let recovery_blocks_used = verification
            .total_missing_blocks
            .min(self.set.recovery_block_count());

        Par2RepairOutcome {
            status,
            files_complete,
            files_renamed,
            files_damaged,
            files_missing,
            available_blocks,
            missing_blocks,
            recovery_blocks_available: self.set.recovery_block_count(),
            recovery_blocks_used,
            bytes_copied,
            bytes_reconstructed,
            packets,
            scan,
            carry: CarryDiagnostics::default(),
            verification,
        }
    }
}

struct VerificationHashTable {
    by_crc: HashMap<u32, Vec<usize>>,
    short_blocks: Vec<usize>,
    slice_size: u64,
    /// Longest `by_crc` bucket: the most CRC candidates a single window can
    /// ever produce, and so the ceiling on the parallel scanner's per-worker
    /// candidate scratch.
    max_crc_bucket: usize,
}

impl VerificationHashTable {
    fn new(blocks: &[SourceBlock], slice_size: u64) -> Self {
        let mut by_crc: HashMap<u32, Vec<usize>> = HashMap::new();
        let mut short_blocks = Vec::new();
        for block in blocks {
            by_crc
                .entry(block.checksum.crc32)
                .or_default()
                .push(block.global_index);
            if block.expected_len < slice_size {
                short_blocks.push(block.global_index);
            }
        }
        let max_crc_bucket = by_crc.values().map(Vec::len).max().unwrap_or(0);
        Self {
            by_crc,
            short_blocks,
            slice_size,
            max_crc_bucket,
        }
    }

    fn estimated_retained_bytes(&self) -> usize {
        let mut bytes = std::mem::size_of::<Self>()
            .saturating_add(
                self.by_crc
                    .capacity()
                    .saturating_mul(std::mem::size_of::<(u32, Vec<usize>)>()),
            )
            .saturating_add(
                self.short_blocks
                    .capacity()
                    .saturating_mul(std::mem::size_of::<usize>()),
            );
        for indexes in self.by_crc.values() {
            bytes = bytes.saturating_add(
                indexes
                    .capacity()
                    .saturating_mul(std::mem::size_of::<usize>()),
            );
        }
        bytes
    }
}

struct RollingBlockScanner<'a> {
    table: &'a VerificationHashTable,
    window_table: [u32; 256],
}

struct PendingMd5Check<'a> {
    block_index: usize,
    data: &'a [u8],
    offset: u64,
    len: u64,
    kind: BlockLocationKind,
}

#[derive(Debug, Clone, Copy)]
struct ScanSkipOptions {
    skip_data: bool,
    skip_leeway: u64,
}

impl ScanSkipOptions {
    #[cfg(test)]
    fn disabled() -> Self {
        Self {
            skip_data: false,
            skip_leeway: ORDERED_SCAN_DEFAULT_SKIP_LEEWAY,
        }
    }

    fn scan_distance(self, slice_size: usize) -> usize {
        if !self.skip_data {
            return 0;
        }
        let skip_leeway = if self.skip_leeway == 0 {
            ORDERED_SCAN_DEFAULT_SKIP_LEEWAY
        } else {
            self.skip_leeway
        };
        skip_leeway
            .saturating_mul(2)
            .min(slice_size as u64)
            .try_into()
            .unwrap_or(slice_size)
    }
}

#[derive(Debug, Clone, Copy)]
struct RollingScanProgress {
    current_step_run: u64,
    scan_offset: usize,
}

impl RollingScanProgress {
    fn new(scan_options: ScanSkipOptions, slice_size: usize) -> Self {
        Self {
            current_step_run: 0,
            scan_offset: scan_options.scan_distance(slice_size) / 2,
        }
    }

    fn record_step(&mut self, stats: &mut FileScanStats) {
        stats.windows_stepped += 1;
        self.current_step_run += 1;
    }

    fn record_jump(&mut self, stats: &mut FileScanStats) {
        stats.jumps_taken += 1;
        stats.max_consecutive_steps = stats.max_consecutive_steps.max(self.current_step_run);
        self.current_step_run = 0;
    }
}

struct BufferedWindowScan<'a, 'scanner, 'blocks> {
    scanner: &'a RollingBlockScanner<'scanner>,
    path: &'a Path,
    kind: BlockLocationKind,
    blocks: &'a mut ScanBlockState<'blocks>,
    scan_options: ScanSkipOptions,
    progress: &'a mut RollingScanProgress,
    stats: &'a mut FileScanStats,
}

#[derive(Clone, Copy)]
struct SourceFileScanLookup<'a> {
    files: &'a [SourceFileEntry],
    file_index_by_id: &'a HashMap<FileId, usize>,
}

struct OrderedWindowMatch<'a> {
    path: &'a Path,
    kind: BlockLocationKind,
    target_file_id: &'a FileId,
    expected_block: Option<usize>,
    data: &'a [u8],
    crc: u32,
    offset: u64,
}

/// Selection-half inputs for one ordered window: everything
/// `select_ordered_match` needs besides the hashed match set.
struct OrderedSelection<'a> {
    path: &'a Path,
    kind: BlockLocationKind,
    target_file_id: &'a FileId,
    expected_block: Option<usize>,
    offset: u64,
}

/// Facts for aligned window `i` (offset `i * slice_size`).
/// `matches` holds ascending block indices confirmed by CRC *and* MD5;
/// empty means no aligned selection is possible at this offset.
#[derive(Default)]
struct AlignedWindowFacts {
    matches: Vec<u32>,
}

fn ordered_scan_facts_allocation_bytes(window_count: usize) -> Option<usize> {
    window_count.checked_mul(std::mem::size_of::<AlignedWindowFacts>())
}

/// Byte budget for the match entries Phase A retains. Only the `u32` payload
/// behind `AlignedWindowFacts::matches` is charged here — the fixed headers,
/// the per-worker read buffers, and the per-worker candidate scratch are all
/// charged up front by [`ordered_scan_admission`]. Shared across Phase A
/// workers, so charges are atomic; a charge that cannot fit aborts the whole
/// phase and the file goes to the serial scanner.
struct OrderedScanMatchBudget {
    remaining: AtomicUsize,
}

impl OrderedScanMatchBudget {
    fn new(bytes: usize) -> Self {
        Self {
            remaining: AtomicUsize::new(bytes),
        }
    }

    fn charge(&self, bytes: usize) -> bool {
        self.remaining
            .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |remaining| {
                remaining.checked_sub(bytes)
            })
            .is_ok()
    }
}

/// How much of `memory_limit` a parallel ordered scan may spend on match
/// entries, and how many windows each worker may hold in its read buffer.
struct OrderedScanAdmission {
    read_windows: usize,
    match_budget: usize,
}

/// Smallest match budget the read buffers must leave behind. A scan whose
/// buffers would swallow the whole limit is useless: every window that matches
/// anything would blow the budget and send the file straight back to the
/// serial scanner.
const ORDERED_SCAN_MATCH_RESERVE_BYTES: usize = 1024 * 1024;

/// Count of Phase A read buffers and scratch vectors that can be live at once.
/// `try_for_each_init` builds one pair per running task, and no more tasks run
/// concurrently than there are pool threads or segments.
fn ordered_scan_workers(window_count: usize, segment_windows: usize) -> usize {
    window_count
        .div_ceil(segment_windows.max(1))
        .min(rayon::current_num_threads())
        .max(1)
}

/// Admission accounting for one parallel ordered scan, in bytes of heap held
/// at once: the fixed `AlignedWindowFacts` headers, one read buffer plus one
/// CRC-candidate scratch per concurrent worker, and the data-dependent match
/// entries Phase A retains. Read buffers shrink to fit rather than refusing
/// the scan, but never below one window per worker and never past the match
/// reserve; whatever the fixed part leaves becomes the match budget. `None`
/// means the fixed part alone does not fit, so the serial scanner takes the
/// file.
fn ordered_scan_admission(
    window_count: usize,
    segment_windows: usize,
    slice_size: usize,
    max_crc_bucket: usize,
    workers: usize,
    memory_limit: usize,
) -> Option<OrderedScanAdmission> {
    if slice_size == 0 || workers == 0 || window_count == 0 {
        return None;
    }
    let facts_bytes = ordered_scan_facts_allocation_bytes(window_count)?;
    let scratch_bytes = max_crc_bucket
        .checked_mul(std::mem::size_of::<u32>())?
        .checked_mul(workers)?;
    let fixed_bytes = facts_bytes.checked_add(scratch_bytes)?;
    let spendable = memory_limit.checked_sub(fixed_bytes)?;

    let wanted_windows = (SCANNER_IO_TARGET_BYTES / slice_size)
        .max(1)
        .min(segment_windows.max(1))
        .min(window_count);
    let affordable_windows =
        spendable.saturating_sub(ORDERED_SCAN_MATCH_RESERVE_BYTES) / workers / slice_size;
    let read_windows = wanted_windows.min(affordable_windows).max(1);
    let read_bytes = read_windows.checked_mul(slice_size)?.checked_mul(workers)?;

    Some(OrderedScanAdmission {
        read_windows,
        match_budget: spendable.checked_sub(read_bytes)?,
    })
}

/// Phase A failure modes. `Refused` means the phase could not stay inside its
/// admitted memory (budget exhausted or an allocation the allocator declined);
/// the caller drops the partial facts and re-runs the file through the serial
/// scanner, which produces the same result either way.
enum WindowFactsError {
    Refused,
    Scan(Par2Error),
}

impl From<io::Error> for WindowFactsError {
    fn from(error: io::Error) -> Self {
        Self::Scan(Par2Error::Io(error))
    }
}

/// How a gap resync hands control back to the aligned merge loop.
enum ResyncOutcome {
    Realigned {
        next_window: usize,
        preferred_next: Option<usize>,
    },
    End,
}

/// Shared read-only inputs for the gap resync loop.
struct OrderedResync<'a> {
    facts: &'a [AlignedWindowFacts],
    ordered_full_blocks: &'a [usize],
    path: &'a Path,
    kind: BlockLocationKind,
    target_file_id: &'a FileId,
}

struct OrderedWindowCursor<'a> {
    file: File,
    path: PathBuf,
    len: usize,
    block_size: usize,
    buffer: Vec<u8>,
    first_offset: usize,
    read_offset: usize,
    current_offset: usize,
    out_index: usize,
    in_index: usize,
    tail_index: usize,
    crc: u32,
    /// Bytes this cursor has actually pulled off the disk, across seeks. A
    /// walk with no skips reads the file exactly once, so the shortfall
    /// against the file length is what the skips saved.
    bytes_read: u64,
    window_table: &'a [u32; 256],
}

impl<'a> OrderedWindowCursor<'a> {
    /// Cursor whose first window starts at `start` (callers guarantee a full
    /// window fits there). The parallel scan's gap resync uses this to read
    /// only the gap region through the bounded two-window buffer.
    fn new_at(
        path: &Path,
        block_size: usize,
        window_table: &'a [u32; 256],
        start: usize,
    ) -> io::Result<Self> {
        let mut file = File::open(path)?;
        let len = file.metadata()?.len() as usize;
        if start > 0 {
            file.seek(SeekFrom::Start(start as u64))?;
        }
        crate::file_cache::advise_range_sequential(
            &file,
            path,
            start as u64,
            len.saturating_sub(start) as u64,
        );
        let buffer_len = block_size.checked_mul(2).ok_or_else(|| {
            io::Error::new(io::ErrorKind::InvalidInput, "scanner buffer overflow")
        })?;
        let mut cursor = Self {
            file,
            path: path.to_path_buf(),
            len,
            block_size,
            buffer: vec![0u8; buffer_len],
            first_offset: start,
            read_offset: start,
            current_offset: start,
            out_index: 0,
            in_index: block_size,
            tail_index: 0,
            crc: 0,
            bytes_read: 0,
            window_table,
        };
        cursor.fill(true)?;
        cursor.crc = checksum::crc32(&cursor.buffer[..block_size]);
        Ok(cursor)
    }

    fn last_full_offset(&self) -> usize {
        self.len - self.block_size
    }

    fn offset(&self) -> usize {
        self.current_offset
    }

    fn data(&self) -> &[u8] {
        &self.buffer[self.out_index..self.out_index + self.block_size]
    }

    fn crc(&self) -> u32 {
        self.crc
    }

    fn bytes_read(&self) -> u64 {
        self.bytes_read
    }

    fn step(&mut self) -> io::Result<bool> {
        if self.current_offset >= self.last_full_offset() {
            self.current_offset = self.last_full_offset().saturating_add(1);
            return Ok(false);
        }

        self.current_offset += 1;
        if self.tail_index <= self.in_index {
            self.fill(true)?;
        }

        let incoming = self.buffer[self.in_index];
        let outgoing = self.buffer[self.out_index];
        self.in_index += 1;
        self.out_index += 1;
        self.crc = crc_slide_char(self.crc, incoming, outgoing, self.window_table);

        if self.out_index == self.block_size {
            self.buffer.copy_within(self.out_index..self.tail_index, 0);
            self.tail_index -= self.block_size;
            self.in_index -= self.block_size;
            self.out_index = 0;
        }

        Ok(true)
    }

    fn jump(&mut self, mut distance: usize) -> io::Result<bool> {
        if distance == 0 {
            return Ok(self.current_offset <= self.last_full_offset());
        }
        if distance == 1 {
            return self.step();
        }
        distance = distance.min(self.block_size);

        let next_offset = self.current_offset.saturating_add(distance);
        if next_offset > self.last_full_offset() {
            self.current_offset = self.last_full_offset().saturating_add(1);
            return Ok(false);
        }

        self.current_offset = next_offset;
        let discard_start = self.out_index + distance;
        let keep = self.tail_index.saturating_sub(discard_start);
        if keep > 0 {
            self.buffer.copy_within(discard_start..self.tail_index, 0);
        }
        self.tail_index = keep;
        self.out_index = 0;
        self.in_index = self.block_size;
        self.fill(true)?;
        self.crc = checksum::crc32(&self.buffer[..self.block_size]);
        Ok(true)
    }

    /// Restart the window at `start`, reading nothing in between.
    ///
    /// This is the difference between [`Self::jump`] and a real skip: `jump`
    /// discards buffered bytes but still streams them off the disk, because
    /// every byte it passes over is a byte the scan was asked to explain. A
    /// seek is only sound where something else already explains the gap, which
    /// is the one thing the seeded-evidence policy establishes.
    ///
    /// Returns `false` when `start` leaves no room for a full window, which
    /// ends the aligned walk.
    fn seek_to(&mut self, start: usize) -> io::Result<bool> {
        if start > self.last_full_offset() {
            self.current_offset = self.last_full_offset().saturating_add(1);
            return Ok(false);
        }
        if start == self.current_offset {
            return Ok(true);
        }
        self.file.seek(SeekFrom::Start(start as u64))?;
        self.read_offset = start;
        self.current_offset = start;
        self.out_index = 0;
        self.in_index = self.block_size;
        self.tail_index = 0;
        self.fill(true)?;
        self.crc = checksum::crc32(&self.buffer[..self.block_size]);
        Ok(true)
    }

    fn fill(&mut self, long_fill: bool) -> io::Result<()> {
        if self.read_offset >= self.len {
            return Ok(());
        }

        let target = if !long_fill && self.tail_index >= self.block_size {
            self.block_size
        } else {
            self.buffer.len()
        };

        while self.tail_index < target && self.read_offset < self.len {
            let want = (target - self.tail_index).min(self.len - self.read_offset);
            let read = self
                .file
                .read(&mut self.buffer[self.tail_index..self.tail_index + want])?;
            if read == 0 {
                break;
            }
            self.tail_index += read;
            self.read_offset += read;
            self.bytes_read = self.bytes_read.saturating_add(read as u64);
        }

        if self.tail_index < self.buffer.len() {
            self.buffer[self.tail_index..].fill(0);
        }
        Ok(())
    }
}

impl Drop for OrderedWindowCursor<'_> {
    fn drop(&mut self) {
        crate::file_cache::drop_touched_file_cache(
            &self.file,
            &self.path,
            self.len as u64,
            self.first_offset as u64,
            (self.read_offset - self.first_offset) as u64,
        );
    }
}

impl<'a> RollingBlockScanner<'a> {
    fn new(table: &'a VerificationHashTable, slice_size: u64) -> Self {
        Self {
            table,
            window_table: generate_window_table(slice_size),
        }
    }

    #[cfg(test)]
    fn scan_file(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        blocks: &mut [SourceBlock],
    ) -> Result<FileScanStats> {
        self.scan_file_with_options(
            path,
            kind,
            files,
            file_index_by_id,
            blocks,
            ScanSkipOptions::disabled(),
        )
    }

    #[cfg(test)]
    fn scan_file_with_options(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        blocks: &mut [SourceBlock],
        scan_options: ScanSkipOptions,
    ) -> Result<FileScanStats> {
        let baseline = blocks.to_vec();
        let mut state = ScanBlockState::new(&baseline);
        let stats = self.scan_file_with_state_options(
            path,
            kind,
            files,
            file_index_by_id,
            &mut state,
            scan_options,
        )?;
        self.relocate_open_short_blocks_in(path, kind, &mut state)?;
        state.apply_to_blocks(blocks);
        Ok(stats)
    }

    fn scan_file_with_state_options(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        blocks: &mut ScanBlockState<'_>,
        scan_options: ScanSkipOptions,
    ) -> Result<FileScanStats> {
        if scanner_uses_mmap_fallback(self.table.slice_size) {
            return self.scan_file_mmap_with_state_options(
                path,
                kind,
                files,
                file_index_by_id,
                blocks,
                scan_options,
            );
        }

        self.scan_file_buffered_with_target_state_options(
            path,
            kind,
            SourceFileScanLookup {
                files,
                file_index_by_id,
            },
            blocks,
            SCANNER_IO_TARGET_BYTES,
            scan_options,
        )
    }

    #[cfg(test)]
    fn scan_file_ordered_canonical(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        lookup: SourceFileScanLookup<'_>,
        target_file: &SourceFileEntry,
        blocks: &mut [SourceBlock],
        scan_options: ScanSkipOptions,
    ) -> Result<FileScanStats> {
        self.scan_file_ordered_canonical_settled(
            path,
            kind,
            lookup,
            target_file,
            blocks,
            scan_options,
            &[],
        )
    }

    #[cfg(test)]
    #[allow(clippy::too_many_arguments)]
    fn scan_file_ordered_canonical_settled(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        lookup: SourceFileScanLookup<'_>,
        target_file: &SourceFileEntry,
        blocks: &mut [SourceBlock],
        scan_options: ScanSkipOptions,
        settled: &[bool],
    ) -> Result<FileScanStats> {
        let baseline = blocks.to_vec();
        let mut state = ScanBlockState::new(&baseline);
        let stats = self.scan_file_ordered_canonical_state(
            path,
            kind,
            lookup,
            target_file,
            &mut state,
            scan_options,
            true,
            DEFAULT_REPAIR_MEMORY_LIMIT,
            None,
            settled,
        )?;
        self.relocate_open_short_blocks_in(path, kind, &mut state)?;
        state.apply_to_blocks(blocks);
        Ok(stats)
    }

    #[allow(clippy::too_many_arguments)]
    fn scan_file_ordered_canonical_state(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        lookup: SourceFileScanLookup<'_>,
        target_file: &SourceFileEntry,
        blocks: &mut ScanBlockState<'_>,
        scan_options: ScanSkipOptions,
        inner_parallel: bool,
        memory_limit: usize,
        cancel: Option<&CancellationToken>,
        // Local slice indices this scan may seek past instead of reading. All
        // false (or empty) is the default and every pre-policy caller.
        settled: &[bool],
    ) -> Result<FileScanStats> {
        // Skip-data sampling is stateful and intentionally lossy, so it keeps
        // the serial scanner; single-thread pools do too. `inner_parallel`
        // is false when the caller is already fanning out across candidate
        // files: parallelism runs on exactly one axis, because nesting the
        // per-file fan-out inside the per-candidate fan-out lets a blocked
        // worker steal other files' whole scan frames onto one stack —
        // measured as an intermittent worker stack overflow on 16-file sets.
        // `!parallel_enabled()` const-folds away on native (the OR chain is
        // unchanged, byte-identical). On wasm it is a cached runtime probe: on
        // single-threaded `wasm32-wasip1` it forces the serial scanner and
        // short-circuits before `rayon::current_num_threads`, so the parallel
        // segment scanner (and its worker pool) is never reached; on
        // `wasm32-wasip1-threads` the ordinary parallel gating below applies.
        //
        // A file with honoured seeded-evidence skips joins skip-data on the
        // serial scanner for the same reason: the parallel scan's Phase A
        // computes facts for every aligned window up front, which is precisely
        // the reading the skip exists to avoid. Taking the serial path costs
        // this file its per-file fan-out and saves it most of its I/O; only a
        // file with at least one honoured skip pays that trade.
        let has_settled_skips = settled.contains(&true);
        if !reedsolomon_rs::threading::parallel_enabled()
            || scan_options.skip_data
            || has_settled_skips
            || ordered_scan_force_serial()
            || !ordered_scan_parallel_enabled()
            || !inner_parallel
            || rayon::current_num_threads() <= 1
        {
            return self.scan_file_ordered_canonical_serial(
                path,
                kind,
                lookup,
                target_file,
                blocks,
                scan_options,
                settled,
            );
        }
        let segment_windows = ordered_scan_segment_windows(self.table.slice_size as usize);
        match self.scan_file_ordered_canonical_parallel(
            path,
            kind,
            lookup,
            target_file,
            blocks,
            scan_options,
            segment_windows,
            memory_limit,
            cancel,
        ) {
            Ok(stats) => Ok(stats),
            Err(Par2Error::Cancelled) => Err(Par2Error::Cancelled),
            // mmap or I/O setup failure: the serial scanner owns the error
            // story (and will surface the same error if it persists).
            Err(_) => self.scan_file_ordered_canonical_serial(
                path,
                kind,
                lookup,
                target_file,
                blocks,
                scan_options,
                settled,
            ),
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn scan_file_ordered_canonical_serial(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        lookup: SourceFileScanLookup<'_>,
        target_file: &SourceFileEntry,
        blocks: &mut ScanBlockState<'_>,
        scan_options: ScanSkipOptions,
        settled: &[bool],
    ) -> Result<FileScanStats> {
        let len = fs::metadata(path)?.len() as usize;
        let mut stats = FileScanStats::new(FileScanMode::OrderedCanonical, len as u64);
        let slice_size = self.table.slice_size as usize;
        if len == 0 || slice_size == 0 {
            return Ok(stats);
        }
        if len < slice_size {
            scan_short_blocks_from_file(
                self.table,
                path,
                kind,
                lookup.files,
                lookup.file_index_by_id,
                blocks,
                len,
            )?;
            return Ok(stats);
        }
        let ordered_full_blocks: Vec<usize> = (0..target_file.block_count)
            .map(|local| target_file.first_block + local)
            .filter(|block_index| blocks.block(*block_index).expected_len == self.table.slice_size)
            .collect();
        let settled_runs = settled_byte_runs(settled, slice_size, len);
        let mut next_run = 0usize;
        let mut settled_slices = 0u32;
        // Enter the file at its first unsettled byte rather than at zero. The
        // cursor fills its buffer as it is constructed, so opening at zero only
        // to seek away would read exactly the bytes this policy exists to
        // avoid.
        let mut entry_offset = 0usize;
        if let Some(&(start, end)) = settled_runs.first()
            && start == 0
        {
            entry_offset = end;
            next_run = 1;
            settled_slices += ((end - start) / slice_size) as u32;
        }
        if entry_offset > len - slice_size {
            // Every aligned window in the file belongs to a settled slice.
            // There is no walk left to run, only the short tail.
            stats.slices_settled_by_evidence = settled_slices;
            stats.bytes_skipped_by_evidence = entry_offset.min(len) as u64;
            scan_short_blocks_from_file(
                self.table,
                path,
                kind,
                lookup.files,
                lookup.file_index_by_id,
                blocks,
                len,
            )?;
            return Ok(stats);
        }
        let mut cursor =
            OrderedWindowCursor::new_at(path, slice_size, &self.window_table, entry_offset)?;
        let entry_local = entry_offset / slice_size;
        let mut preferred_next = ordered_full_blocks
            .iter()
            .position(|block_index| *block_index >= target_file.first_block + entry_local);
        let mut current_step_run = 0u64;
        let scan_distance = scan_options.scan_distance(slice_size);
        let scan_skip = if scan_distance > 0 {
            slice_size.saturating_sub(scan_distance)
        } else {
            0
        };
        let mut scan_offset = scan_distance / 2;

        while cursor.offset() <= cursor.last_full_offset() {
            // Retire runs the cursor has already passed. A block match or a
            // skip-data jump can land past or inside a run; either way the run
            // is simply not taken, and the bytes are read as they always were.
            while settled_runs
                .get(next_run)
                .is_some_and(|(_, end)| *end <= cursor.offset())
            {
                next_run += 1;
            }
            if let Some(&(start, end)) = settled_runs.get(next_run)
                && cursor.offset() == start
            {
                settled_slices = settled_slices.saturating_add(((end - start) / slice_size) as u32);
                stats.jumps_taken += 1;
                stats.max_consecutive_steps = stats.max_consecutive_steps.max(current_step_run);
                current_step_run = 0;
                scan_offset = scan_distance / 2;
                next_run += 1;
                // Resume the ordered expectation at the first full block that
                // starts at or after the run, exactly as a match-driven jump
                // would have left it.
                let next_local = end / slice_size;
                preferred_next = ordered_full_blocks
                    .iter()
                    .position(|block_index| *block_index >= target_file.first_block + next_local);
                if !cursor.seek_to(end)? {
                    break;
                }
                continue;
            }

            let expected_block = preferred_next
                .and_then(|position| ordered_full_blocks.get(position))
                .copied();
            let selected = self.scan_ordered_window(
                OrderedWindowMatch {
                    path,
                    kind,
                    target_file_id: &target_file.file_id,
                    expected_block,
                    data: cursor.data(),
                    crc: cursor.crc(),
                    offset: cursor.offset() as u64,
                },
                blocks,
            );

            if let Some(selected) = selected {
                if blocks.block(selected).file_id == target_file.file_id {
                    preferred_next = ordered_full_blocks
                        .iter()
                        .position(|block_index| *block_index == selected)
                        .and_then(|position| {
                            ordered_full_blocks.get(position + 1).map(|_| position + 1)
                        });
                } else {
                    preferred_next = None;
                }

                stats.jumps_taken += 1;
                stats.max_consecutive_steps = stats.max_consecutive_steps.max(current_step_run);
                current_step_run = 0;
                scan_offset = scan_distance / 2;

                if !cursor.jump(blocks.block(selected).expected_len as usize)? {
                    break;
                }
                continue;
            }

            preferred_next = None;
            if !cursor.step()? {
                break;
            }

            stats.windows_stepped += 1;
            current_step_run += 1;

            if scan_skip > 0 {
                scan_offset += 1;
                if scan_offset >= scan_distance && cursor.offset() < cursor.last_full_offset() {
                    stats.jumps_taken += 1;
                    stats.max_consecutive_steps = stats.max_consecutive_steps.max(current_step_run);
                    current_step_run = 0;

                    if !cursor.jump(scan_skip)? {
                        break;
                    }
                    scan_offset = 0;
                }
            }
        }

        stats.max_consecutive_steps = stats.max_consecutive_steps.max(current_step_run);
        stats.slices_settled_by_evidence = settled_slices;
        if settled_slices > 0 {
            // Measured, not assumed: without a skip this walk streams the whole
            // file, so whatever it did not read is what the skips saved. That
            // is narrower than the settled ranges themselves — a window
            // byte-stepping out of a damaged region still reads into the
            // settled slice that follows it — and this counter reports the
            // narrower, true number.
            stats.bytes_skipped_by_evidence = (len as u64).saturating_sub(cursor.bytes_read());
        }

        scan_short_blocks_from_file(
            self.table,
            path,
            kind,
            lookup.files,
            lookup.file_index_by_id,
            blocks,
            len,
        )?;

        Ok(stats)
    }

    fn scan_ordered_window(
        &self,
        window: OrderedWindowMatch<'_>,
        blocks: &mut ScanBlockState<'_>,
    ) -> Option<usize> {
        let matches = self.ordered_window_md5_matches(blocks.baseline(), window.data, window.crc);
        self.select_ordered_match(
            OrderedSelection {
                path: window.path,
                kind: window.kind,
                target_file_id: window.target_file_id,
                expected_block: window.expected_block,
                offset: window.offset,
            },
            &matches,
            blocks,
        )
    }

    /// Hashing half of the ordered window check: block indices whose CRC and
    /// MD5 both match the window, ascending. Expected-independent — the
    /// expected-block fast path can never select a block this set lacks, and
    /// the MD5 is computed exactly when any size-eligible CRC candidate
    /// exists, matching the serial lazy-init.
    fn ordered_window_md5_matches(
        &self,
        blocks: &[SourceBlock],
        data: &[u8],
        crc: u32,
    ) -> Vec<u32> {
        let mut matches = Vec::new();
        self.collect_ordered_window_md5_matches(blocks, data, crc, &mut matches);
        matches
    }

    /// [`Self::ordered_window_md5_matches`] into a caller-owned buffer, which
    /// it clears first. Phase A reuses one buffer per worker so it can size
    /// each retained match vector exactly, which is what its budget charges.
    fn collect_ordered_window_md5_matches(
        &self,
        blocks: &[SourceBlock],
        data: &[u8],
        crc: u32,
        matches: &mut Vec<u32>,
    ) {
        matches.clear();
        let Some(candidates) = self.table.by_crc.get(&crc) else {
            return;
        };
        let mut md5 = None;
        for block_index in candidates {
            let block = &blocks[*block_index];
            if block.expected_len != self.table.slice_size {
                continue;
            }
            let digest = *md5.get_or_insert_with(|| checksum::md5(data));
            if block.checksum.md5 == digest {
                matches.push(*block_index as u32);
            }
        }
    }

    /// Selection half of the ordered window check: applies the expected-block
    /// fast path, the duplicate-slice gate, and the rank preference over an
    /// already-hashed match set, then records the winner.
    fn select_ordered_match(
        &self,
        selection: OrderedSelection<'_>,
        matches: &[u32],
        blocks: &mut ScanBlockState<'_>,
    ) -> Option<usize> {
        let mut selected = None;

        if let Some(expected_block) = selection.expected_block
            && matches.contains(&(expected_block as u32))
            && can_select_ordered_match(
                expected_block,
                Some(expected_block),
                selection.path,
                blocks,
            )
        {
            selected = Some(expected_block);
        }

        for block_index in matches {
            let block_index = *block_index as usize;
            if Some(block_index) == selection.expected_block && selected == Some(block_index) {
                continue;
            }
            if can_select_ordered_match(
                block_index,
                selection.expected_block,
                selection.path,
                blocks,
            ) && preferred_ordered_match(
                selected,
                block_index,
                selection.expected_block,
                *selection.target_file_id,
                blocks,
            ) {
                selected = Some(block_index);
            }
        }

        if let Some(selected) = selected {
            let block = blocks.block(selected);
            record_block_location(
                blocks,
                selected,
                BlockLocation {
                    source: SourceLocation::Path(selection.path.to_path_buf()),
                    offset: selection.offset,
                    len: block.expected_len,
                    kind: selection.kind,
                },
            );
        }

        selected
    }

    /// Parallel ordered canonical scan: Phase A computes expected-independent
    /// facts for every slice-aligned window in parallel, Phase B replays the
    /// serial cursor state machine over those facts, and Phase C byte-steps
    /// through gaps, splicing back into Phase B when a match realigns. All
    /// reads go through bounded buffers (positional reads in Phase A, the
    /// serial cursor in Phase C). Everything the scan holds at once — facts
    /// headers, per-worker read buffers and scratch, and the match entries
    /// Phase A retains — is admitted under the configured working-memory
    /// budget; scans that do not fit, or that outgrow the match budget
    /// mid-flight, go to the serial scanner instead.
    #[allow(clippy::too_many_arguments)]
    fn scan_file_ordered_canonical_parallel(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        lookup: SourceFileScanLookup<'_>,
        target_file: &SourceFileEntry,
        blocks: &mut ScanBlockState<'_>,
        scan_options: ScanSkipOptions,
        segment_windows: usize,
        memory_limit: usize,
        cancel: Option<&CancellationToken>,
    ) -> Result<FileScanStats> {
        let file = File::open(path)?;
        let len = file.metadata()?.len() as usize;
        let slice_size = self.table.slice_size as usize;
        if len == 0 || slice_size == 0 || len < slice_size {
            // Close the handle before the serial scan reopens `path`. Only
            // wasm targets lint here: their `std::fs::File` is an unsupported
            // stub holding nothing droppable, whereas on unix/windows it owns
            // a descriptor whose `Drop` does the close this relies on.
            #[allow(clippy::drop_non_drop)]
            drop(file);
            // No settled slices: a file with any is routed to the serial
            // scanner before this function is reached.
            return self.scan_file_ordered_canonical_serial(
                path,
                kind,
                lookup,
                target_file,
                blocks,
                scan_options,
                &[],
            );
        }
        let last_full_offset = len - slice_size;
        let window_count = last_full_offset / slice_size + 1;
        let segment_windows = segment_windows.max(1);
        let mut facts: Vec<AlignedWindowFacts> = Vec::new();
        // The header reservation runs only once the accounting admits the
        // scan, and is itself fallible; either refusal takes the serial path.
        let admission = ordered_scan_admission(
            window_count,
            segment_windows,
            slice_size,
            self.table.max_crc_bucket,
            ordered_scan_workers(window_count, segment_windows),
            memory_limit,
        )
        .filter(|_| facts.try_reserve_exact(window_count).is_ok());
        let Some(admission) = admission else {
            #[allow(clippy::drop_non_drop)]
            drop(file);
            return self.scan_file_ordered_canonical_serial(
                path,
                kind,
                lookup,
                target_file,
                blocks,
                scan_options,
                &[],
            );
        };

        crate::file_cache::advise_sequential(&file, path, len as u64);
        let mut stats = FileScanStats::new(FileScanMode::OrderedCanonicalParallel, len as u64);
        facts.resize_with(window_count, AlignedWindowFacts::default);
        let baseline = blocks.baseline();
        let shared_file = &file;
        let match_budget = OrderedScanMatchBudget::new(admission.match_budget);
        let phase_a = facts
            .par_chunks_mut(segment_windows)
            .enumerate()
            .try_for_each_init(
                || (Vec::new(), Vec::new()),
                |(read_buffer, candidates), (segment_index, segment)| {
                    self.compute_aligned_window_facts(
                        shared_file,
                        baseline,
                        segment,
                        segment_index * segment_windows,
                        admission.read_windows,
                        read_buffer,
                        candidates,
                        &match_budget,
                        cancel,
                    )
                },
            );
        match phase_a {
            Ok(()) => {}
            // Cancellation and I/O keep the pre-existing story: the caller
            // decides whether to propagate or re-run the file serially.
            Err(WindowFactsError::Scan(error)) => {
                #[allow(clippy::drop_non_drop)]
                drop(file);
                return Err(error);
            }
            // A refusal is not a failure — the partial facts go away and the
            // file is rescanned serially from a clean slate. Phase A only
            // reads `blocks`, so no partial selection has to be unwound.
            Err(WindowFactsError::Refused) => {
                drop(facts);
                #[allow(clippy::drop_non_drop)]
                drop(file);
                return self.scan_file_ordered_canonical_serial(
                    path,
                    kind,
                    lookup,
                    target_file,
                    blocks,
                    scan_options,
                    &[],
                );
            }
        }

        let ordered_full_blocks: Vec<usize> = (0..target_file.block_count)
            .map(|local| target_file.first_block + local)
            .filter(|block_index| blocks.block(*block_index).expected_len == self.table.slice_size)
            .collect();
        let resync = OrderedResync {
            facts: &facts,
            ordered_full_blocks: &ordered_full_blocks,
            path,
            kind,
            target_file_id: &target_file.file_id,
        };

        let mut preferred_next = (!ordered_full_blocks.is_empty()).then_some(0usize);
        let mut current_step_run = 0u64;
        let mut window_index = 0usize;
        while window_index < window_count {
            let expected_block = preferred_next
                .and_then(|position| ordered_full_blocks.get(position))
                .copied();
            let offset = window_index * slice_size;
            let selected = self.select_ordered_match(
                OrderedSelection {
                    path,
                    kind,
                    target_file_id: &target_file.file_id,
                    expected_block,
                    offset: offset as u64,
                },
                &facts[window_index].matches,
                blocks,
            );

            if let Some(selected) = selected {
                preferred_next = ordered_preferred_after_selection(
                    &ordered_full_blocks,
                    selected,
                    &target_file.file_id,
                    blocks,
                );
                stats.jumps_taken += 1;
                stats.max_consecutive_steps = stats.max_consecutive_steps.max(current_step_run);
                current_step_run = 0;
                window_index += 1;
                continue;
            }

            // A mismatch clears the expected chain; the resync outcome
            // carries the replacement `preferred_next` back across the
            // boundary. Mirrors the serial cursor: a failed step off the
            // last full window ends the scan without counting a step.
            if offset >= last_full_offset {
                break;
            }
            stats.windows_stepped += 1;
            current_step_run += 1;
            match self.rolling_resync_ordered(
                &resync,
                offset + 1,
                blocks,
                &mut stats,
                &mut current_step_run,
            )? {
                ResyncOutcome::Realigned {
                    next_window,
                    preferred_next: next_preferred,
                } => {
                    window_index = next_window;
                    preferred_next = next_preferred;
                }
                ResyncOutcome::End => break,
            }
        }

        stats.max_consecutive_steps = stats.max_consecutive_steps.max(current_step_run);

        let short_result = scan_short_blocks_from_file(
            self.table,
            path,
            kind,
            lookup.files,
            lookup.file_index_by_id,
            blocks,
            len,
        );
        crate::file_cache::drop_file_cache(&file, path, 0, len as u64);
        short_result?;

        Ok(stats)
    }

    /// Phase A worker: fills one segment's facts. Reads only the hash table
    /// and the immutable baseline blocks, so segments run lock-free. Windows
    /// arrive through `read_buffer` in whole-window chunks of `read_windows`,
    /// positionally read from the shared handle. Hashing is the serial
    /// scanner's single-shot CRC-gated MD5: multi-lane MD5 batching lost here
    /// because every lane required a padded copy of its window (it may return
    /// per-arch if measurement justifies it).
    ///
    /// `read_buffer` and `candidates` are the worker-owned buffers the
    /// admission already paid for; every retained match vector is charged
    /// against `match_budget` at its exact size, so a duplicate-heavy file
    /// refuses rather than growing past the working-memory limit.
    #[allow(clippy::too_many_arguments)]
    fn compute_aligned_window_facts(
        &self,
        file: &File,
        blocks: &[SourceBlock],
        facts: &mut [AlignedWindowFacts],
        first_window: usize,
        read_windows: usize,
        read_buffer: &mut Vec<u8>,
        candidates: &mut Vec<u32>,
        match_budget: &OrderedScanMatchBudget,
        cancel: Option<&CancellationToken>,
    ) -> std::result::Result<(), WindowFactsError> {
        if let Some(cancel) = cancel
            && cancel.is_cancelled()
        {
            return Err(WindowFactsError::Scan(Par2Error::Cancelled));
        }

        let slice_size = self.table.slice_size as usize;
        let read_windows = read_windows.max(1);
        let mut slot = 0usize;
        while slot < facts.len() {
            let read_count = read_windows.min(facts.len() - slot);
            let read_len = read_count
                .checked_mul(slice_size)
                .ok_or(WindowFactsError::Refused)?;
            if read_buffer.len() < read_len {
                read_buffer
                    .try_reserve(read_len - read_buffer.len())
                    .map_err(|_| WindowFactsError::Refused)?;
                read_buffer.resize(read_len, 0);
            }
            let read_offset = ((first_window + slot) * slice_size) as u64;
            read_exact_file_at(file, &mut read_buffer[..read_len], read_offset)?;
            for (index, window) in read_buffer[..read_len].chunks_exact(slice_size).enumerate() {
                let crc = checksum::crc32(window);
                self.collect_ordered_window_md5_matches(blocks, window, crc, candidates);
                if candidates.is_empty() {
                    continue;
                }
                let charge = candidates
                    .len()
                    .checked_mul(std::mem::size_of::<u32>())
                    .ok_or(WindowFactsError::Refused)?;
                if !match_budget.charge(charge) {
                    return Err(WindowFactsError::Refused);
                }
                let mut retained = Vec::new();
                retained
                    .try_reserve_exact(candidates.len())
                    .map_err(|_| WindowFactsError::Refused)?;
                retained.extend_from_slice(candidates);
                facts[slot + index].matches = retained;
            }
            slot += read_count;
        }
        Ok(())
    }

    /// Phase C: byte-steps from `start` exactly as the serial cursor would
    /// after a failed window, consuming precomputed facts whenever the
    /// position is slice-aligned. Returns `Realigned` when a match lands on
    /// an aligned offset so the merge loop can resume from facts. Gap bytes
    /// are read through the serial scanner's bounded cursor, so only the gap
    /// region is touched and only two windows are ever resident.
    fn rolling_resync_ordered(
        &self,
        resync: &OrderedResync<'_>,
        start: usize,
        blocks: &mut ScanBlockState<'_>,
        stats: &mut FileScanStats,
        current_step_run: &mut u64,
    ) -> Result<ResyncOutcome> {
        let slice_size = self.table.slice_size as usize;
        let mut preferred_next: Option<usize> = None;
        let mut cursor =
            OrderedWindowCursor::new_at(resync.path, slice_size, &self.window_table, start)?;

        loop {
            let expected_block = preferred_next
                .and_then(|position| resync.ordered_full_blocks.get(position))
                .copied();
            let offset = cursor.offset();
            let selection = OrderedSelection {
                path: resync.path,
                kind: resync.kind,
                target_file_id: resync.target_file_id,
                expected_block,
                offset: offset as u64,
            };
            let aligned_window = offset
                .is_multiple_of(slice_size)
                .then(|| offset / slice_size);
            let selected = if let Some(window_index) = aligned_window {
                self.select_ordered_match(selection, &resync.facts[window_index].matches, blocks)
            } else {
                let matches =
                    self.ordered_window_md5_matches(blocks.baseline(), cursor.data(), cursor.crc());
                self.select_ordered_match(selection, &matches, blocks)
            };

            if let Some(selected) = selected {
                let next_preferred = ordered_preferred_after_selection(
                    resync.ordered_full_blocks,
                    selected,
                    resync.target_file_id,
                    blocks,
                );
                stats.jumps_taken += 1;
                stats.max_consecutive_steps = stats.max_consecutive_steps.max(*current_step_run);
                *current_step_run = 0;
                if let Some(window_index) = aligned_window {
                    return Ok(ResyncOutcome::Realigned {
                        next_window: window_index + 1,
                        preferred_next: next_preferred,
                    });
                }
                preferred_next = next_preferred;
                // The cursor jump recomputes a fresh window CRC, matching the
                // serial scanner's post-jump recompute.
                if !cursor.jump(slice_size)? {
                    return Ok(ResyncOutcome::End);
                }
                continue;
            }

            preferred_next = None;
            if !cursor.step()? {
                return Ok(ResyncOutcome::End);
            }
            stats.windows_stepped += 1;
            *current_step_run += 1;
        }
    }

    #[cfg(test)]
    fn scan_file_buffered_with_target(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        blocks: &mut [SourceBlock],
        read_target: usize,
    ) -> Result<FileScanStats> {
        self.scan_file_buffered_with_target_options(
            path,
            kind,
            SourceFileScanLookup {
                files,
                file_index_by_id,
            },
            blocks,
            read_target,
            ScanSkipOptions::disabled(),
        )
    }

    #[cfg(test)]
    fn scan_file_buffered_with_target_options(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        lookup: SourceFileScanLookup<'_>,
        blocks: &mut [SourceBlock],
        read_target: usize,
        scan_options: ScanSkipOptions,
    ) -> Result<FileScanStats> {
        let baseline = blocks.to_vec();
        let mut state = ScanBlockState::new(&baseline);
        let stats = self.scan_file_buffered_with_target_state_options(
            path,
            kind,
            lookup,
            &mut state,
            read_target,
            scan_options,
        )?;
        self.relocate_open_short_blocks_in(path, kind, &mut state)?;
        state.apply_to_blocks(blocks);
        Ok(stats)
    }

    fn scan_file_buffered_with_target_state_options(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        lookup: SourceFileScanLookup<'_>,
        blocks: &mut ScanBlockState<'_>,
        read_target: usize,
        scan_options: ScanSkipOptions,
    ) -> Result<FileScanStats> {
        let mut file = File::open(path)?;
        let len = file.metadata()?.len() as usize;
        crate::file_cache::advise_sequential(&file, path, len as u64);
        let mut stats = FileScanStats::new(FileScanMode::RollingGeneric, len as u64);
        if len == 0 {
            return Ok(stats);
        }

        let mut total_read = 0usize;
        let slice_size = self.table.slice_size as usize;
        if slice_size > 0 && len >= slice_size {
            let overlap = slice_size - 1;
            let fresh_read_target = slice_size.max(read_target);
            let buffer_len = overlap.checked_add(fresh_read_target).ok_or_else(|| {
                io::Error::new(io::ErrorKind::InvalidInput, "scanner buffer size overflow")
            })?;
            let mut buffer = vec![0u8; buffer_len];
            let mut valid_len = 0usize;
            let mut base_offset = 0usize;
            let mut next_unscanned_offset = 0usize;
            let mut scan_progress = RollingScanProgress::new(scan_options, slice_size);
            let mut scan_context = BufferedWindowScan {
                scanner: self,
                path,
                kind,
                blocks,
                scan_options,
                progress: &mut scan_progress,
                stats: &mut stats,
            };

            loop {
                if valid_len == buffer.len() {
                    let keep = overlap.min(valid_len);
                    buffer.copy_within(valid_len - keep..valid_len, 0);
                    base_offset += valid_len - keep;
                    valid_len = keep;
                }

                let read_len = file.read(&mut buffer[valid_len..])?;
                total_read += read_len;
                valid_len += read_len;

                scan_buffered_windows(
                    &mut scan_context,
                    &buffer[..valid_len],
                    base_offset,
                    &mut next_unscanned_offset,
                );

                if read_len == 0 {
                    break;
                }
            }
        }

        if !scan_options.skip_data {
            stats.max_consecutive_steps = stats.windows_stepped;
        }
        let short_result = scan_short_blocks_from_file(
            self.table,
            path,
            kind,
            lookup.files,
            lookup.file_index_by_id,
            blocks,
            len,
        );
        crate::file_cache::drop_touched_file_cache(&file, path, len as u64, 0, total_read as u64);
        short_result?;

        Ok(stats)
    }

    #[cfg(test)]
    fn scan_file_mmap(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        blocks: &mut [SourceBlock],
    ) -> Result<FileScanStats> {
        self.scan_file_mmap_with_options(
            path,
            kind,
            files,
            file_index_by_id,
            blocks,
            ScanSkipOptions::disabled(),
        )
    }

    #[cfg(test)]
    fn scan_file_mmap_with_options(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        blocks: &mut [SourceBlock],
        scan_options: ScanSkipOptions,
    ) -> Result<FileScanStats> {
        let baseline = blocks.to_vec();
        let mut state = ScanBlockState::new(&baseline);
        let stats = self.scan_file_mmap_with_state_options(
            path,
            kind,
            files,
            file_index_by_id,
            &mut state,
            scan_options,
        )?;
        self.relocate_open_short_blocks_in(path, kind, &mut state)?;
        state.apply_to_blocks(blocks);
        Ok(stats)
    }

    fn scan_file_mmap_with_state_options(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        files: &[SourceFileEntry],
        file_index_by_id: &HashMap<FileId, usize>,
        blocks: &mut ScanBlockState<'_>,
        scan_options: ScanSkipOptions,
    ) -> Result<FileScanStats> {
        let file = File::open(path)?;
        let len = file.metadata()?.len() as usize;
        crate::file_cache::advise_sequential(&file, path, len as u64);
        let mut stats = FileScanStats::new(FileScanMode::RollingGeneric, len as u64);
        if len == 0 {
            return Ok(stats);
        }

        let map = MappedFile::map(&file)?;
        let slice_size = self.table.slice_size as usize;
        if slice_size > 0 && len >= slice_size {
            let mut crc = checksum::crc32(&map[..slice_size]);
            let last = len - slice_size;
            let scan_distance = scan_options.scan_distance(slice_size);
            let scan_skip = if scan_distance > 0 {
                slice_size.saturating_sub(scan_distance)
            } else {
                0
            };
            let mut scan_progress = RollingScanProgress::new(scan_options, slice_size);
            let scanner_batch_lanes = scanner_md5_batch_lanes(slice_size);
            let mut pending = Vec::with_capacity(scanner_batch_lanes);
            let mut offset = 0usize;
            while offset <= last {
                let mut saw_crc_candidate = false;
                if let Some(candidates) = self.table.by_crc.get(&crc) {
                    for block_index in candidates {
                        let block = blocks.block(*block_index);
                        if block.expected_len != self.table.slice_size {
                            continue;
                        }
                        if !can_record_block_location(blocks, *block_index, path, kind) {
                            continue;
                        }
                        saw_crc_candidate = true;
                        let data = &map[offset..offset + slice_size];
                        if scanner_batch_lanes < 2 {
                            record_matching_md5_block(
                                blocks,
                                *block_index,
                                data,
                                path,
                                offset as u64,
                                block.expected_len,
                                kind,
                            );
                            continue;
                        }
                        pending.push(PendingMd5Check {
                            block_index: *block_index,
                            data,
                            offset: offset as u64,
                            len: block.expected_len,
                            kind,
                        });
                        if pending.len() == scanner_batch_lanes {
                            flush_pending_md5_checks(&mut pending, blocks, path);
                        }
                    }
                }
                if offset < last {
                    crc = crc_slide_char(
                        crc,
                        map[offset + slice_size],
                        map[offset],
                        &self.window_table,
                    );
                    offset += 1;
                    scan_progress.record_step(&mut stats);

                    if scan_skip > 0 {
                        if saw_crc_candidate {
                            scan_progress.scan_offset = scan_distance / 2;
                        } else {
                            scan_progress.scan_offset = scan_progress.scan_offset.saturating_add(1);
                            if scan_progress.scan_offset >= scan_distance && offset < last {
                                scan_progress.record_jump(&mut stats);
                                scan_progress.scan_offset = 0;
                                offset = offset.saturating_add(scan_skip).min(last);
                                crc = checksum::crc32(&map[offset..offset + slice_size]);
                            }
                        }
                    }
                } else {
                    break;
                }
            }
            flush_pending_md5_checks(&mut pending, blocks, path);
            stats.max_consecutive_steps = stats
                .max_consecutive_steps
                .max(scan_progress.current_step_run);
        }
        if !scan_options.skip_data {
            stats.max_consecutive_steps = stats.windows_stepped;
        }

        for block_index in &self.table.short_blocks {
            if blocks.location(*block_index).is_some() {
                continue;
            }
            let block = blocks.block(*block_index);
            let short_len = block.expected_len as usize;
            if short_len == 0 || short_len > len {
                continue;
            }
            if let Some(file) = file_index_by_id
                .get(&block.file_id)
                .and_then(|idx| files.get(*idx))
                && file.safe_path == path
            {
                let offset = block.local_index as u64 * self.table.slice_size;
                if offset <= usize::MAX as u64 {
                    let offset = offset as usize;
                    if offset.checked_add(short_len).is_some_and(|end| end <= len)
                        && short_block_matches(
                            &map[offset..offset + short_len],
                            self.table.slice_size,
                            block,
                        )
                    {
                        record_block_location(
                            blocks,
                            *block_index,
                            BlockLocation {
                                source: SourceLocation::Path(path.to_path_buf()),
                                offset: offset as u64,
                                len: block.expected_len,
                                kind,
                            },
                        );
                        continue;
                    }
                }
            }
            let tail_offset = len - short_len;
            if short_block_matches(
                &map[tail_offset..tail_offset + short_len],
                self.table.slice_size,
                block,
            ) {
                record_block_location(
                    blocks,
                    *block_index,
                    BlockLocation {
                        source: SourceLocation::Path(path.to_path_buf()),
                        offset: tail_offset as u64,
                        len: block.expected_len,
                        kind,
                    },
                );
            }
        }

        drop(map);
        crate::file_cache::drop_file_cache(&file, path, 0, len as u64);
        Ok(stats)
    }

    /// Test-only mirror of the production two-phase shape: scan one candidate,
    /// then run the relocation search over whatever short blocks that scan left
    /// open. Production defers the same search to
    /// [`RepairState::relocate_open_short_blocks`], which runs it once over the
    /// merged state of a whole candidate batch instead of once per candidate.
    #[cfg(test)]
    fn relocate_open_short_blocks_in(
        &self,
        path: &Path,
        kind: BlockLocationKind,
        blocks: &mut ScanBlockState<'_>,
    ) -> Result<ShortRelocationStats> {
        let mut stats = ShortRelocationStats::default();
        let Ok(metadata) = fs::metadata(path) else {
            return Ok(stats);
        };
        let len = metadata.len() as usize;
        if len == 0 {
            return Ok(stats);
        }
        let open = open_short_blocks(self.table, blocks, self.table.slice_size);
        let mut scan = ShortRelocationScan {
            table: self.table,
            path,
            kind,
            open: &open,
            blocks,
            stats: &mut stats,
        };
        scan_shifted_short_blocks_from_file(&mut scan, len)?;
        Ok(stats)
    }
}

fn ordered_scan_force_serial() -> bool {
    static FORCE_SERIAL: LazyLock<bool> = LazyLock::new(|| {
        std::env::var(ORDERED_SCAN_SERIAL_ENV)
            .is_ok_and(|value| value == "1" || value.eq_ignore_ascii_case("true"))
    });
    *FORCE_SERIAL
}

/// The parallel ordered scan is the default. Its first cut (whole-file
/// mmap + padded-lane MD5 batching) measured slower than the serial cursor
/// at 40x the memory and was made opt-in; the rework onto bounded buffered
/// reads and single-shot MD5 then passed the recorded gate on the x86 box
/// (damaged 2 GB verify: parallel 5.44 s / 81 MB max RSS vs serial 7.10 s /
/// 53 MB), flipping the default here. `WEAVER_PAR2_PARALLEL_SCAN=0`
/// disables it; `WEAVER_PAR2_SERIAL_SCAN=1` remains the hard force.
fn ordered_scan_parallel_enabled() -> bool {
    static ENABLED: LazyLock<bool> = LazyLock::new(|| {
        !std::env::var(ORDERED_SCAN_PARALLEL_ENV)
            .is_ok_and(|value| value == "0" || value.eq_ignore_ascii_case("false"))
    });
    *ENABLED
}

fn ordered_scan_segment_windows(slice_size: usize) -> usize {
    if slice_size == 0 {
        return 1;
    }
    (SCANNER_PARALLEL_SEGMENT_TARGET_BYTES / slice_size).clamp(1, 4096)
}

/// The serial scanner's post-selection `preferred_next` rule: continue the
/// chain past the selected block when it belongs to the target file, drop it
/// otherwise.
fn ordered_preferred_after_selection(
    ordered_full_blocks: &[usize],
    selected: usize,
    target_file_id: &FileId,
    blocks: &ScanBlockState<'_>,
) -> Option<usize> {
    if blocks.block(selected).file_id != *target_file_id {
        return None;
    }
    ordered_full_blocks
        .iter()
        .position(|block_index| *block_index == selected)
        .and_then(|position| ordered_full_blocks.get(position + 1).map(|_| position + 1))
}

fn ordered_match_rank(
    block_index: usize,
    expected_block: Option<usize>,
    preferred_file_id: FileId,
    blocks: &ScanBlockState<'_>,
) -> (u8, usize) {
    if Some(block_index) == expected_block {
        return (0, block_index);
    }
    if blocks.block(block_index).file_id == preferred_file_id {
        return (1, block_index);
    }
    (2, block_index)
}

fn can_select_ordered_match(
    block_index: usize,
    expected_block: Option<usize>,
    path: &Path,
    blocks: &ScanBlockState<'_>,
) -> bool {
    match blocks.location(block_index) {
        None => true,
        Some(location) if Some(block_index) == expected_block => !location.source.is_path(path),
        Some(_) => false,
    }
}

fn preferred_ordered_match(
    current: Option<usize>,
    candidate: usize,
    expected_block: Option<usize>,
    preferred_file_id: FileId,
    blocks: &ScanBlockState<'_>,
) -> bool {
    let candidate_rank = ordered_match_rank(candidate, expected_block, preferred_file_id, blocks);
    current.is_none_or(|current| {
        candidate_rank < ordered_match_rank(current, expected_block, preferred_file_id, blocks)
    })
}

fn log_file_scan(
    path: &Path,
    kind: BlockLocationKind,
    stats: FileScanStats,
    blocks_confirmed: u32,
    elapsed: Duration,
) {
    debug!(
        path = %path.display(),
        ?kind,
        scan_mode = stats.mode.as_str(),
        bytes_scanned = stats.bytes_scanned,
        windows_stepped = stats.windows_stepped,
        jumps_taken = stats.jumps_taken,
        max_consecutive_steps = stats.max_consecutive_steps,
        blocks_confirmed,
        elapsed_ms = elapsed.as_millis(),
        "completed par2 file scan"
    );

    if stats.max_consecutive_steps >= SCANNER_SLOW_WARN_STEPS
        || elapsed >= SCANNER_SLOW_WARN_DURATION
    {
        warn!(
            path = %path.display(),
            ?kind,
            scan_mode = stats.mode.as_str(),
            bytes_scanned = stats.bytes_scanned,
            windows_stepped = stats.windows_stepped,
            jumps_taken = stats.jumps_taken,
            max_consecutive_steps = stats.max_consecutive_steps,
            blocks_confirmed,
            elapsed_ms = elapsed.as_millis(),
            "slow par2 file scan"
        );
    }
}

/// One candidate's share of the deferred short-block relocation search.
///
/// The ordinary file-scan counters never see this work — it happens after the
/// scan, over a candidate the scan already read — so it gets its own record.
/// `short_lengths` is what the sweep actually looked for: one full pass over
/// the candidate per entry.
fn log_short_relocation(
    path: &Path,
    kind: BlockLocationKind,
    short_lengths: &[usize],
    stats: &ShortRelocationStats,
    elapsed: Duration,
) {
    debug!(
        path = %path.display(),
        ?kind,
        scan_mode = "short_relocation",
        short_lengths = ?short_lengths,
        short_lengths_attempted = short_lengths.len(),
        windows_stepped = stats.windows_stepped,
        bytes_reread = stats.bytes_read,
        blocks_placed = stats.blocks_placed,
        elapsed_ms = elapsed.as_millis(),
        "completed par2 short-block relocation scan"
    );

    if stats.windows_stepped >= SCANNER_SLOW_WARN_STEPS || elapsed >= SCANNER_SLOW_WARN_DURATION {
        warn!(
            path = %path.display(),
            ?kind,
            scan_mode = "short_relocation",
            short_lengths = ?short_lengths,
            short_lengths_attempted = short_lengths.len(),
            windows_stepped = stats.windows_stepped,
            bytes_reread = stats.bytes_read,
            blocks_placed = stats.blocks_placed,
            elapsed_ms = elapsed.as_millis(),
            "slow par2 short-block relocation scan"
        );
    }
}

fn log_short_relocation_pass(
    candidates_considered: usize,
    candidates_scanned: u32,
    candidates_skipped: u32,
    open_short_blocks: usize,
    stats: &ShortRelocationStats,
    elapsed: Duration,
) {
    debug!(
        candidates_considered,
        candidates_scanned,
        candidates_skipped,
        open_short_blocks,
        windows_stepped = stats.windows_stepped,
        bytes_reread = stats.bytes_read,
        blocks_placed = stats.blocks_placed,
        elapsed_ms = elapsed.as_millis(),
        "completed par2 short-block relocation pass"
    );

    if stats.windows_stepped >= SCANNER_SLOW_WARN_STEPS || elapsed >= SCANNER_SLOW_WARN_DURATION {
        warn!(
            candidates_considered,
            candidates_scanned,
            candidates_skipped,
            open_short_blocks,
            windows_stepped = stats.windows_stepped,
            bytes_reread = stats.bytes_read,
            blocks_placed = stats.blocks_placed,
            elapsed_ms = elapsed.as_millis(),
            "slow par2 short-block relocation pass"
        );
    }
}

fn scan_buffered_windows(
    scan: &mut BufferedWindowScan<'_, '_, '_>,
    buffer: &[u8],
    base_offset: usize,
    next_unscanned_offset: &mut usize,
) {
    let scanner = scan.scanner;
    let path = scan.path;
    let kind = scan.kind;
    let scan_options = scan.scan_options;
    let slice_size = scanner.table.slice_size as usize;
    if slice_size == 0 || buffer.len() < slice_size {
        return;
    }

    let last_local_offset = buffer.len() - slice_size;
    let mut local_offset = next_unscanned_offset.saturating_sub(base_offset);
    if local_offset > last_local_offset {
        return;
    }

    let scan_distance = scan_options.scan_distance(slice_size);
    let scan_skip = if scan_distance > 0 {
        slice_size.saturating_sub(scan_distance)
    } else {
        0
    };
    let scanner_batch_lanes = scanner_md5_batch_lanes(slice_size);
    let mut pending = Vec::with_capacity(scanner_batch_lanes);
    let mut crc = checksum::crc32(&buffer[local_offset..local_offset + slice_size]);

    loop {
        let mut saw_crc_candidate = false;
        if let Some(candidates) = scanner.table.by_crc.get(&crc) {
            for block_index in candidates {
                let expected_len = scan.blocks.block(*block_index).expected_len;
                if expected_len != scanner.table.slice_size {
                    continue;
                }
                if !can_record_block_location(scan.blocks, *block_index, path, kind) {
                    continue;
                }
                saw_crc_candidate = true;
                let data = &buffer[local_offset..local_offset + slice_size];
                let absolute_offset = (base_offset + local_offset) as u64;
                if scanner_batch_lanes < 2 {
                    record_matching_md5_block(
                        scan.blocks,
                        *block_index,
                        data,
                        path,
                        absolute_offset,
                        expected_len,
                        kind,
                    );
                    continue;
                }
                pending.push(PendingMd5Check {
                    block_index: *block_index,
                    data,
                    offset: absolute_offset,
                    len: expected_len,
                    kind,
                });
                if pending.len() == scanner_batch_lanes {
                    flush_pending_md5_checks(&mut pending, scan.blocks, path);
                }
            }
        }

        if local_offset == last_local_offset {
            break;
        }

        crc = crc_slide_char(
            crc,
            buffer[local_offset + slice_size],
            buffer[local_offset],
            &scanner.window_table,
        );
        local_offset += 1;
        scan.progress.record_step(scan.stats);
        *next_unscanned_offset = base_offset + local_offset;

        if scan_skip > 0 {
            if saw_crc_candidate {
                scan.progress.scan_offset = scan_distance / 2;
            } else {
                scan.progress.scan_offset = scan.progress.scan_offset.saturating_add(1);
                if scan.progress.scan_offset >= scan_distance && local_offset < last_local_offset {
                    let jump_offset = (base_offset + local_offset).saturating_add(scan_skip);
                    scan.progress.record_jump(scan.stats);
                    scan.progress.scan_offset = 0;

                    if jump_offset > base_offset + last_local_offset {
                        *next_unscanned_offset = jump_offset;
                        flush_pending_md5_checks(&mut pending, scan.blocks, path);
                        scan.stats.max_consecutive_steps = scan
                            .stats
                            .max_consecutive_steps
                            .max(scan.progress.current_step_run);
                        return;
                    }

                    local_offset = jump_offset - base_offset;
                    *next_unscanned_offset = jump_offset;
                    crc = checksum::crc32(&buffer[local_offset..local_offset + slice_size]);
                }
            }
        }
    }

    flush_pending_md5_checks(&mut pending, scan.blocks, path);
    *next_unscanned_offset = base_offset + last_local_offset + 1;
    scan.stats.max_consecutive_steps = scan
        .stats
        .max_consecutive_steps
        .max(scan.progress.current_step_run);
}

fn scan_short_blocks_from_file(
    table: &VerificationHashTable,
    path: &Path,
    kind: BlockLocationKind,
    files: &[SourceFileEntry],
    file_index_by_id: &HashMap<FileId, usize>,
    blocks: &mut ScanBlockState<'_>,
    len: usize,
) -> Result<()> {
    let max_tail_len = table
        .short_blocks
        .iter()
        .filter_map(|block_index| {
            let block = blocks.block(*block_index);
            let short_len = block.expected_len as usize;
            (blocks.location(*block_index).is_none() && short_len > 0 && short_len <= len)
                .then_some(short_len)
        })
        .max()
        .unwrap_or(0);

    let tail = if max_tail_len > 0 {
        read_exact_file_range(path, (len - max_tail_len) as u64, max_tail_len)?
    } else {
        Vec::new()
    };

    for block_index in &table.short_blocks {
        if blocks.location(*block_index).is_some() {
            continue;
        }
        let block = blocks.block(*block_index);
        let short_len = block.expected_len as usize;
        if short_len == 0 || short_len > len {
            continue;
        }
        if let Some(file) = file_index_by_id
            .get(&block.file_id)
            .and_then(|idx| files.get(*idx))
            && file.safe_path == path
        {
            let offset = block.local_index as u64 * table.slice_size;
            if offset <= usize::MAX as u64 {
                let offset = offset as usize;
                if offset.checked_add(short_len).is_some_and(|end| end <= len) {
                    let data = read_exact_file_range(path, offset as u64, short_len)?;
                    if short_block_matches(&data, table.slice_size, block) {
                        record_block_location(
                            blocks,
                            *block_index,
                            BlockLocation {
                                source: SourceLocation::Path(path.to_path_buf()),
                                offset: offset as u64,
                                len: block.expected_len,
                                kind,
                            },
                        );
                        continue;
                    }
                }
            }
        }

        let tail_offset = len - short_len;
        let tail_start = tail.len() - short_len;
        if short_block_matches(&tail[tail_start..], table.slice_size, block) {
            record_block_location(
                blocks,
                *block_index,
                BlockLocation {
                    source: SourceLocation::Path(path.to_path_buf()),
                    offset: tail_offset as u64,
                    len: block.expected_len,
                    kind,
                },
            );
        }
    }

    Ok(())
}

/// Everything the exhaustive short-block relocation search works on for one
/// candidate: the table it matches against, the candidate it re-reads, the set
/// of short blocks whose placement is still open, the shared block state it
/// records into, and its own accounting.
struct ShortRelocationScan<'a, 'blocks> {
    table: &'a VerificationHashTable,
    path: &'a Path,
    kind: BlockLocationKind,
    /// Indexed by block index; `true` for a short block still worth hunting.
    open: &'a [bool],
    blocks: &'a mut ScanBlockState<'blocks>,
    stats: &'a mut ShortRelocationStats,
}

/// The per-length constants of one relocation sweep, hoisted out of the
/// window loop.
struct ShortWindowParams<'a> {
    short_len: usize,
    zero_combine: &'a checksum::Crc32CombineOp,
    zero_crc: u32,
    window_table: &'a [u32; 256],
}

/// Sweep one candidate for every still-open short length that fits in it.
/// Returns the lengths it attempted, for logging.
fn scan_shifted_short_blocks_from_file(
    scan: &mut ShortRelocationScan<'_, '_>,
    len: usize,
) -> Result<Vec<usize>> {
    let lengths = open_short_lengths(scan.table, scan.blocks, scan.open, len);
    for short_len in &lengths {
        scan_shifted_short_len_from_file(scan, len, *short_len)?;
    }

    Ok(lengths)
}

/// Short block placements the relocation search is still allowed to improve.
///
/// A short block already sitting at its own slice offset is settled: whichever
/// container it was found in is a positional copy of its file — the file
/// itself, or a renamed or obfuscated copy of it — so the block is exactly
/// where it belongs and the same MD5-verified bytes found elsewhere could only
/// be an equivalent source. Hunting for it again is pure cost, and it is that
/// cost, repeated per candidate, that made a healthy multi-file set quadratic.
///
/// A placement at any other offset stays open, so a better placement can still
/// displace it exactly as it could when every candidate searched its own
/// snapshot and the merge arbitrated between them.
fn open_short_blocks(
    table: &VerificationHashTable,
    blocks: &ScanBlockState<'_>,
    slice_size: u64,
) -> Vec<bool> {
    let mut open = vec![false; blocks.baseline().len()];
    for block_index in &table.short_blocks {
        open[*block_index] = !short_block_is_settled(blocks, *block_index, slice_size);
    }
    open
}

fn short_block_is_settled(
    blocks: &ScanBlockState<'_>,
    block_index: usize,
    slice_size: u64,
) -> bool {
    let Some(location) = blocks.location(block_index) else {
        return false;
    };
    let block = blocks.block(block_index);
    location.offset == u64::from(block.local_index).saturating_mul(slice_size)
        && location.len == block.expected_len
}

/// Total bytes the merged spans cover, merging overlaps. Sorts in place.
fn merged_span_bytes(spans: &mut [(u64, u64)]) -> u64 {
    spans.sort_unstable();
    let mut covered = 0u64;
    let mut reach = 0u64;
    for (offset, len) in spans.iter() {
        let end = offset.saturating_add(*len);
        let start = (*offset).max(reach);
        if end > start {
            covered = covered.saturating_add(end - start);
            reach = end;
        }
    }
    covered
}

/// The distinct short lengths still worth sweeping a `len`-byte candidate for.
/// Deduping by length is what makes the sweep affordable: one pass over the
/// candidate answers every open short block of that length at once.
fn open_short_lengths(
    table: &VerificationHashTable,
    blocks: &ScanBlockState<'_>,
    open: &[bool],
    len: usize,
) -> Vec<usize> {
    let mut lengths: Vec<usize> = table
        .short_blocks
        .iter()
        .filter_map(|block_index| {
            let block = blocks.block(*block_index);
            let short_len = block.expected_len as usize;
            (open.get(*block_index).copied().unwrap_or(false) && short_len > 0 && short_len <= len)
                .then_some(short_len)
        })
        .collect();
    lengths.sort_unstable();
    lengths.dedup();
    lengths
}

fn scan_shifted_short_len_from_file(
    scan: &mut ShortRelocationScan<'_, '_>,
    len: usize,
    short_len: usize,
) -> Result<()> {
    if short_len == 0 || short_len > len {
        return Ok(());
    }
    let table = scan.table;
    let path = scan.path;

    if short_len > SCANNER_IO_TARGET_BYTES {
        let file = File::open(path)?;
        let map = MappedFile::map(&file)?;
        scan.stats.bytes_read = scan.stats.bytes_read.saturating_add(map.len() as u64);
        scan_shifted_short_len_from_slice(scan, &map, short_len);
        drop(map);
        crate::file_cache::drop_file_cache(&file, path, 0, len as u64);
        return Ok(());
    }

    let mut file = File::open(path)?;
    let overlap = short_len.saturating_sub(1);
    let fresh_read_target = SCANNER_IO_TARGET_BYTES;
    let buffer_len = overlap.checked_add(fresh_read_target).ok_or_else(|| {
        io::Error::new(io::ErrorKind::InvalidInput, "scanner buffer size overflow")
    })?;
    let mut buffer = vec![0u8; buffer_len];
    let mut valid_len = 0usize;
    let mut base_offset = 0usize;
    let mut next_unscanned_offset = 0usize;
    let mut total_read = 0usize;
    let window_table = generate_window_table(short_len as u64);
    let pad_len = table.slice_size.saturating_sub(short_len as u64);
    let zero_crc = crc32_zeros(pad_len);
    let zero_combine = checksum::Crc32CombineOp::new(pad_len);

    loop {
        if valid_len == buffer.len() {
            let keep = overlap.min(valid_len);
            buffer.copy_within(valid_len - keep..valid_len, 0);
            base_offset += valid_len - keep;
            valid_len = keep;
        }

        let read_len = file.read(&mut buffer[valid_len..])?;
        total_read += read_len;
        valid_len += read_len;
        scan.stats.bytes_read = scan.stats.bytes_read.saturating_add(read_len as u64);

        scan_shifted_short_windows(
            scan,
            &ShortWindowParams {
                short_len,
                zero_combine: &zero_combine,
                zero_crc,
                window_table: &window_table,
            },
            &buffer[..valid_len],
            base_offset,
            &mut next_unscanned_offset,
        );

        if read_len == 0 {
            break;
        }
    }

    crate::file_cache::drop_touched_file_cache(&file, path, len as u64, 0, total_read as u64);
    Ok(())
}

fn scan_shifted_short_len_from_slice(
    scan: &mut ShortRelocationScan<'_, '_>,
    data: &[u8],
    short_len: usize,
) {
    if short_len == 0 || data.len() < short_len {
        return;
    }

    let pad_len = scan.table.slice_size.saturating_sub(short_len as u64);
    let zero_crc = crc32_zeros(pad_len);
    let zero_combine = checksum::Crc32CombineOp::new(pad_len);
    let window_table = generate_window_table(short_len as u64);
    let mut next_unscanned_offset = 0usize;
    scan_shifted_short_windows(
        scan,
        &ShortWindowParams {
            short_len,
            zero_combine: &zero_combine,
            zero_crc,
            window_table: &window_table,
        },
        data,
        0,
        &mut next_unscanned_offset,
    );
}

fn scan_shifted_short_windows(
    scan: &mut ShortRelocationScan<'_, '_>,
    params: &ShortWindowParams<'_>,
    buffer: &[u8],
    base_offset: usize,
    next_unscanned_offset: &mut usize,
) {
    let ShortWindowParams {
        short_len,
        zero_combine,
        zero_crc,
        window_table,
    } = *params;
    let table = scan.table;
    let path = scan.path;
    let kind = scan.kind;
    if short_len == 0 || buffer.len() < short_len {
        return;
    }

    let last_local_offset = buffer.len() - short_len;
    let mut local_offset = next_unscanned_offset.saturating_sub(base_offset);
    if local_offset > last_local_offset {
        return;
    }

    let mut crc = checksum::crc32(&buffer[local_offset..local_offset + short_len]);
    let mut windows_stepped = 0u64;
    loop {
        let padded_crc = zero_combine.combine(crc, zero_crc);
        if let Some(candidates) = table.by_crc.get(&padded_crc) {
            let data = &buffer[local_offset..local_offset + short_len];
            let absolute_offset = (base_offset + local_offset) as u64;
            for block_index in candidates {
                let block = scan.blocks.block(*block_index);
                // Gating on the recording guard, not only on `open`, keeps the
                // sweep from taking a hold it is not allowed to displace — an
                // access-backed one above all — and skips the MD5 confirmation
                // for any block whose placement could not have stood anyway.
                if !scan.open.get(*block_index).copied().unwrap_or(false)
                    || block.expected_len as usize != short_len
                    || !can_record_block_location(scan.blocks, *block_index, path, kind)
                {
                    continue;
                }
                if short_block_matches(data, table.slice_size, block) {
                    scan.stats.blocks_placed = scan.stats.blocks_placed.saturating_add(1);
                    record_block_location(
                        scan.blocks,
                        *block_index,
                        BlockLocation {
                            source: SourceLocation::Path(path.to_path_buf()),
                            offset: absolute_offset,
                            len: short_len as u64,
                            kind,
                        },
                    );
                }
            }
        }

        if local_offset == last_local_offset {
            break;
        }

        crc = crc_slide_char(
            crc,
            buffer[local_offset + short_len],
            buffer[local_offset],
            window_table,
        );
        local_offset += 1;
        windows_stepped += 1;
    }

    scan.stats.windows_stepped = scan.stats.windows_stepped.saturating_add(windows_stepped);
    *next_unscanned_offset = base_offset + last_local_offset + 1;
}

fn read_exact_file_range(path: &Path, offset: u64, len: usize) -> io::Result<Vec<u8>> {
    let mut file = File::open(path)?;
    let file_len = file.metadata()?.len();
    file.seek(SeekFrom::Start(offset))?;
    let mut data = vec![0u8; len];
    file.read_exact(&mut data)?;
    crate::file_cache::drop_touched_file_cache(&file, path, file_len, offset, len as u64);
    Ok(data)
}

fn scanner_uses_mmap_fallback(slice_size: u64) -> bool {
    slice_size > SCANNER_MMAP_FALLBACK_SLICE_BYTES as u64
}

fn record_block_location(
    blocks: &mut ScanBlockState<'_>,
    block_index: usize,
    location: BlockLocation,
) {
    blocks.record_location(block_index, location);
}

fn can_record_block_location(
    blocks: &ScanBlockState<'_>,
    block_index: usize,
    path: &Path,
    kind: BlockLocationKind,
) -> bool {
    blocks.location(block_index).is_none_or(|existing| {
        // Scanning only ever produces path locations, so an access-backed
        // incumbent (which scanning cannot have produced) is never displaced.
        kind < existing.kind
            || (kind == existing.kind && existing.path().is_some_and(|held| path < held))
    })
}

/// Candidate blocks batched per multi-buffer MD5 call while scanning.
///
/// The kernel width comes from the ISA ([`md5_simd::max_lanes`]: 8 on AVX2, 4
/// on NEON/SSE2/simd128, 1 scalar); the memory budget caps how many
/// slice-sized candidates may be held at once on top of that.
fn scanner_md5_batch_lanes(slice_size: usize) -> usize {
    if slice_size == 0 {
        return 1;
    }
    (SCANNER_MD5_BATCH_MEMORY_BYTES / slice_size).clamp(1, md5_simd::max_lanes())
}

fn record_matching_md5_block(
    blocks: &mut ScanBlockState<'_>,
    block_index: usize,
    data: &[u8],
    path: &Path,
    offset: u64,
    len: u64,
    kind: BlockLocationKind,
) {
    if !can_record_block_location(blocks, block_index, path, kind) {
        return;
    }
    let md5 = checksum::md5(data);
    if blocks.block(block_index).checksum.md5 == md5 {
        record_block_location(
            blocks,
            block_index,
            BlockLocation {
                source: SourceLocation::Path(path.to_path_buf()),
                offset,
                len,
                kind,
            },
        );
    }
}

fn flush_pending_md5_checks(
    pending: &mut Vec<PendingMd5Check<'_>>,
    blocks: &mut ScanBlockState<'_>,
    path: &Path,
) {
    if pending.is_empty() {
        return;
    }

    let inputs = pending.iter().map(|check| check.data).collect::<Vec<_>>();
    let md5s = md5_simd::md5_multi(&inputs, None);
    for (check, md5) in pending.iter().zip(md5s) {
        if !can_record_block_location(blocks, check.block_index, path, check.kind) {
            continue;
        }
        if blocks.block(check.block_index).checksum.md5 == md5 {
            record_block_location(
                blocks,
                check.block_index,
                BlockLocation {
                    source: SourceLocation::Path(path.to_path_buf()),
                    offset: check.offset,
                    len: check.len,
                    kind: check.kind,
                },
            );
        }
    }
    pending.clear();
}

fn short_block_matches(data: &[u8], slice_size: u64, block: &SourceBlock) -> bool {
    padded_crc(data, slice_size) == block.checksum.crc32
        && padded_md5(data, slice_size) == block.checksum.md5
}

fn check_cancel(options: &Par2RepairerOptions) -> Result<()> {
    if let Some(cancel) = options.cancel.as_ref()
        && cancel.is_cancelled()
    {
        return Err(Par2Error::Cancelled);
    }
    Ok(())
}

fn discover_adjacent_par2_files(par2_paths: &[PathBuf]) -> io::Result<Vec<PathBuf>> {
    let mut out = Vec::new();
    for path in par2_paths {
        out.extend(discover_related_par2_files(path)?);
    }
    out.sort();
    out.dedup();
    Ok(out)
}

fn discover_related_par2_files(path: &Path) -> io::Result<Vec<PathBuf>> {
    let dir = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    let Some(stem) = par2_base_name(path) else {
        return Ok(Vec::new());
    };

    let mut out = Vec::new();
    let Ok(entries) = fs::read_dir(dir) else {
        return Ok(out);
    };
    for entry in entries {
        let Ok(entry) = entry else {
            continue;
        };
        let Ok(file_type) = entry.file_type() else {
            continue;
        };
        if !file_type.is_file() {
            continue;
        }
        let candidate = entry.path();
        if candidate == path {
            continue;
        }
        if !is_par2_path(&candidate) || !related_par2_name_matches(&stem, &candidate) {
            continue;
        }
        out.push(candidate);
    }
    out.sort();
    Ok(out)
}

fn discover_source_primary_par2_file(path: &Path) -> io::Result<Option<PathBuf>> {
    let dir = path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .unwrap_or_else(|| Path::new("."));
    let Some(stem) = path.file_name().and_then(|name| name.to_str()) else {
        return Ok(None);
    };

    let lower = dir.join(format!("{stem}.par2"));
    if lower.is_file() {
        return Ok(Some(lower));
    }

    let upper = dir.join(format!("{stem}.PAR2"));
    if upper.is_file() {
        return Ok(Some(upper));
    }

    Ok(None)
}

fn related_par2_name_matches(stem: &str, path: &Path) -> bool {
    if stem.is_empty() {
        return true;
    }
    path.file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| {
            let suffix = name.strip_prefix(stem).unwrap_or_default();
            suffix.starts_with('.') && suffix[1..].contains('.')
        })
}

fn par2_base_name(path: &Path) -> Option<String> {
    let mut name = path.file_name()?.to_str()?.to_owned();
    loop {
        let dot = name.rfind('.')?;
        let tail = name[dot + 1..].to_owned();
        name.truncate(dot);
        if tail.eq_ignore_ascii_case("par2") {
            break;
        }
    }

    if let Some(dot) = name.rfind('.')
        && volume_suffix_matches(&name[dot + 1..])
    {
        name.truncate(dot);
    }

    Some(name)
}

fn volume_suffix_matches(tail: &str) -> bool {
    let mut state = 0u8;
    for byte in tail.bytes() {
        match state {
            0 if byte.eq_ignore_ascii_case(&b'v') => state = 1,
            1 if byte.eq_ignore_ascii_case(&b'o') => state = 2,
            2 if byte.eq_ignore_ascii_case(&b'l') => state = 3,
            3 if byte.is_ascii_digit() => {}
            3 if byte == b'-' || byte == b'+' => state = 4,
            4 if byte.is_ascii_digit() => {}
            _ => return false,
        }
    }
    true
}

fn discover_candidate_files(base_dir: &Path) -> io::Result<Vec<PathBuf>> {
    discover_files_matching(base_dir, |path| !has_par2_marker(path))
}

fn is_par2_path(path: &Path) -> bool {
    path.extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(|ext| ext == "par2" || ext == "PAR2")
}

fn has_par2_marker(path: &Path) -> bool {
    let path = path.to_string_lossy();
    path.contains(".par2") || path.contains(".PAR2")
}

fn canonical_extra_path(path: &Path) -> PathBuf {
    fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
}

fn discover_files_matching<F>(base_dir: &Path, mut matches: F) -> io::Result<Vec<PathBuf>>
where
    F: FnMut(&Path) -> bool,
{
    let mut out = Vec::new();
    let mut stack = vec![base_dir.to_path_buf()];
    while let Some(dir) = stack.pop() {
        let Ok(entries) = fs::read_dir(&dir) else {
            continue;
        };
        for entry in entries {
            let Ok(entry) = entry else {
                continue;
            };
            let path = entry.path();
            let Ok(file_type) = entry.file_type() else {
                continue;
            };
            if file_type.is_dir() {
                if !should_skip_candidate(&path) {
                    stack.push(path);
                }
            } else if file_type.is_file() && matches(&path) {
                out.push(path);
            }
        }
    }
    out.sort();
    Ok(out)
}

fn should_skip_candidate(path: &Path) -> bool {
    path.file_name()
        .and_then(|name| name.to_str())
        .is_some_and(is_generated_par2_artifact_name)
}

fn read_first_16k(path: &Path) -> io::Result<Vec<u8>> {
    let mut file = File::open(path)?;
    let file_len = file.metadata()?.len();
    let mut buf = vec![0u8; 16_384];
    // Fill, don't single-read: a short read here would silently hash fewer
    // bytes than the 16k quick hash is defined over. See `disk::read_filled`.
    let read = crate::disk::read_filled(&mut file, &mut buf)?;
    crate::file_cache::drop_touched_file_cache(&file, path, file_len, 0, read as u64);
    buf.truncate(read);
    Ok(buf)
}

fn hash_file(path: &Path) -> io::Result<[u8; 16]> {
    let mut file = File::open(path)?;
    let file_len = file.metadata()?.len();
    crate::file_cache::advise_sequential(&file, path, file_len);
    let mut hasher = Md5State::new();
    // The 1 MiB read buffer must live on the heap on every target: wasm's shadow
    // stack is ~1 MiB total, and MSVC reserves 1 MiB for the main thread, so a
    // frame this large overflows both. `verify_or_repair` runs on the caller's
    // thread, and rayon steal-on-block can nest this frame deeper still.
    //
    // Do not regress this to a guarded stack buffer either: when this function
    // inlines, LLVM hoists its static allocas into the caller's entry block,
    // so the reservation lands in the caller's prologue no matter which branch
    // runs — a caller-side guard like `should_skip_full_hash` skips the
    // hashing work, never the stack cost.
    let mut buf = vec![0u8; 1024 * 1024];
    let mut total_read = 0u64;
    loop {
        let read = file.read(&mut buf)?;
        if read == 0 {
            break;
        }
        hasher.update(&buf[..read]);
        total_read += read as u64;
    }
    crate::file_cache::drop_touched_file_cache(&file, path, file_len, 0, total_read);
    Ok(hasher.finalize())
}

const SOURCE_CHANGED_PREFIX: &str = "PAR2 source changed: ";
const VIRTUAL_SOURCE_CHANGED_PREFIX: &str = "PAR2 virtual source changed: file ";

fn source_changed_io(path: &Path) -> io::Error {
    io::Error::new(
        io::ErrorKind::InvalidData,
        format!("{SOURCE_CHANGED_PREFIX}{}", path.display()),
    )
}

/// Whether an error reports that a source moved out from under a read, in
/// either its path-backed or its access-backed spelling. A repair that
/// consumed a carried analysis treats this as "the carry was wrong after all"
/// and falls back to a fresh scan; nothing has been installed by the time it
/// can be raised.
fn is_source_changed_error(error: &Par2Error) -> bool {
    let Par2Error::Io(source) = error else {
        return false;
    };
    let message = source.to_string();
    message.starts_with(SOURCE_CHANGED_PREFIX) || message.starts_with(VIRTUAL_SOURCE_CHANGED_PREFIX)
}

/// The location-shaped counterpart to [`source_changed_io`]. Access-backed
/// sources have no path to name, so they report their PAR2 file identifier
/// under a distinct prefix that path-oriented callers do not misread.
fn source_location_changed_io(source: &SourceLocation) -> io::Error {
    match source {
        SourceLocation::Path(path) => source_changed_io(path),
        SourceLocation::Access(file_id) => io::Error::new(
            io::ErrorKind::InvalidData,
            format!("{VIRTUAL_SOURCE_CHANGED_PREFIX}{file_id}"),
        ),
    }
}

/// Fill `dst` from an access-backed source, refusing a short read. Access
/// implementations may return fewer bytes than requested; a source block is
/// only usable whole, so a short read is a changed source.
fn read_exact_from_access(
    access: &(dyn FileAccess + Send + Sync),
    file_id: &FileId,
    offset: u64,
    dst: &mut [u8],
) -> io::Result<()> {
    let mut filled = 0usize;
    while filled < dst.len() {
        let read =
            access.read_file_range_into(file_id, offset + filled as u64, &mut dst[filled..])?;
        if read == 0 {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "access source ended before the requested range completed",
            ));
        }
        filled += read;
    }
    Ok(())
}

/// A forward-only reader over one clean repair source, whichever kind it is.
///
/// The staging copies below consume their source strictly in order, which is
/// the one shape both a `File` and a [`FileAccess`] handle serve equally well.
/// Keeping the two behind this reader is what lets a virtual source stage into
/// repair scratch without any code path holding a path for it.
enum SourceReader<'a> {
    File {
        file: File,
        path: &'a Path,
        source_len: u64,
    },
    Access {
        access: &'a (dyn FileAccess + Send + Sync),
        file_id: FileId,
        offset: u64,
    },
}

impl<'a> SourceReader<'a> {
    /// Open `source` for a sequential read of `len` bytes from `offset`.
    /// A path source is opened and bounds-checked here; an access source is
    /// bound to its handle, which is required to be present.
    fn open(
        source: &'a SourceLocation,
        access: Option<&'a (dyn FileAccess + Send + Sync)>,
        offset: u64,
        len: u64,
    ) -> io::Result<Self> {
        match source {
            SourceLocation::Path(path) => {
                let mut file = File::open(path).map_err(|_| source_changed_io(path))?;
                let source_len = file.metadata().map_err(|_| source_changed_io(path))?.len();
                if offset.checked_add(len).is_none_or(|end| end > source_len) {
                    return Err(source_changed_io(path));
                }
                file.seek(SeekFrom::Start(offset))
                    .map_err(|_| source_changed_io(path))?;
                Ok(Self::File {
                    file,
                    path,
                    source_len,
                })
            }
            SourceLocation::Access(file_id) => {
                let access = access.ok_or_else(|| source_location_changed_io(source))?;
                Ok(Self::Access {
                    access,
                    file_id: *file_id,
                    offset,
                })
            }
        }
    }

    /// Total length of a path source. Access sources have no such fact: their
    /// length is whatever the set says it is.
    fn source_len(&self) -> Option<u64> {
        match self {
            Self::File { source_len, .. } => Some(*source_len),
            Self::Access { .. } => None,
        }
    }

    fn read_exact(&mut self, dst: &mut [u8]) -> io::Result<()> {
        match self {
            Self::File { file, path, .. } => {
                file.read_exact(dst).map_err(|_| source_changed_io(path))
            }
            Self::Access {
                access,
                file_id,
                offset,
            } => {
                read_exact_from_access(*access, file_id, *offset, dst)
                    .map_err(|_| source_location_changed_io(&SourceLocation::Access(*file_id)))?;
                *offset += dst.len() as u64;
                Ok(())
            }
        }
    }
}

fn copy_block_range_validated(
    block: &SourceBlock,
    slice_size: u64,
    range: &BlockCopyRange,
    access: Option<&(dyn FileAccess + Send + Sync)>,
) -> io::Result<()> {
    if range.len != block.expected_len {
        return Err(source_location_changed_io(&range.src));
    }
    let mut input = SourceReader::open(&range.src, access, range.src_offset, range.len)?;
    if let Some(parent) = range.dst.parent() {
        fs::create_dir_all(parent)?;
    }
    let mut output = OpenOptions::new().write(true).open(&range.dst)?;
    output.seek(SeekFrom::Start(range.dst_offset))?;
    let mut checksum = checksum::SliceChecksumState::new();
    let mut remaining = range.len;
    let mut buf = vec![0u8; remaining.clamp(1, 256 * 1024) as usize];
    while remaining > 0 {
        let take = remaining.min(buf.len() as u64) as usize;
        input.read_exact(&mut buf[..take])?;
        output.write_all(&buf[..take])?;
        checksum.update(&buf[..take]);
        remaining -= take as u64;
    }
    output.flush()?;
    let (crc32, md5) = checksum.finalize(Some(slice_size));
    if crc32 != block.checksum.crc32 || md5 != block.checksum.md5 {
        return Err(source_location_changed_io(&range.src));
    }
    Ok(())
}

fn copy_complete_file_validated(
    file: &SourceFileEntry,
    blocks: &[SourceBlock],
    slice_size: u64,
    src: &SourceLocation,
    access: Option<&(dyn FileAccess + Send + Sync)>,
    dst: &Path,
) -> io::Result<()> {
    let mut input = SourceReader::open(src, access, 0, file.length)?;
    // A physical source of the wrong length is a changed source, even when its
    // leading bytes still hash correctly.
    if input.source_len().is_some_and(|len| len != file.length) {
        return Err(source_location_changed_io(src));
    }
    if let Some(parent) = dst.parent() {
        fs::create_dir_all(parent)?;
    }
    let mut output = OpenOptions::new().write(true).open(dst)?;
    let mut full_hash = Md5State::new();
    let mut copied = 0u64;
    let mut buf = vec![0u8; 256 * 1024];
    let block_iter: Box<dyn Iterator<Item = Option<&SourceBlock>>> = if blocks.is_empty() {
        Box::new(std::iter::once(None))
    } else {
        Box::new(blocks.iter().map(Some))
    };
    for block in block_iter {
        let expected_len = block.map_or(file.length, |block| block.expected_len);
        let mut remaining = expected_len;
        let mut slice_checksum = checksum::SliceChecksumState::new();
        while remaining > 0 {
            let take = remaining.min(buf.len() as u64) as usize;
            input.read_exact(&mut buf[..take])?;
            output.write_all(&buf[..take])?;
            full_hash.update(&buf[..take]);
            slice_checksum.update(&buf[..take]);
            remaining -= take as u64;
            copied += take as u64;
        }
        if let Some(block) = block {
            let (crc32, md5) = slice_checksum.finalize(Some(slice_size));
            if crc32 != block.checksum.crc32 || md5 != block.checksum.md5 {
                return Err(source_location_changed_io(src));
            }
        }
    }
    output.flush()?;
    if copied != file.length || full_hash.finalize() != file.hash_full {
        return Err(source_location_changed_io(src));
    }
    Ok(())
}

/// Copy a clean span from either source kind into a path-addressed target.
/// Repair outputs are always real files; only the *read* side virtualizes.
fn copy_source_range(
    src: &SourceLocation,
    access: Option<&(dyn FileAccess + Send + Sync)>,
    src_offset: u64,
    dst: &Path,
    dst_offset: u64,
    len: u64,
) -> io::Result<()> {
    let Some(path) = src.path() else {
        let mut input = SourceReader::open(src, access, src_offset, len)?;
        if let Some(parent) = dst.parent() {
            fs::create_dir_all(parent)?;
        }
        let mut output = OpenOptions::new().write(true).open(dst)?;
        output.seek(SeekFrom::Start(dst_offset))?;
        let mut remaining = len;
        let mut buf = vec![0u8; remaining.clamp(1, 256 * 1024) as usize];
        while remaining > 0 {
            let take = remaining.min(buf.len() as u64) as usize;
            input.read_exact(&mut buf[..take])?;
            output.write_all(&buf[..take])?;
            remaining -= take as u64;
        }
        output.flush()?;
        crate::file_cache::drop_file_cache(&output, dst, dst_offset, len);
        return Ok(());
    };
    copy_range(path, src_offset, dst, dst_offset, len)
}

fn copy_range(
    src: &Path,
    src_offset: u64,
    dst: &Path,
    dst_offset: u64,
    len: u64,
) -> io::Result<()> {
    let mut input = File::open(src)?;
    let source_len = input.metadata()?.len();
    crate::file_cache::advise_range_sequential(&input, src, src_offset, len);
    input.seek(SeekFrom::Start(src_offset))?;
    if let Some(parent) = dst.parent() {
        fs::create_dir_all(parent)?;
    }
    let mut output = OpenOptions::new().write(true).open(dst)?;
    output.seek(SeekFrom::Start(dst_offset))?;

    #[cfg(target_os = "linux")]
    {
        // File-to-file io::copy stays in the kernel when the filesystem
        // supports range copies; the take() bound caps the span at `len`.
        let copied = io::copy(&mut (&mut input).take(len), &mut output)?;
        if copied != len {
            return Err(io::Error::new(
                io::ErrorKind::UnexpectedEof,
                "source exhausted before the copy range completed",
            ));
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        // The generic io::copy buffer is smaller than this; keep the wider
        // userspace loop where no kernel-copy path exists.
        let mut remaining = len;
        let mut buf = [0u8; 64 * 1024];
        while remaining > 0 {
            let take = remaining.min(buf.len() as u64) as usize;
            input.read_exact(&mut buf[..take])?;
            output.write_all(&buf[..take])?;
            remaining -= take as u64;
        }
    }
    output.flush()?;
    crate::file_cache::drop_touched_file_cache(&input, src, source_len, src_offset, len);
    // Destination advice remains opportunistic; avoid forced writeback for large repairs.
    crate::file_cache::drop_file_cache(&output, dst, dst_offset, len);
    Ok(())
}

fn push_block_copy_range(ranges: &mut Vec<BlockCopyRange>, next: BlockCopyRange) {
    if next.len == 0 {
        return;
    }
    if let Some(last) = ranges.last_mut()
        && last.can_extend(&next)
    {
        last.extend(&next);
        return;
    }
    ranges.push(next);
}

fn unique_repair_dir(base_dir: &Path) -> PathBuf {
    let stamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_nanos())
        .unwrap_or_default();
    base_dir.join(format!(".weaver-par2-repair-{stamp}"))
}

fn unique_backup_path(path: &Path) -> io::Result<PathBuf> {
    let name = path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("target");
    for index in 1u32.. {
        let candidate = path.with_file_name(format!("{name}.{index}"));
        match fs::symlink_metadata(&candidate) {
            Ok(_) => continue,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(candidate),
            Err(error) => return Err(error),
        }
    }
    Err(io::Error::new(
        io::ErrorKind::AlreadyExists,
        format!("no available backup suffix for {}", path.display()),
    ))
}

fn rollback_installed_files(
    base_dir: &Path,
    installed_targets: &[PathBuf],
    backups: &[(PathBuf, PathBuf)],
) {
    for target in installed_targets.iter().rev() {
        let _ = crate::disk::remove_file_within_base(base_dir, target);
        crate::file_cache::drop_path_cache(target);
    }

    for (target, backup) in backups.iter().rev() {
        let _ = crate::disk::remove_file_within_base(base_dir, target);
        if crate::disk::rename_within_base(base_dir, backup, target).is_ok() {
            crate::file_cache::drop_path_cache(backup);
            crate::file_cache::drop_path_cache(target);
        }
    }
}

fn purge_files_best_effort<I, P>(paths: I)
where
    I: IntoIterator<Item = P>,
    P: AsRef<Path>,
{
    for path in paths {
        let path = path.as_ref();
        match fs::remove_file(path) {
            Ok(()) => crate::file_cache::drop_path_cache(path),
            Err(error) if error.kind() == io::ErrorKind::NotFound => {}
            Err(_) => {}
        }
    }
}

fn padded_crc(data: &[u8], pad_to: u64) -> u32 {
    let mut hasher = Crc32Hasher::new();
    hasher.update(data);
    update_crc_zeros(&mut hasher, pad_to.saturating_sub(data.len() as u64));
    hasher.finalize()
}

fn crc32_zeros(len: u64) -> u32 {
    let mut hasher = Crc32Hasher::new();
    update_crc_zeros(&mut hasher, len);
    hasher.finalize()
}

/// MD5 of one short block, zero-padded to `pad_to`.
///
/// Deliberately the single-stream backend rather than [`md5_simd::md5_multi`]:
/// a multi-buffer kernel driven with one input leaves every other lane idle and
/// still pays the vector round latency, so it is slower here than the ordinary
/// MD5 implementation. Multi-buffer only pays off with several independent
/// messages in flight, which is what the batched scanner and verifier feed it.
fn padded_md5(data: &[u8], pad_to: u64) -> [u8; 16] {
    let mut hasher = Md5State::new();
    hasher.update(data);
    update_md5_zeros(&mut hasher, pad_to.saturating_sub(data.len() as u64));
    hasher.finalize()
}

fn update_crc_zeros(hasher: &mut Crc32Hasher, mut len: u64) {
    while len > 0 {
        let take = len.min(ZERO_PAD_CHUNK.len() as u64) as usize;
        hasher.update(&ZERO_PAD_CHUNK[..take]);
        len -= take as u64;
    }
}

fn update_md5_zeros(hasher: &mut Md5State, mut len: u64) {
    while len > 0 {
        let take = len.min(ZERO_PAD_CHUNK.len() as u64) as usize;
        hasher.update(&ZERO_PAD_CHUNK[..take]);
        len -= take as u64;
    }
}

static CRC_TABLE: LazyLock<[u32; 256]> = LazyLock::new(|| {
    let mut table = [0u32; 256];
    for i in 0..=255u32 {
        let mut crc = i;
        for _ in 0..8 {
            crc = (crc >> 1) ^ if crc & 1 != 0 { 0xEDB8_8320 } else { 0 };
        }
        table[i as usize] = crc;
    }
    table
});

static CRC_POWER: LazyLock<[u32; 32]> = LazyLock::new(|| {
    let mut power = [0u32; 32];
    let mut k = 0x8000_0000u32 >> 1;
    for i in 0..32 {
        power[(i + 32 - 3) & 31] = k;
        k = gf32_multiply(k, k, 0xEDB8_8320);
    }
    power
});

fn gf32_multiply(mut a: u32, mut b: u32, polynomial: u32) -> u32 {
    let mut product = 0u32;
    for _ in 0..31 {
        if b >> 31 != 0 {
            product ^= a;
        }
        a = (a >> 1) ^ if a & 1 != 0 { polynomial } else { 0 };
        b <<= 1;
    }
    if b >> 31 != 0 {
        product ^= a;
    }
    product
}

fn crc_exp8(mut n: u64) -> u32 {
    let mut result = 0x8000_0000u32;
    let mut power = 0usize;
    n %= 0xffff_ffff;
    while n != 0 {
        if n & 1 != 0 {
            result = gf32_multiply(result, CRC_POWER[power], 0xEDB8_8320);
        }
        n >>= 1;
        power = (power + 1) & 31;
    }
    result
}

fn generate_window_table(window: u64) -> [u32; 256] {
    let coeff = crc_exp8(window);
    let mut mask = gf32_multiply(!0, coeff, 0xEDB8_8320);
    mask = gf32_multiply(mask, 0x8080_0000, 0xEDB8_8320);
    mask ^= !0;

    let mut table = [0u32; 256];
    for i in 0..=255usize {
        table[i] = gf32_multiply(CRC_TABLE[i], coeff, 0xEDB8_8320) ^ mask;
    }
    table
}

fn crc_slide_char(crc: u32, new: u8, old: u8, window_table: &[u32; 256]) -> u32 {
    let crc = crc ^ !0;
    ((crc >> 8) & 0x00ff_ffff)
        ^ CRC_TABLE[((crc as u8) ^ new) as usize]
        ^ window_table[old as usize]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::verify::verify_all;
    use std::collections::BTreeMap;
    use std::path::{Path, PathBuf};

    use crate::checksum::SliceChecksumState;
    use crate::types::RecoverySetId;
    use tempfile::tempdir;

    #[cfg(feature = "slow-tests")]
    use std::ffi::OsStr;

    #[test]
    fn armed_repair_staging_guard_removes_failed_output() {
        let dir = tempdir().unwrap();
        let staging = dir.path().join(".weaver-par2-repair-test");
        fs::create_dir_all(&staging).unwrap();
        fs::write(staging.join("partial.bin"), b"partial").unwrap();

        drop(RepairStagingGuard::new(staging.clone()));

        assert!(!staging.exists());
    }

    fn rewrite_same_size_and_restore_mtime(path: &Path, replacement: &[u8]) {
        let modified = fs::metadata(path).unwrap().modified().unwrap();
        assert_eq!(fs::metadata(path).unwrap().len(), replacement.len() as u64);
        fs::write(path, replacement).unwrap();
        let file = fs::OpenOptions::new().write(true).open(path).unwrap();
        file.set_times(std::fs::FileTimes::new().set_modified(modified))
            .unwrap();
        assert_eq!(fs::metadata(path).unwrap().modified().unwrap(), modified);
    }

    fn validated_source_block(file_id: FileId, path: &Path, expected: &[u8]) -> SourceBlock {
        let mut state = SliceChecksumState::new();
        state.update(expected);
        let (crc32, md5) = state.finalize(Some(expected.len() as u64));
        SourceBlock {
            global_index: 0,
            file_id,
            local_index: 0,
            expected_len: expected.len() as u64,
            checksum: SliceChecksum { crc32, md5 },
            location: Some(BlockLocation {
                source: SourceLocation::Path(path.to_path_buf()),
                offset: 0,
                len: expected.len() as u64,
                kind: BlockLocationKind::Canonical,
            }),
        }
    }

    #[test]
    fn reconstruction_rejects_same_size_source_change_with_restored_mtime() {
        let dir = tempdir().unwrap();
        let source = dir.path().join("source.bin");
        let expected = b"good";
        fs::write(&source, expected).unwrap();
        let snapshot = HashMap::from([(source.clone(), stat_for_carry(&source))]);
        let file_id = FileId::from_bytes([0x41; 16]);
        let block = validated_source_block(file_id, &source, expected);
        rewrite_same_size_and_restore_mtime(&source, b"evil");
        assert_eq!(stat_for_carry(&source), snapshot[&source]);
        let access = RepairExecutionAccess::new(
            dir.path().join("staging"),
            &[],
            &[block],
            &HashSet::new(),
            expected.len() as u64,
            RepairExecutionContext {
                source_snapshots: Some(snapshot),
                ..RepairExecutionContext::default()
            },
        )
        .unwrap();

        let error =
            crate::verify::FileAccess::read_file_range(&access, &file_id, 0, expected.len() as u64)
                .unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        assert!(!dir.path().join("installed.bin").exists());
    }

    #[test]
    fn streaming_short_slice_pads_crc_and_accepts_one_stripe_replay() {
        let dir = tempdir().unwrap();
        let source = dir.path().join("short.bin");
        let payload = b"tail";
        fs::write(&source, payload).unwrap();
        let file_id = FileId::from_bytes([0x44; 16]);
        let mut checksum = SliceChecksumState::new();
        checksum.update(payload);
        let (crc32, md5) = checksum.finalize(Some(8));
        let block = SourceBlock {
            global_index: 0,
            file_id,
            local_index: 0,
            expected_len: payload.len() as u64,
            checksum: SliceChecksum { crc32, md5 },
            location: Some(BlockLocation {
                source: SourceLocation::Path(source),
                offset: 0,
                len: payload.len() as u64,
                kind: BlockLocationKind::Canonical,
            }),
        };
        let access = RepairExecutionAccess::new(
            dir.path().join("staging"),
            &[],
            &[block],
            &HashSet::new(),
            8,
            RepairExecutionContext::default(),
        )
        .unwrap();

        for _ in 0..2 {
            let mut read = vec![0u8; payload.len()];
            assert_eq!(
                crate::verify::FileAccess::read_file_range_into(&access, &file_id, 0, &mut read,)
                    .unwrap(),
                payload.len()
            );
            assert_eq!(read, payload);
        }
        assert_eq!(access.validation_bytes(), payload.len() as u64);
    }

    #[test]
    fn streaming_validation_replays_only_current_outer_stripe() {
        let dir = tempdir().unwrap();
        let source = dir.path().join("multistripe.bin");
        let payload = b"12345678";
        fs::write(&source, payload).unwrap();
        let file_id = FileId::from_bytes([0x46; 16]);
        let block = validated_source_block(file_id, &source, payload);
        let access = RepairExecutionAccess::new(
            dir.path().join("staging"),
            &[],
            &[block],
            &HashSet::new(),
            payload.len() as u64,
            RepairExecutionContext::default(),
        )
        .unwrap();

        for (offset, expected) in [(0, &payload[..4]), (4, &payload[4..])] {
            let mut read = vec![0u8; expected.len()];
            assert_eq!(
                crate::verify::FileAccess::read_file_range_into(
                    &access, &file_id, offset, &mut read,
                )
                .unwrap(),
                expected.len()
            );
            assert_eq!(read, expected);
        }
        let mut replay = vec![0u8; 4];
        assert_eq!(
            crate::verify::FileAccess::read_file_range_into(&access, &file_id, 4, &mut replay)
                .unwrap(),
            replay.len()
        );
        assert_eq!(replay, &payload[4..]);

        let mut stale = vec![0u8; 4];
        assert!(
            crate::verify::FileAccess::read_file_range_into(&access, &file_id, 0, &mut stale,)
                .is_err()
        );
        assert_eq!(access.validation_bytes(), payload.len() as u64);
    }

    #[test]
    fn reconstruction_copy_uses_read_buffer_and_cached_positional_writer() {
        let dir = tempdir().unwrap();
        let source = dir.path().join("source.bin");
        let staging = dir.path().join("staging");
        let target = staging.join("installed.bin");
        let payload = b"copy-me!";
        fs::write(&source, payload).unwrap();
        fs::create_dir_all(&staging).unwrap();
        fs::write(&target, vec![0u8; payload.len()]).unwrap();

        let file_id = FileId::from_bytes([0x45; 16]);
        let block = validated_source_block(file_id, &source, payload);
        let file = SourceFileEntry {
            file_id,
            par2_name: "installed.bin".to_owned(),
            safe_path: target.clone(),
            safe_name: "installed.bin".to_owned(),
            length: payload.len() as u64,
            hash_full: [0; 16],
            hash_16k: [0; 16],
            recoverable: true,
            first_block: 0,
            expected_block_count: 1,
            block_count: 1,
            target_exists: false,
            complete_location: None,
            non_canonical_complete_source_count: 0,
        };
        let mut staged = HashSet::new();
        staged.insert(file_id);
        let access = RepairExecutionAccess::new(
            staging,
            &[file],
            &[block],
            &staged,
            8,
            RepairExecutionContext {
                reconstruction_copy_targets: HashMap::from([(
                    (file_id, 0),
                    BlockCopyRange {
                        src: SourceLocation::Path(source),
                        src_offset: 0,
                        dst: target,
                        dst_offset: 0,
                        len: payload.len() as u64,
                    },
                )]),
                ..RepairExecutionContext::default()
            },
        )
        .unwrap();

        let mut read = vec![0u8; payload.len()];
        assert_eq!(
            crate::verify::FileAccess::read_file_range_into(&access, &file_id, 0, &mut read)
                .unwrap(),
            payload.len()
        );
        assert_eq!(read, payload);
        assert_eq!(
            fs::read(access.repair_path_for(&file_id).unwrap()).unwrap(),
            payload
        );
        assert_eq!(access.staged_writers.lock().unwrap().len(), 1);
    }

    #[test]
    fn whole_file_copy_rejects_same_size_source_change_and_cleans_staging() {
        let dir = tempdir().unwrap();
        let source = dir.path().join("source.bin");
        let expected = b"whole-file";
        fs::write(&source, expected).unwrap();
        let file_id = FileId::from_bytes([0x42; 16]);
        let block = validated_source_block(file_id, &source, expected);
        let file = SourceFileEntry {
            file_id,
            par2_name: "installed.bin".to_owned(),
            safe_path: dir.path().join("installed.bin"),
            safe_name: "installed.bin".to_owned(),
            length: expected.len() as u64,
            hash_full: checksum::md5(expected),
            hash_16k: checksum::md5(expected),
            recoverable: true,
            first_block: 0,
            expected_block_count: 1,
            block_count: 1,
            target_exists: false,
            complete_location: None,
            non_canonical_complete_source_count: 0,
        };
        rewrite_same_size_and_restore_mtime(&source, b"changed!!!");
        let staging = dir.path().join(".weaver-par2-repair-whole");
        fs::create_dir_all(&staging).unwrap();
        let destination = staging.join("installed.bin");
        File::create(&destination).unwrap();
        let guard = RepairStagingGuard::new(staging.clone());

        let error = copy_complete_file_validated(
            &file,
            &[block],
            expected.len() as u64,
            &SourceLocation::Path(source),
            None,
            &destination,
        )
        .unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        drop(guard);
        assert!(!staging.exists());
        assert!(!file.safe_path.exists());
    }

    /// Staging from a virtual source writes the served bytes and validates
    /// them, opening nothing on disk but the destination.
    #[test]
    fn whole_file_copy_stages_a_virtual_source_without_a_path() {
        let dir = tempdir().unwrap();
        let expected = b"virtual-whole-file-payload!!";
        let file_id = FileId::from_bytes([0x71; 16]);
        let mut memory = crate::verify::MemoryFileAccess::new();
        memory.add_file(file_id, expected.to_vec());
        let source = SourceLocation::Access(file_id);
        let mut blocks = Vec::new();
        for (index, chunk) in expected.chunks(8).enumerate() {
            let mut state = SliceChecksumState::new();
            state.update(chunk);
            let (crc32, md5) = state.finalize(Some(8));
            blocks.push(SourceBlock {
                global_index: index,
                file_id,
                local_index: index as u32,
                expected_len: chunk.len() as u64,
                checksum: SliceChecksum { crc32, md5 },
                location: Some(BlockLocation {
                    source: source.clone(),
                    offset: index as u64 * 8,
                    len: chunk.len() as u64,
                    kind: BlockLocationKind::Canonical,
                }),
            });
        }
        let file = SourceFileEntry {
            file_id,
            par2_name: "installed.bin".to_owned(),
            safe_path: dir.path().join("installed.bin"),
            safe_name: "installed.bin".to_owned(),
            length: expected.len() as u64,
            hash_full: checksum::md5(expected),
            hash_16k: checksum::md5(expected),
            recoverable: true,
            first_block: 0,
            expected_block_count: blocks.len(),
            block_count: blocks.len(),
            target_exists: false,
            complete_location: None,
            non_canonical_complete_source_count: 0,
        };
        let staging = dir.path().join(".weaver-par2-repair-virtual");
        fs::create_dir_all(&staging).unwrap();
        let destination = staging.join("installed.bin");
        File::create(&destination).unwrap();

        copy_complete_file_validated(&file, &blocks, 8, &source, Some(&memory), &destination)
            .unwrap();

        assert_eq!(fs::read(&destination).unwrap(), expected);
        // Nothing was created at the file's own path: only the read side is
        // virtual, and the write side went where it was told.
        assert!(!file.safe_path.exists());
    }

    /// A virtual source serving the wrong bytes is refused exactly as a
    /// changed file is, and names the file identity rather than a path.
    #[test]
    fn intact_block_copy_rejects_a_virtual_source_serving_wrong_bytes() {
        let dir = tempdir().unwrap();
        let expected = b"block";
        let file_id = FileId::from_bytes([0x72; 16]);
        let mut state = SliceChecksumState::new();
        state.update(expected);
        let (crc32, md5) = state.finalize(Some(expected.len() as u64));
        let block = SourceBlock {
            global_index: 0,
            file_id,
            local_index: 0,
            expected_len: expected.len() as u64,
            checksum: SliceChecksum { crc32, md5 },
            location: None,
        };
        let mut memory = crate::verify::MemoryFileAccess::new();
        memory.add_file(file_id, b"wrong".to_vec());
        let staging = dir.path().join(".weaver-par2-repair-virtual-block");
        fs::create_dir_all(&staging).unwrap();
        let destination = staging.join("installed.bin");
        File::create(&destination).unwrap();
        let range = BlockCopyRange {
            src: SourceLocation::Access(file_id),
            src_offset: 0,
            dst: destination,
            dst_offset: 0,
            len: expected.len() as u64,
        };

        let error =
            copy_block_range_validated(&block, expected.len() as u64, &range, Some(&memory))
                .unwrap_err();

        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        assert!(error.to_string().contains("virtual source changed"));
    }

    /// Without a handle there is nothing to read a virtual source from, and
    /// the refusal must not degrade into a filesystem lookup.
    #[test]
    fn virtual_source_without_a_handle_is_refused_not_resolved() {
        let dir = tempdir().unwrap();
        let file_id = FileId::from_bytes([0x73; 16]);
        let staging = dir.path().join(".weaver-par2-repair-no-handle");
        fs::create_dir_all(&staging).unwrap();
        let destination = staging.join("installed.bin");
        File::create(&destination).unwrap();
        let range = BlockCopyRange {
            src: SourceLocation::Access(file_id),
            src_offset: 0,
            dst: destination,
            dst_offset: 0,
            len: 4,
        };
        let block = SourceBlock {
            global_index: 0,
            file_id,
            local_index: 0,
            expected_len: 4,
            checksum: SliceChecksum {
                crc32: 0,
                md5: [0; 16],
            },
            location: None,
        };

        let error = copy_block_range_validated(&block, 4, &range, None).unwrap_err();

        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        assert!(error.to_string().contains("virtual source changed"));
    }

    #[test]
    fn intact_block_copy_rejects_same_size_source_change_and_cleans_staging() {
        let dir = tempdir().unwrap();
        let source = dir.path().join("source.bin");
        let expected = b"block";
        fs::write(&source, expected).unwrap();
        let file_id = FileId::from_bytes([0x43; 16]);
        let block = validated_source_block(file_id, &source, expected);
        rewrite_same_size_and_restore_mtime(&source, b"wrong");
        let staging = dir.path().join(".weaver-par2-repair-block");
        fs::create_dir_all(&staging).unwrap();
        let destination = staging.join("installed.bin");
        File::create(&destination).unwrap();
        let range = BlockCopyRange {
            src: SourceLocation::Path(source),
            src_offset: 0,
            dst: destination,
            dst_offset: 0,
            len: expected.len() as u64,
        };
        let guard = RepairStagingGuard::new(staging.clone());

        let error =
            copy_block_range_validated(&block, expected.len() as u64, &range, None).unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        drop(guard);
        assert!(!staging.exists());
        assert!(!dir.path().join("installed.bin").exists());
    }

    fn restore_carried_modified_time(carry: &ScanCarry, path: &Path) {
        let expected = carry
            .snapshot
            .iter()
            .find(|stat| stat.path == path)
            .expect("path is in carried stat snapshot");
        let Some(modified) = expected
            .state
            .as_ref()
            .and_then(FileStatFingerprint::modified)
        else {
            panic!("carried path exists as a regular file with a readable mtime");
        };
        let file = fs::OpenOptions::new().write(true).open(path).unwrap();
        file.set_times(std::fs::FileTimes::new().set_modified(modified))
            .unwrap();
        assert_eq!(
            stat_for_carry(path),
            *expected,
            "test must force the stat gate to accept stale carry"
        );
    }

    fn synthetic_set(files: &[(&str, &[u8])], slice_size: u64) -> Par2FileSet {
        let mut recovery_file_ids = Vec::new();
        let mut descriptions = HashMap::new();
        let mut slice_checksums = HashMap::new();

        for (index, (filename, bytes)) in files.iter().enumerate() {
            let mut raw_id = [0u8; 16];
            raw_id[12..].copy_from_slice(&((index as u32) + 1).to_be_bytes());
            let file_id = FileId::from_bytes(raw_id);
            recovery_file_ids.push(file_id);

            let hash_full = checksum::md5(bytes);
            let hash_16k = checksum::md5(&bytes[..bytes.len().min(16 * 1024)]);
            let mut checksums = Vec::new();
            for chunk in bytes.chunks(slice_size as usize) {
                let mut state = SliceChecksumState::new();
                state.update(chunk);
                let pad_to = ((chunk.len() as u64) < slice_size).then_some(slice_size);
                let (crc32, md5) = state.finalize(pad_to);
                checksums.push(SliceChecksum { crc32, md5 });
            }

            descriptions.insert(
                file_id,
                crate::par2_set::FileDescription {
                    file_id,
                    hash_full,
                    hash_16k,
                    length: bytes.len() as u64,
                    par2_name: (*filename).to_string(),
                    filename: (*filename).to_string(),
                },
            );
            slice_checksums.insert(file_id, checksums);
        }

        Par2FileSet {
            recovery_set_id: RecoverySetId::from_bytes([7; 16]),
            slice_size,
            recovery_file_ids,
            non_recovery_file_ids: Vec::new(),
            files: descriptions,
            slice_checksums,
            recovery_slices: BTreeMap::new(),
            creator: None,
        }
    }

    fn write_synthetic_par2_file(
        dir: &Path,
        name: &str,
        files: &[(&str, &[u8])],
        slice_size: u64,
    ) -> PathBuf {
        let file_ids: Vec<FileId> = (0..files.len())
            .map(|index| {
                let mut raw_id = [0u8; 16];
                raw_id[12..].copy_from_slice(&((index as u32) + 1).to_be_bytes());
                FileId::from_bytes(raw_id)
            })
            .collect();

        let mut main_body = Vec::new();
        main_body.extend_from_slice(&slice_size.to_le_bytes());
        main_body.extend_from_slice(&(files.len() as u32).to_le_bytes());
        for file_id in &file_ids {
            main_body.extend_from_slice(file_id.as_bytes());
        }
        let recovery_set_id = checksum::md5(&main_body);

        let mut stream = make_full_packet(
            crate::packet::header::TYPE_MAIN,
            &main_body,
            recovery_set_id,
        );
        for ((filename, bytes), file_id) in files.iter().zip(file_ids.iter()) {
            let mut fd_body = Vec::new();
            fd_body.extend_from_slice(file_id.as_bytes());
            fd_body.extend_from_slice(&checksum::md5(bytes));
            fd_body.extend_from_slice(&checksum::md5(&bytes[..bytes.len().min(16 * 1024)]));
            fd_body.extend_from_slice(&(bytes.len() as u64).to_le_bytes());
            fd_body.extend_from_slice(filename.as_bytes());
            while fd_body.len() % 4 != 0 {
                fd_body.push(0);
            }
            stream.extend_from_slice(&make_full_packet(
                crate::packet::header::TYPE_FILE_DESC,
                &fd_body,
                recovery_set_id,
            ));

            let mut ifsc_body = Vec::new();
            ifsc_body.extend_from_slice(file_id.as_bytes());
            for chunk in bytes.chunks(slice_size as usize) {
                let mut state = SliceChecksumState::new();
                state.update(chunk);
                let pad_to = ((chunk.len() as u64) < slice_size).then_some(slice_size);
                let (crc32, md5) = state.finalize(pad_to);
                ifsc_body.extend_from_slice(&md5);
                ifsc_body.extend_from_slice(&crc32.to_le_bytes());
            }
            stream.extend_from_slice(&make_full_packet(
                crate::packet::header::TYPE_IFSC,
                &ifsc_body,
                recovery_set_id,
            ));
        }

        let path = dir.join(name);
        fs::write(&path, stream).unwrap();
        path
    }

    fn make_full_packet(packet_type: &[u8; 16], body: &[u8], recovery_set_id: [u8; 16]) -> Vec<u8> {
        let length = (crate::packet::header::HEADER_SIZE + body.len()) as u64;
        let mut hash_input = Vec::new();
        hash_input.extend_from_slice(&recovery_set_id);
        hash_input.extend_from_slice(packet_type);
        hash_input.extend_from_slice(body);
        let packet_hash = checksum::md5(&hash_input);

        let mut data = Vec::new();
        data.extend_from_slice(crate::packet::header::MAGIC);
        data.extend_from_slice(&length.to_le_bytes());
        data.extend_from_slice(&packet_hash);
        data.extend_from_slice(&recovery_set_id);
        data.extend_from_slice(packet_type);
        data.extend_from_slice(body);
        data
    }

    #[test]
    fn par2_base_name_strips_volume_suffix() {
        assert_eq!(
            par2_base_name(Path::new("movie.vol000+001.par2")).as_deref(),
            Some("movie")
        );
        assert_eq!(
            par2_base_name(Path::new("movie.vol000-001.PAR2")).as_deref(),
            Some("movie")
        );
        assert_eq!(
            par2_base_name(Path::new("movie.extra.par2")).as_deref(),
            Some("movie.extra")
        );
    }

    #[test]
    fn discover_adjacent_par2_files_uses_set_stem_sibling_scope() {
        let dir = tempdir().unwrap();
        let nested = dir.path().join("nested");
        fs::create_dir(&nested).unwrap();

        let main = dir.path().join("movie.par2");
        let sibling_recovery = dir.path().join("movie.vol000+001.par2");
        let sibling_upper = dir.path().join("movie.vol001+001.PAR2");
        let sibling_mixed_extension = dir.path().join("movie.vol002+001.Par2");
        let sibling_main_upper = dir.path().join("movie.PAR2");
        let unrelated = dir.path().join("other.vol000+001.par2");
        let nested_recovery = nested.join("movie.vol002+001.par2");

        for path in [
            &main,
            &sibling_recovery,
            &sibling_upper,
            &sibling_mixed_extension,
            &sibling_main_upper,
            &unrelated,
            &nested_recovery,
        ] {
            fs::write(path, b"not parsed in this test").unwrap();
        }

        let discovered = discover_adjacent_par2_files(std::slice::from_ref(&main)).unwrap();

        assert_eq!(discovered, vec![sibling_recovery, sibling_upper]);
    }

    #[test]
    fn discover_source_primary_par2_file_uses_set_stem() {
        let dir = tempdir().unwrap();
        let source = dir.path().join("movie.mkv");
        let volume_only = dir.path().join("movie.mkv.vol000+001.par2");
        fs::write(&source, b"source").unwrap();
        fs::write(&volume_only, b"volume").unwrap();

        assert_eq!(discover_source_primary_par2_file(&source).unwrap(), None);

        let lower_primary = dir.path().join("movie.mkv.par2");
        let upper_primary = dir.path().join("movie.mkv.PAR2");
        fs::write(&upper_primary, b"primary").unwrap();
        let expected_upper_only = if lower_primary.is_file() {
            lower_primary.clone()
        } else {
            upper_primary
        };
        assert_eq!(
            discover_source_primary_par2_file(&source).unwrap(),
            Some(expected_upper_only)
        );

        fs::write(&lower_primary, b"primary").unwrap();
        assert_eq!(
            discover_source_primary_par2_file(&source).unwrap(),
            Some(lower_primary)
        );
    }

    #[cfg(unix)]
    #[test]
    fn discover_adjacent_par2_files_skips_unreadable_sibling_directory() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempdir().unwrap();
        let main = dir.path().join("movie.par2");
        fs::write(&main, b"not parsed in this test").unwrap();

        let original_perms = fs::metadata(dir.path()).unwrap().permissions();
        let mut closed_perms = original_perms.clone();
        closed_perms.set_mode(0o0);
        fs::set_permissions(dir.path(), closed_perms).unwrap();

        let discovered = discover_adjacent_par2_files(std::slice::from_ref(&main));

        fs::set_permissions(dir.path(), original_perms).unwrap();

        assert_eq!(discovered.unwrap(), Vec::<PathBuf>::new());
    }

    #[test]
    fn load_inventory_ignores_unusable_par2_marker_extra_paths() {
        let dir = tempdir().unwrap();
        let mut main_body = Vec::new();
        main_body.extend_from_slice(&4u64.to_le_bytes());
        main_body.extend_from_slice(&0u32.to_le_bytes());
        let rsid = checksum::md5(&main_body);
        let main_path = dir.path().join("target.par2");
        fs::write(
            &main_path,
            make_full_packet(crate::packet::header::TYPE_MAIN, &main_body, rsid),
        )
        .unwrap();

        let junk_marker_path = dir.path().join("junk.par2.bak");
        fs::write(&junk_marker_path, b"not a PAR2 packet stream").unwrap();

        let mut options =
            Par2RepairerOptions::new(dir.path().to_path_buf(), vec![main_path.clone()]);
        options
            .extra_paths
            .push(dir.path().join("missing.par2.bak"));
        options.extra_paths.push(junk_marker_path);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();

        assert_eq!(inventory.set.recovery_block_count(), 0);
        assert_eq!(inventory.diagnostics.corrupt_packets, 0);
        assert_eq!(inventory.purge_paths, vec![main_path]);
    }

    #[test]
    fn load_inventory_remembers_optional_adjacent_par2_files_for_purge() {
        let dir = tempdir().unwrap();
        let mut main_body = Vec::new();
        main_body.extend_from_slice(&4u64.to_le_bytes());
        main_body.extend_from_slice(&0u32.to_le_bytes());
        let rsid = checksum::md5(&main_body);
        let main_path = dir.path().join("target.par2");
        let corrupt_adjacent = dir.path().join("target.vol000+001.par2");
        fs::write(
            &main_path,
            make_full_packet(crate::packet::header::TYPE_MAIN, &main_body, rsid),
        )
        .unwrap();
        fs::write(&corrupt_adjacent, b"not a PAR2 packet stream").unwrap();

        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), vec![main_path.clone()]);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();

        assert_eq!(inventory.diagnostics.corrupt_packets, 0);
        assert_eq!(inventory.purge_paths, vec![main_path, corrupt_adjacent]);
    }

    #[test]
    fn load_inventory_prefers_adjacent_recovery_over_duplicate_marker_extra() {
        let dir = tempdir().unwrap();
        let mut main_body = Vec::new();
        main_body.extend_from_slice(&4u64.to_le_bytes());
        main_body.extend_from_slice(&0u32.to_le_bytes());
        let rsid = checksum::md5(&main_body);

        let main_path = dir.path().join("target.par2");
        fs::write(
            &main_path,
            make_full_packet(crate::packet::header::TYPE_MAIN, &main_body, rsid),
        )
        .unwrap();

        let mut sibling_recovery_body = Vec::new();
        sibling_recovery_body.extend_from_slice(&0u32.to_le_bytes());
        sibling_recovery_body.extend_from_slice(&[0x11; 4]);
        fs::write(
            dir.path().join("target.vol000+001.par2"),
            make_full_packet(
                crate::packet::header::TYPE_RECOVERY,
                &sibling_recovery_body,
                rsid,
            ),
        )
        .unwrap();

        let mut extra_recovery_body = Vec::new();
        extra_recovery_body.extend_from_slice(&0u32.to_le_bytes());
        extra_recovery_body.extend_from_slice(&[0x22; 4]);
        let extra_path = dir.path().join("target.par2.bak");
        fs::write(
            &extra_path,
            make_full_packet(
                crate::packet::header::TYPE_RECOVERY,
                &extra_recovery_body,
                rsid,
            ),
        )
        .unwrap();

        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), vec![main_path]);
        options.extra_paths.push(extra_path);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();

        let recovery = inventory.set.recovery_slices.get(&0).unwrap();
        assert_eq!(recovery.data.to_vec().unwrap(), vec![0x11; 4]);
    }

    #[test]
    fn load_inventory_reads_par2_marker_extra_paths_as_packets() {
        let dir = tempdir().unwrap();
        let file_id = FileId::from_bytes([1; 16]);
        let file_data = b"abcd";
        let slice_size = 4u64;

        let mut main_body = Vec::new();
        main_body.extend_from_slice(&slice_size.to_le_bytes());
        main_body.extend_from_slice(&1u32.to_le_bytes());
        main_body.extend_from_slice(file_id.as_bytes());
        let rsid = checksum::md5(&main_body);

        let mut fd_body = Vec::new();
        fd_body.extend_from_slice(file_id.as_bytes());
        fd_body.extend_from_slice(&checksum::md5(file_data));
        fd_body.extend_from_slice(&checksum::md5(file_data));
        fd_body.extend_from_slice(&(file_data.len() as u64).to_le_bytes());
        fd_body.extend_from_slice(b"target.bin");
        while fd_body.len() % 4 != 0 {
            fd_body.push(0);
        }

        let mut slice_state = SliceChecksumState::new();
        slice_state.update(file_data);
        let (crc32, md5) = slice_state.finalize(None);
        let mut ifsc_body = Vec::new();
        ifsc_body.extend_from_slice(file_id.as_bytes());
        ifsc_body.extend_from_slice(&md5);
        ifsc_body.extend_from_slice(&crc32.to_le_bytes());

        let mut main_stream = Vec::new();
        main_stream.extend_from_slice(&make_full_packet(
            crate::packet::header::TYPE_MAIN,
            &main_body,
            rsid,
        ));
        main_stream.extend_from_slice(&make_full_packet(
            crate::packet::header::TYPE_FILE_DESC,
            &fd_body,
            rsid,
        ));
        main_stream.extend_from_slice(&make_full_packet(
            crate::packet::header::TYPE_IFSC,
            &ifsc_body,
            rsid,
        ));

        let mut recovery_body = Vec::new();
        recovery_body.extend_from_slice(&0u32.to_le_bytes());
        recovery_body.extend_from_slice(&[0xAB; 4]);
        let mut recovery_stream = Vec::new();
        recovery_stream.extend_from_slice(&make_full_packet(
            crate::packet::header::TYPE_MAIN,
            &main_body,
            rsid,
        ));
        recovery_stream.extend_from_slice(&make_full_packet(
            crate::packet::header::TYPE_RECOVERY,
            &recovery_body,
            rsid,
        ));

        let main_path = dir.path().join("target.par2");
        let extra_recovery_path = dir.path().join("target.par2.bak");
        fs::write(&main_path, main_stream).unwrap();
        fs::write(&extra_recovery_path, recovery_stream).unwrap();

        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), vec![main_path]);
        options.extra_paths.push(extra_recovery_path);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();

        assert_eq!(inventory.set.recovery_block_count(), 1);
        assert!(inventory.set.recovery_slices.contains_key(&0));
    }

    #[test]
    fn unique_backup_path_uses_numbered_suffixes() {
        let dir = tempdir().unwrap();
        let target = dir.path().join("target.bin");
        fs::write(&target, b"target").unwrap();

        assert_eq!(
            unique_backup_path(&target).unwrap(),
            dir.path().join("target.bin.1")
        );
        fs::write(dir.path().join("target.bin.1"), b"first backup").unwrap();
        fs::write(dir.path().join("target.bin.2"), b"second backup").unwrap();

        assert_eq!(
            unique_backup_path(&target).unwrap(),
            dir.path().join("target.bin.3")
        );
    }

    #[test]
    fn block_copy_ranges_coalesce_contiguous_runs() {
        let src = PathBuf::from("source.bin");
        let other_src = PathBuf::from("other-source.bin");
        let dst = PathBuf::from("target.bin");
        let other_dst = PathBuf::from("other-target.bin");
        let mut ranges = Vec::new();

        push_block_copy_range(
            &mut ranges,
            BlockCopyRange {
                src: SourceLocation::Path(src.clone()),
                src_offset: 0,
                dst: dst.clone(),
                dst_offset: 0,
                len: 1024,
            },
        );
        push_block_copy_range(
            &mut ranges,
            BlockCopyRange {
                src: SourceLocation::Path(src.clone()),
                src_offset: 1024,
                dst: dst.clone(),
                dst_offset: 1024,
                len: 1024,
            },
        );
        push_block_copy_range(
            &mut ranges,
            BlockCopyRange {
                src: SourceLocation::Path(src.clone()),
                src_offset: 4096,
                dst: dst.clone(),
                dst_offset: 4096,
                len: 1024,
            },
        );
        push_block_copy_range(
            &mut ranges,
            BlockCopyRange {
                src: SourceLocation::Path(other_src),
                src_offset: 5120,
                dst: dst.clone(),
                dst_offset: 5120,
                len: 1024,
            },
        );
        push_block_copy_range(
            &mut ranges,
            BlockCopyRange {
                src: SourceLocation::Path(src),
                src_offset: 6144,
                dst: other_dst,
                dst_offset: 6144,
                len: 1024,
            },
        );

        assert_eq!(ranges.len(), 4);
        assert_eq!(ranges[0].src_offset, 0);
        assert_eq!(ranges[0].dst_offset, 0);
        assert_eq!(ranges[0].len, 2048);
        assert_eq!(ranges[1].src_offset, 4096);
        assert_eq!(ranges[1].len, 1024);
    }

    #[test]
    fn copy_range_preserves_small_range_from_large_source() {
        let dir = tempdir().unwrap();
        let src = dir.path().join("source.bin");
        let dst = dir.path().join("dest.bin");
        let payload = b"small range from a sparse large source";
        let source_offset = 4096u64;
        let dest_offset = 128u64;

        let mut source = File::create(&src).unwrap();
        source.set_len(64 * 1024 * 1024 + 4096).unwrap();
        source.seek(SeekFrom::Start(source_offset)).unwrap();
        source.write_all(payload).unwrap();
        drop(source);

        let dest = File::create(&dst).unwrap();
        dest.set_len(1024).unwrap();
        drop(dest);

        copy_range(&src, source_offset, &dst, dest_offset, payload.len() as u64).unwrap();

        let bytes = fs::read(&dst).unwrap();
        assert_eq!(
            &bytes[..dest_offset as usize],
            vec![0u8; dest_offset as usize]
        );
        assert_eq!(
            &bytes[dest_offset as usize..dest_offset as usize + payload.len()],
            payload
        );
        assert_eq!(fs::metadata(&src).unwrap().len(), 64 * 1024 * 1024 + 4096);
    }

    #[test]
    fn preview_survives_tiny_memory_limit_via_matrix_budget_floor() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let file_data: Vec<u8> = (0..256u32).map(|i| (i % 251) as u8).collect();
        let mut set = synthetic_set(&[("data.bin", &file_data)], slice_size);
        for exponent in 0..2u32 {
            set.recovery_slices.insert(
                exponent,
                crate::par2_set::RecoverySlice {
                    exponent,
                    data: vec![0u8; slice_size as usize].into(),
                },
            );
        }

        let mut damaged = file_data.clone();
        damaged[..64].fill(0);
        damaged[64..128].fill(0);
        fs::write(dir.path().join("data.bin"), damaged).unwrap();

        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        options.file_set = Some(set);
        options.repair = false;
        options.memory_limit = Some(8);

        // The decode matrix no longer competes with the slice-buffer budget,
        // so a tiny configured limit still previews as repairable.
        let outcome = Par2Repairer::new(options).verify_or_repair().unwrap();
        assert_eq!(outcome.status, Par2RepairStatus::RepairPossible);
    }

    #[test]
    fn preview_reports_resource_limited_for_sets_over_total_slice_cap() {
        let dir = tempdir().unwrap();
        let slice_size = 4u64;
        // Two files of 20000 slices each: 40000 total, over the 32768 cap.
        let file_a = vec![0xA5u8; 80_000];
        let file_b = vec![0x5Au8; 80_000];
        let mut set = synthetic_set(&[("a.bin", &file_a), ("b.bin", &file_b)], slice_size);
        for exponent in 0..40_000u32 {
            set.recovery_slices.insert(
                exponent,
                crate::par2_set::RecoverySlice {
                    exponent,
                    data: vec![0u8; slice_size as usize].into(),
                },
            );
        }

        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        options.file_set = Some(set);
        options.repair = false;

        let outcome = Par2Repairer::new(options).verify_or_repair().unwrap();
        assert_eq!(outcome.status, Par2RepairStatus::ResourceLimited);
        assert!(matches!(
            outcome.verification.repairable,
            Repairability::ResourceLimited { .. }
        ));
    }

    #[cfg(feature = "slow-tests")]
    fn crate_fixture_dir(name: &str) -> PathBuf {
        let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        let crate_fixture = manifest_dir.join("tests/fixtures").join(name);
        if crate_fixture.is_dir() {
            return crate_fixture;
        }

        panic!(
            "missing slow-test fixture {name}; looked in {}",
            crate_fixture.display()
        );
    }

    #[cfg(feature = "slow-tests")]
    fn copy_dir_contents(src: &Path, dst: &Path) {
        for entry in fs::read_dir(src).unwrap() {
            let entry = entry.unwrap();
            let src_path = entry.path();
            let dst_path = dst.join(entry.file_name());
            if entry.file_type().unwrap().is_dir() {
                fs::create_dir_all(&dst_path).unwrap();
                copy_dir_contents(&src_path, &dst_path);
            } else {
                fs::copy(&src_path, &dst_path).unwrap();
            }
        }
    }

    #[cfg(feature = "slow-tests")]
    fn copy_fixture_dir(name: &str) -> tempfile::TempDir {
        let dir = tempdir().unwrap();
        copy_dir_contents(&crate_fixture_dir(name), dir.path());
        dir
    }

    #[cfg(feature = "slow-tests")]
    fn collect_paths(dir: &Path, prefix: &str, extension: &str) -> Vec<PathBuf> {
        let mut paths: Vec<PathBuf> = fs::read_dir(dir)
            .unwrap()
            .filter_map(|entry| entry.ok().map(|e| e.path()))
            .filter(|path| {
                path.extension() == Some(OsStr::new(extension))
                    && path
                        .file_name()
                        .and_then(OsStr::to_str)
                        .is_some_and(|name| name.starts_with(prefix))
            })
            .collect();
        paths.sort();
        paths
    }

    #[test]
    fn rolling_crc_matches_direct_crc() {
        let data: Vec<u8> = (0..4096u32).map(|value| (value % 251) as u8).collect();
        let window = 257usize;
        let table = generate_window_table(window as u64);
        let mut crc = checksum::crc32(&data[..window]);
        for offset in 0..=data.len() - window {
            assert_eq!(crc, checksum::crc32(&data[offset..offset + window]));
            if offset < data.len() - window {
                crc = crc_slide_char(crc, data[offset + window], data[offset], &table);
            }
        }
    }

    fn block_location_summary(blocks: &[SourceBlock]) -> BlockLocationSummary {
        blocks
            .iter()
            .map(|block| {
                block.location.as_ref().map(|location| {
                    (
                        location
                            .path()
                            .expect("scanned location is a path")
                            .to_path_buf(),
                        location.offset,
                        location.len,
                        location.kind,
                    )
                })
            })
            .collect()
    }

    type BlockLocationSummaryEntry = (PathBuf, u64, u64, BlockLocationKind);
    type BlockLocationSummary = Vec<Option<BlockLocationSummaryEntry>>;

    fn scan_with_mmap(
        state: &RepairState,
        path: &Path,
        kind: BlockLocationKind,
    ) -> BlockLocationSummary {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        scanner
            .scan_file_mmap(
                path,
                kind,
                &state.files,
                &state.file_index_by_id,
                &mut blocks,
            )
            .unwrap();
        block_location_summary(&blocks)
    }

    fn scan_with_mmap_stats(
        state: &RepairState,
        path: &Path,
        kind: BlockLocationKind,
    ) -> (BlockLocationSummary, FileScanStats) {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        let stats = scanner
            .scan_file_mmap(
                path,
                kind,
                &state.files,
                &state.file_index_by_id,
                &mut blocks,
            )
            .unwrap();
        (block_location_summary(&blocks), stats)
    }

    fn scan_with_ordered_canonical(
        state: &RepairState,
        path: &Path,
    ) -> (BlockLocationSummary, FileScanStats) {
        scan_with_ordered_canonical_options(
            state,
            path,
            ScanSkipOptions {
                skip_data: false,
                skip_leeway: ORDERED_SCAN_DEFAULT_SKIP_LEEWAY,
            },
        )
    }

    fn scan_with_ordered_canonical_options(
        state: &RepairState,
        path: &Path,
        scan_options: ScanSkipOptions,
    ) -> (BlockLocationSummary, FileScanStats) {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        let target = state
            .files
            .iter()
            .find(|file| file.safe_path == path)
            .unwrap();
        let stats = scanner
            .scan_file_ordered_canonical(
                path,
                BlockLocationKind::Canonical,
                SourceFileScanLookup {
                    files: &state.files,
                    file_index_by_id: &state.file_index_by_id,
                },
                target,
                &mut blocks,
                scan_options,
            )
            .unwrap();
        (block_location_summary(&blocks), stats)
    }

    /// Pre-locate `settled_locals` from "evidence", then run the ordered
    /// canonical scan with the skip policy either honouring them (`honour`) or
    /// ignoring them. Both arms start from the same located state, so the only
    /// difference between them is the policy.
    fn scan_with_settled_evidence(
        state: &RepairState,
        path: &Path,
        settled_locals: &[usize],
        honour: bool,
    ) -> (BlockLocationSummary, FileScanStats) {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        let target = state
            .files
            .iter()
            .find(|file| file.safe_path == path)
            .unwrap()
            .clone();
        let mut settled = vec![false; target.block_count];
        for &local in settled_locals {
            let block_index = target.first_block + local;
            blocks[block_index].location = Some(BlockLocation {
                source: SourceLocation::Path(path.to_path_buf()),
                offset: local as u64 * state.set.slice_size,
                len: blocks[block_index].expected_len,
                kind: BlockLocationKind::Canonical,
            });
            settled[local] = true;
        }
        if !honour {
            settled = vec![false; target.block_count];
        }
        let stats = scanner
            .scan_file_ordered_canonical_settled(
                path,
                BlockLocationKind::Canonical,
                SourceFileScanLookup {
                    files: &state.files,
                    file_index_by_id: &state.file_index_by_id,
                },
                &target,
                &mut blocks,
                ScanSkipOptions::disabled(),
                &settled,
            )
            .unwrap();
        (block_location_summary(&blocks), stats)
    }

    #[test]
    fn settled_byte_runs_coalesce_and_drop_ranges_past_the_file() {
        assert_eq!(settled_byte_runs(&[], 64, 384), Vec::new());
        assert_eq!(
            settled_byte_runs(&[true, true, false, true, false, true], 64, 384),
            vec![(0, 128), (192, 256), (320, 384)]
        );
        // A slice the set describes but the file on disk is too short to hold
        // is a discrepancy for the scan to find, never one to seek over.
        assert_eq!(
            settled_byte_runs(&[true, true, true], 64, 100),
            vec![(0, 64)]
        );
    }

    #[test]
    fn evidence_skip_leaves_the_ordered_scan_locations_unchanged() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let mut target = Vec::new();
        for block in 0..8u8 {
            target.extend(
                (0..slice_size as usize)
                    .map(|index| block.wrapping_mul(37).wrapping_add(index as u8)),
            );
        }
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        let mut damaged = target.clone();
        damaged[3 * slice_size as usize..4 * slice_size as usize].fill(0xEE);
        fs::write(&candidate, &damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let settled_locals = [0usize, 1, 2, 4, 5, 6, 7];
        let (read_in_full, full_stats) =
            scan_with_settled_evidence(&state, &candidate, &settled_locals, false);
        let (with_skips, skip_stats) =
            scan_with_settled_evidence(&state, &candidate, &settled_locals, true);

        assert_eq!(with_skips, read_in_full, "the skip must not move a block");
        assert_eq!(full_stats.slices_settled_by_evidence, 0);
        assert_eq!(full_stats.bytes_skipped_by_evidence, 0);
        assert_eq!(skip_stats.slices_settled_by_evidence, 7);
        assert!(
            skip_stats.bytes_skipped_by_evidence > 0,
            "a honoured skip must show up as bytes not read"
        );
        assert!(
            skip_stats.bytes_skipped_by_evidence < target.len() as u64,
            "the damaged slice still has to be read"
        );
        assert!(skip_stats.windows_stepped < full_stats.windows_stepped);
    }

    #[test]
    fn an_entirely_settled_file_is_not_walked_at_all() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let target: Vec<u8> = (0..4 * slice_size as usize).map(|i| i as u8).collect();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, &target).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (_, stats) = scan_with_settled_evidence(&state, &candidate, &[0, 1, 2, 3], true);

        assert_eq!(stats.slices_settled_by_evidence, 4);
        assert_eq!(stats.bytes_skipped_by_evidence, target.len() as u64);
        assert_eq!(stats.windows_stepped, 0);
    }

    #[test]
    fn a_settled_slice_with_no_recorded_location_is_never_skipped() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let target: Vec<u8> = (0..4 * slice_size as usize).map(|i| i as u8).collect();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, &target).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        let file = state.files.first().unwrap();
        let fingerprint = stat_fingerprint(&candidate).unwrap();

        let mut trust = EvidenceScanTrust::default();
        for local in 0..4u32 {
            trust.record(file.file_id, &candidate, local, fingerprint.clone());
        }

        // Nothing is located yet, so nothing may be skipped: a skip is only
        // ever permitted over a block the state already holds.
        let blocks = ScanBlockState::new(&state.blocks);
        let settled = evidence_settled_slices(&trust, file, &candidate, &blocks, slice_size);
        assert!(settled.iter().all(|set| !*set));

        // With the locations in place the same plan settles every slice.
        let mut located = state.blocks.clone();
        for local in 0..4usize {
            located[file.first_block + local].location = Some(BlockLocation {
                source: SourceLocation::Path(candidate.clone()),
                offset: local as u64 * slice_size,
                len: slice_size,
                kind: BlockLocationKind::Canonical,
            });
        }
        let blocks = ScanBlockState::new(&located);
        let settled = evidence_settled_slices(&trust, file, &candidate, &blocks, slice_size);
        assert_eq!(settled, vec![true; 4]);

        // A stat the file no longer matches refuses every one of them, with no
        // error: the file is simply read in full.
        bump_modified_time(&candidate);
        let settled = evidence_settled_slices(&trust, file, &candidate, &blocks, slice_size);
        assert!(settled.iter().all(|set| !*set));
    }

    #[test]
    fn a_trust_plan_naming_two_paths_for_one_file_settles_nothing() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let target: Vec<u8> = (0..2 * slice_size as usize).map(|i| i as u8).collect();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        let decoy = dir.path().join("elsewhere.bin");
        fs::write(&candidate, &target).unwrap();
        fs::write(&decoy, &target).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        let file = state.files.first().unwrap();
        let fingerprint = stat_fingerprint(&candidate).unwrap();

        // The conflict latches: a verdict for the canonical path arriving
        // after the decoy must not revive the entry, whichever order the map
        // hands them over in.
        for order in [[&candidate, &decoy], [&decoy, &candidate]] {
            let mut trust = EvidenceScanTrust::default();
            trust.record(file.file_id, order[0], 0, fingerprint.clone());
            trust.record(file.file_id, order[1], 1, fingerprint.clone());
            trust.record(file.file_id, order[0], 2, fingerprint.clone());

            let blocks = ScanBlockState::new(&state.blocks);
            assert!(
                evidence_settled_slices(&trust, file, &candidate, &blocks, slice_size)
                    .iter()
                    .all(|set| !*set)
            );
        }
    }

    fn scan_with_buffered(
        state: &RepairState,
        path: &Path,
        kind: BlockLocationKind,
        read_target: usize,
    ) -> Vec<Option<(PathBuf, u64, u64, BlockLocationKind)>> {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        scanner
            .scan_file_buffered_with_target(
                path,
                kind,
                &state.files,
                &state.file_index_by_id,
                &mut blocks,
                read_target,
            )
            .unwrap();
        block_location_summary(&blocks)
    }

    fn scan_with_buffered_options(
        state: &RepairState,
        path: &Path,
        kind: BlockLocationKind,
        read_target: usize,
        scan_options: ScanSkipOptions,
    ) -> (BlockLocationSummary, FileScanStats) {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        let stats = scanner
            .scan_file_buffered_with_target_options(
                path,
                kind,
                SourceFileScanLookup {
                    files: &state.files,
                    file_index_by_id: &state.file_index_by_id,
                },
                &mut blocks,
                read_target,
                scan_options,
            )
            .unwrap();
        (block_location_summary(&blocks), stats)
    }

    #[test]
    fn buffered_scan_matches_mmap_for_intact_full_blocks() {
        let dir = tempdir().unwrap();
        let target: Vec<u8> = (0..256u32).map(|value| (value % 251) as u8).collect();
        let set = synthetic_set(&[("target.bin", &target)], 64);
        let candidate = dir.path().join("candidate.bin");
        fs::write(&candidate, &target).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let mmap = scan_with_mmap(&state, &candidate, BlockLocationKind::Extra);
        let buffered = scan_with_buffered(&state, &candidate, BlockLocationKind::Extra, 96);

        assert_eq!(buffered, mmap);
    }

    #[test]
    fn buffered_scan_matches_mmap_for_damaged_partial_matches() {
        let dir = tempdir().unwrap();
        let target = b"aaaabbbbccccdddd".to_vec();
        let set = synthetic_set(&[("target.bin", &target)], 4);
        let candidate = dir.path().join("partial.bin");
        fs::write(&candidate, b"xxxxbbbbzzzzdddd").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let mmap = scan_with_mmap(&state, &candidate, BlockLocationKind::Extra);
        let buffered = scan_with_buffered(&state, &candidate, BlockLocationKind::Extra, 7);

        assert_eq!(buffered, mmap);
    }

    #[test]
    fn ordered_canonical_scan_matches_generic_locations_for_shifted_damage() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let mut target = Vec::new();
        let mut blocks = Vec::new();
        for block in 0..6u8 {
            let bytes = (0..slice_size as usize)
                .map(|index| block.wrapping_mul(37).wrapping_add(index as u8))
                .collect::<Vec<_>>();
            target.extend_from_slice(&bytes);
            blocks.push(bytes);
        }
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        let mut damaged = Vec::new();
        damaged.extend_from_slice(&blocks[0]);
        damaged.extend_from_slice(&blocks[1]);
        damaged.extend_from_slice(&blocks[3]);
        damaged.extend_from_slice(&blocks[4]);
        damaged.extend_from_slice(&blocks[5]);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (generic_locations, generic_stats) =
            scan_with_mmap_stats(&state, &candidate, BlockLocationKind::Canonical);
        let (ordered_locations, ordered_stats) = scan_with_ordered_canonical(&state, &candidate);

        assert_eq!(ordered_locations, generic_locations);
        assert!(ordered_stats.jumps_taken >= 3);
        assert!(ordered_stats.windows_stepped < generic_stats.windows_stepped);
    }

    #[test]
    fn ordered_canonical_scan_preserves_mixed_block_harvesting() {
        let dir = tempdir().unwrap();
        let alpha = b"aaaabbbbccccdddd".to_vec();
        let beta = b"1111222233334444".to_vec();
        let set = synthetic_set(&[("alpha.bin", &alpha), ("beta.bin", &beta)], 4);
        let candidate = dir.path().join("alpha.bin");
        fs::write(&candidate, b"aaaa2222ccccxxxx").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (generic_locations, _) =
            scan_with_mmap_stats(&state, &candidate, BlockLocationKind::Canonical);
        let (ordered_locations, ordered_stats) = scan_with_ordered_canonical(&state, &candidate);

        assert_eq!(ordered_locations, generic_locations);
        assert_eq!(
            ordered_locations[5],
            Some((candidate.clone(), 4, 4, BlockLocationKind::Canonical))
        );
        assert!(ordered_stats.jumps_taken >= 1);
    }

    #[test]
    fn ordered_canonical_scan_ignores_already_used_duplicate_block_when_jumping() {
        let dir = tempdir().unwrap();
        let target = b"aaaabbbbaaaacccc".to_vec();
        let set = synthetic_set(&[("target.bin", &target)], 4);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, b"aaaaaaaacccc").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (ordered_locations, ordered_stats) = scan_with_ordered_canonical(&state, &candidate);

        assert_eq!(
            ordered_locations[0],
            Some((candidate.clone(), 0, 4, BlockLocationKind::Canonical))
        );
        assert_eq!(
            ordered_locations[2],
            Some((candidate.clone(), 4, 4, BlockLocationKind::Canonical))
        );
        assert!(ordered_stats.jumps_taken >= 2);
    }

    #[test]
    fn ordered_canonical_scan_checks_shifted_short_file_below_slice_size() {
        let dir = tempdir().unwrap();
        let target = b"ABCDE".to_vec();
        let set = synthetic_set(&[("target.bin", &target)], 8);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, b"xABCDEy").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (ordered_locations, ordered_stats) = scan_with_ordered_canonical(&state, &candidate);

        assert_eq!(
            ordered_locations[0],
            Some((candidate.clone(), 1, 5, BlockLocationKind::Canonical))
        );
        assert_eq!(ordered_stats.windows_stepped, 0);
    }

    #[test]
    fn shifted_large_short_block_scans_without_large_heap_buffer() {
        let dir = tempdir().unwrap();
        let short_len = SCANNER_IO_TARGET_BYTES + 1;
        let slice_size = short_len as u64 + 1024;
        let target = (0..short_len)
            .map(|index| (index as u8).wrapping_mul(31).wrapping_add(7))
            .collect::<Vec<_>>();
        let set = synthetic_set(&[("large-short.bin", &target)], slice_size);
        let candidate = dir.path().join("large-short.bin");
        let mut damaged = Vec::with_capacity(short_len + 2);
        damaged.push(0xA5);
        damaged.extend_from_slice(&target);
        damaged.push(0x5A);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (ordered_locations, _) = scan_with_ordered_canonical(&state, &candidate);

        assert_eq!(
            ordered_locations[0],
            Some((
                candidate.clone(),
                1,
                short_len as u64,
                BlockLocationKind::Canonical
            ))
        );
    }

    #[test]
    fn ordered_canonical_scan_checks_every_byte_through_long_miss_runs() {
        let dir = tempdir().unwrap();
        let slice_size = 1024u64;
        let block = |seed: u8| {
            (0..slice_size as usize)
                .map(|index| seed.wrapping_add(index as u8))
                .collect::<Vec<_>>()
        };
        let first = block(3);
        let second = block(71);
        let target = [first.as_slice(), second.as_slice()].concat();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        let mut damaged = Vec::new();
        damaged.extend_from_slice(&first);
        damaged.extend(std::iter::repeat_n(0xEE, 176));
        damaged.extend_from_slice(&second);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (generic_locations, generic_stats) =
            scan_with_mmap_stats(&state, &candidate, BlockLocationKind::Canonical);
        let (ordered_locations, ordered_stats) = scan_with_ordered_canonical(&state, &candidate);

        assert_eq!(ordered_locations, generic_locations);
        assert_eq!(
            ordered_locations[1],
            Some((
                candidate.clone(),
                slice_size + 176,
                slice_size,
                BlockLocationKind::Canonical
            ))
        );
        assert!(ordered_stats.jumps_taken >= 2);
        assert!(ordered_stats.windows_stepped > 64);
        assert!(ordered_stats.windows_stepped <= generic_stats.windows_stepped);
    }

    #[test]
    fn ordered_canonical_scan_can_skip_long_in_place_miss_runs_when_enabled() {
        let dir = tempdir().unwrap();
        let slice_size = 1024u64;
        let make_block = |seed: u8| {
            (0..slice_size as usize)
                .map(|index| seed.wrapping_mul(17).wrapping_add(index as u8))
                .collect::<Vec<_>>()
        };
        let blocks = [
            make_block(3),
            make_block(31),
            make_block(71),
            make_block(109),
        ];
        let target = blocks
            .iter()
            .flat_map(|block| block.iter().copied())
            .collect::<Vec<_>>();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        let mut damaged = target.clone();
        damaged[slice_size as usize..(slice_size as usize * 2)].fill(0xEE);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (default_locations, default_stats) = scan_with_ordered_canonical(&state, &candidate);
        let (skip_locations, skip_stats) = scan_with_ordered_canonical_options(
            &state,
            &candidate,
            ScanSkipOptions {
                skip_data: true,
                skip_leeway: ORDERED_SCAN_DEFAULT_SKIP_LEEWAY,
            },
        );

        assert_eq!(skip_locations, default_locations);
        assert_eq!(
            skip_locations[0],
            Some((
                candidate.clone(),
                0,
                slice_size,
                BlockLocationKind::Canonical
            ))
        );
        assert_eq!(skip_locations[1], None);
        assert_eq!(
            skip_locations[2],
            Some((
                candidate.clone(),
                slice_size * 2,
                slice_size,
                BlockLocationKind::Canonical
            ))
        );
        assert_eq!(
            skip_locations[3],
            Some((
                candidate.clone(),
                slice_size * 3,
                slice_size,
                BlockLocationKind::Canonical
            ))
        );
        assert!(default_stats.windows_stepped >= slice_size);
        assert!(skip_stats.windows_stepped < default_stats.windows_stepped / 2);
        assert!(skip_stats.max_consecutive_steps <= ORDERED_SCAN_DEFAULT_SKIP_LEEWAY);
    }

    fn scan_ordered_serial_direct(
        state: &RepairState,
        path: &Path,
    ) -> (BlockLocationSummary, FileScanStats) {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        let baseline = blocks.clone();
        let mut scan_state = ScanBlockState::new(&baseline);
        let target = state
            .files
            .iter()
            .find(|file| file.safe_path == path)
            .unwrap();
        let stats = scanner
            .scan_file_ordered_canonical_serial(
                path,
                BlockLocationKind::Canonical,
                SourceFileScanLookup {
                    files: &state.files,
                    file_index_by_id: &state.file_index_by_id,
                },
                target,
                &mut scan_state,
                ScanSkipOptions::disabled(),
                &[],
            )
            .unwrap();
        scan_state.apply_to_blocks(&mut blocks);
        (block_location_summary(&blocks), stats)
    }

    fn scan_ordered_parallel_direct(
        state: &RepairState,
        path: &Path,
        segment_windows: usize,
    ) -> (BlockLocationSummary, FileScanStats) {
        scan_ordered_parallel_direct_with_memory_limit(
            state,
            path,
            segment_windows,
            DEFAULT_REPAIR_MEMORY_LIMIT,
        )
    }

    fn scan_ordered_parallel_direct_with_memory_limit(
        state: &RepairState,
        path: &Path,
        segment_windows: usize,
        memory_limit: usize,
    ) -> (BlockLocationSummary, FileScanStats) {
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();
        let baseline = blocks.clone();
        let mut scan_state = ScanBlockState::new(&baseline);
        let target = state
            .files
            .iter()
            .find(|file| file.safe_path == path)
            .unwrap();
        let stats = scanner
            .scan_file_ordered_canonical_parallel(
                path,
                BlockLocationKind::Canonical,
                SourceFileScanLookup {
                    files: &state.files,
                    file_index_by_id: &state.file_index_by_id,
                },
                target,
                &mut scan_state,
                ScanSkipOptions::disabled(),
                segment_windows,
                memory_limit,
                None,
            )
            .unwrap();
        scan_state.apply_to_blocks(&mut blocks);
        (block_location_summary(&blocks), stats)
    }

    fn scan_stat_counters(stats: FileScanStats) -> (u64, u64, u64, u64) {
        (
            stats.bytes_scanned,
            stats.windows_stepped,
            stats.jumps_taken,
            stats.max_consecutive_steps,
        )
    }

    /// Runs the serial scanner and the parallel scanner (forced-tiny and
    /// default segment sizes) over the same candidate and asserts identical
    /// block locations and scan counters. Returns the serial result for
    /// fixture-specific assertions.
    fn assert_ordered_scan_parity(
        state: &RepairState,
        path: &Path,
    ) -> (BlockLocationSummary, FileScanStats) {
        let (serial_locations, serial_stats) = scan_ordered_serial_direct(state, path);
        let default_segment = ordered_scan_segment_windows(state.set.slice_size as usize);
        for segment_windows in [1usize, 2, default_segment] {
            let (parallel_locations, parallel_stats) =
                scan_ordered_parallel_direct(state, path, segment_windows);
            assert_eq!(
                parallel_locations, serial_locations,
                "locations diverged with segment_windows={segment_windows}"
            );
            assert_eq!(
                scan_stat_counters(parallel_stats),
                scan_stat_counters(serial_stats),
                "scan counters diverged with segment_windows={segment_windows}"
            );
        }
        (serial_locations, serial_stats)
    }

    fn seeded_block(seed: u8, slice_size: usize) -> Vec<u8> {
        (0..slice_size)
            .map(|index| {
                seed.wrapping_mul(37)
                    .wrapping_add((index as u8).wrapping_mul(11))
            })
            .collect()
    }

    #[test]
    fn ordered_parallel_scan_matches_serial_for_intact_file() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let mut target = Vec::new();
        for seed in 0..6u8 {
            target.extend_from_slice(&seeded_block(seed, slice_size as usize));
        }
        target.extend_from_slice(b"tail!");
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, &target).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, stats) = assert_ordered_scan_parity(&state, &candidate);

        assert!(locations.iter().all(Option::is_some));
        assert_eq!(stats.windows_stepped, 0);
        assert_eq!(stats.jumps_taken, 6);
    }

    #[test]
    fn ordered_parallel_scan_matches_serial_for_deleted_full_block() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let blocks: Vec<Vec<u8>> = (0..6u8)
            .map(|seed| seeded_block(seed, slice_size as usize))
            .collect();
        let target: Vec<u8> = blocks.concat();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        let mut damaged = Vec::new();
        for (index, block) in blocks.iter().enumerate() {
            if index != 2 {
                damaged.extend_from_slice(block);
            }
        }
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, _) = assert_ordered_scan_parity(&state, &candidate);
        let generic = scan_with_mmap(&state, &candidate, BlockLocationKind::Canonical);

        assert_eq!(locations, generic);
        assert_eq!(locations[2], None);
        assert_eq!(
            locations[3],
            Some((
                candidate.clone(),
                slice_size * 2,
                slice_size,
                BlockLocationKind::Canonical
            ))
        );
    }

    #[test]
    fn ordered_parallel_scan_matches_serial_for_insertion_gap() {
        let dir = tempdir().unwrap();
        let slice_size = 1024u64;
        let first = seeded_block(3, slice_size as usize);
        let second = seeded_block(71, slice_size as usize);
        let target = [first.as_slice(), second.as_slice()].concat();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate = dir.path().join("target.bin");
        let mut damaged = Vec::new();
        damaged.extend_from_slice(&first);
        damaged.extend(std::iter::repeat_n(0xEE, 176));
        damaged.extend_from_slice(&second);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, stats) = assert_ordered_scan_parity(&state, &candidate);

        assert_eq!(
            locations[1],
            Some((
                candidate.clone(),
                slice_size + 176,
                slice_size,
                BlockLocationKind::Canonical
            ))
        );
        assert!(stats.windows_stepped > 64);
    }

    #[test]
    fn ordered_parallel_scan_realigns_mid_file_after_compensating_deletion() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let blocks: Vec<Vec<u8>> = (0..6u8)
            .map(|seed| seeded_block(seed.wrapping_add(11), slice_size))
            .collect();
        let target: Vec<u8> = blocks.concat();
        let set = synthetic_set(&[("target.bin", &target)], slice_size as u64);
        let candidate = dir.path().join("target.bin");
        // Insert 17 junk bytes before block 1, then replace block 2 with 47
        // junk bytes: block 1 matches unaligned, block 3 realigns exactly at
        // 3 * slice_size, so the resync must splice back into the aligned
        // merge mid-file.
        let insert_len = 17usize;
        let mut damaged = Vec::new();
        damaged.extend_from_slice(&blocks[0]);
        damaged.extend(std::iter::repeat_n(0xEE, insert_len));
        damaged.extend_from_slice(&blocks[1]);
        damaged.extend(std::iter::repeat_n(0xDD, slice_size - insert_len));
        damaged.extend_from_slice(&blocks[3]);
        damaged.extend_from_slice(&blocks[4]);
        damaged.extend_from_slice(&blocks[5]);
        assert_eq!(damaged.len(), target.len());
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, stats) = assert_ordered_scan_parity(&state, &candidate);

        let aligned = |index: u64| {
            Some((
                candidate.clone(),
                index * slice_size as u64,
                slice_size as u64,
                BlockLocationKind::Canonical,
            ))
        };
        assert_eq!(locations[0], aligned(0));
        assert_eq!(
            locations[1],
            Some((
                candidate.clone(),
                (slice_size + insert_len) as u64,
                slice_size as u64,
                BlockLocationKind::Canonical
            ))
        );
        assert_eq!(locations[2], None);
        assert_eq!(locations[3], aligned(3));
        assert_eq!(locations[4], aligned(4));
        assert_eq!(locations[5], aligned(5));
        assert_eq!(stats.jumps_taken, 5);
    }

    #[test]
    fn ordered_parallel_scan_stays_misaligned_through_unaligned_tail() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let blocks: Vec<Vec<u8>> = (0..5u8)
            .map(|seed| seeded_block(seed.wrapping_add(29), slice_size))
            .collect();
        let target: Vec<u8> = blocks.concat();
        let set = synthetic_set(&[("target.bin", &target)], slice_size as u64);
        let candidate = dir.path().join("target.bin");
        // Delete 17 bytes (not a multiple of the slice size) from block 1:
        // every later block sits at an unaligned offset until EOF, so the
        // scan never realigns after the gap.
        let mut damaged = Vec::new();
        damaged.extend_from_slice(&blocks[0]);
        damaged.extend_from_slice(&blocks[1][..slice_size - 17]);
        damaged.extend_from_slice(&blocks[2]);
        damaged.extend_from_slice(&blocks[3]);
        damaged.extend_from_slice(&blocks[4]);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, _) = assert_ordered_scan_parity(&state, &candidate);
        let generic = scan_with_mmap(&state, &candidate, BlockLocationKind::Canonical);

        assert_eq!(locations, generic);
        assert_eq!(locations[1], None);
        for index in [2u64, 3, 4] {
            assert_eq!(
                locations[index as usize],
                Some((
                    candidate.clone(),
                    index * slice_size as u64 - 17,
                    slice_size as u64,
                    BlockLocationKind::Canonical
                ))
            );
        }
    }

    #[test]
    fn ordered_parallel_scan_dedupes_duplicate_blocks_across_segment_boundary() {
        let dir = tempdir().unwrap();
        let target = b"aaaabbbbaaaacccc".to_vec();
        let set = synthetic_set(&[("target.bin", &target)], 4);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, b"aaaaaaaacccc").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        // The parity helper forces one- and two-window segments, so the
        // duplicate pair lands in different Phase A tasks.
        let (locations, _) = assert_ordered_scan_parity(&state, &candidate);

        assert_eq!(
            locations[0],
            Some((candidate.clone(), 0, 4, BlockLocationKind::Canonical))
        );
        assert_eq!(
            locations[2],
            Some((candidate.clone(), 4, 4, BlockLocationKind::Canonical))
        );
    }

    #[test]
    fn ordered_parallel_scan_prefers_target_blocks_over_cross_file_duplicates() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let shared = seeded_block(200, slice_size);
        let alpha = [
            shared.clone(),
            seeded_block(1, slice_size),
            seeded_block(2, slice_size),
            seeded_block(3, slice_size),
        ]
        .concat();
        let beta = [
            seeded_block(4, slice_size),
            shared.clone(),
            seeded_block(5, slice_size),
            seeded_block(6, slice_size),
        ]
        .concat();
        let set = synthetic_set(
            &[("alpha.bin", &alpha), ("beta.bin", &beta)],
            slice_size as u64,
        );
        let candidate = dir.path().join("alpha.bin");
        // Junk replaces alpha's first block, pushing the shared block to an
        // offset only reachable through the resync loop; the target's copy
        // (rank 1) must win over beta's identical block (rank 2).
        let mut damaged = Vec::new();
        damaged.extend(std::iter::repeat_n(0xEE, slice_size));
        damaged.extend_from_slice(&shared);
        damaged.extend_from_slice(&alpha[slice_size * 2..]);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, _) = assert_ordered_scan_parity(&state, &candidate);

        assert_eq!(
            locations[0],
            Some((
                candidate.clone(),
                slice_size as u64,
                slice_size as u64,
                BlockLocationKind::Canonical
            ))
        );
        // Beta's identical block stays unclaimed by alpha's scan.
        assert_eq!(locations[5], None);
    }

    #[test]
    fn ordered_parallel_scan_matches_serial_for_mixed_harvesting() {
        let dir = tempdir().unwrap();
        let alpha = b"aaaabbbbccccdddd".to_vec();
        let beta = b"1111222233334444".to_vec();
        let set = synthetic_set(&[("alpha.bin", &alpha), ("beta.bin", &beta)], 4);
        let candidate = dir.path().join("alpha.bin");
        fs::write(&candidate, b"aaaa2222ccccxxxx").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, _) = assert_ordered_scan_parity(&state, &candidate);

        assert_eq!(
            locations[5],
            Some((candidate.clone(), 4, 4, BlockLocationKind::Canonical))
        );
    }

    #[test]
    fn ordered_parallel_scan_matches_serial_with_segment_boundary_damage() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let blocks: Vec<Vec<u8>> = (0..8u8)
            .map(|seed| seeded_block(seed.wrapping_add(53), slice_size))
            .collect();
        let target: Vec<u8> = blocks.concat();
        let set = synthetic_set(&[("target.bin", &target)], slice_size as u64);
        let candidate = dir.path().join("target.bin");
        // With two-window segments, window 1 is the last of segment 0 and
        // window 2 the first of segment 1; damaging both spans the boundary.
        let mut damaged = target.clone();
        damaged[slice_size..slice_size * 3].fill(0xEE);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, _) = assert_ordered_scan_parity(&state, &candidate);

        assert_eq!(locations[1], None);
        assert_eq!(locations[2], None);
        for index in [0usize, 3, 4, 5, 6, 7] {
            assert_eq!(
                locations[index],
                Some((
                    candidate.clone(),
                    index as u64 * slice_size as u64,
                    slice_size as u64,
                    BlockLocationKind::Canonical
                ))
            );
        }
    }

    #[test]
    fn ordered_parallel_scan_matches_serial_with_adjacent_damage_mid_segment() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let blocks: Vec<Vec<u8>> = (0..10u8)
            .map(|seed| seeded_block(seed.wrapping_add(101), slice_size))
            .collect();
        let target: Vec<u8> = blocks.concat();
        let set = synthetic_set(&[("target.bin", &target)], slice_size as u64);
        let candidate = dir.path().join("target.bin");
        // Adjacent damaged windows 3 and 4 sit inside a single default-size
        // segment, so the gap resync starts and realigns without crossing a
        // segment boundary.
        let mut damaged = target.clone();
        damaged[slice_size * 3..slice_size * 5].fill(0xEE);
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, _) = assert_ordered_scan_parity(&state, &candidate);

        assert_eq!(locations[3], None);
        assert_eq!(locations[4], None);
        assert!(locations.iter().filter(|entry| entry.is_some()).count() == 8);
    }

    #[test]
    fn ordered_parallel_scan_facts_allocation_is_checked() {
        let fact_size = std::mem::size_of::<AlignedWindowFacts>();
        assert_eq!(ordered_scan_facts_allocation_bytes(0), Some(0));
        assert_eq!(ordered_scan_facts_allocation_bytes(3), Some(fact_size * 3));
        assert_eq!(ordered_scan_facts_allocation_bytes(usize::MAX), None);
    }

    /// Smallest working-memory limit whose admission still leaves room for
    /// `match_bytes` of retained match entries. Searched rather than derived:
    /// the read buffers shrink as the limit does, so the match budget is not
    /// monotone in the limit and has no closed form. `match_bytes` is a sound
    /// floor for the search — the budget can never exceed the whole limit.
    fn smallest_limit_admitting_matches(
        window_count: usize,
        segment_windows: usize,
        slice_size: usize,
        max_crc_bucket: usize,
        match_bytes: usize,
        search_span: usize,
    ) -> usize {
        let workers = ordered_scan_workers(window_count, segment_windows);
        (match_bytes..=match_bytes + search_span)
            .find(|limit| {
                ordered_scan_admission(
                    window_count,
                    segment_windows,
                    slice_size,
                    max_crc_bucket,
                    workers,
                    *limit,
                )
                .is_some_and(|admission| admission.match_budget >= match_bytes)
            })
            .expect("no limit inside the search span admits the scan")
    }

    #[test]
    fn ordered_parallel_scan_falls_back_when_facts_exceed_memory_limit() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let target = seeded_block(76, slice_size);
        let set = synthetic_set(&[("target.bin", &target)], slice_size as u64);
        let candidate = dir.path().join("target.bin");
        let mut oversized = target.clone();
        oversized.resize(slice_size * 9, 0xEE);
        fs::write(&candidate, oversized).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        let facts_bytes = ordered_scan_facts_allocation_bytes(9).unwrap();
        // Only the first window matches the set's single block.
        let retained_bytes = std::mem::size_of::<u32>();
        let admitting_limit = smallest_limit_admitting_matches(
            9,
            2,
            slice_size,
            state.hash_table.max_crc_bucket,
            retained_bytes,
            64 * 1024,
        );
        assert!(facts_bytes < admitting_limit);

        let (parallel_locations, parallel_stats) =
            scan_ordered_parallel_direct_with_memory_limit(&state, &candidate, 2, admitting_limit);
        assert_eq!(parallel_stats.mode, FileScanMode::OrderedCanonicalParallel);

        for starved_limit in [admitting_limit - 1, facts_bytes - 1] {
            let (fallback_locations, fallback_stats) =
                scan_ordered_parallel_direct_with_memory_limit(
                    &state,
                    &candidate,
                    2,
                    starved_limit,
                );
            assert_eq!(fallback_stats.mode, FileScanMode::OrderedCanonical);
            assert_eq!(fallback_locations, parallel_locations);
        }
    }

    /// The admission gap the fixed-header budget missed: Phase A retains one
    /// `Vec<u32>` of matching slice indices per aligned window, so a recovery
    /// set with many byte-identical slices multiplies retained bytes far past
    /// the header cost the old accounting checked. 256 duplicate slices over
    /// 2,048 aligned windows is 48 KiB of headers against 2 MiB of retained
    /// entries.
    #[test]
    fn ordered_parallel_scan_refuses_duplicate_slice_match_blowup() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let duplicate_count = 256usize;
        let window_count = 2048usize;
        let segment_windows = 128usize;

        let duplicate = seeded_block(211, slice_size);
        let dupes: Vec<u8> = std::iter::repeat_n(duplicate.as_slice(), duplicate_count)
            .flatten()
            .copied()
            .collect();
        // Every slice of the recorded target is unique, so the candidate's
        // windows can only match the duplicate pool.
        let mut target = Vec::with_capacity(window_count * slice_size);
        for index in 0..window_count {
            target.extend_from_slice(&(index as u32).to_le_bytes());
            target.resize((index + 1) * slice_size, 0x5A);
        }
        let set = synthetic_set(
            &[("dupes.bin", &dupes), ("target.bin", &target)],
            slice_size as u64,
        );
        let candidate = dir.path().join("target.bin");
        let damaged: Vec<u8> = std::iter::repeat_n(duplicate.as_slice(), window_count)
            .flatten()
            .copied()
            .collect();
        fs::write(&candidate, damaged).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        assert_eq!(state.hash_table.max_crc_bucket, duplicate_count);

        let retained_bytes = window_count * duplicate_count * std::mem::size_of::<u32>();
        let facts_bytes = ordered_scan_facts_allocation_bytes(window_count).unwrap();
        assert!(facts_bytes * 16 < retained_bytes);

        let admitting_limit = smallest_limit_admitting_matches(
            window_count,
            segment_windows,
            slice_size,
            duplicate_count,
            retained_bytes,
            8 * 1024 * 1024,
        );
        // One byte short of the retained demand, yet far past what the old
        // header-only accounting checked: this is the shape that used to stay
        // in the parallel scanner while holding 2 MiB it never budgeted. The
        // one-byte gap pins the admission budget to the true retained size
        // rather than merely somewhere near it.
        let starved_limit = admitting_limit - 1;
        assert!(facts_bytes < starved_limit);
        let starved = ordered_scan_admission(
            window_count,
            segment_windows,
            slice_size,
            duplicate_count,
            ordered_scan_workers(window_count, segment_windows),
            starved_limit,
        )
        .unwrap();
        assert_eq!(starved.match_budget, retained_bytes - 1);

        let (serial_locations, serial_stats) = scan_ordered_serial_direct(&state, &candidate);

        let (parallel_locations, parallel_stats) = scan_ordered_parallel_direct_with_memory_limit(
            &state,
            &candidate,
            segment_windows,
            admitting_limit,
        );
        assert_eq!(parallel_stats.mode, FileScanMode::OrderedCanonicalParallel);
        assert_eq!(parallel_locations, serial_locations);
        assert_eq!(
            scan_stat_counters(parallel_stats),
            scan_stat_counters(serial_stats)
        );

        let (refused_locations, refused_stats) = scan_ordered_parallel_direct_with_memory_limit(
            &state,
            &candidate,
            segment_windows,
            starved_limit,
        );
        assert_eq!(refused_stats.mode, FileScanMode::OrderedCanonical);
        assert_eq!(refused_locations, serial_locations);
        assert_eq!(
            scan_stat_counters(refused_stats),
            scan_stat_counters(serial_stats)
        );
    }

    #[test]
    fn ordered_parallel_scan_handles_single_window_file() {
        let dir = tempdir().unwrap();
        let slice_size = 64usize;
        let target = seeded_block(77, slice_size);
        let set = synthetic_set(&[("target.bin", &target)], slice_size as u64);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, &target).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (locations, stats) = assert_ordered_scan_parity(&state, &candidate);
        assert_eq!(
            locations[0],
            Some((
                candidate.clone(),
                0,
                slice_size as u64,
                BlockLocationKind::Canonical
            ))
        );
        assert_eq!(stats.jumps_taken, 1);
        assert_eq!(stats.windows_stepped, 0);

        // Damaged single window: the failed step off the only full window
        // must end the scan without counting a step, in both modes.
        fs::write(&candidate, vec![0xEE; slice_size]).unwrap();
        let (damaged_locations, damaged_stats) = assert_ordered_scan_parity(&state, &candidate);
        assert_eq!(damaged_locations[0], None);
        assert_eq!(damaged_stats.windows_stepped, 0);
        assert_eq!(damaged_stats.jumps_taken, 0);
    }

    #[test]
    fn full_state_scan_with_renamed_copy_matches_between_thread_pools() {
        let slice_size = 64usize;
        let blocks: Vec<Vec<u8>> = (0..6u8)
            .map(|seed| seeded_block(seed.wrapping_add(151), slice_size))
            .collect();
        let mut data: Vec<u8> = blocks.concat();
        data.extend_from_slice(b"short-tail");

        let run_scan = |threads: usize| {
            let dir = tempdir().unwrap();
            let set = synthetic_set(&[("target.bin", &data)], slice_size as u64);
            let mut damaged = data.clone();
            damaged[slice_size * 2..slice_size * 3].fill(0xEE);
            fs::write(dir.path().join("target.bin"), &damaged).unwrap();
            fs::write(dir.path().join("renamed-copy.bin"), &data).unwrap();

            let pool = rayon::ThreadPoolBuilder::new()
                .num_threads(threads)
                .build()
                .unwrap();
            let mut state = RepairState::from_set(dir.path(), set).unwrap();
            let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
            pool.install(|| state.scan(&options)).unwrap();
            let verification = state.verification_result();
            let locations: Vec<Option<(u64, u64, BlockLocationKind, String)>> = state
                .blocks
                .iter()
                .map(|block| {
                    block.location.as_ref().map(|location| {
                        (
                            location.offset,
                            location.len,
                            location.kind,
                            location
                                .path()
                                .expect("scanned location is a path")
                                .file_name()
                                .unwrap()
                                .to_string_lossy()
                                .into_owned(),
                        )
                    })
                })
                .collect();
            let statuses: Vec<String> = verification
                .files
                .iter()
                .map(|file| match &file.status {
                    FileStatus::Renamed(path) => {
                        format!("Renamed({})", path.file_name().unwrap().to_string_lossy())
                    }
                    status => format!("{status:?}"),
                })
                .collect();
            (
                locations,
                statuses,
                verification.total_missing_blocks,
                verification
                    .files
                    .iter()
                    .map(|file| file.valid_slices.clone())
                    .collect::<Vec<_>>(),
            )
        };

        // One thread forces the serial ordered scanner through the
        // dispatcher; four threads take the parallel path.
        let serial = run_scan(1);
        let parallel = run_scan(4);
        assert_eq!(parallel.0, serial.0);
        assert_eq!(parallel.1, serial.1);
        assert_eq!(parallel.2, serial.2);
        assert_eq!(parallel.3, serial.3);
        assert_eq!(serial.2, 0, "complete renamed copy supplies every block");
    }

    struct ParityLcg(u64);

    impl ParityLcg {
        fn next(&mut self) -> u64 {
            self.0 = self
                .0
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            self.0 >> 33
        }

        fn below(&mut self, bound: u64) -> u64 {
            self.next() % bound.max(1)
        }
    }

    #[test]
    fn ordered_parallel_scan_randomized_parity_matches_serial() {
        let slice_size = 64usize;
        let mut rng = ParityLcg(0x5EED_CAFE_F00D_D00D);

        for iteration in 0..24 {
            let dir = tempdir().unwrap();
            let block_count = 4 + rng.below(8) as usize;
            // A small seed alphabet makes duplicate block content likely.
            let mut target = Vec::new();
            for _ in 0..block_count {
                target.extend_from_slice(&seeded_block(rng.below(4) as u8, slice_size));
            }
            if rng.below(2) == 0 {
                let tail_len = 1 + rng.below(slice_size as u64 - 1) as usize;
                target.extend((0..tail_len).map(|_| rng.below(256) as u8));
            }
            let set = synthetic_set(&[("target.bin", &target)], slice_size as u64);

            let mut damaged = target.clone();
            for _ in 0..=rng.below(3) {
                if damaged.is_empty() {
                    break;
                }
                match rng.below(4) {
                    0 => {
                        // In-place corruption.
                        let start = rng.below(damaged.len() as u64) as usize;
                        let len = (1 + rng.below(2 * slice_size as u64) as usize)
                            .min(damaged.len() - start);
                        for byte in &mut damaged[start..start + len] {
                            *byte ^= 0x5A;
                        }
                    }
                    1 => {
                        // Insertion.
                        let at = rng.below(damaged.len() as u64 + 1) as usize;
                        let len = 1 + rng.below(2 * slice_size as u64) as usize;
                        let junk: Vec<u8> = (0..len).map(|_| rng.below(256) as u8).collect();
                        damaged.splice(at..at, junk);
                    }
                    2 => {
                        // Deletion.
                        let start = rng.below(damaged.len() as u64) as usize;
                        let len = (1 + rng.below(2 * slice_size as u64) as usize)
                            .min(damaged.len() - start);
                        damaged.drain(start..start + len);
                    }
                    _ => {
                        // Duplicate a source block's content at a random spot.
                        if damaged.len() >= slice_size {
                            let source = rng.below(block_count as u64) as usize * slice_size;
                            let at = rng.below((damaged.len() - slice_size) as u64 + 1) as usize;
                            let copy = target[source..source + slice_size].to_vec();
                            damaged[at..at + slice_size].copy_from_slice(&copy);
                        }
                    }
                }
            }

            let candidate = dir.path().join("target.bin");
            fs::write(&candidate, &damaged).unwrap();
            let state = RepairState::from_set(dir.path(), set).unwrap();

            let (serial_locations, serial_stats) = scan_ordered_serial_direct(&state, &candidate);
            let default_segment = ordered_scan_segment_windows(slice_size);
            for segment_windows in [2usize, default_segment] {
                let (parallel_locations, parallel_stats) =
                    scan_ordered_parallel_direct(&state, &candidate, segment_windows);
                assert_eq!(
                    parallel_locations,
                    serial_locations,
                    "iteration {iteration}: locations diverged (segment_windows={segment_windows}, damaged_len={})",
                    damaged.len()
                );
                assert_eq!(
                    scan_stat_counters(parallel_stats),
                    scan_stat_counters(serial_stats),
                    "iteration {iteration}: counters diverged (segment_windows={segment_windows}, damaged_len={})",
                    damaged.len()
                );
            }
        }
    }

    #[test]
    fn buffered_generic_scan_can_skip_long_extra_miss_runs_when_enabled() {
        let dir = tempdir().unwrap();
        let slice_size = 128 * 1024u64;
        let target: Vec<u8> = (0..slice_size as usize)
            .map(|index| (index as u8).wrapping_mul(31).wrapping_add(7))
            .collect();
        let set = synthetic_set(&[("target.bin", &target)], slice_size);
        let candidate_data = vec![0xA5; slice_size as usize * 6];
        let candidate = dir.path().join("unrelated-extra.bin");
        fs::write(&candidate, &candidate_data).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let (default_locations, default_stats) = scan_with_buffered_options(
            &state,
            &candidate,
            BlockLocationKind::Extra,
            candidate_data.len(),
            ScanSkipOptions {
                skip_data: false,
                skip_leeway: ORDERED_SCAN_DEFAULT_SKIP_LEEWAY,
            },
        );
        let (skip_locations, skip_stats) = scan_with_buffered_options(
            &state,
            &candidate,
            BlockLocationKind::Extra,
            candidate_data.len(),
            ScanSkipOptions {
                skip_data: true,
                skip_leeway: ORDERED_SCAN_DEFAULT_SKIP_LEEWAY,
            },
        );

        assert_eq!(skip_locations, default_locations);
        assert!(skip_locations.iter().all(Option::is_none));
        assert_eq!(default_stats.jumps_taken, 0);
        assert!(skip_stats.jumps_taken > 0);
        assert!(skip_stats.windows_stepped < default_stats.windows_stepped / 2);
        assert!(skip_stats.max_consecutive_steps <= ORDERED_SCAN_DEFAULT_SKIP_LEEWAY * 2);
    }

    #[test]
    fn buffered_scan_finds_block_across_refill_overlap() {
        let dir = tempdir().unwrap();
        let target: Vec<u8> = (0..64u32)
            .map(|value| (value as u8).wrapping_mul(5).wrapping_add(9))
            .collect();
        let set = synthetic_set(&[("target.bin", &target)], 64);
        let mut candidate_data = vec![0xAA; 150];
        candidate_data.extend_from_slice(&target);
        candidate_data.extend_from_slice(&[0x55; 37]);
        let candidate = dir.path().join("cross-boundary.bin");
        fs::write(&candidate, candidate_data).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();

        scanner
            .scan_file_buffered_with_target(
                &candidate,
                BlockLocationKind::Extra,
                &state.files,
                &state.file_index_by_id,
                &mut blocks,
                80,
            )
            .unwrap();

        let location = blocks[0].location.as_ref().unwrap();
        assert_eq!(location.path(), Some(candidate.as_path()));
        assert_eq!(location.offset, 150);
    }

    #[test]
    fn shifted_short_block_checks_match_mmap_and_buffered() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, b"ABCDEFGHxx12345JUNK").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();

        let mmap = scan_with_mmap(&state, &candidate, BlockLocationKind::Canonical);
        let buffered = scan_with_buffered(&state, &candidate, BlockLocationKind::Canonical, 8);

        assert_eq!(buffered, mmap);
        assert_eq!(
            buffered[1],
            Some((candidate.clone(), 10, 5, BlockLocationKind::Canonical))
        );
    }

    #[test]
    fn large_slice_scanner_uses_mmap_fallback_and_remains_correct() {
        let dir = tempdir().unwrap();
        let slice_size = (SCANNER_MMAP_FALLBACK_SLICE_BYTES + 1) as u64;
        let data = (0..slice_size as usize)
            .map(|index| (index as u8).wrapping_mul(31).wrapping_add(1))
            .collect::<Vec<_>>();
        let set = synthetic_set(&[("large.bin", &data)], slice_size);
        let candidate = dir.path().join("large.bin");
        fs::write(&candidate, &data).unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let mut blocks = state.blocks.clone();

        assert!(scanner_uses_mmap_fallback(slice_size));
        scanner
            .scan_file(
                &candidate,
                BlockLocationKind::Canonical,
                &state.files,
                &state.file_index_by_id,
                &mut blocks,
            )
            .unwrap();

        assert!(blocks.iter().all(|block| block.location.is_some()));
    }

    #[test]
    fn scan_finds_complete_renamed_file_and_copy_only_repair_installs_canonical() {
        let dir = tempdir().unwrap();
        let data = b"block-zero--block-one--tail".to_vec();
        let set = synthetic_set(&[("nested/movie.r00", &data)], 8);
        let renamed = dir.path().join("scrambled.bin");
        fs::write(&renamed, &data).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 0);
        assert!(!state.files_are_canonical_complete());
        assert_eq!(
            state
                .outcome(
                    Par2RepairStatus::RepairPossible,
                    0,
                    0,
                    PacketDiagnostics::default(),
                    ScanDiagnostics::default(),
                    verification.clone(),
                )
                .files_renamed,
            1
        );

        let wrong_block_source = dir.path().join("wrong-block.bin");
        fs::write(&wrong_block_source, vec![0u8; data.len()]).unwrap();
        let file = state
            .files
            .iter()
            .find(|file| file.safe_name == "nested/movie.r00")
            .unwrap();
        state.blocks[file.first_block].location = Some(BlockLocation {
            source: SourceLocation::Path(wrong_block_source),
            offset: 0,
            len: state.blocks[file.first_block].expected_len,
            kind: BlockLocationKind::Extra,
        });

        let repair = state.repair(&options, &verification).unwrap();
        let access = DiskFileAccess::new(repair.install_dir.clone(), &state.set);
        let post = verify_all(&state.set, &access);
        assert_eq!(post.total_missing_blocks, 0);

        state.install_repaired_files(&repair, &options).unwrap();
        assert_eq!(fs::read(dir.path().join("nested/movie.r00")).unwrap(), data);
        assert!(renamed.exists());
    }

    #[test]
    fn scan_uses_partial_blocks_from_extra_file() {
        let dir = tempdir().unwrap();
        let target = b"aaaabbbbccccdddd".to_vec();
        let set = synthetic_set(&[("target.bin", &target)], 4);
        fs::write(dir.path().join("partial.bin"), b"xxxxbbbbzzzzdddd").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 2);
        assert_eq!(
            state
                .blocks
                .iter()
                .filter(|block| block.location.is_some())
                .count(),
            2
        );
    }

    #[test]
    fn scan_parallel_extra_batch_merges_partial_blocks() {
        let dir = tempdir().unwrap();
        let target = b"aaaabbbbccccdddd".to_vec();
        let set = synthetic_set(&[("target.bin", &target)], 4);
        let first = dir.path().join("first.partial");
        let second = dir.path().join("second.partial");
        fs::write(&first, b"aaaaxxxxccccyyyy").unwrap();
        fs::write(&second, b"zzzzbbbbqqqqdddd").unwrap();

        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(2)
            .build()
            .unwrap();
        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let scan = pool.install(|| state.scan(&options)).unwrap();
        let verification = state.verification_result();

        assert_eq!(scan.files_scanned, 2);
        assert_eq!(scan.blocks_found, 4);
        assert_eq!(verification.total_missing_blocks, 0);
        assert_eq!(
            state.blocks[0]
                .location
                .as_ref()
                .and_then(|location| location.path()),
            Some(first.as_path())
        );
        assert_eq!(
            state.blocks[1]
                .location
                .as_ref()
                .and_then(|location| location.path()),
            Some(second.as_path())
        );
        assert_eq!(
            state.blocks[2]
                .location
                .as_ref()
                .and_then(|location| location.path()),
            Some(first.as_path())
        );
        assert_eq!(
            state.blocks[3]
                .location
                .as_ref()
                .and_then(|location| location.path()),
            Some(second.as_path())
        );
    }

    #[test]
    fn copy_only_repair_assembles_mixed_target_from_extra_blocks() {
        let dir = tempdir().unwrap();
        let target = b"aaaabbbbccccdddd".to_vec();
        let set = synthetic_set(&[("target.bin", &target)], 4);
        fs::write(dir.path().join("target.bin"), b"aaaaxxxxccccyyyy").unwrap();
        fs::write(dir.path().join("extra.bin"), b"zzzzbbbbqqqqdddd").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 0);
        assert!(!state.files_are_canonical_complete());
        assert!(matches!(
            verification.repairable,
            Repairability::Repairable {
                blocks_needed: 0,
                ..
            }
        ));

        let repair = state.repair(&options, &verification).unwrap();
        state.install_repaired_files(&repair, &options).unwrap();
        assert_eq!(fs::read(dir.path().join("target.bin")).unwrap(), target);
    }

    #[test]
    fn copy_only_repair_corrects_swapped_complete_files() {
        let dir = tempdir().unwrap();
        let alpha = b"alpha---alpha---".to_vec();
        let beta = b"beta----beta----".to_vec();
        let set = synthetic_set(&[("alpha.bin", &alpha), ("beta.bin", &beta)], 8);
        fs::write(dir.path().join("alpha.bin"), &beta).unwrap();
        fs::write(dir.path().join("beta.bin"), &alpha).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 0);
        assert_eq!(
            state
                .files
                .iter()
                .filter(|file| file.complete_location.is_some())
                .count(),
            2
        );
        assert!(!state.files_are_canonical_complete());

        let repair = state.repair(&options, &verification).unwrap();
        state.install_repaired_files(&repair, &options).unwrap();
        assert_eq!(fs::read(dir.path().join("alpha.bin")).unwrap(), alpha);
        assert_eq!(fs::read(dir.path().join("beta.bin")).unwrap(), beta);
    }

    #[test]
    fn scan_skips_extra_candidates_when_canonical_files_are_complete() {
        let dir = tempdir().unwrap();
        let data = b"complete-target".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 4);
        fs::write(dir.path().join("target.bin"), &data).unwrap();
        fs::write(dir.path().join("aaa-extra.bin"), b"unrelated extra data").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let scan = state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(scan.files_scanned, 1);
        assert_eq!(scan.bytes_scanned, data.len() as u64);
        assert_eq!(verification.total_missing_blocks, 0);
        assert!(state.files_are_canonical_complete());
        assert!(matches!(verification.repairable, Repairability::NotNeeded));
    }

    #[test]
    fn scan_skips_par2_marker_extra_paths() {
        let dir = tempdir().unwrap();
        let data = b"complete-target-hidden-in-par2-path".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 4);
        let marker_paths = [
            dir.path().join("extra.par2"),
            dir.path().join("extra.PAR2"),
            dir.path().join("extra.par2.bak"),
        ];
        for path in &marker_paths {
            fs::write(path, &data).unwrap();
        }

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        options.extra_paths.extend(marker_paths.iter().cloned());
        let scan = state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(scan.files_scanned, 0);
        assert_eq!(verification.total_missing_blocks, state.blocks.len() as u32);
        assert!(state.blocks.iter().all(|block| block.location.is_none()));
    }

    #[test]
    fn scan_ignores_zero_byte_extra_as_complete_source() {
        let dir = tempdir().unwrap();
        let set = synthetic_set(&[("target.bin", b"")], 4);
        let extra = dir.path().join("renamed-empty.bin");
        fs::write(&extra, []).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        options.extra_paths.push(extra);
        let scan = state.scan(&options).unwrap();

        assert_eq!(scan.files_scanned, 1);
        assert!(state.files[0].complete_location.is_none());
    }

    #[test]
    fn scan_canonicalizes_explicit_extra_paths_before_deduping() {
        let dir = tempdir().unwrap();
        let base = dir.path().join("base");
        fs::create_dir(&base).unwrap();
        let data = b"complete-target-from-extra".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 4);
        let extra = dir.path().join("extra.bin");
        fs::write(&extra, &data).unwrap();
        fs::create_dir(dir.path().join("subdir")).unwrap();

        let mut state = RepairState::from_set(&base, set).unwrap();
        let mut options = Par2RepairerOptions::new(base, Vec::new());
        options.extra_paths.push(extra.clone());
        options
            .extra_paths
            .push(dir.path().join("subdir").join("..").join("extra.bin"));
        let scan = state.scan(&options).unwrap();
        let verification = state.verification_result();
        let canonical_extra = canonical_extra_path(&extra);

        assert_eq!(scan.files_scanned, 1);
        assert_eq!(verification.total_missing_blocks, 0);
        assert_eq!(
            state.blocks[0]
                .location
                .as_ref()
                .and_then(|location| location.path()),
            Some(canonical_extra.as_path())
        );
    }

    #[cfg(unix)]
    #[test]
    fn scan_does_not_follow_symlinked_extra_directories() {
        use std::os::unix::fs::symlink;

        let dir = tempdir().unwrap();
        let base = dir.path().join("base");
        let outside = dir.path().join("outside");
        fs::create_dir(&base).unwrap();
        fs::create_dir(&outside).unwrap();

        let data = b"outside-complete-target".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 4);
        fs::write(outside.join("candidate.bin"), &data).unwrap();
        symlink(&outside, base.join("linked-outside")).unwrap();

        let mut state = RepairState::from_set(&base, set).unwrap();
        let options = Par2RepairerOptions::new(base.clone(), Vec::new());
        let scan = state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(scan.files_scanned, 0);
        assert_eq!(verification.total_missing_blocks, state.blocks.len() as u32);
        assert!(state.blocks.iter().all(|block| block.location.is_none()));
    }

    #[cfg(unix)]
    #[test]
    fn scan_does_not_follow_explicit_symlinked_extra_files() {
        use std::os::unix::fs::symlink;

        let dir = tempdir().unwrap();
        let base = dir.path().join("base");
        fs::create_dir(&base).unwrap();
        let data = b"symlinked-complete-target".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 4);
        let outside = dir.path().join("outside.bin");
        let linked = base.join("linked-extra.bin");
        fs::write(&outside, &data).unwrap();
        symlink(&outside, &linked).unwrap();

        let mut state = RepairState::from_set(&base, set).unwrap();
        let mut options = Par2RepairerOptions::new(base, Vec::new());
        options.extra_paths.push(linked);
        let scan = state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(scan.files_scanned, 0);
        assert_eq!(verification.total_missing_blocks, state.blocks.len() as u32);
        assert!(state.blocks.iter().all(|block| block.location.is_none()));
    }

    #[cfg(unix)]
    #[test]
    fn scan_skips_unreadable_extra_directories() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempdir().unwrap();
        let base = dir.path().join("base");
        let closed = base.join("closed");
        fs::create_dir_all(&closed).unwrap();

        let data = b"visible-complete-target".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 4);
        let visible = base.join("candidate.bin");
        fs::write(&visible, &data).unwrap();

        let original_perms = fs::metadata(&closed).unwrap().permissions();
        let mut closed_perms = original_perms.clone();
        closed_perms.set_mode(0o0);
        fs::set_permissions(&closed, closed_perms).unwrap();

        let mut state = RepairState::from_set(&base, set).unwrap();
        let options = Par2RepairerOptions::new(base.clone(), Vec::new());
        let scan = state.scan(&options);

        fs::set_permissions(&closed, original_perms).unwrap();

        let scan = scan.unwrap();
        assert_eq!(scan.files_scanned, 1);
        assert_eq!(
            state.blocks[0]
                .location
                .as_ref()
                .and_then(|location| location.path()),
            Some(visible.as_path())
        );
    }

    #[test]
    fn duplicate_basenames_in_different_directories_stay_distinct() {
        let dir = tempdir().unwrap();
        let first = b"first---payload".to_vec();
        let second = b"second--payload".to_vec();
        let set = synthetic_set(
            &[
                ("season1/episode.mkv", &first),
                ("season2/episode.mkv", &second),
            ],
            8,
        );
        fs::create_dir_all(dir.path().join("season1")).unwrap();
        fs::create_dir_all(dir.path().join("season2")).unwrap();
        fs::write(dir.path().join("season1/episode.mkv"), &first).unwrap();
        fs::write(dir.path().join("season2/episode.mkv"), &second).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 0);
        assert!(state.files_are_canonical_complete());
        assert!(matches!(verification.repairable, Repairability::NotNeeded));
    }

    #[test]
    fn recoverable_file_without_ifsc_verifies_by_full_hash() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        set.slice_checksums.clear();
        fs::write(dir.path().join("target.bin"), &data).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 0);
        assert!(matches!(
            verification.files.first().map(|file| &file.status),
            Some(FileStatus::Complete)
        ));
        assert!(matches!(verification.repairable, Repairability::NotNeeded));
    }

    #[test]
    fn large_recoverable_file_without_ifsc_does_not_skip_full_hash() {
        let dir = tempdir().unwrap();
        let data = (0..CANONICAL_COMPLETE_HASH_SKIP_BYTES + 17)
            .map(|idx| (idx % 251) as u8)
            .collect::<Vec<_>>();
        let mut set = synthetic_set(&[("target.bin", &data)], 64);
        set.slice_checksums.clear();
        fs::write(dir.path().join("target.bin"), &data).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(state.inconsistent_packets, 1);
        assert_eq!(state.files[0].block_count, 0);
        assert_eq!(verification.total_missing_blocks, 0);
        assert!(matches!(
            verification.files.first().map(|file| &file.status),
            Some(FileStatus::Complete)
        ));
        assert!(matches!(verification.repairable, Repairability::NotNeeded));
    }

    #[test]
    fn recoverable_file_without_ifsc_rejects_wrong_existing_target() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        set.slice_checksums.clear();
        fs::write(dir.path().join("target.bin"), b"ccccdddd").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(state.inconsistent_packets, 1);
        assert_eq!(state.files[0].block_count, 0);
        assert_eq!(verification.total_missing_blocks, 2);
        assert!(matches!(
            verification.files.first().map(|file| &file.status),
            Some(FileStatus::Damaged(2))
        ));
        assert!(matches!(
            verification.repairable,
            Repairability::Insufficient { .. }
        ));
    }

    #[test]
    fn recoverable_file_without_ifsc_is_repairable_with_enough_parity() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        set.slice_checksums.clear();
        for exponent in 0..2u32 {
            set.recovery_slices.insert(
                exponent,
                crate::par2_set::RecoverySlice {
                    exponent,
                    data: bytes::Bytes::from(vec![0u8; 4]).into(),
                },
            );
        }
        fs::write(dir.path().join("target.bin"), b"ccccdddd").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(state.inconsistent_packets, 1);
        assert_eq!(state.files[0].block_count, 0);
        assert_eq!(verification.total_missing_blocks, 2);
        assert!(matches!(
            verification.repairable,
            Repairability::Repairable {
                blocks_needed: 2,
                blocks_available: 2
            }
        ));
    }

    #[test]
    fn recoverable_file_with_invalid_ifsc_stays_visible_but_unrepairable() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        let file_id = set.recovery_file_ids[0];
        set.slice_checksums.get_mut(&file_id).unwrap().pop();

        let state = RepairState::from_set(dir.path(), set).unwrap();
        let verification = state.verification_result();

        assert_eq!(state.inconsistent_packets, 1);
        assert_eq!(state.files[0].block_count, 0);
        assert_eq!(verification.total_missing_blocks, 2);
        assert!(matches!(
            verification.files.first().map(|file| &file.status),
            Some(FileStatus::Missing)
        ));
        assert!(matches!(
            verification.repairable,
            Repairability::Insufficient { .. }
        ));
    }

    #[test]
    fn recoverable_file_with_invalid_ifsc_verifies_by_full_hash() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        let file_id = set.recovery_file_ids[0];
        set.slice_checksums.get_mut(&file_id).unwrap().pop();
        fs::write(dir.path().join("target.bin"), &data).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(state.inconsistent_packets, 1);
        assert_eq!(state.files[0].block_count, 0);
        assert_eq!(verification.total_missing_blocks, 0);
        assert!(matches!(
            verification.files.first().map(|file| &file.status),
            Some(FileStatus::Complete)
        ));
        assert!(matches!(verification.repairable, Repairability::NotNeeded));
    }

    #[test]
    fn recoverable_file_without_description_is_discarded() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        let file_id = set.recovery_file_ids[0];
        set.files.remove(&file_id);
        set.slice_checksums.remove(&file_id);

        let state = RepairState::from_set(dir.path(), set).unwrap();
        let verification = state.verification_result();

        assert_eq!(state.inconsistent_packets, 1);
        assert_eq!(state.discarded_recoverable_files, 1);
        assert!(state.files.is_empty());
        assert_eq!(verification.files.len(), 0);
        assert_eq!(verification.total_missing_blocks, 0);
        assert!(matches!(
            verification.repairable,
            Repairability::Insufficient { .. }
        ));
        assert!(!state.files_are_canonical_complete());
    }

    #[test]
    fn recovery_packet_with_wrong_size_is_discarded_from_capacity() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        set.recovery_slices.insert(
            0,
            crate::par2_set::RecoverySlice {
                exponent: 0,
                data: crate::packet::recovery::RecoverySliceData::InMemory(
                    bytes::Bytes::from_static(b"bad"),
                ),
            },
        );

        let state = RepairState::from_set(dir.path(), set).unwrap();

        assert_eq!(state.discarded_recovery_blocks, 1);
        assert_eq!(state.set.recovery_block_count(), 0);
    }

    #[test]
    fn recoverable_file_without_ifsc_can_be_copy_only_adopted() {
        let dir = tempdir().unwrap();
        let data = b"aaaabbbb".to_vec();
        let mut set = synthetic_set(&[("target.bin", &data)], 4);
        set.slice_checksums.clear();
        fs::write(dir.path().join("renamed.bin"), &data).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 0);
        assert!(matches!(
            verification.files.first().map(|file| &file.status),
            Some(FileStatus::Renamed(_))
        ));
        assert!(matches!(
            verification.repairable,
            Repairability::Repairable {
                blocks_needed: 0,
                ..
            }
        ));

        let repair = state.repair(&options, &verification).unwrap();
        state.install_repaired_files(&repair, &options).unwrap();

        assert_eq!(fs::read(dir.path().join("target.bin")).unwrap(), data);
    }

    #[test]
    fn install_repaired_files_does_not_replace_a_directory() {
        let dir = tempdir().unwrap();
        let data = b"target--target--".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        let extra = dir.path().join("target.extra");
        let target = dir.path().join("target.bin");
        fs::write(&extra, &data).unwrap();
        fs::create_dir(&target).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        options.extra_paths = vec![extra.clone()];
        state.scan(&options).unwrap();
        let verification = state.verification_result();
        let repair = state.repair(&options, &verification).unwrap();
        let error = state
            .install_repaired_files(&repair, &options)
            .expect_err("a repair must not replace an existing directory");

        assert!(matches!(error, Par2Error::Io(_)));
        assert!(target.is_dir());
        assert_eq!(fs::read(extra).unwrap(), data);
    }

    #[cfg(unix)]
    #[test]
    fn install_repaired_files_rolls_back_previous_targets_on_later_error() {
        let dir = tempdir().unwrap();
        let first = b"first---first---".to_vec();
        let second = b"second--second--".to_vec();
        let first_damaged = b"damaged-first---".to_vec();
        let set = synthetic_set(&[("first.bin", &first), ("second.bin", &second)], 8);
        let first_extra = dir.path().join("first.extra");
        let second_extra = dir.path().join("second.extra");
        let dangling_target = dir.path().join("missing-link-target.bin");

        fs::write(dir.path().join("first.bin"), &first_damaged).unwrap();
        fs::write(&first_extra, &first).unwrap();
        fs::write(&second_extra, &second).unwrap();
        std::os::unix::fs::symlink(&dangling_target, dir.path().join("second.bin")).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let mut options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        options.extra_paths = vec![first_extra, second_extra];
        state.scan(&options).unwrap();
        let verification = state.verification_result();

        assert_eq!(verification.total_missing_blocks, 0);
        let repair = state.repair(&options, &verification).unwrap();
        let error = state
            .install_repaired_files(&repair, &options)
            .expect_err("dangling second symlink target should fail install");

        assert!(matches!(error, Par2Error::Io(_)));
        assert_eq!(
            fs::read(dir.path().join("first.bin")).unwrap(),
            first_damaged
        );
        assert!(
            fs::symlink_metadata(dir.path().join("second.bin"))
                .unwrap()
                .file_type()
                .is_symlink()
        );
        assert!(
            fs::read_dir(dir.path())
                .unwrap()
                .filter_map(|entry| entry.ok())
                .all(|entry| !entry
                    .file_name()
                    .to_string_lossy()
                    .contains(".weaver-par2-backup."))
        );
    }

    #[test]
    fn short_block_scan_matches_canonical_offset_with_trailing_garbage() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        fs::write(dir.path().join("target.bin"), b"ABCDEFGH12345JUNK").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();

        let file = state
            .files
            .iter()
            .find(|file| file.safe_name == "target.bin")
            .unwrap();
        let last_block = &state.blocks[file.first_block + file.block_count - 1];
        let location = last_block.location.as_ref().unwrap();
        assert_eq!(
            location.path(),
            Some(dir.path().join("target.bin").as_path())
        );
        assert_eq!(location.offset, 8);
    }

    #[test]
    fn short_block_scan_matches_shifted_extra_file_data() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        fs::write(dir.path().join("interior.bin"), b"xxxx12345yyyy").unwrap();
        fs::write(dir.path().join("tail.bin"), b"zzzz12345").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        state.scan(&options).unwrap();

        let file = state
            .files
            .iter()
            .find(|file| file.safe_name == "target.bin")
            .unwrap();
        let last_block = &state.blocks[file.first_block + file.block_count - 1];
        let location = last_block.location.as_ref().unwrap();
        assert_eq!(
            location.path(),
            Some(dir.path().join("interior.bin").as_path())
        );
        assert_eq!(location.offset, 4);
    }

    /// Distinct pseudo-random bytes per seed, so no two synthetic files ever
    /// share block content by accident.
    fn relocation_filler(seed: u64, len: usize) -> Vec<u8> {
        let mut value = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1;
        (0..len)
            .map(|_| {
                value ^= value << 13;
                value ^= value >> 7;
                value ^= value << 17;
                (value & 0xFF) as u8
            })
            .collect()
    }

    /// A set of `count` files, each `full_slices` whole slices plus a terminal
    /// short slice of `short_len` bytes, written to disk under their canonical
    /// names with one whole slice corrupted so no whole-file hash match can
    /// short-circuit the block scan.
    fn damaged_canonical_short_block_set(
        dir: &Path,
        slice_size: u64,
        full_slices: usize,
        tails: &[usize],
    ) -> Par2FileSet {
        let sources = tails
            .iter()
            .enumerate()
            .map(|(index, tail)| {
                (
                    format!("part{index}.bin"),
                    relocation_filler(index as u64 + 1, full_slices * slice_size as usize + tail),
                )
            })
            .collect::<Vec<_>>();
        let described = sources
            .iter()
            .map(|(name, data)| (name.as_str(), data.as_slice()))
            .collect::<Vec<_>>();
        let set = synthetic_set(&described, slice_size);
        for (name, data) in &sources {
            let mut damaged = data.clone();
            damaged[slice_size as usize..2 * slice_size as usize].fill(0xEE);
            fs::write(dir.join(name), damaged).unwrap();
        }
        set
    }

    fn short_block_of<'a>(state: &'a RepairState, safe_name: &str) -> &'a SourceBlock {
        let file = state
            .files
            .iter()
            .find(|file| file.safe_name == safe_name)
            .expect("described file");
        &state.blocks[file.first_block + file.block_count - 1]
    }

    fn distinct_short_lengths(state: &RepairState) -> Vec<u64> {
        let mut lengths = state
            .hash_table
            .short_blocks
            .iter()
            .map(|index| state.blocks[*index].expected_len)
            .collect::<Vec<_>>();
        lengths.sort_unstable();
        lengths.dedup();
        lengths
    }

    /// The common case, and the shape that used to be quadratic: every file
    /// carries a terminal short block at its own slice offset, so the targeted
    /// checks place all of them and the exhaustive relocation search is never
    /// entered — even though every candidate is damaged elsewhere and so still
    /// holds unexplained bytes.
    #[test]
    fn canonical_terminal_short_blocks_never_enter_the_relocation_search() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let set = damaged_canonical_short_block_set(dir.path(), slice_size, 3, &[21, 21, 21, 21]);

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();

        assert_eq!(distinct_short_lengths(&state), vec![21]);
        for index in 0..4 {
            let block = short_block_of(&state, &format!("part{index}.bin"));
            let location = block
                .location
                .as_ref()
                .expect("terminal short block placed");
            assert_eq!(location.offset, 3 * slice_size);
            assert_eq!(
                location.path(),
                Some(dir.path().join(format!("part{index}.bin")).as_path())
            );
        }
        assert_eq!(diagnostics.short_relocation_candidates_scanned, 0);
        assert_eq!(diagnostics.short_relocation_windows_stepped, 0);
        assert_eq!(diagnostics.short_relocation_bytes_read, 0);
    }

    /// The measured production shape: many files sharing one short length plus
    /// a final file with a different one. Two distinct lengths used to mean two
    /// whole-file sweeps *per candidate*; now they mean none.
    #[test]
    fn two_distinct_short_lengths_never_enter_the_relocation_search() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let set = damaged_canonical_short_block_set(dir.path(), slice_size, 3, &[21, 21, 21, 37]);

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();

        assert_eq!(distinct_short_lengths(&state), vec![21, 37]);
        for index in 0..4 {
            let block = short_block_of(&state, &format!("part{index}.bin"));
            let location = block
                .location
                .as_ref()
                .expect("terminal short block placed");
            assert_eq!(location.offset, 3 * slice_size);
        }
        assert_eq!(diagnostics.short_relocation_candidates_scanned, 0);
        assert_eq!(diagnostics.short_relocation_windows_stepped, 0);
    }

    /// A fully obfuscated download: no canonical name exists, so every file is
    /// an extra candidate and every short block is unplaced when the candidate
    /// scan starts. The tail check still places each one at its own slice
    /// offset inside its renamed container, so the merged state closes them all
    /// and no candidate is swept. This is the second-pass blow-up case.
    #[test]
    fn an_all_obfuscated_set_never_enters_the_relocation_search() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let sources = (0..4u64)
            .map(|index| {
                (
                    format!("part{index}.bin"),
                    relocation_filler(index + 1, 3 * slice_size as usize + 21),
                )
            })
            .collect::<Vec<_>>();
        let described = sources
            .iter()
            .map(|(name, data)| (name.as_str(), data.as_slice()))
            .collect::<Vec<_>>();
        let set = synthetic_set(&described, slice_size);
        for (index, (_, data)) in sources.iter().enumerate() {
            let mut damaged = data.clone();
            damaged[slice_size as usize..2 * slice_size as usize].fill(0xEE);
            fs::write(dir.path().join(format!("{index:02}.obfuscated")), damaged).unwrap();
        }

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();

        for index in 0..4 {
            let block = short_block_of(&state, &format!("part{index}.bin"));
            let location = block
                .location
                .as_ref()
                .expect("terminal short block placed");
            assert_eq!(location.offset, 3 * slice_size);
            assert_eq!(
                location.path(),
                Some(dir.path().join(format!("{index:02}.obfuscated")).as_path())
            );
            assert_eq!(location.kind, BlockLocationKind::Extra);
        }
        assert_eq!(diagnostics.short_relocation_candidates_scanned, 0);
        assert_eq!(diagnostics.short_relocation_windows_stepped, 0);
    }

    /// The structural guard behind every counter assertion above: none of the
    /// per-candidate scan entry points runs the exhaustive relocation search
    /// itself. Each one scans a private pre-merge snapshot, so a search there
    /// cannot see what other candidates already placed — which is exactly how
    /// it became quadratic. A displaced short block must therefore survive the
    /// scan phase unplaced, and be found only when the deferred pass asks.
    #[test]
    fn the_candidate_scan_phase_defers_the_relocation_search() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, b"ABCDEFGHxx12345JUNK").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);
        let baseline = state.blocks.clone();
        let lookup = SourceFileScanLookup {
            files: &state.files,
            file_index_by_id: &state.file_index_by_id,
        };
        let target_file = state
            .files
            .iter()
            .find(|file| file.safe_path == candidate)
            .expect("described file");

        let mut generic = ScanBlockState::new(&baseline);
        scanner
            .scan_file_with_state_options(
                &candidate,
                BlockLocationKind::Canonical,
                &state.files,
                &state.file_index_by_id,
                &mut generic,
                ScanSkipOptions::disabled(),
            )
            .unwrap();
        let mut ordered = ScanBlockState::new(&baseline);
        scanner
            .scan_file_ordered_canonical_state(
                &candidate,
                BlockLocationKind::Canonical,
                lookup,
                target_file,
                &mut ordered,
                ScanSkipOptions::disabled(),
                true,
                DEFAULT_REPAIR_MEMORY_LIMIT,
                None,
                &[],
            )
            .unwrap();
        let mut mapped = ScanBlockState::new(&baseline);
        scanner
            .scan_file_mmap_with_state_options(
                &candidate,
                BlockLocationKind::Canonical,
                &state.files,
                &state.file_index_by_id,
                &mut mapped,
                ScanSkipOptions::disabled(),
            )
            .unwrap();

        for scanned in [&generic, &ordered, &mapped] {
            assert!(scanned.location(0).is_some(), "aligned block still placed");
            assert!(
                scanned.location(1).is_none(),
                "scan phase must not relocate short blocks"
            );
        }

        let stats = scanner
            .relocate_open_short_blocks_in(&candidate, BlockLocationKind::Canonical, &mut generic)
            .unwrap();

        assert_eq!(stats.blocks_placed, 1);
        assert!(stats.windows_stepped > 0);
        assert!(stats.bytes_read > 0);
        assert_eq!(
            generic.location(1).map(|location| location.offset),
            Some(10)
        );
    }

    /// Scanning only ever produces path locations, so an access-backed hold —
    /// evidence no scan could have made — must survive a scan match rather
    /// than lose to one. The relocation sweep is the one recording site that
    /// could break that: "open" means only that a hold is not at the block's
    /// own slice offset, and an access-backed hold at any other offset is
    /// open. The sweep must decline it even with matching bytes in hand.
    #[test]
    fn relocation_never_displaces_an_access_backed_incumbent() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        let candidate = dir.path().join("target.bin");
        fs::write(&candidate, b"ABCDEFGHxx12345JUNK").unwrap();
        let state = RepairState::from_set(dir.path(), set).unwrap();
        let scanner = RollingBlockScanner::new(&state.hash_table, state.set.slice_size);

        let mut baseline = state.blocks.clone();
        let held = BlockLocation {
            source: SourceLocation::Access(baseline[1].file_id),
            offset: 3,
            len: baseline[1].expected_len,
            kind: BlockLocationKind::Canonical,
        };
        assert_ne!(
            held.offset,
            u64::from(baseline[1].local_index) * state.set.slice_size,
            "the hold must not be at the block's own slice offset"
        );
        baseline[1].location = Some(held.clone());

        let mut blocks = ScanBlockState::new(&baseline);
        assert!(
            open_short_blocks(&state.hash_table, &blocks, state.set.slice_size)[1],
            "a hold away from the block's slice offset leaves it open"
        );

        let stats = scanner
            .relocate_open_short_blocks_in(&candidate, BlockLocationKind::Canonical, &mut blocks)
            .unwrap();

        assert!(
            stats.windows_stepped > 0,
            "the candidate carrying the matching bytes really was swept"
        );
        assert_eq!(stats.blocks_placed, 0);
        assert_eq!(blocks.location(1), Some(&held));
    }

    /// The regression a plain "canonical candidates skip the search" gate would
    /// cause: the short block really is displaced inside its own canonically
    /// named file, so neither the owner-offset check nor the tail check reaches
    /// it and only the deferred search can.
    #[test]
    fn a_shifted_short_block_inside_a_canonical_file_is_still_found() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        fs::write(dir.path().join("target.bin"), b"ABCDEFGHxx12345JUNK").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();

        let location = short_block_of(&state, "target.bin")
            .location
            .as_ref()
            .expect("displaced short block placed");
        assert_eq!(
            location.path(),
            Some(dir.path().join("target.bin").as_path())
        );
        assert_eq!(location.offset, 10);
        assert_eq!(diagnostics.short_relocation_candidates_scanned, 1);
        assert_eq!(diagnostics.short_relocation_blocks_placed, 1);
        assert!(diagnostics.short_relocation_windows_stepped > 0);
        assert!(diagnostics.short_relocation_bytes_read > 0);
    }

    /// The same displacement in an obfuscated extra file: the owning file is
    /// gone and the copy carries a trailing suffix, so the short block sits at
    /// neither the owner offset nor the candidate tail.
    #[test]
    fn a_shifted_short_block_inside_an_extra_file_is_still_found() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        fs::write(dir.path().join("obfuscated.dat"), b"xxABCDEFGH12345TRAILER").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();

        let location = short_block_of(&state, "target.bin")
            .location
            .as_ref()
            .expect("displaced short block placed");
        assert_eq!(
            location.path(),
            Some(dir.path().join("obfuscated.dat").as_path())
        );
        assert_eq!(location.offset, 10);
        assert_eq!(location.kind, BlockLocationKind::Extra);
        assert_eq!(diagnostics.short_relocation_candidates_scanned, 1);
        assert_eq!(diagnostics.short_relocation_blocks_placed, 1);
    }

    /// Two equally valid copies: the search runs in candidate order and stops
    /// as soon as the block is settled, so the first candidate wins and the
    /// second is never swept.
    #[test]
    fn relocation_settles_on_the_first_candidate_that_carries_the_block() {
        let dir = tempdir().unwrap();
        let data = b"ABCDEFGH12345".to_vec();
        let set = synthetic_set(&[("target.bin", &data)], 8);
        fs::write(dir.path().join("aaa.dat"), b"ABCDEFGH12345TRAILER").unwrap();
        fs::write(dir.path().join("zzz.dat"), b"ABCDEFGH12345TRAILER").unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();

        let location = short_block_of(&state, "target.bin")
            .location
            .as_ref()
            .expect("displaced short block placed");
        assert_eq!(location.path(), Some(dir.path().join("aaa.dat").as_path()));
        assert_eq!(location.offset, 8);
        assert_eq!(diagnostics.short_relocation_candidates_scanned, 1);
    }

    /// A short block whose owner is missing outright stays unplaced, and the
    /// one candidate the pass could have swept is skipped because the merged
    /// state already accounts for every byte of it. This is the guard: an
    /// unresolvable short block must not turn intact candidates into work.
    #[test]
    fn an_explained_candidate_is_never_swept_for_a_missing_short_block() {
        let dir = tempdir().unwrap();
        // Large enough that the canonical whole-file hash check is skipped, so
        // the intact file really does reach the block scan and become a
        // relocation target rather than an early complete-file match.
        let slice_size = 256 * 1024u64;
        let present = relocation_filler(1, 4 * slice_size as usize + 1000);
        let absent = relocation_filler(2, slice_size as usize + 77);
        let set = synthetic_set(
            &[("present.bin", &present), ("absent.bin", &absent)],
            slice_size,
        );
        fs::write(dir.path().join("present.bin"), &present).unwrap();

        let mut state = RepairState::from_set(dir.path(), set).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();

        assert_eq!(
            short_block_of(&state, "present.bin")
                .location
                .as_ref()
                .map(|location| location.offset),
            Some(4 * slice_size)
        );
        assert!(short_block_of(&state, "absent.bin").location.is_none());
        assert_eq!(diagnostics.short_relocation_candidates_scanned, 0);
        assert_eq!(diagnostics.short_relocation_candidates_skipped, 1);
        assert_eq!(diagnostics.short_relocation_windows_stepped, 0);
    }

    #[test]
    fn inventory_discards_conflicting_recovery_only_packets() {
        let dir = tempdir().unwrap();
        let main_body = {
            let mut body = Vec::new();
            body.extend_from_slice(&4u64.to_le_bytes());
            body.extend_from_slice(&0u32.to_le_bytes());
            body
        };
        let active_set_id = checksum::md5(&main_body);
        fs::write(
            dir.path().join("active.par2"),
            make_full_packet(crate::packet::header::TYPE_MAIN, &main_body, active_set_id),
        )
        .unwrap();

        let mut recovery_body = Vec::new();
        recovery_body.extend_from_slice(&0u32.to_le_bytes());
        recovery_body.extend_from_slice(&[0xAA; 4]);
        let conflicting_recovery = dir.path().join("other.vol00+01.par2");
        fs::write(
            &conflicting_recovery,
            make_full_packet(
                crate::packet::header::TYPE_RECOVERY,
                &recovery_body,
                [9; 16],
            ),
        )
        .unwrap();

        let mut options = Par2RepairerOptions::new(
            dir.path().to_path_buf(),
            vec![dir.path().join("active.par2")],
        );
        options.recovery_paths.push(conflicting_recovery);
        let repairer = Par2Repairer::new(options);
        let inventory = repairer.load_inventory().unwrap();

        assert_eq!(inventory.set.recovery_block_count(), 0);
        assert_eq!(inventory.diagnostics.conflicting_packets, 1);
    }

    #[test]
    fn inventory_counts_duplicate_packets_without_changing_first_wins() {
        let dir = tempdir().unwrap();
        let main_body = {
            let mut body = Vec::new();
            body.extend_from_slice(&4u64.to_le_bytes());
            body.extend_from_slice(&0u32.to_le_bytes());
            body
        };
        let active_set_id = checksum::md5(&main_body);
        let main_packet =
            make_full_packet(crate::packet::header::TYPE_MAIN, &main_body, active_set_id);
        let mut par2_file = Vec::new();
        par2_file.extend_from_slice(&main_packet);
        par2_file.extend_from_slice(&main_packet);
        fs::write(dir.path().join("active.par2"), par2_file).unwrap();

        let repairer = Par2Repairer::new(Par2RepairerOptions::new(
            dir.path().to_path_buf(),
            vec![dir.path().join("active.par2")],
        ));
        let inventory = repairer.load_inventory().unwrap();

        assert_eq!(inventory.diagnostics.packets_loaded, 2);
        assert_eq!(inventory.diagnostics.duplicate_packets, 1);
        assert_eq!(inventory.set.recovery_file_ids.len(), 0);
    }

    /// One Main packet with `slice_size`, no files.
    fn empty_main_packet(slice_size: u64) -> (Vec<u8>, [u8; 16]) {
        let mut body = Vec::new();
        body.extend_from_slice(&slice_size.to_le_bytes());
        body.extend_from_slice(&0u32.to_le_bytes());
        let rsid = checksum::md5(&body);
        (
            make_full_packet(crate::packet::header::TYPE_MAIN, &body, rsid),
            rsid,
        )
    }

    fn recovery_packet(exponent: u32, payload: &[u8], rsid: [u8; 16]) -> Vec<u8> {
        let mut body = Vec::with_capacity(4 + payload.len());
        body.extend_from_slice(&exponent.to_le_bytes());
        body.extend_from_slice(payload);
        make_full_packet(crate::packet::header::TYPE_RECOVERY, &body, rsid)
    }

    /// A `.par2` file holding one Main packet followed by `count` minimal
    /// recovery packets with consecutive exponents.
    fn write_recovery_run(path: &Path, exponents: std::ops::Range<u32>) -> [u8; 16] {
        let (main, rsid) = empty_main_packet(4);
        let mut stream = main;
        stream.reserve(exponents.len() * 72);
        for exponent in exponents {
            stream.extend_from_slice(&recovery_packet(exponent, &[0xAB; 4], rsid));
        }
        fs::write(path, &stream).unwrap();
        rsid
    }

    fn repairer_for(dir: &Path, par2: Vec<PathBuf>) -> Par2RepairerOptions {
        Par2RepairerOptions::new(dir.to_path_buf(), par2)
    }

    /// The reported amplification: a ~4.5 MiB file of 65,537 minimal recovery
    /// packets. It must load under the default budget, keep exactly the usable
    /// exponents, and refuse the one packet whose exponent is outside the GF
    /// domain — without ever materialising the packet stream.
    #[test]
    fn inventory_loads_a_sixty_five_thousand_packet_recovery_file() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("amplify.par2");
        // 0..=65_535 are usable; 65_536 is one past the domain, which is what
        // makes this 65,537 packets rather than 65,536.
        write_recovery_run(&path, 0..65_537);
        assert!(fs::metadata(&path).unwrap().len() > 4 * 1024 * 1024);

        let inventory = Par2Repairer::new(repairer_for(dir.path(), vec![path]))
            .load_inventory()
            .unwrap();

        assert_eq!(
            inventory.set.recovery_block_count(),
            crate::packet::RECOVERY_EXPONENT_DOMAIN as u32
        );
        assert!(
            inventory
                .set
                .recovery_slices
                .contains_key(&crate::packet::MAX_RECOVERY_EXPONENT)
        );
        assert!(
            !inventory
                .set
                .recovery_slices
                .contains_key(&(crate::packet::MAX_RECOVERY_EXPONENT + 1))
        );
        // Main plus every packet the file held, out-of-domain one included.
        assert_eq!(inventory.diagnostics.packets_loaded, 65_538);
        assert_eq!(inventory.diagnostics.duplicate_packets, 0);
        assert_eq!(inventory.diagnostics.conflicting_packets, 0);
    }

    /// The same file, one packet over a configured retained-packet limit.
    #[test]
    fn inventory_refuses_one_packet_past_the_configured_retained_limit() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("run.par2");
        write_recovery_run(&path, 0..64);

        // Main plus 64 recovery blocks is 65 retained packets.
        let mut options = repairer_for(dir.path(), vec![path.clone()]);
        options.packet_scan_limits = PacketScanLimits::default().with_max_retained_packets(65);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();
        assert_eq!(inventory.set.recovery_block_count(), 64);

        let mut options = repairer_for(dir.path(), vec![path]);
        options.packet_scan_limits = PacketScanLimits::default().with_max_retained_packets(64);
        let error = Par2Repairer::new(options).load_inventory().unwrap_err();
        assert!(matches!(error, Par2Error::ResourceLimitExceeded { .. }));
    }

    /// Two `.par2` inputs that each fit on their own but do not fit together.
    /// One budget spans the load, so the second file is what trips it.
    #[test]
    fn inventory_budget_is_shared_across_every_par2_input() {
        let dir = tempdir().unwrap();
        let first = dir.path().join("first.par2");
        let second = dir.path().join("second.par2");
        write_recovery_run(&first, 0..32);
        write_recovery_run(&second, 32..64);

        let limits = PacketScanLimits::default().with_max_retained_packets(40);
        let mut options = repairer_for(dir.path(), vec![first.clone()]);
        options.packet_scan_limits = limits;
        assert_eq!(
            Par2Repairer::new(options)
                .load_inventory()
                .unwrap()
                .set
                .recovery_block_count(),
            32
        );

        let mut options = repairer_for(dir.path(), vec![first.clone(), second.clone()]);
        options.packet_scan_limits = limits;
        let error = Par2Repairer::new(options).load_inventory().unwrap_err();
        assert!(matches!(error, Par2Error::ResourceLimitExceeded { .. }));

        // No partial inventory is reachable: the load either returns a complete
        // Par2FileSet or it returns an error carrying none.
        let mut options = repairer_for(dir.path(), vec![first, second]);
        options.packet_scan_limits = PacketScanLimits::default().with_max_retained_packets(65);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();
        assert_eq!(inventory.set.recovery_block_count(), 64);
    }

    /// Duplicates are scan work, not retention: a set replicated many times
    /// over must still fit a budget sized for its logical inventory.
    #[test]
    fn inventory_duplicates_spend_work_budget_but_not_retention_budget() {
        let dir = tempdir().unwrap();
        let (main, rsid) = empty_main_packet(4);
        let recovery = recovery_packet(0, &[0xAB; 4], rsid);
        let mut stream = main.clone();
        for _ in 0..500 {
            stream.extend_from_slice(&main);
            stream.extend_from_slice(&recovery);
        }
        let path = dir.path().join("redundant.par2");
        fs::write(&path, &stream).unwrap();

        let mut options = repairer_for(dir.path(), vec![path]);
        // Exactly the logical inventory: one Main and one recovery block.
        options.packet_scan_limits = PacketScanLimits::default().with_max_retained_packets(2);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();

        assert_eq!(inventory.set.recovery_block_count(), 1);
        assert_eq!(inventory.diagnostics.packets_loaded, 1_001);
        assert_eq!(inventory.diagnostics.duplicate_packets, 999);
    }

    /// The examined meter counts packets the inventory never keeps, so a stream
    /// that is nothing but redundancy still has a ceiling.
    #[test]
    fn inventory_examined_meter_bounds_pure_redundancy() {
        let dir = tempdir().unwrap();
        let (main, rsid) = empty_main_packet(4);
        let recovery = recovery_packet(0, &[0xAB; 4], rsid);
        let mut stream = main;
        for _ in 0..64 {
            stream.extend_from_slice(&recovery);
        }
        let path = dir.path().join("redundant.par2");
        fs::write(&path, &stream).unwrap();

        let mut options = repairer_for(dir.path(), vec![path]);
        options.packet_scan_limits = PacketScanLimits::default().with_max_examined_packets(16);
        let error = Par2Repairer::new(options).load_inventory().unwrap_err();
        assert!(matches!(error, Par2Error::ResourceLimitExceeded { .. }));
    }

    /// An input with no Main packet anywhere leaves every packet staged. The
    /// stage still has to be flushed so the load reports the real reason.
    #[test]
    fn inventory_without_a_main_packet_reports_the_missing_main() {
        let dir = tempdir().unwrap();
        let mut stream = Vec::new();
        for exponent in 0..8u32 {
            stream.extend_from_slice(&recovery_packet(exponent, &[0xAB; 4], [0x5C; 16]));
        }
        let path = dir.path().join("mainless.par2");
        fs::write(&path, &stream).unwrap();

        let error = Par2Repairer::new(repairer_for(dir.path(), vec![path]))
            .load_inventory()
            .unwrap_err();
        assert!(matches!(error, Par2Error::NoMainPacket));
    }

    /// Packets that precede the first Main packet cannot be filtered yet, so
    /// they are staged. Staging is budgeted like anything else.
    #[test]
    fn packets_staged_before_the_first_main_are_charged_to_the_budget() {
        let dir = tempdir().unwrap();
        let (main, rsid) = empty_main_packet(4);
        // Recovery packets first, then the Main packet: the layout par2cmdline
        // actually writes for a volume file.
        let mut stream = Vec::new();
        for exponent in 0..32u32 {
            stream.extend_from_slice(&recovery_packet(exponent, &[0xAB; 4], rsid));
        }
        stream.extend_from_slice(&main);
        let path = dir.path().join("recovery-first.par2");
        fs::write(&path, &stream).unwrap();

        let mut options = repairer_for(dir.path(), vec![path.clone()]);
        options.packet_scan_limits = PacketScanLimits::default().with_max_retained_packets(33);
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();
        assert_eq!(inventory.set.recovery_block_count(), 32);
        assert_eq!(inventory.diagnostics.packets_loaded, 33);

        let mut options = repairer_for(dir.path(), vec![path]);
        options.packet_scan_limits = PacketScanLimits::default().with_max_retained_packets(16);
        let error = Par2Repairer::new(options).load_inventory().unwrap_err();
        assert!(matches!(error, Par2Error::ResourceLimitExceeded { .. }));
    }

    /// Every packet of a conflicting volume is counted and discarded, and the
    /// volume contributes nothing, so it is not offered for purge.
    #[test]
    fn inventory_discards_a_whole_conflicting_volume_and_leaves_it_unpurged() {
        let dir = tempdir().unwrap();
        let active = dir.path().join("active.par2");
        write_recovery_run(&active, 0..4);

        let mut foreign_body = Vec::new();
        foreign_body.extend_from_slice(&8u64.to_le_bytes());
        foreign_body.extend_from_slice(&0u32.to_le_bytes());
        let foreign_rsid = checksum::md5(&foreign_body);
        let mut foreign = make_full_packet(
            crate::packet::header::TYPE_MAIN,
            &foreign_body,
            foreign_rsid,
        );
        for exponent in 0..4u32 {
            foreign.extend_from_slice(&recovery_packet(exponent, &[0xCD; 8], foreign_rsid));
        }
        let foreign_path = dir.path().join("foreign.par2");
        fs::write(&foreign_path, &foreign).unwrap();

        let mut options = repairer_for(dir.path(), vec![active.clone()]);
        options.recovery_paths.push(foreign_path.clone());
        let inventory = Par2Repairer::new(options).load_inventory().unwrap();

        assert_eq!(inventory.set.recovery_block_count(), 4);
        assert_eq!(inventory.diagnostics.conflicting_packets, 5);
        assert_eq!(inventory.diagnostics.packets_loaded, 5);
        // The conflicting volume is a `.par2` path, so it stays purgeable by
        // name exactly as before, while contributing nothing.
        assert_eq!(inventory.purge_paths, vec![active, foreign_path]);
    }

    #[test]
    fn inventory_scanning_stops_when_cancelled() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("run.par2");
        write_recovery_run(&path, 0..256);

        let cancel = CancellationToken::new();
        cancel.cancel();
        let mut options = repairer_for(dir.path(), vec![path]);
        options.cancel = Some(cancel);
        let error = Par2Repairer::new(options).load_inventory().unwrap_err();
        assert!(matches!(error, Par2Error::Cancelled));
    }

    /// Cancellation asserted after the last packet is read still has to be
    /// observed, before the set is assembled and handed back.
    #[test]
    fn inventory_construction_observes_cancellation_after_the_last_packet() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("run.par2");
        write_recovery_run(&path, 0..8);

        let cancel = CancellationToken::new();
        let budget =
            PacketScanBudget::with_cancellation(PacketScanLimits::default(), Some(cancel.clone()));
        let mut loader = InventoryLoader::new(&budget);
        loader.begin_file(path.clone(), true);
        scan_packets_from_path_bounded(&path, &budget, &mut loader).unwrap();
        loader.end_file(false);

        cancel.cancel();
        assert!(matches!(loader.finish(), Err(Par2Error::Cancelled)));
    }

    /// The inventory keeps recovery payloads file-backed, and the deferred
    /// packet-hash check still works against the interned volume path.
    #[test]
    fn inventory_recovery_payloads_stay_file_backed_and_still_validate() {
        let dir = tempdir().unwrap();
        let (main, rsid) = empty_main_packet(8);
        let mut stream = main;
        for exponent in 0..4u32 {
            stream.extend_from_slice(&recovery_packet(exponent, &[0xC3; 8], rsid));
        }
        let path = dir.path().join("volume.par2");
        fs::write(&path, &stream).unwrap();

        let inventory = Par2Repairer::new(repairer_for(dir.path(), vec![path]))
            .load_inventory()
            .unwrap();

        assert_eq!(inventory.set.recovery_block_count(), 4);
        for (exponent, slice) in &inventory.set.recovery_slices {
            assert!(slice.data.as_bytes().is_none(), "payload stays on disk");
            assert_eq!(slice.data.to_vec().unwrap(), vec![0xC3; 8]);
            assert!(slice.data.validate_packet_hash(&rsid, *exponent).unwrap());
        }
    }

    #[cfg(feature = "slow-tests")]
    #[test]
    fn crate_fixture_missing_volume_repairs_and_reverifies_clean() {
        let temp = copy_fixture_dir("rar5_lz_plain");
        fs::remove_file(temp.path().join("fixture_rar5_lz_plain.part4.rar")).unwrap();

        let par2_paths = collect_paths(temp.path(), "fixture_rar5_lz_plain_repair", "par2");
        let mut preview = Par2RepairerOptions::new(temp.path().to_path_buf(), par2_paths.clone());
        preview.repair = false;
        let preview_outcome = Par2Repairer::new(preview).verify_or_repair().unwrap();
        assert_eq!(preview_outcome.status, Par2RepairStatus::RepairPossible);
        assert!(preview_outcome.verification.total_missing_blocks > 0);

        let outcome = Par2Repairer::new(Par2RepairerOptions::new(
            temp.path().to_path_buf(),
            par2_paths.clone(),
        ))
        .verify_or_repair()
        .unwrap();

        assert_eq!(outcome.status, Par2RepairStatus::Repaired);
        assert_eq!(outcome.verification.total_missing_blocks, 0);

        let mut reverify = Par2RepairerOptions::new(temp.path().to_path_buf(), par2_paths);
        reverify.repair = false;
        let clean = Par2Repairer::new(reverify).verify_or_repair().unwrap();
        assert_eq!(clean.status, Par2RepairStatus::Verified, "{clean:#?}");
        assert_eq!(clean.verification.total_missing_blocks, 0, "{clean:#?}");
    }

    #[test]
    fn scan_carry_applies_when_disk_unchanged_and_refuses_on_drift() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let file_data: Vec<u8> = (0..1024u32).map(|i| (i % 251) as u8).collect();
        let set = synthetic_set(&[("data.bin", &file_data)], slice_size);
        fs::write(dir.path().join("data.bin"), &file_data).unwrap();

        let mut state = RepairState::from_set(dir.path(), set.clone()).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();
        let carry = state.scan_carry(&diagnostics);
        let baseline = format!("{:?}", state.verification_result());

        let mut fresh = RepairState::from_set(dir.path(), set.clone()).unwrap();
        let applied = fresh.try_apply_carry(&carry);
        assert!(applied.is_some(), "carry must apply to an unchanged tree");
        assert_eq!(
            format!("{:?}", fresh.verification_result()),
            baseline,
            "carried state must reproduce the scanned verification"
        );

        // Rewriting a file with identical content still changes its mtime;
        // any observed drift must refuse the carry.
        let reference = fs::read(dir.path().join("data.bin")).unwrap();
        std::thread::sleep(Duration::from_millis(20));
        fs::write(dir.path().join("data.bin"), &reference).unwrap();
        let mut drifted = RepairState::from_set(dir.path(), set).unwrap();
        assert!(
            drifted.try_apply_carry(&carry).is_none(),
            "mtime drift must invalidate the carry"
        );
    }

    #[test]
    fn stale_scan_carry_falls_back_to_fresh_scan() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let file_data: Vec<u8> = (0..1024u32).map(|i| ((i * 7 + 3) % 251) as u8).collect();
        let set = synthetic_set(&[("data.bin", &file_data)], slice_size);
        let data_path = dir.path().join("data.bin");

        let mut damaged = file_data.clone();
        damaged[..64].fill(0);
        fs::write(&data_path, &damaged).unwrap();

        let mut analyze = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        analyze.file_set = Some(set.clone());
        analyze.repair = false;
        let (analyze_outcome, carry) = Par2Repairer::new(analyze)
            .verify_or_repair_carrying()
            .unwrap();
        assert!(analyze_outcome.verification.total_missing_blocks > 0);
        let carry = carry.expect("carrying pass returns scan state");

        // The damage is healed out-of-band with the same file length. Some
        // filesystems can leave the stat snapshot indistinguishable, so an
        // execute pass may apply the carry first. It must still fresh-retry
        // before returning stale terminal state.
        fs::write(&data_path, &file_data).unwrap();
        restore_carried_modified_time(&carry, &data_path);
        let mut execute = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        execute.file_set = Some(set);
        execute.repair = true;
        execute.scan_carry = Some(carry);
        let outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();
        assert_eq!(outcome.status, Par2RepairStatus::Verified, "{outcome:#?}");
        assert!(outcome.carry.carry_attempted);
        assert!(outcome.carry.carry_applied);
        assert!(outcome.carry.carry_retried_fresh);
        assert_eq!(
            outcome.carry.carry_retry_reason,
            Some(CarryRetryReason::RepairRequested)
        );
    }

    #[test]
    fn stale_verified_scan_carry_retries_before_reporting_success() {
        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let file_data: Vec<u8> = (0..1024u32).map(|i| ((i * 11 + 5) % 251) as u8).collect();
        let par2_path = write_synthetic_par2_file(
            dir.path(),
            "data.par2",
            &[("data.bin", &file_data)],
            slice_size,
        );
        let data_path = dir.path().join("data.bin");
        fs::write(&data_path, &file_data).unwrap();

        let mut analyze =
            Par2RepairerOptions::new(dir.path().to_path_buf(), vec![par2_path.clone()]);
        analyze.repair = false;
        let (analyze_outcome, carry) = Par2Repairer::new(analyze)
            .verify_or_repair_carrying()
            .unwrap();
        assert_eq!(analyze_outcome.status, Par2RepairStatus::Verified);
        let carry = carry.expect("carrying pass returns scan state");

        let mut damaged = file_data;
        damaged[..64].fill(0);
        fs::write(&data_path, &damaged).unwrap();
        restore_carried_modified_time(&carry, &data_path);

        let mut execute =
            Par2RepairerOptions::new(dir.path().to_path_buf(), vec![par2_path.clone()]);
        execute.repair = true;
        execute.purge = true;
        execute.scan_carry = Some(carry);
        let outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();
        assert_eq!(
            outcome.status,
            Par2RepairStatus::Insufficient,
            "{outcome:#?}"
        );
        assert_eq!(outcome.verification.total_missing_blocks, 1);
        assert!(outcome.carry.carry_attempted);
        assert!(outcome.carry.carry_applied);
        assert!(outcome.carry.carry_retried_fresh);
        assert_eq!(
            outcome.carry.carry_retry_reason,
            Some(CarryRetryReason::RepairRequested)
        );
        assert!(
            par2_path.exists(),
            "speculative carried Verified must not purge PAR2 files before fresh verification fails"
        );
    }

    /// A carried copy-only repair whose source was rewritten to the same
    /// length *and* had its mtime restored is exactly the change a stat
    /// fingerprint cannot see. The pre-mutation gate accepts it — correctly,
    /// on the evidence available to it — and the validated read that a
    /// consumed carry always takes then catches it on the bytes, before
    /// anything is installed. The retry reason therefore names the changed
    /// input rather than the bare repair request.
    #[test]
    fn carried_repair_request_fresh_scans_before_mutation() {
        let dir = tempdir().unwrap();
        let slice_size = 8u64;
        let file_data = b"alpha---beta----".to_vec();
        let set = synthetic_set(&[("target.bin", &file_data)], slice_size);
        let extra_path = dir.path().join("renamed.bin");
        fs::write(&extra_path, &file_data).unwrap();

        let mut analyze = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        analyze.file_set = Some(set.clone());
        analyze.repair = false;
        analyze.extra_paths = vec![extra_path.clone()];
        let (preview, carry) = Par2Repairer::new(analyze)
            .verify_or_repair_carrying()
            .unwrap();
        assert_eq!(preview.status, Par2RepairStatus::RepairPossible);
        assert_eq!(preview.verification.total_missing_blocks, 0);
        let carry = carry.expect("analyze pass carries renamed source state");

        let stale_source = b"wrong---blocks--".to_vec();
        assert_eq!(stale_source.len(), file_data.len());
        fs::write(&extra_path, stale_source).unwrap();
        restore_carried_modified_time(&carry, &canonical_extra_path(&extra_path));

        let mut execute = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        execute.file_set = Some(set);
        execute.extra_paths = vec![extra_path];
        execute.scan_carry = Some(carry);
        let outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();

        assert_eq!(
            outcome.status,
            Par2RepairStatus::Insufficient,
            "{outcome:#?}"
        );
        assert!(
            !dir.path().join("target.bin").exists(),
            "stale carried copy-only source must not be installed before a fresh scan"
        );
        assert!(outcome.carry.carry_attempted);
        assert!(outcome.carry.carry_applied);
        assert!(outcome.carry.carry_retried_fresh);
        assert!(!outcome.carry.carry_consumed_for_repair);
        assert_eq!(
            outcome.carry.carry_retry_reason,
            Some(CarryRetryReason::RepairInputChanged)
        );
    }

    /// A small real PAR2 set with real recovery volumes, so repairs against it
    /// are genuine Reed-Solomon reconstructions rather than whole-file copies.
    fn create_recoverable_set(dir: &Path, files: &[(&str, &[u8])]) {
        let sources: Vec<PathBuf> = files
            .iter()
            .map(|(name, bytes)| {
                let path = dir.join(name);
                fs::write(&path, bytes).unwrap();
                path
            })
            .collect();
        let mut options = crate::create::Par2CreatorOptions::with_output(
            dir.join("set"),
            Some(dir.to_path_buf()),
            sources,
        );
        options.block_sizing = crate::create::BlockSizing::Bytes(64);
        options.recovery_amount = crate::create::RecoveryAmount::Count(16);
        let creator = crate::create::Par2Creator::new(options);
        let plan = creator.plan().unwrap();
        creator.create(&plan).unwrap();
    }

    fn par2_paths_in(dir: &Path) -> Vec<PathBuf> {
        let mut paths: Vec<PathBuf> = fs::read_dir(dir)
            .unwrap()
            .filter_map(|entry| {
                let path = entry.unwrap().path();
                is_par2_path(&path).then_some(path)
            })
            .collect();
        paths.sort();
        paths
    }

    fn copy_flat_dir(from: &Path, to: &Path) {
        fs::create_dir_all(to).unwrap();
        for entry in fs::read_dir(from).unwrap() {
            let entry = entry.unwrap();
            if entry.file_type().unwrap().is_file() {
                fs::copy(entry.path(), to.join(entry.file_name())).unwrap();
            }
        }
    }

    fn damage_first_slice(path: &Path) {
        let mut bytes = fs::read(path).unwrap();
        bytes[..64].fill(0);
        fs::write(path, bytes).unwrap();
    }

    fn bump_modified_time(path: &Path) {
        let modified = fs::metadata(path).unwrap().modified().unwrap();
        let file = fs::OpenOptions::new().write(true).open(path).unwrap();
        file.set_times(std::fs::FileTimes::new().set_modified(modified + Duration::from_secs(1)))
            .unwrap();
    }

    /// One way a repair input can change, named for assertion messages.
    type InputMutation = (&'static str, fn(&Path));

    /// Every way a repair input can change that a `stat` call can see. Each
    /// entry mutates the file at the given path.
    fn stat_visible_input_mutations() -> Vec<InputMutation> {
        vec![
            ("mtime bump", bump_modified_time),
            ("same-length content change", |path| {
                let len = fs::metadata(path).unwrap().len() as usize;
                fs::write(path, vec![0xA5u8; len]).unwrap();
                // A write's automatic mtime update is not portable enough to
                // make this same-length rewrite stat-visible on its own.
                bump_modified_time(path);
            }),
            ("truncate", |path| {
                let len = fs::metadata(path).unwrap().len();
                fs::OpenOptions::new()
                    .write(true)
                    .open(path)
                    .unwrap()
                    .set_len(len / 2)
                    .unwrap();
            }),
            ("append", |path| {
                let mut file = fs::OpenOptions::new().append(true).open(path).unwrap();
                file.write_all(&[0x5Au8; 64]).unwrap();
            }),
            ("rename away", |path| {
                fs::rename(path, path.with_file_name("moved-aside.dat")).unwrap();
            }),
            ("delete", |path| {
                fs::remove_file(path).unwrap();
            }),
        ]
    }

    fn scanned_state_with_carry(dir: &Path, set: &Par2FileSet) -> (RepairState, ScanCarry) {
        let mut state = RepairState::from_set(dir, set.clone()).unwrap();
        let options = Par2RepairerOptions::new(dir.to_path_buf(), Vec::new());
        let diagnostics = state.scan(&options).unwrap();
        let carry = state.scan_carry(&diagnostics);
        (state, carry)
    }

    /// The carry exists so the execute pass costs nothing. On an unchanged
    /// tree it must repair on the analysis it was handed rather than reading
    /// the whole set a second time, and the bytes it writes must be exactly
    /// the bytes the two-scan path writes.
    ///
    /// `carry_applied` says this pass installed carried state — which is the
    /// arm that excludes running `scan` at all — and `!carry_retried_fresh`
    /// says no second pass ran either, so between them no scan happened here.
    #[test]
    fn carried_repair_consumes_the_analysis_without_a_second_scan() {
        let alpha: Vec<u8> = (0..512u32).map(|i| (i % 251) as u8).collect();
        let beta: Vec<u8> = (0..512u32).map(|i| ((i * 7 + 11) % 241) as u8).collect();

        let carried_dir = tempdir().unwrap();
        let fresh_dir = tempdir().unwrap();
        create_recoverable_set(
            carried_dir.path(),
            &[("alpha.bin", &alpha), ("beta.bin", &beta)],
        );
        copy_flat_dir(carried_dir.path(), fresh_dir.path());
        for dir in [carried_dir.path(), fresh_dir.path()] {
            damage_first_slice(&dir.join("alpha.bin"));
        }

        let par2 = par2_paths_in(carried_dir.path());
        let mut analyze = Par2RepairerOptions::new(carried_dir.path().to_path_buf(), par2.clone());
        analyze.repair = false;
        let (preview, carry) = Par2Repairer::new(analyze)
            .verify_or_repair_carrying()
            .unwrap();
        assert_eq!(preview.status, Par2RepairStatus::RepairPossible);
        assert!(preview.verification.total_missing_blocks > 0);
        assert!(!preview.scan.carried, "the analyze pass scans for real");
        let carry = carry.expect("analyze pass carries scan state");

        let mut execute = Par2RepairerOptions::new(carried_dir.path().to_path_buf(), par2);
        execute.scan_carry = Some(carry);
        let carried_outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();

        assert_eq!(
            carried_outcome.status,
            Par2RepairStatus::Repaired,
            "{carried_outcome:#?}"
        );
        assert!(carried_outcome.carry.carry_applied);
        assert!(carried_outcome.carry.carry_consumed_for_repair);
        assert!(
            !carried_outcome.carry.carry_retried_fresh,
            "consuming the carry means no second scan: {carried_outcome:#?}"
        );
        assert_eq!(carried_outcome.carry.carry_retry_reason, None);
        assert!(
            carried_outcome.scan.carried,
            "the reported scan counters belong to the analyze pass"
        );

        let fresh_outcome = Par2Repairer::new(Par2RepairerOptions::new(
            fresh_dir.path().to_path_buf(),
            par2_paths_in(fresh_dir.path()),
        ))
        .verify_or_repair()
        .unwrap();
        assert_eq!(fresh_outcome.status, Par2RepairStatus::Repaired);
        assert!(!fresh_outcome.carry.carry_attempted);

        for (name, original) in [("alpha.bin", &alpha), ("beta.bin", &beta)] {
            let carried_bytes = fs::read(carried_dir.path().join(name)).unwrap();
            let fresh_bytes = fs::read(fresh_dir.path().join(name)).unwrap();
            assert_eq!(carried_bytes, *original, "{name} must be restored");
            assert_eq!(
                carried_bytes, fresh_bytes,
                "{name} must be byte-identical across the carried and fresh repair paths"
            );
        }
    }

    /// A repair input that changed visibly between the two passes must send
    /// the execute pass back to a real scan, and must still repair correctly
    /// from what it finds there. The carry never applies in these cases: the
    /// snapshot check at the top of the pass already refuses, which is why the
    /// outcome records no retry — one pass, one honest scan.
    #[test]
    fn stat_visible_input_change_between_passes_falls_back_to_a_fresh_scan() {
        let alpha: Vec<u8> = (0..512u32).map(|i| (i % 251) as u8).collect();
        let beta: Vec<u8> = (0..512u32).map(|i| ((i * 7 + 11) % 241) as u8).collect();

        let master = tempdir().unwrap();
        create_recoverable_set(master.path(), &[("alpha.bin", &alpha), ("beta.bin", &beta)]);

        for (label, mutate) in stat_visible_input_mutations() {
            let dir = tempdir().unwrap();
            copy_flat_dir(master.path(), dir.path());
            damage_first_slice(&dir.path().join("alpha.bin"));

            let par2 = par2_paths_in(dir.path());
            let mut analyze = Par2RepairerOptions::new(dir.path().to_path_buf(), par2.clone());
            analyze.repair = false;
            let (preview, carry) = Par2Repairer::new(analyze)
                .verify_or_repair_carrying()
                .unwrap();
            assert_eq!(preview.status, Par2RepairStatus::RepairPossible, "{label}");
            let carry = carry.expect("analyze pass carries scan state");

            mutate(&dir.path().join("beta.bin"));

            let mut execute = Par2RepairerOptions::new(dir.path().to_path_buf(), par2);
            execute.scan_carry = Some(carry);
            let outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();

            assert_eq!(
                outcome.status,
                Par2RepairStatus::Repaired,
                "{label} must still repair: {outcome:#?}"
            );
            assert!(outcome.carry.carry_attempted, "{label}");
            assert!(
                !outcome.carry.carry_applied,
                "{label}: visible drift must refuse the carry outright"
            );
            assert!(!outcome.carry.carry_consumed_for_repair, "{label}");
            assert!(
                !outcome.scan.carried,
                "{label}: the pass must report its own scan"
            );
            assert_eq!(
                fs::read(dir.path().join("alpha.bin")).unwrap(),
                alpha,
                "{label}"
            );
            assert_eq!(
                fs::read(dir.path().join("beta.bin")).unwrap(),
                beta,
                "{label}"
            );
        }
    }

    /// The pre-mutation gate itself, exercised directly: it runs immediately
    /// before repair mutates anything, and it is the check that licenses
    /// skipping the scan. Reaching its refusal arms end-to-end is not possible
    /// through the public entry point — the snapshot check at the top of the
    /// pass sees the same drift first — so the arms are pinned here.
    #[test]
    fn carry_repair_gate_refuses_every_stat_visible_change_to_an_input() {
        let file_data: Vec<u8> = (0..1024u32).map(|i| ((i * 3 + 1) % 251) as u8).collect();

        for (label, mutate) in stat_visible_input_mutations() {
            let dir = tempdir().unwrap();
            let data_path = dir.path().join("data.bin");
            fs::write(&data_path, &file_data).unwrap();
            let set = synthetic_set(&[("data.bin", &file_data)], 64);

            let (state, carry) = scanned_state_with_carry(dir.path(), &set);
            assert_eq!(
                state.carry_repair_inputs_unchanged(&carry),
                Ok(()),
                "{label}: an untouched tree must pass the gate"
            );

            mutate(&data_path);
            assert_eq!(
                state.carry_repair_inputs_unchanged(&carry),
                Err(CarryRetryReason::RepairInputChanged),
                "{label} must refuse the carry before mutation"
            );
        }
    }

    /// An access-backed input has no filesystem identity to re-stat, and a
    /// carry records no serving-handle generation, so there is no honest
    /// signal that the bytes behind it are still the ones the scan read. The
    /// gate refuses rather than guessing, whatever the path-backed inputs say.
    #[test]
    fn carry_repair_gate_refuses_an_access_backed_input() {
        let dir = tempdir().unwrap();
        let file_data: Vec<u8> = (0..1024u32).map(|i| ((i * 5 + 9) % 251) as u8).collect();
        fs::write(dir.path().join("data.bin"), &file_data).unwrap();
        let set = synthetic_set(&[("data.bin", &file_data)], 64);

        let (mut state, carry) = scanned_state_with_carry(dir.path(), &set);
        assert_eq!(state.carry_repair_inputs_unchanged(&carry), Ok(()));

        let file_id = state.files[0].file_id;
        let block = state.blocks.last_mut().expect("scanned block");
        block
            .location
            .as_mut()
            .expect("block resolved to the canonical file")
            .source = SourceLocation::Access(file_id);

        assert_eq!(
            state.carry_repair_inputs_unchanged(&carry),
            Err(CarryRetryReason::RepairInputNotFingerprinted)
        );
    }

    /// The whole point of the carry on a real set: the execute pass reads no
    /// source bytes to analyse, repairs on the analysis it was handed, and
    /// still re-verifies clean afterwards.
    #[cfg(feature = "slow-tests")]
    #[test]
    fn carried_scan_execute_repairs_and_reverifies_clean() {
        let temp = copy_fixture_dir("rar5_lz_plain");
        fs::remove_file(temp.path().join("fixture_rar5_lz_plain.part4.rar")).unwrap();
        let par2_paths = collect_paths(temp.path(), "fixture_rar5_lz_plain_repair", "par2");

        let mut analyze = Par2RepairerOptions::new(temp.path().to_path_buf(), par2_paths.clone());
        analyze.repair = false;
        let (preview, carry) = Par2Repairer::new(analyze)
            .verify_or_repair_carrying()
            .unwrap();
        assert_eq!(preview.status, Par2RepairStatus::RepairPossible);
        let carry = carry.expect("analyze pass carries scan state");

        let mut execute = Par2RepairerOptions::new(temp.path().to_path_buf(), par2_paths.clone());
        execute.scan_carry = Some(carry);
        let outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();
        assert_eq!(outcome.status, Par2RepairStatus::Repaired);
        assert_eq!(outcome.verification.total_missing_blocks, 0);
        assert!(outcome.carry.carry_attempted);
        assert!(outcome.carry.carry_applied);
        assert!(
            outcome.carry.carry_consumed_for_repair,
            "an unchanged tree must repair on the carried analysis"
        );
        assert!(
            !outcome.carry.carry_retried_fresh,
            "consuming the carry means no second scan: {outcome:#?}"
        );
        assert_eq!(outcome.carry.carry_retry_reason, None);
        assert!(
            outcome.scan.carried,
            "the reported scan counters belong to the analyze pass"
        );

        let mut reverify = Par2RepairerOptions::new(temp.path().to_path_buf(), par2_paths);
        reverify.repair = false;
        let clean = Par2Repairer::new(reverify).verify_or_repair().unwrap();
        assert_eq!(clean.status, Par2RepairStatus::Verified, "{clean:#?}");
        assert_eq!(clean.verification.total_missing_blocks, 0, "{clean:#?}");
    }

    // --- Externally-constructed carries -----------------------------------

    /// The set the repairer itself would load from `par2_paths`, so a carry
    /// built against it is built against the same layout the repair will use.
    fn loaded_set(dir: &Path, par2_paths: &[PathBuf]) -> Par2FileSet {
        let options = Par2RepairerOptions::new(dir.to_path_buf(), par2_paths.to_vec());
        Par2Repairer::new(options)
            .load_inventory()
            .expect("load PAR2 inventory")
            .set
    }

    /// A host's own verification of `set` under `dir`, plus the stat
    /// fingerprints captured for it — the two inputs
    /// [`ScanCarry::from_verification`] takes.
    ///
    /// The fingerprints are captured after the read, which is the honest
    /// order: a file that changed *during* the read ends up with a fingerprint
    /// that no longer matches what the read saw only if it changed again, and
    /// a file that changed after it is exactly what the gate exists to catch.
    fn host_verification(
        dir: &Path,
        set: &Par2FileSet,
    ) -> (VerificationResult, HashMap<FileId, FileStatFingerprint>) {
        let access = DiskFileAccess::new(dir.to_path_buf(), set);
        let verification = verify::verify_selected_file_ids(set, &access, &set.recovery_file_ids);
        let fingerprints = set
            .recovery_file_ids
            .iter()
            .filter_map(|file_id| {
                let desc = set.files.get(file_id)?;
                let fingerprint = FileStatFingerprint::capture_path(dir.join(&desc.filename))?;
                Some((*file_id, fingerprint))
            })
            .collect();
        (verification, fingerprints)
    }

    fn external_carry(dir: &Path, set: &Par2FileSet) -> ScanCarry {
        let (verification, fingerprints) = host_verification(dir, set);
        ScanCarry::from_verification(dir, set, &verification, &fingerprints)
            .expect("host verification builds a carry")
    }

    /// The point of the whole feature: a host that already read the payload
    /// hands its verification across the boundary, and the repair runs on it
    /// without reading the set a second time. `carry_consumed_for_repair`
    /// with no `carry_retried_fresh` is the single-read shape, and the bytes
    /// written must be the bytes the ordinary two-pass path writes.
    #[test]
    fn external_carry_repairs_on_the_host_analysis_without_a_scan() {
        let alpha: Vec<u8> = (0..512u32).map(|i| (i % 251) as u8).collect();
        let beta: Vec<u8> = (0..512u32).map(|i| ((i * 7 + 11) % 241) as u8).collect();

        let carried_dir = tempdir().unwrap();
        let fresh_dir = tempdir().unwrap();
        create_recoverable_set(
            carried_dir.path(),
            &[("alpha.bin", &alpha), ("beta.bin", &beta)],
        );
        copy_flat_dir(carried_dir.path(), fresh_dir.path());
        for dir in [carried_dir.path(), fresh_dir.path()] {
            damage_first_slice(&dir.join("alpha.bin"));
        }

        let par2 = par2_paths_in(carried_dir.path());
        let set = loaded_set(carried_dir.path(), &par2);
        let (verification, fingerprints) = host_verification(carried_dir.path(), &set);
        assert!(
            verification.total_missing_blocks > 0,
            "the host's own pass must see the damage: {verification:#?}"
        );
        let carry =
            ScanCarry::from_verification(carried_dir.path(), &set, &verification, &fingerprints)
                .expect("host verification builds a carry");
        assert_eq!(
            carry.diagnostics.bytes_scanned, 0,
            "this crate read nothing to build the carry"
        );
        assert!(
            carry.diagnostics.bytes_skipped_by_evidence > 0,
            "the bytes behind the carried verdicts are disclosed as unread"
        );

        let mut execute = Par2RepairerOptions::new(carried_dir.path().to_path_buf(), par2);
        execute.scan_carry = Some(Arc::new(carry));
        let carried_outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();

        assert_eq!(
            carried_outcome.status,
            Par2RepairStatus::Repaired,
            "{carried_outcome:#?}"
        );
        assert!(carried_outcome.carry.carry_applied);
        assert!(
            carried_outcome.carry.carry_consumed_for_repair,
            "an unchanged tree must repair on the host's analysis: {carried_outcome:#?}"
        );
        assert!(
            !carried_outcome.carry.carry_retried_fresh,
            "consuming the carry means no scan happened here: {carried_outcome:#?}"
        );
        assert!(carried_outcome.scan.carried);

        let fresh_outcome = Par2Repairer::new(Par2RepairerOptions::new(
            fresh_dir.path().to_path_buf(),
            par2_paths_in(fresh_dir.path()),
        ))
        .verify_or_repair()
        .unwrap();
        assert_eq!(fresh_outcome.status, Par2RepairStatus::Repaired);

        for (name, original) in [("alpha.bin", &alpha), ("beta.bin", &beta)] {
            let carried_bytes = fs::read(carried_dir.path().join(name)).unwrap();
            assert_eq!(carried_bytes, *original, "{name} must be restored");
            assert_eq!(
                carried_bytes,
                fs::read(fresh_dir.path().join(name)).unwrap(),
                "{name} must be byte-identical across the external-carry and fresh paths"
            );
        }
    }

    /// The trust contract's first defence. The host attested that `beta.bin`
    /// was intact and fingerprinted it; the file then changed at the same
    /// length with a moved mtime. `stat` can see that, so the snapshot gate
    /// refuses the carry outright and the pass scans for real — the
    /// attestation costs the scan it was meant to save and nothing else.
    #[test]
    fn external_carry_with_a_stat_visible_change_falls_back_to_a_fresh_scan() {
        let alpha: Vec<u8> = (0..512u32).map(|i| (i % 251) as u8).collect();
        let beta: Vec<u8> = (0..512u32).map(|i| ((i * 7 + 11) % 241) as u8).collect();

        let dir = tempdir().unwrap();
        create_recoverable_set(dir.path(), &[("alpha.bin", &alpha), ("beta.bin", &beta)]);
        damage_first_slice(&dir.path().join("alpha.bin"));

        let par2 = par2_paths_in(dir.path());
        let set = loaded_set(dir.path(), &par2);
        let carry = external_carry(dir.path(), &set);

        // Same length, moved mtime: the change a stat call can see.
        bump_modified_time(&dir.path().join("beta.bin"));

        let mut execute = Par2RepairerOptions::new(dir.path().to_path_buf(), par2);
        execute.scan_carry = Some(Arc::new(carry));
        let outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();

        assert_eq!(outcome.status, Par2RepairStatus::Repaired, "{outcome:#?}");
        assert!(outcome.carry.carry_attempted);
        assert!(
            !outcome.carry.carry_applied,
            "a fingerprint that no longer matches must refuse the carry outright: {outcome:#?}"
        );
        assert!(!outcome.carry.carry_consumed_for_repair);
        assert!(
            !outcome.scan.carried,
            "the pass must report the scan it actually ran"
        );
        assert_eq!(fs::read(dir.path().join("alpha.bin")).unwrap(), alpha);
        assert_eq!(fs::read(dir.path().join("beta.bin")).unwrap(), beta);
    }

    /// The trust contract's last defence, and the one that makes a false
    /// attestation harmless rather than merely unlikely to be believed.
    ///
    /// Here the host attested `beta.bin` intact, its bytes were then replaced
    /// at the same length, and its mtime was restored — the one drift a stat
    /// fingerprint provably cannot see. Both stat gates therefore accept, on
    /// the evidence available to them, and the repair proceeds on the carried
    /// analysis. The validated read that a consumed carry always takes then
    /// catches the change on the bytes, against `beta.bin`'s own IFSC
    /// checksums, before anything is installed: the pass retries from a fresh
    /// scan naming the changed input, and the output is correct.
    #[test]
    fn external_carry_validated_read_catches_a_stat_invisible_byte_flip() {
        let alpha: Vec<u8> = (0..512u32).map(|i| (i % 251) as u8).collect();
        let beta: Vec<u8> = (0..512u32).map(|i| ((i * 7 + 11) % 241) as u8).collect();

        let dir = tempdir().unwrap();
        create_recoverable_set(dir.path(), &[("alpha.bin", &alpha), ("beta.bin", &beta)]);
        damage_first_slice(&dir.path().join("alpha.bin"));

        let par2 = par2_paths_in(dir.path());
        let set = loaded_set(dir.path(), &par2);
        let carry = external_carry(dir.path(), &set);

        let beta_path = dir.path().join("beta.bin");
        let mut flipped = beta.clone();
        flipped[100] ^= 0xFF;
        fs::write(&beta_path, &flipped).unwrap();
        restore_carried_modified_time(&carry, &beta_path);

        let mut execute = Par2RepairerOptions::new(dir.path().to_path_buf(), par2);
        execute.scan_carry = Some(Arc::new(carry));
        let outcome = Par2Repairer::new(execute).verify_or_repair().unwrap();

        assert!(outcome.carry.carry_applied, "{outcome:#?}");
        assert!(
            outcome.carry.carry_retried_fresh,
            "the validated read must send the pass back to a real scan: {outcome:#?}"
        );
        assert_eq!(
            outcome.carry.carry_retry_reason,
            Some(CarryRetryReason::RepairInputChanged),
            "{outcome:#?}"
        );
        assert!(
            !outcome.carry.carry_consumed_for_repair,
            "a repair that read a changed source did not consume the carry"
        );
        // Whatever the retried pass could make of the tree, the one thing that
        // must never happen is a file written from bytes nothing checked.
        assert_ne!(
            fs::read(&beta_path).unwrap(),
            flipped,
            "the corrupted source must not survive as the installed file"
        );
    }

    /// A carry built from a host verification must place missing and damaged
    /// files in exactly the internal states a real scan of the same tree
    /// produces, or repair would treat a target as an input. Comparing the
    /// two verification results end to end is the check: they are what every
    /// downstream decision reads.
    #[test]
    fn external_carry_reproduces_a_native_scan_for_missing_and_damaged_files() {
        let intact: Vec<u8> = (0..1024u32).map(|i| (i % 251) as u8).collect();
        let damaged_source: Vec<u8> = (0..1024u32).map(|i| ((i * 3 + 1) % 251) as u8).collect();
        let gone: Vec<u8> = (0..1024u32).map(|i| ((i * 5 + 2) % 251) as u8).collect();

        let dir = tempdir().unwrap();
        let slice_size = 64u64;
        let set = synthetic_set(
            &[
                ("intact.bin", &intact),
                ("damaged.bin", &damaged_source),
                ("gone.bin", &gone),
            ],
            slice_size,
        );
        fs::write(dir.path().join("intact.bin"), &intact).unwrap();
        let mut damaged = damaged_source.clone();
        damaged[128..192].fill(0);
        fs::write(dir.path().join("damaged.bin"), &damaged).unwrap();
        // `gone.bin` is never written.

        let mut scanned = RepairState::from_set(dir.path(), set.clone()).unwrap();
        let options = Par2RepairerOptions::new(dir.path().to_path_buf(), Vec::new());
        scanned.scan(&options).unwrap();
        let native = format!("{:?}", scanned.verification_result());

        let carry = external_carry(dir.path(), &set);
        let mut carried = RepairState::from_set(dir.path(), set).unwrap();
        assert!(
            carried.try_apply_carry(&carry).is_some(),
            "a carry built from an unchanged tree must apply"
        );
        assert_eq!(
            format!("{:?}", carried.verification_result()),
            native,
            "an external carry must reproduce the scan's verdicts exactly"
        );
    }

    /// An attestation that contradicts itself, or the set it claims to
    /// describe, is a caller bug and is refused rather than absorbed: a carry
    /// this crate could not make sense of is the one thing that must never
    /// reach a repair.
    #[test]
    fn external_carry_refuses_inconsistent_attestations() {
        let data: Vec<u8> = (0..256u32).map(|i| (i % 251) as u8).collect();
        let dir = tempdir().unwrap();
        let set = synthetic_set(&[("data.bin", &data)], 64);
        fs::write(dir.path().join("data.bin"), &data).unwrap();
        let (verification, fingerprints) = host_verification(dir.path(), &set);
        assert!(matches!(verification.files[0].status, FileStatus::Complete));

        let build = |verification: &VerificationResult,
                     fingerprints: &HashMap<FileId, FileStatFingerprint>| {
            ScanCarry::from_verification(dir.path(), &set, verification, fingerprints)
        };

        assert!(
            build(&verification, &fingerprints).is_ok(),
            "the consistent attestation must build"
        );

        let mut short = verification.clone();
        short.files[0].valid_slices.pop();
        assert!(matches!(
            build(&short, &fingerprints),
            Err(ExternalCarryError::SliceCountMismatch { .. })
        ));

        let mut miscounted = verification.clone();
        miscounted.files[0].missing_slice_count = 1;
        assert!(matches!(
            build(&miscounted, &fingerprints),
            Err(ExternalCarryError::DamagedCountMismatch { .. })
        ));

        let mut lying_complete = verification.clone();
        lying_complete.files[0].valid_slices[0] = false;
        lying_complete.files[0].missing_slice_count = 1;
        assert!(matches!(
            build(&lying_complete, &fingerprints),
            Err(ExternalCarryError::IncompleteCompleteFile { .. })
        ));

        let mut renamed = verification.clone();
        renamed.files[0].status = FileStatus::Renamed(dir.path().join("elsewhere.bin"));
        assert!(matches!(
            build(&renamed, &fingerprints),
            Err(ExternalCarryError::RelocatedFile { .. })
        ));

        let mut missing_with_content = verification.clone();
        missing_with_content.files[0].status = FileStatus::Missing;
        assert!(matches!(
            build(&missing_with_content, &fingerprints),
            Err(ExternalCarryError::MissingFileWithContent { .. })
        ));

        assert!(
            matches!(
                build(&verification, &HashMap::new()),
                Err(ExternalCarryError::UnfingerprintedFile { .. })
            ),
            "a present file with no fingerprint has nothing for the gate to check"
        );

        let mut uncovered = verification.clone();
        uncovered.files.clear();
        assert!(matches!(
            build(&uncovered, &fingerprints),
            Err(ExternalCarryError::UncoveredFile { .. })
        ));

        let mut unknown = verification.clone();
        let mut stranger = unknown.files[0].clone();
        stranger.file_id = FileId::from_bytes([0xEE; 16]);
        unknown.files.push(stranger);
        assert!(matches!(
            build(&unknown, &fingerprints),
            Err(ExternalCarryError::UnknownFile { .. })
        ));

        let mut duplicated = verification.clone();
        duplicated.files.push(duplicated.files[0].clone());
        assert!(matches!(
            build(&duplicated, &fingerprints),
            Err(ExternalCarryError::DuplicateFile { .. })
        ));
    }

    /// The carried files and blocks replace the receiving state's own, so a
    /// carry laid out from a different set must never be installed. File IDs
    /// nearly settle it; the recovery set ID and slice size close what they
    /// leave open.
    #[test]
    fn external_carry_from_another_set_is_refused() {
        let data: Vec<u8> = (0..256u32).map(|i| (i % 251) as u8).collect();
        let dir = tempdir().unwrap();
        fs::write(dir.path().join("data.bin"), &data).unwrap();

        let coarse = synthetic_set(&[("data.bin", &data)], 128);
        let carry = external_carry(dir.path(), &coarse);

        let mut fine_set = synthetic_set(&[("data.bin", &data)], 64);
        fine_set.recovery_set_id = RecoverySetId::from_bytes([9; 16]);
        let mut fine = RepairState::from_set(dir.path(), fine_set).unwrap();
        assert!(
            fine.try_apply_carry(&carry).is_none(),
            "a carry from another set must not be installed"
        );
    }
}