summa-core 2.0.0

Core async search engine library with WASM support
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
//! Posting list implementation with compact representation
//!
//! Text blocks hold 128 postings: delta-coded doc ids followed by term
//! frequencies, each array encoded by one of the [`PostingCodec`]s
//! (`docs/posting-codecs.md`):
//! - `Rounded` (default): widths rounded to 0/8/16/32 bits, SIMD widening
//! - `Packed`: exact bit widths (BP128 style)
//! - `Pfor`: exact width with patched exceptions (OptP4D style)
//! - `Simd4x`: four-lane library packing and integrated strict document deltas
//!
//! The codec is stored per block in the header, so a single list (for example
//! the output of a merge) may mix codecs.

mod groups;
mod impacts;
mod reader;
mod validation;
use groups::GroupWords;
use impacts::{ImpactBuilder, ImpactTable};

pub(crate) use reader::{DeferredPosting, PostingListReader};

#[cfg(feature = "native")]
mod compact;
#[cfg(feature = "native")]
pub(crate) use compact::{PostingBlockSource, PostingStreamWriter};

use byteorder::{LittleEndian, WriteBytesExt};
use std::io::{self, Write};

use super::bitpacking4x;
use super::horizontal_bp128::{pack_block_n as pack_bits, unpack_block_n as unpack_bits};
use super::opt_p4d::{find_optimal_bit_width, pack_with_exceptions};
use crate::DocId;
use crate::directories::OwnedBytes;
use crate::structures::simd;

/// Encoding of the packed arrays inside one posting block.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PostingCodec {
    /// Widths rounded up to 0/8/16/32 bits; decodes with plain SIMD widening.
    /// This is the low-overhead performance baseline.
    #[default]
    Rounded = 0,
    /// Exact bit widths (BP128 style): ~1.8× smaller than `Rounded` on the
    /// repository benchmark for ~10 % slower decoding.
    Packed = 1,
    /// Exact width with up to 10 % patched exceptions (OptP4D style):
    /// smallest, ~30 % slower decoding than `Rounded`.
    Pfor = 2,
    /// Library SIMD packing of full blocks with exact horizontal tails.
    /// Documents use gap-minus-one values; positions use the same block policy.
    Simd4x = 3,
}

impl PostingCodec {
    /// Codec id stored in the top two bits of the block header's `doc_bits`.
    const HEADER_SHIFT: u32 = 6;
    const WIDTH_MASK: u8 = 0x3F;

    /// The two-bit id field is fully assigned. A fifth codec cannot be
    /// signalled in the block header: it needs a footer flag plus an
    /// `INDEX_META_FORMAT_VERSION` bump (see `docs/posting-codecs.md`).
    fn from_header_byte(doc_bits: u8) -> io::Result<(Self, u8)> {
        let width = doc_bits & Self::WIDTH_MASK;
        let codec = match doc_bits >> Self::HEADER_SHIFT {
            0 => PostingCodec::Rounded,
            1 => PostingCodec::Packed,
            2 => PostingCodec::Pfor,
            _ => PostingCodec::Simd4x,
        };
        if width > 32 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("posting block doc-id width {width} exceeds 32 bits"),
            ));
        }
        Ok((codec, width))
    }

    /// The four-lane kernel requires a full block. Existing rounded tails
    /// avoid scalar bit extraction on short runs preserved by normal merge.
    pub(super) fn for_count(self, count: usize) -> Self {
        if self == Self::Simd4x && count < BLOCK_SIZE {
            Self::Rounded
        } else {
            self
        }
    }

    fn header_byte(self, width: u8) -> u8 {
        ((self as u8) << Self::HEADER_SHIFT) | width
    }

    pub fn parse(s: &str) -> Option<Self> {
        match s.to_ascii_lowercase().as_str() {
            "rounded" | "default" => Some(PostingCodec::Rounded),
            "packed" | "bp128" | "exact" => Some(PostingCodec::Packed),
            "pfor" | "optp4d" | "patched" => Some(PostingCodec::Pfor),
            "simd4x" => Some(PostingCodec::Simd4x),
            _ => None,
        }
    }
}

impl std::fmt::Display for PostingCodec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            PostingCodec::Rounded => "rounded",
            PostingCodec::Packed => "packed",
            PostingCodec::Pfor => "pfor",
            PostingCodec::Simd4x => "simd4x",
        })
    }
}

// ── Exact-width bit packing (Packed codec) ───────────────────────────────

/// Bytes needed for `count` values at `width` bits.
#[inline]
fn packed_bytes(count: usize, width: u8) -> usize {
    (count * width as usize).div_ceil(8)
}

// ── Patched packing (Pfor codec) ─────────────────────────────────────────

/// Payload of one `Pfor` array: `[n_exceptions u8][packed low bits][(pos u8, high u32) × n]`.
fn pack_pfor(values: &[u32], out: &mut Vec<u8>) -> u8 {
    let (width, _, _) = find_optimal_bit_width(values);
    let (packed, exceptions) = pack_with_exceptions(values, width);
    out.push(exceptions.len() as u8);
    out.extend_from_slice(&packed);
    for (pos, high) in exceptions {
        out.push(pos);
        out.extend_from_slice(&high.to_le_bytes());
    }
    width
}

/// Byte length of a `Pfor` array payload for `count` values at `width`.
fn pfor_payload_len(input: &[u8], count: usize, width: u8) -> io::Result<usize> {
    let n_exceptions = *input
        .first()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "posting block truncated"))?
        as usize;
    Ok(1 + packed_bytes(count, width) + n_exceptions * 5)
}

/// Decode one `Pfor` array: the packed low bits, then each `(pos, high)`
/// exception patched in place straight from the table. No per-decode scratch.
fn unpack_pfor(input: &[u8], width: u8, out: &mut [u32], count: usize) -> io::Result<()> {
    let n_exceptions = *input
        .first()
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "posting block truncated"))?
        as usize;
    let table_at = 1 + packed_bytes(count, width);
    let table_end = table_at + n_exceptions * 5;
    if input.len() < table_end {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "posting block exception table truncated",
        ));
    }
    let out = &mut out[..count];
    unpack_bits(&input[1..table_at], width, out, count);
    if width < 32 {
        for entry in input[table_at..table_end].chunks_exact(5) {
            let pos = entry[0] as usize;
            let high = u32::from_le_bytes([entry[1], entry[2], entry[3], entry[4]]);
            if pos < count {
                out[pos] |= high << width;
            }
        }
    }
    Ok(())
}

/// A posting entry containing doc_id and term frequency
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Posting {
    pub doc_id: DocId,
    pub term_freq: u32,
}

/// Compact posting list with delta encoding
#[derive(Debug, Clone, Default)]
pub struct PostingList {
    postings: Vec<Posting>,
}

impl PostingList {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            postings: Vec::with_capacity(capacity),
        }
    }

    /// Add a posting (must be added in doc_id order)
    pub fn push(&mut self, doc_id: DocId, term_freq: u32) {
        debug_assert!(
            self.postings.is_empty() || self.postings.last().unwrap().doc_id < doc_id,
            "Postings must be added in sorted order"
        );
        self.postings.push(Posting { doc_id, term_freq });
    }

    /// Add a posting, incrementing term_freq if doc already exists
    pub fn add(&mut self, doc_id: DocId, term_freq: u32) {
        if let Some(last) = self.postings.last_mut()
            && last.doc_id == doc_id
        {
            last.term_freq += term_freq;
            return;
        }
        self.postings.push(Posting { doc_id, term_freq });
    }

    /// Get document count
    pub fn doc_count(&self) -> u32 {
        self.postings.len() as u32
    }

    pub fn len(&self) -> usize {
        self.postings.len()
    }

    pub fn is_empty(&self) -> bool {
        self.postings.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = &Posting> {
        self.postings.iter()
    }
}

/// Iterator over posting list that supports seeking
pub struct PostingListIterator<'a> {
    postings: &'a [Posting],
    position: usize,
}

impl<'a> PostingListIterator<'a> {
    pub fn new(posting_list: &'a PostingList) -> Self {
        Self {
            postings: &posting_list.postings,
            position: 0,
        }
    }

    /// Current document ID, or TERMINATED if exhausted
    pub fn doc(&self) -> DocId {
        if self.position < self.postings.len() {
            self.postings[self.position].doc_id
        } else {
            TERMINATED
        }
    }

    /// Current term frequency
    pub fn term_freq(&self) -> u32 {
        if self.position < self.postings.len() {
            self.postings[self.position].term_freq
        } else {
            0
        }
    }

    /// Advance to next posting, returns new doc_id or TERMINATED
    pub fn advance(&mut self) -> DocId {
        self.position += 1;
        self.doc()
    }

    /// Seek to first doc_id >= target (binary search on remaining postings)
    pub fn seek(&mut self, target: DocId) -> DocId {
        crate::observe::search_work!(posting_seeks += 1);
        let remaining = &self.postings[self.position..];
        let offset = remaining.partition_point(|p| p.doc_id < target);
        self.position += offset;
        self.doc()
    }

    /// Size hint for remaining elements
    pub fn size_hint(&self) -> usize {
        self.postings.len().saturating_sub(self.position)
    }
}

/// Sentinel value indicating iterator is exhausted
pub const TERMINATED: DocId = DocId::MAX;

/// Block-based posting list with 2-level skip index.
///
/// Each block contains up to `BLOCK_SIZE` postings encoded as packed bit-width arrays.
/// Skip entries use a compact 2-level structure for cache-friendly seeking:
/// - **Level-0** (16 bytes/block): `first_doc`, `last_doc`, `offset`, `max_weight`
/// - **Level-1** (4 bytes/group): `last_doc` per `L1_INTERVAL` blocks
///
/// Seek algorithm: binary search L1, then linear scan ≤`L1_INTERVAL` L0 entries.
pub const BLOCK_SIZE: usize = 128;

/// Number of L0 blocks per L1 skip entry.
const L1_INTERVAL: usize = 8;

/// Compact level-0 skip entry — 16 bytes.
/// `length` is omitted: computable from the block's 8-byte header.
const L0_SIZE: usize = 16;

/// Level-1 skip entry — 4 bytes (just `last_doc`).
const L1_SIZE: usize = 4;

/// Legacy footer: stream_len(8) + l0_count(4) + l1_count(4) + doc_count(4) + max_tf(4) = 24 bytes.
const FOOTER_SIZE: usize = 24;

/// Current footer: the legacy footer followed by `total_positions(8) +
/// flags(4) + min_len(4) + magic(4)`. A list ends with the magic iff it has
/// the extended footer; a legacy footer ends with `max_tf`, which the u16
/// term frequency of the builder keeps far below the magic, so both forms
/// remain readable.
const FOOTER_V2_SIZE: usize = FOOTER_SIZE + 20;

/// "BPL2" little-endian.
const FOOTER_MAGIC: u32 = 0x324C_5042;

/// Footer flag: a `u64` position cursor per L0 block follows the L1 entries.
const FLAG_POS_CURSORS: u32 = 1;

/// Footer flag: the fourth L0 word packs `max_tf` (low 16 bits) and the
/// block's minimum scoring-unit length (high 16 bits) instead of an `f32`
/// max tf, so a block bound can use real length normalisation.
const FLAG_LEN_BOUNDS: u32 = 2;

/// Footer flag: a packed `(max_tf, min_len)` word per L1 group follows the
/// L1 `last_doc` entries (superblock bounds: the maximum and minimum over
/// the group's blocks), so an executor can skip eight blocks at once.
const FLAG_L1_BOUNDS: u32 = 4;

/// Optional downward-rounded length/TF minima, L0 then L1, after cursors.
const FLAG_RATIO_BOUNDS: u32 = 8;

/// Optional complete frequency/length envelopes after ratio metadata.
const FLAG_IMPACT_BOUNDS: u32 = 16;
/// Combined impact directory: L0 records followed by L1 group records.
const FLAG_GROUP_IMPACT_BOUNDS: u32 = 32;
const FLAG_COMPACT_HEADERS: u32 = 64;
const FLAG_SHORT_CURSORS: u32 = 128;

fn append_ratio_groups(ratios: &mut Vec<u8>, blocks: usize) {
    for start in (0..blocks).step_by(L1_INTERVAL) {
        let ratio = (start..(start + L1_INTERVAL).min(blocks))
            .map(|i| read_ratio(ratios, i))
            .fold(f32::INFINITY, f32::min);
        ratios.extend_from_slice(&ratio.to_le_bytes());
    }
}

#[inline]
fn read_ratio(bytes: &[u8], index: usize) -> f32 {
    let at = index * 4;
    f32::from_le_bytes(bytes[at..at + 4].try_into().unwrap())
}

fn validate_ratios(bytes: &[u8]) -> io::Result<()> {
    if bytes.chunks_exact(4).any(|value| {
        let ratio = f32::from_le_bytes(value.try_into().unwrap());
        !ratio.is_finite() || ratio < 0.0
    }) {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "invalid posting ratio bound",
        ));
    }
    Ok(())
}

fn lower_length_ratio(length: u32, tf: u32) -> f32 {
    if tf == 0 {
        return 0.0;
    }
    // f64 represents both inputs exactly. One f32 step down covers division
    // and conversion rounding, including an exactly representable quotient.
    ((length as f64 / tf as f64) as f32).next_down().max(0.0)
}

/// Superblock bounds derived from packed L0 words: per `L1_INTERVAL` group
/// the maximum `max_tf` and minimum `min_len` of its blocks.
fn group_bounds_from_l0(l0: &[u8], l0_count: usize) -> Vec<u32> {
    let mut groups = Vec::with_capacity(l0_count.div_ceil(L1_INTERVAL));
    let mut idx = 0;
    while idx < l0_count {
        let end = (idx + L1_INTERVAL).min(l0_count);
        let mut max_tf = 0u32;
        let mut min_len = u32::MAX;
        for block in idx..end {
            let (_, _, _, word) = read_l0(l0, block);
            let (tf, len) = unpack_bounds(word, true);
            max_tf = max_tf.max(tf);
            min_len = min_len.min(len.unwrap_or(1));
        }
        groups.push(pack_bounds(max_tf, min_len));
        idx = end;
    }
    groups
}

/// Pack block bounds into the fourth L0 word (both saturate at u16).
#[inline]
fn pack_bounds(max_tf: u32, min_len: u32) -> u32 {
    max_tf.min(u16::MAX as u32) | (min_len.min(u16::MAX as u32) << 16)
}

/// Unpack the fourth L0 word: `(max_tf, min_len)`; `min_len` is `None` for
/// legacy lists whose word is an `f32` max tf.
#[inline]
fn unpack_bounds(word: u32, packed: bool) -> (u32, Option<u32>) {
    if packed {
        (word & 0xFFFF, Some(word >> 16))
    } else {
        (f32::from_bits(word) as u32, None)
    }
}

/// Size of one position cursor (`u64`: values before the block in the
/// term's position stream).
const CURSOR_SIZE: usize = 8;

/// Parsed footer of either format plus the derived section layout.
#[derive(Debug, Clone, Copy)]
struct Footer {
    compact_headers: bool,
    short_cursors: bool,
    stream_len: usize,
    l0_count: usize,
    l1_count: usize,
    doc_count: u32,
    max_tf: u32,
    total_positions: u64,
    has_cursors: bool,
    len_bounds: bool,
    l1_bounds: bool,
    ratio_bounds: bool,
    impact_bounds: bool,
    group_impact_bounds: bool,
    min_len: u32,
}

impl Footer {
    fn parse(raw: &[u8]) -> io::Result<Self> {
        Self::parse_tail(raw, raw.len())
    }

    fn parse_tail(raw: &[u8], total_len: usize) -> io::Result<Self> {
        if raw.len() < FOOTER_SIZE {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "posting data too short",
            ));
        }
        let extended = raw.len() >= FOOTER_V2_SIZE
            && u32::from_le_bytes(raw[raw.len() - 4..].try_into().unwrap()) == FOOTER_MAGIC;
        let f = raw.len()
            - if extended {
                FOOTER_V2_SIZE
            } else {
                FOOTER_SIZE
            };
        let stream_len = usize::try_from(u64::from_le_bytes(raw[f..f + 8].try_into().unwrap()))
            .map_err(|_| {
                io::Error::new(
                    io::ErrorKind::InvalidData,
                    "posting stream exceeds address space",
                )
            })?;
        let l0_count = u32::from_le_bytes(raw[f + 8..f + 12].try_into().unwrap()) as usize;
        let l1_count = u32::from_le_bytes(raw[f + 12..f + 16].try_into().unwrap()) as usize;
        let doc_count = u32::from_le_bytes(raw[f + 16..f + 20].try_into().unwrap());
        let max_tf = u32::from_le_bytes(raw[f + 20..f + 24].try_into().unwrap());
        let (total_positions, flags, min_len) = if extended {
            let total = u64::from_le_bytes(raw[f + 24..f + 32].try_into().unwrap());
            let flags = u32::from_le_bytes(raw[f + 32..f + 36].try_into().unwrap());
            let min_len = u32::from_le_bytes(raw[f + 36..f + 40].try_into().unwrap());
            (total, flags, min_len)
        } else {
            (0, 0, 0)
        };
        if flags
            & !(FLAG_POS_CURSORS
                | FLAG_LEN_BOUNDS
                | FLAG_L1_BOUNDS
                | FLAG_RATIO_BOUNDS
                | FLAG_IMPACT_BOUNDS
                | FLAG_GROUP_IMPACT_BOUNDS
                | FLAG_COMPACT_HEADERS
                | FLAG_SHORT_CURSORS)
            != 0
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "unknown posting footer flags",
            ));
        }
        let footer = Self {
            compact_headers: flags & FLAG_COMPACT_HEADERS != 0,
            short_cursors: flags & FLAG_SHORT_CURSORS != 0,
            stream_len,
            l0_count,
            l1_count,
            doc_count,
            max_tf,
            total_positions,
            has_cursors: flags & FLAG_POS_CURSORS != 0,
            len_bounds: flags & FLAG_LEN_BOUNDS != 0,
            l1_bounds: flags & FLAG_L1_BOUNDS != 0,
            ratio_bounds: flags & FLAG_RATIO_BOUNDS != 0,
            impact_bounds: flags & FLAG_IMPACT_BOUNDS != 0,
            group_impact_bounds: flags & FLAG_GROUP_IMPACT_BOUNDS != 0,
            min_len,
        };
        if footer.short_cursors && (!footer.has_cursors || footer.total_positions > u32::MAX as u64)
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "invalid short position cursors",
            ));
        }
        if footer.group_impact_bounds && (!footer.impact_bounds || !footer.l1_bounds) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "group impacts require L0 impacts and L1 bounds",
            ));
        }
        let end = l0_count
            .checked_mul(L0_SIZE + if footer.compact_headers { 4 } else { 0 })
            .and_then(|n| {
                l1_count
                    .checked_mul(L1_SIZE + if footer.l1_bounds { 4 } else { 0 })
                    .and_then(|m| n.checked_add(m))
            })
            .and_then(|n| {
                l0_count
                    .checked_mul(if footer.has_cursors {
                        footer.cursor_size()
                    } else {
                        0
                    })
                    .and_then(|m| n.checked_add(m))
            })
            .and_then(|n| {
                if footer.ratio_bounds {
                    l0_count
                        .checked_add(l1_count)
                        .and_then(|m| m.checked_mul(4))
                        .and_then(|m| n.checked_add(m))
                } else {
                    Some(n)
                }
            })
            .and_then(|n| n.checked_add(stream_len));
        let footer_offset = total_len.saturating_sub(raw.len() - f);
        if end.is_none_or(|end| {
            if footer.impact_bounds {
                !footer.len_bounds
                    || !footer.ratio_bounds
                    || l0_count
                        .checked_add(if footer.group_impact_bounds {
                            l1_count
                        } else {
                            0
                        })
                        .and_then(|n| n.checked_add(1))
                        .and_then(|n| n.checked_mul(4))
                        .and_then(|n| n.checked_add(end))
                        .is_none_or(|minimum| minimum > footer_offset)
            } else {
                end != footer_offset
            }
        }) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "posting list sections do not match the footer offset",
            ));
        }
        Ok(footer)
    }

    fn l0_start(&self) -> usize {
        self.stream_len
    }
    fn impact_record_count(&self) -> usize {
        self.l0_count
            + if self.group_impact_bounds {
                self.l1_count
            } else {
                0
            }
    }
    fn l0_end(&self) -> usize {
        self.l0_start() + self.l0_count * L0_SIZE
    }
    fn l1_start(&self) -> usize {
        self.l0_end()
            + if self.compact_headers {
                self.l0_count * 4
            } else {
                0
            }
    }
    fn cursor_size(&self) -> usize {
        if self.short_cursors { 4 } else { CURSOR_SIZE }
    }
    fn l1_end(&self) -> usize {
        self.l1_start() + self.l1_count * L1_SIZE
    }
    fn l1_bounds_end(&self) -> usize {
        self.l1_end() + if self.l1_bounds { self.l1_count * 4 } else { 0 }
    }
    fn ratios_end(&self) -> usize {
        self.cursors_end()
            + if self.ratio_bounds {
                (self.l0_count + self.l1_count) * 4
            } else {
                0
            }
    }
    fn cursors_end(&self) -> usize {
        self.l1_bounds_end()
            + if self.has_cursors {
                self.l0_count * self.cursor_size()
            } else {
                0
            }
    }
}

/// Read a compact L0 entry from raw bytes at the given index: `(first_doc,
/// last_doc, offset, bounds word)`. The bounds word is packed `(max_tf,
/// min_len)` for current lists and an `f32` max tf for legacy ones; see
/// [`unpack_bounds`].
///
/// Uses a single bounds check (`[..L0_SIZE]`) instead of 4× `try_into().unwrap()`.
#[inline]
fn read_l0(bytes: &[u8], idx: usize) -> (u32, u32, u32, u32) {
    let b = &bytes[idx * L0_SIZE..][..L0_SIZE];
    let first_doc = u32::from_le_bytes([b[0], b[1], b[2], b[3]]);
    let last_doc = u32::from_le_bytes([b[4], b[5], b[6], b[7]]);
    let offset = u32::from_le_bytes([b[8], b[9], b[10], b[11]]);
    let bounds = u32::from_le_bytes([b[12], b[13], b[14], b[15]]);
    (first_doc, last_doc, offset, bounds)
}

/// Content check that structural admission cannot make without decoding:
/// a block's ids must be strictly increasing and span exactly its L0 range.
/// Branch-free so the 128-value pass vectorises on the decode path.
#[inline]
fn verify_block_docs(
    docs: &[u32],
    first: u32,
    last: u32,
    byte_gaps: Option<&[u8]>,
    strict_gap_width: Option<u8>,
) -> bool {
    let ordered = if strict_gap_width.is_some_and(|width| width <= 25) {
        // Gap-minus-one guarantees positive gaps. At most 127 gaps of at
        // most 2^25 sum to less than 2^32: a wrap would put the endpoint
        // below the start. Ordered matching endpoints prove every prefix.
        // Full blocks' reserved first gap is checked before this call.
        debug_assert!(docs.len() <= BLOCK_SIZE);
        true
    } else if let Some(gaps) = byte_gaps {
        // At most 127 byte-sized gaps: their sum cannot wrap a u32 more than
        // once. Nonzero gaps and matching ordered endpoints therefore prove
        // strict ordering without re-reading the decoded u32 array.
        debug_assert_eq!(gaps.len() + 1, docs.len());
        let mut nonzero = true;
        for &gap in gaps {
            nonzero &= gap != 0;
        }
        nonzero
    } else {
        let mut ordered = true;
        for pair in docs.windows(2) {
            ordered &= pair[0] < pair[1];
        }
        ordered
    };
    ordered
        && first <= last
        && last != TERMINATED
        && docs.first() == Some(&first)
        && docs.last() == Some(&last)
}

/// Write a compact L0 entry.
#[inline]
fn write_l0(buf: &mut Vec<u8>, first_doc: u32, last_doc: u32, offset: u32, bounds: u32) {
    buf.extend_from_slice(&first_doc.to_le_bytes());
    buf.extend_from_slice(&last_doc.to_le_bytes());
    buf.extend_from_slice(&offset.to_le_bytes());
    buf.extend_from_slice(&bounds.to_le_bytes());
}

/// Byte length of block `idx` from the L0 offsets: the next block's offset
/// (or the stream end) minus this block's offset. Header-independent, so a
/// block payload may carry codec-specific variable-length data.
#[inline]
fn block_len_from_l0(l0_bytes: &[u8], l0_count: usize, stream_len: usize, idx: usize) -> usize {
    let (_, _, offset, _) = read_l0(l0_bytes, idx);
    let end = if idx + 1 < l0_count {
        read_l0(l0_bytes, idx + 1).2 as usize
    } else {
        stream_len
    };
    end.saturating_sub(offset as usize)
}

/// Encoded doc-id delta array and tf array of one block, with the header
/// width bytes to store for them.
struct EncodedBlock {
    doc_bits: u8,
    tf_bits: u8,
}

/// Append the packed arrays of one block to `stream` using `codec`.
fn encode_block_arrays(
    codec: PostingCodec,
    deltas: &[u32],
    tfs: &[u32],
    stream: &mut Vec<u8>,
) -> EncodedBlock {
    let codec = codec.for_count(tfs.len());
    match codec {
        PostingCodec::Simd4x => EncodedBlock {
            doc_bits: codec.header_byte(bitpacking4x::encode_gaps(deltas, stream)),
            tf_bits: bitpacking4x::encode(tfs, stream),
        },
        PostingCodec::Rounded => {
            let max_delta = deltas.iter().copied().max().unwrap_or(0);
            let doc_bits = simd::round_bit_width(simd::bits_needed(max_delta));
            let max_tf = tfs.iter().copied().max().unwrap_or(0);
            let tf_bits = simd::round_bit_width(simd::bits_needed(max_tf));
            if !deltas.is_empty() {
                let rounded = simd::RoundedBitWidth::from_u8(doc_bits);
                let start = stream.len();
                stream.resize(start + deltas.len() * rounded.bytes_per_value(), 0);
                simd::pack_rounded(deltas, rounded, &mut stream[start..]);
            }
            {
                let rounded = simd::RoundedBitWidth::from_u8(tf_bits);
                let start = stream.len();
                stream.resize(start + tfs.len() * rounded.bytes_per_value(), 0);
                simd::pack_rounded(tfs, rounded, &mut stream[start..]);
            }
            EncodedBlock {
                doc_bits: codec.header_byte(doc_bits),
                tf_bits,
            }
        }
        PostingCodec::Packed => {
            let max_delta = deltas.iter().copied().max().unwrap_or(0);
            let doc_bits = simd::bits_needed(max_delta);
            let max_tf = tfs.iter().copied().max().unwrap_or(0);
            let tf_bits = simd::bits_needed(max_tf);
            pack_bits(deltas, doc_bits, stream);
            pack_bits(tfs, tf_bits, stream);
            EncodedBlock {
                doc_bits: codec.header_byte(doc_bits),
                tf_bits,
            }
        }
        PostingCodec::Pfor => {
            let doc_bits = if deltas.is_empty() {
                0
            } else {
                pack_pfor(deltas, stream)
            };
            let tf_bits = pack_pfor(tfs, stream);
            EncodedBlock {
                doc_bits: codec.header_byte(doc_bits),
                tf_bits,
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct BlockPostingList {
    compact_headers: bool,
    short_cursors: bool,
    /// Explicit deserialization checks decoded ordering; segment queries trust the writer.
    verify_content: bool,
    /// First decoding failure detected in this immutable segment reader.
    content_error: Option<std::sync::Arc<reader::PostingIntegrity>>,
    /// Block data stream (packed blocks laid out sequentially).
    stream: OwnedBytes,
    /// Level-0 skip entries: `(first_doc, last_doc, offset, max_weight)` × `l0_count`.
    /// 16 bytes per entry, followed by compact descriptors when present.
    /// Supports O(1) random access without another reference-counted slice.
    l0_bytes: OwnedBytes,
    /// Number of blocks (= number of L0 entries).
    l0_count: usize,
    /// Level-1 skip `last_doc` values — one per `L1_INTERVAL` blocks.
    /// Borrowed little-endian words; opening does not copy the group directory.
    l1_docs: GroupWords,
    /// Packed `(max_tf, min_len)` per L1 group (superblock bounds); empty
    /// for legacy lists.
    l1_bounds: GroupWords,
    /// Optional L0 then L1 length/TF ratio minima, borrowed from index bytes.
    ratios: Option<OwnedBytes>,
    /// Validated borrowed offsets and compact integer envelope records.
    impacts: Option<ImpactTable>,
    /// Total posting count.
    doc_count: u32,
    /// Max TF across all blocks.
    max_tf: u32,
    /// Per-block position cursors (`u64` × `l0_count`): number of values in
    /// the term's position stream before the block. `None` for terms
    /// without positions and for legacy lists.
    pos_cursors: Option<OwnedBytes>,
    /// Sum of term frequencies (= values in the position stream) when
    /// cursors are present.
    total_positions: u64,
    /// Whether L0 bounds words are packed `(max_tf, min_len)`.
    len_bounds: bool,
    /// Minimum scoring-unit length over the whole list (with `len_bounds`).
    min_len: u32,
}

impl BlockPostingList {
    /// Read L0 entry by block index. Returns `(first_doc, last_doc, offset, bounds word)`.
    #[inline]
    fn read_l0_entry(&self, idx: usize) -> (u32, u32, u32, u32) {
        read_l0(&self.l0_bytes, idx)
    }

    /// Build from a posting list.
    ///
    /// Block format (8-byte header + packed arrays):
    /// ```text
    /// [count: u16][first_doc: u32][doc_id_bits: u8][tf_bits: u8]
    /// [packed doc_id deltas: (count-1) × bytes_per_value(doc_id_bits)]
    /// [packed tfs: count × bytes_per_value(tf_bits)]
    /// ```
    pub fn from_posting_list(list: &PostingList) -> io::Result<Self> {
        Self::build(list, false, None, PostingCodec::Rounded, false, false)
    }

    /// Build a list using an explicit per-block codec.
    pub fn from_posting_list_with_codec(
        list: &PostingList,
        codec: PostingCodec,
    ) -> io::Result<Self> {
        Self::build(list, false, None, codec, false, false)
    }

    /// Build with position cursors on demand and, when `length_of` is given,
    /// the minimum scoring-unit length per block (and over the list) so
    /// MaxScore bounds use real length normalisation. Without lengths the
    /// minimum is 1, which any real unit satisfies.
    pub fn from_posting_list_with(
        list: &PostingList,
        with_positions: bool,
        length_of: Option<&dyn Fn(DocId) -> u32>,
    ) -> io::Result<Self> {
        Self::build(
            list,
            with_positions,
            length_of,
            PostingCodec::Rounded,
            false,
            false,
        )
    }

    /// Build with the complete physical layout policy used by index writers.
    pub fn from_posting_list_with_options(
        list: &PostingList,
        with_positions: bool,
        length_of: Option<&dyn Fn(DocId) -> u32>,
        codec: PostingCodec,
    ) -> io::Result<Self> {
        Self::build(list, with_positions, length_of, codec, false, false)
    }

    /// Build optional score-independent ratio bounds, retaining the existing codec.
    pub fn from_posting_list_with_ratio_bounds(
        list: &PostingList,
        with_positions: bool,
        length_of: Option<&dyn Fn(DocId) -> u32>,
        codec: PostingCodec,
    ) -> io::Result<Self> {
        Self::build(list, with_positions, length_of, codec, true, false)
    }

    /// Build complete, bounded frequency/length envelopes for multi-block lists.
    /// Implies ratio bounds and requires the effective scoring-length callback.
    pub fn from_posting_list_with_impact_bounds(
        list: &PostingList,
        with_positions: bool,
        length_of: Option<&dyn Fn(DocId) -> u32>,
        codec: PostingCodec,
    ) -> io::Result<Self> {
        if length_of.is_none() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "impact bounds require scoring lengths",
            ));
        }
        Self::build(list, with_positions, length_of, codec, true, true)
    }

    fn build(
        list: &PostingList,
        with_positions: bool,
        length_of: Option<&dyn Fn(DocId) -> u32>,
        codec: PostingCodec,
        ratio_bounds: bool,
        impact_bounds: bool,
    ) -> io::Result<Self> {
        // Persisted scoring lengths saturate at `MAX_CHUNK_LENGTH`. A ratio or
        // envelope derived from a longer raw length would be over-tight, so
        // the constructor caps here rather than trusting every caller to.
        let capped = |id: DocId| {
            length_of
                .map_or(0, |length_of| length_of(id))
                .min(crate::segment::chunk_map::MAX_CHUNK_LENGTH)
        };
        let length_of: Option<&dyn Fn(DocId) -> u32> = if ratio_bounds {
            length_of.map(|_| &capped as &dyn Fn(DocId) -> u32)
        } else {
            length_of
        };
        let mut ratios = (ratio_bounds && length_of.is_some()).then(Vec::new);
        let mut impacts = (impact_bounds && length_of.is_some() && list.len() > BLOCK_SIZE)
            .then(|| ImpactBuilder::with_groups(list.len().div_ceil(BLOCK_SIZE)))
            .transpose()?;
        let mut points = [(0u32, 0u32); BLOCK_SIZE];
        let mut stream: Vec<u8> = Vec::new();
        let mut l0_buf: Vec<u8> = Vec::new();
        let mut l1_docs: Vec<u32> = Vec::new();
        let mut cursors: Vec<u8> = Vec::new();
        let mut positions_so_far = 0u64;
        let mut l0_count = 0usize;
        let mut max_tf = 0u32;
        let mut list_min_len = u32::MAX;

        let postings = &list.postings;
        let mut i = 0;

        // Temp buffers reused across blocks
        let mut deltas = Vec::with_capacity(BLOCK_SIZE);
        let mut tf_buf = Vec::with_capacity(BLOCK_SIZE);

        while i < postings.len() {
            if stream.len() > u32::MAX as usize {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "posting list stream exceeds u32::MAX bytes",
                ));
            }
            let block_start = stream.len() as u32;
            let block_end = (i + BLOCK_SIZE).min(postings.len());
            let block = &postings[i..block_end];
            let count = block.len();

            // Compute block's max term frequency for block-max pruning
            let block_max_tf = block.iter().map(|p| p.term_freq).max().unwrap_or(0);
            max_tf = max_tf.max(block_max_tf);

            let base_doc_id = block.first().unwrap().doc_id;
            let last_doc_id = block.last().unwrap().doc_id;

            // Delta-encode doc IDs (skip first — stored in header)
            deltas.clear();
            let mut prev = base_doc_id;
            for posting in block.iter().skip(1) {
                deltas.push(posting.doc_id - prev);
                prev = posting.doc_id;
            }

            // Collect TFs
            tf_buf.clear();
            tf_buf.extend(block.iter().map(|p| p.term_freq));

            // Write 8-byte header: [count: u16][first_doc: u32][doc_bits: u8][tf_bits: u8]
            // (`doc_bits` carries the codec id in its top two bits); the
            // packed arrays follow.
            stream.write_u16::<LittleEndian>(count as u16)?;
            stream.write_u32::<LittleEndian>(base_doc_id)?;
            let header_at = stream.len();
            stream.push(0);
            stream.push(0);
            let encoded = encode_block_arrays(codec, &deltas, &tf_buf, &mut stream);
            stream[header_at] = encoded.doc_bits;
            stream[header_at + 1] = encoded.tf_bits;

            // L0 skip entry with the block's bounds
            let block_min_len = length_of.map_or(1, |length_of| {
                block
                    .iter()
                    .map(|p| length_of(p.doc_id).max(1))
                    .min()
                    .unwrap_or(1)
            });
            list_min_len = list_min_len.min(block_min_len);
            if let Some(ratios) = &mut ratios {
                let ratio = block
                    .iter()
                    .map(|p| lower_length_ratio(length_of.unwrap()(p.doc_id).max(1), p.term_freq))
                    .fold(f32::INFINITY, f32::min);
                ratios.extend_from_slice(&ratio.to_le_bytes());
            }
            if let Some(impacts) = &mut impacts {
                for (point, posting) in points.iter_mut().zip(block) {
                    let length = length_of.unwrap()(posting.doc_id);
                    *point = (
                        posting.term_freq,
                        if length == 0 {
                            posting.term_freq
                        } else {
                            length
                        },
                    );
                }
                impacts.append_points(&mut points[..count])?;
            }
            write_l0(
                &mut l0_buf,
                base_doc_id,
                last_doc_id,
                block_start,
                pack_bounds(block_max_tf, block_min_len),
            );
            l0_count += 1;
            if with_positions {
                cursors.extend_from_slice(&positions_so_far.to_le_bytes());
                positions_so_far += block.iter().map(|p| p.term_freq as u64).sum::<u64>();
            }

            // L1 entry at the end of each L1_INTERVAL group
            if l0_count.is_multiple_of(L1_INTERVAL) {
                l1_docs.push(last_doc_id);
            }

            i = block_end;
        }

        // Final L1 entry for partial group
        if !l0_count.is_multiple_of(L1_INTERVAL) && l0_count > 0 {
            let (_, last_doc, _, _) = read_l0(&l0_buf, l0_count - 1);
            l1_docs.push(last_doc);
        }
        let l1_bounds = group_bounds_from_l0(&l0_buf, l0_count);
        if let Some(ratios) = &mut ratios {
            append_ratio_groups(ratios, l0_count);
        }

        Ok(Self {
            compact_headers: false,
            short_cursors: false,
            verify_content: true,
            content_error: None,
            stream: OwnedBytes::new(stream),
            l0_bytes: OwnedBytes::new(l0_buf),
            l0_count,
            l1_docs: l1_docs.into(),
            l1_bounds: l1_bounds.into(),
            ratios: ratios.map(OwnedBytes::new),
            impacts: if let Some(mut impacts) = impacts {
                impacts.append_groups_with(l0_count, |_| Ok(None))?;
                impacts.finish()
            } else {
                None
            },
            doc_count: postings.len() as u32,
            max_tf,
            pos_cursors: with_positions.then(|| OwnedBytes::new(cursors)),
            total_positions: positions_so_far,
            len_bounds: true,
            min_len: if list_min_len == u32::MAX {
                1
            } else {
                list_min_len
            },
        })
    }

    /// Serialize the block posting list (footer-based: stream first).
    ///
    /// Format:
    /// ```text
    /// [stream: block data]
    /// [L0 entries: l0_count × 16 bytes (first_doc, last_doc, offset, max_weight)]
    /// [L1 entries: l1_count × 4 bytes (last_doc)]
    /// [L1 bounds: l1_count × 4 bytes (packed max_tf, min_len), FLAG_L1_BOUNDS]
    /// [position cursors: l0_count × 8 bytes, only with positions]
    /// [footer: stream_len(8) + l0_count(4) + l1_count(4) + doc_count(4) + max_tf(4)
    ///          + total_positions(8) + flags(4) + min_len(4) + magic(4) = 44 bytes]
    /// ```
    pub fn serialize<W: Write>(&self, writer: &mut W) -> io::Result<()> {
        self.serialize_layout(writer, self.compact_headers)
    }

    /// Representation policy retained by explicit field reordering.
    #[cfg(all(feature = "native", test))]
    pub(crate) fn has_compact_headers(&self) -> bool {
        self.compact_headers
    }

    /// Separate fixed-width block metadata from payload pages. Pfor retains
    /// its existing framing because exception structure lives in the payload.
    pub fn serialize_compact<W: Write>(&self, writer: &mut W) -> io::Result<()> {
        let compact =
            (0..self.num_blocks()).all(|i| self.block_codec(i) != Some(PostingCodec::Pfor));
        self.serialize_layout(writer, compact)
    }

    fn serialize_layout<W: Write>(&self, writer: &mut W, compact: bool) -> io::Result<()> {
        let source_header = if self.compact_headers { 0 } else { 8 };
        let output_header = if compact { 0 } else { 8 };
        let output_offset =
            |offset: usize, block: usize| offset - block * source_header + block * output_header;
        let stream_len = output_offset(self.stream.len(), self.num_blocks());
        if compact == self.compact_headers {
            writer.write_all(&self.stream)?;
        } else {
            for i in 0..self.num_blocks() {
                if !compact {
                    writer.write_all(&self.block_header(i))?;
                }
                writer.write_all(self.block_payload(i))?;
            }
        }
        for i in 0..self.num_blocks() {
            let (first, last, offset, bounds) = self.read_l0_entry(i);
            writer.write_u32::<LittleEndian>(first)?;
            writer.write_u32::<LittleEndian>(last)?;
            writer.write_u32::<LittleEndian>(
                u32::try_from(output_offset(offset as usize, i))
                    .map_err(|_| io::Error::other("posting stream offset overflow"))?,
            )?;
            writer.write_u32::<LittleEndian>(bounds)?;
        }
        if compact {
            for i in 0..self.num_blocks() {
                let header = self.block_header(i);
                writer.write_all(&header[..2])?;
                writer.write_all(&header[6..])?;
            }
        }
        writer.write_all(self.l1_docs.bytes())?;
        writer.write_all(self.l1_bounds.bytes())?;
        let short_cursors =
            compact && self.pos_cursors.is_some() && self.total_positions <= u32::MAX as u64;
        if self.pos_cursors.is_some() {
            for i in 0..self.num_blocks() {
                let cursor = self.pos_cursor(i).unwrap();
                if short_cursors {
                    writer.write_u32::<LittleEndian>(cursor as u32)?;
                } else {
                    writer.write_u64::<LittleEndian>(cursor)?;
                }
            }
        }
        if let Some(ratios) = &self.ratios {
            writer.write_all(ratios)?;
        }
        if let Some(impacts) = &self.impacts {
            writer.write_all(impacts.bytes())?;
        }
        Self::write_footer(
            writer,
            stream_len as u64,
            self.l0_count,
            self.l1_docs.len(),
            self.doc_count,
            self.max_tf,
            self.total_positions,
            self.pos_cursors.is_some(),
            self.len_bounds.then_some(self.min_len),
            !self.l1_bounds.is_empty(),
            self.ratios.is_some(),
            self.impacts.is_some(),
            self.has_group_impact_bounds(),
            if compact { FLAG_COMPACT_HEADERS } else { 0 }
                | if short_cursors { FLAG_SHORT_CURSORS } else { 0 },
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn write_footer<W: Write>(
        writer: &mut W,
        stream_len: u64,
        l0_count: usize,
        l1_count: usize,
        doc_count: u32,
        max_tf: u32,
        total_positions: u64,
        has_cursors: bool,
        min_len: Option<u32>,
        l1_bounds: bool,
        ratio_bounds: bool,
        impact_bounds: bool,
        group_impact_bounds: bool,
        layout_flags: u32,
    ) -> io::Result<()> {
        writer.write_u64::<LittleEndian>(stream_len)?;
        writer.write_u32::<LittleEndian>(l0_count as u32)?;
        writer.write_u32::<LittleEndian>(l1_count as u32)?;
        writer.write_u32::<LittleEndian>(doc_count)?;
        writer.write_u32::<LittleEndian>(max_tf)?;
        writer.write_u64::<LittleEndian>(total_positions)?;
        let mut flags = layout_flags;
        if has_cursors {
            flags |= FLAG_POS_CURSORS;
        }
        if min_len.is_some() {
            flags |= FLAG_LEN_BOUNDS;
        }
        if l1_bounds {
            flags |= FLAG_L1_BOUNDS;
        }
        if ratio_bounds {
            flags |= FLAG_RATIO_BOUNDS;
        }
        if impact_bounds {
            flags |= FLAG_IMPACT_BOUNDS;
        }
        if group_impact_bounds {
            flags |= FLAG_GROUP_IMPACT_BOUNDS;
        }
        writer.write_u32::<LittleEndian>(flags)?;
        writer.write_u32::<LittleEndian>(min_len.unwrap_or(0))?;
        writer.write_u32::<LittleEndian>(FOOTER_MAGIC)?;
        Ok(())
    }

    /// Deserialize from a byte slice (either footer format).
    pub fn deserialize(raw: &[u8]) -> io::Result<Self> {
        Self::deserialize_zero_copy(OwnedBytes::new(raw.to_vec()))
    }

    /// Zero-copy deserialization from OwnedBytes.
    /// Stream, L0, L1 and cursors are sliced from the source without copying.
    pub fn deserialize_zero_copy(raw: OwnedBytes) -> io::Result<Self> {
        let footer = Self::validate_bytes(&raw)?;
        Ok(Self::from_layout(raw, footer))
    }

    fn validate_bytes(raw: &[u8]) -> io::Result<Footer> {
        let footer = Footer::parse(raw)?;
        validation::validate_list(raw, &footer)?;
        if footer.ratio_bounds {
            validate_ratios(&raw[footer.cursors_end()..footer.ratios_end()])?;
        }
        if footer.impact_bounds {
            ImpactTable::validate(
                &raw[footer.ratios_end()..raw.len() - FOOTER_V2_SIZE],
                footer.impact_record_count(),
            )?;
        }
        Ok(footer)
    }

    // The footer proves section extents. The owning query reader trusts interior
    // contents; explicit deserialization additionally validates them.
    fn from_layout(raw: OwnedBytes, footer: Footer) -> Self {
        let ratios = footer
            .ratio_bounds
            .then(|| raw.slice(footer.cursors_end()..footer.ratios_end()));
        let l1_docs = GroupWords::borrowed(raw.slice(footer.l1_start()..footer.l1_end()));
        let l1_bounds = GroupWords::borrowed(raw.slice(footer.l1_end()..footer.l1_bounds_end()));
        let pos_cursors = footer
            .has_cursors
            .then(|| raw.slice(footer.l1_bounds_end()..footer.cursors_end()));

        Self {
            compact_headers: footer.compact_headers,
            short_cursors: footer.short_cursors,
            verify_content: true,
            content_error: None,
            stream: raw.slice(0..footer.stream_len),
            l0_bytes: raw.slice(footer.l0_start()..footer.l1_start()),
            l0_count: footer.l0_count,
            l1_docs,
            l1_bounds,
            ratios,
            impacts: footer.impact_bounds.then(|| {
                ImpactTable::from_validated(
                    raw.slice(footer.ratios_end()..raw.len() - FOOTER_V2_SIZE),
                    footer.impact_record_count(),
                )
            }),
            doc_count: footer.doc_count,
            max_tf: footer.max_tf,
            pos_cursors,
            total_positions: footer.total_positions,
            len_bounds: footer.len_bounds,
            min_len: footer.min_len,
        }
    }

    /// Minimum scoring-unit length over the list, when the list stores
    /// length bounds (`None` for legacy lists).
    pub fn min_len(&self) -> Option<u32> {
        self.len_bounds.then_some(self.min_len)
    }

    /// Whether optional ratio bounds are present (zero entries mean unknown).
    pub fn has_ratio_bounds(&self) -> bool {
        self.ratios.is_some()
    }

    /// Whether the list stores an impact directory, including unknown records.
    pub fn has_impact_bounds(&self) -> bool {
        self.impacts.is_some()
    }

    /// Envelope diagnostics: absent directory/out-of-range is `None`; an unknown
    /// record is `Some(0)`; a populated record has 1–8 complete frontier points.
    pub fn block_impact_point_count(&self, block: usize) -> Option<usize> {
        if block >= self.l0_count {
            return None;
        }
        self.impacts
            .as_ref()?
            .record(block)
            .map(|r| r.first().copied().unwrap_or(0) as usize)
    }

    /// Whether the optional table also stores group envelopes.
    pub fn has_group_impact_bounds(&self) -> bool {
        self.impacts
            .as_ref()
            .is_some_and(|t| t.record_count() > self.l0_count)
    }

    /// Point count for the group containing this block; zero means unknown.
    #[cfg(test)]
    pub fn group_impact_point_count(&self, block: usize) -> Option<usize> {
        if block >= self.l0_count {
            return None;
        }
        self.impacts
            .as_ref()?
            .record(self.l0_count + block / L1_INTERVAL)
            .map(|r| r.first().copied().unwrap_or(0) as usize)
    }

    pub(crate) fn group_impact_minimum(
        &self,
        block: usize,
        reciprocal: f64,
        ratio: f64,
    ) -> Option<f64> {
        if block >= self.l0_count {
            return None;
        }
        self.impacts
            .as_ref()?
            .minimum(self.l0_count + block / L1_INTERVAL, reciprocal, ratio)
    }

    pub(crate) fn block_impact_minimum(
        &self,
        block: usize,
        reciprocal: f64,
        ratio: f64,
    ) -> Option<f64> {
        if block >= self.l0_count {
            return None;
        }
        self.impacts.as_ref()?.minimum(block, reciprocal, ratio)
    }

    /// Conservative minimum length/TF for a block, or zero if unavailable.
    pub(crate) fn block_length_ratio(&self, block: usize) -> f32 {
        self.ratios
            .as_ref()
            .map_or(0.0, |ratios| read_ratio(ratios, block))
    }

    pub(crate) fn group_length_ratio(&self, block: usize) -> f32 {
        self.ratios.as_ref().map_or(0.0, |ratios| {
            read_ratio(ratios, self.l0_count + block / L1_INTERVAL)
        })
    }

    /// `(max_tf, min_len)` of a block; `min_len` is `None` for legacy lists.
    #[inline]
    pub fn block_bounds(&self, block_idx: usize) -> Option<(u32, Option<u32>)> {
        if block_idx >= self.l0_count {
            return None;
        }
        let (_, _, _, word) = self.read_l0_entry(block_idx);
        let (max_tf, min_len) = unpack_bounds(word, self.len_bounds);
        // A packed maximum can saturate; the full-width list maximum remains
        // a conservative bound. Never interpret saturation as an actual TF.
        let max_tf = if self.len_bounds && max_tf == u16::MAX as u32 {
            max_tf.max(self.max_tf)
        } else {
            max_tf
        };
        Some((max_tf, min_len))
    }

    /// `(max_tf, min_len)` over the L1 group (`L1_INTERVAL` blocks) that
    /// contains `block_idx`; `None` for legacy lists without group bounds.
    #[inline]
    pub fn group_bounds(&self, block_idx: usize) -> Option<(u32, u32)> {
        if block_idx >= self.l0_count {
            return None;
        }
        let word = self.l1_bounds.get(block_idx / L1_INTERVAL)?;
        let (max_tf, min_len) = unpack_bounds(word, true);
        let max_tf = if max_tf == u16::MAX as u32 {
            max_tf.max(self.max_tf)
        } else {
            max_tf
        };
        Some((max_tf, min_len.unwrap_or(1)))
    }

    /// Last doc of the L1 group containing `block_idx`.
    #[inline]
    pub fn group_last_doc(&self, block_idx: usize) -> Option<DocId> {
        self.l1_docs.get(block_idx / L1_INTERVAL)
    }

    /// Whether `block_idx` opens an L1 group.
    #[inline]
    pub fn is_group_start(&self, block_idx: usize) -> bool {
        block_idx.is_multiple_of(L1_INTERVAL)
    }

    /// Index of the first block after the L1 group containing `block_idx`
    /// (clamped to the block count).
    #[inline]
    pub fn next_group_block(&self, block_idx: usize) -> usize {
        ((block_idx / L1_INTERVAL + 1) * L1_INTERVAL).min(self.l0_count)
    }

    /// Whether serialized bytes carry position cursors (cheap footer check).
    pub fn has_cursors_bytes(raw: &[u8]) -> bool {
        Footer::parse(raw).is_ok_and(|footer| footer.has_cursors)
    }

    /// Whether this list carries a position cursor per block.
    pub fn has_position_cursors(&self) -> bool {
        self.pos_cursors.is_some()
    }

    #[inline]
    fn block_header(&self, block: usize) -> [u8; 8] {
        let (first, _, offset, _) = self.read_l0_entry(block);
        if self.compact_headers {
            let headers = &self.l0_bytes[self.l0_count * L0_SIZE..];
            let mut header = [0; 8];
            header[..2].copy_from_slice(&headers[block * 4..block * 4 + 2]);
            header[2..6].copy_from_slice(&first.to_le_bytes());
            header[6..].copy_from_slice(&headers[block * 4 + 2..block * 4 + 4]);
            header
        } else {
            self.stream[offset as usize..offset as usize + 8]
                .try_into()
                .unwrap()
        }
    }

    #[inline]
    fn block_payload(&self, block: usize) -> &[u8] {
        let offset = self.read_l0_entry(block).2 as usize;
        let header = if self.compact_headers { 0 } else { 8 };
        &self.stream[offset + header..offset + self.block_len(block)]
    }

    /// Number of values in the term's position stream (0 without cursors).
    pub fn total_positions(&self) -> u64 {
        self.total_positions
    }

    /// Values in the term's position stream before block `block_idx`.
    #[inline]
    pub fn pos_cursor(&self, block_idx: usize) -> Option<u64> {
        let cursors = self.pos_cursors.as_ref()?;
        let size = if self.short_cursors { 4 } else { CURSOR_SIZE };
        let p = block_idx * size;
        cursors.get(p..p + size).map(|b| {
            if self.short_cursors {
                u64::from(u32::from_le_bytes(b.try_into().unwrap()))
            } else {
                u64::from_le_bytes(b.try_into().unwrap())
            }
        })
    }

    pub fn doc_count(&self) -> u32 {
        self.doc_count
    }

    /// Get maximum term frequency (for MaxScore upper bound computation)
    pub fn max_tf(&self) -> u32 {
        self.max_tf
    }

    /// Get number of blocks
    pub fn num_blocks(&self) -> usize {
        self.l0_count
    }

    /// Get block's max term frequency for block-max pruning
    pub fn block_max_tf(&self, block_idx: usize) -> Option<u32> {
        self.block_bounds(block_idx).map(|(max_tf, _)| max_tf)
    }

    /// Concatenate blocks from multiple posting lists with doc_id remapping.
    /// This is O(num_blocks) instead of O(num_postings).
    pub fn concatenate_blocks(sources: &[(BlockPostingList, u32)]) -> io::Result<Self> {
        // Admission precedes all output allocation/copying; typed sources have
        // already validated payloads, but their requested rebasing is new.
        let mut previous_last = None;
        let mut total_docs = 0u32;
        let mut total_positions = 0u64;
        for (source, offset) in sources {
            validation::validate_remap(
                &source.l0_bytes,
                source.l0_count,
                *offset,
                &mut previous_last,
            )?;
            total_docs = total_docs.checked_add(source.doc_count).ok_or_else(|| {
                io::Error::new(io::ErrorKind::InvalidData, "merged posting count overflow")
            })?;
            total_positions = total_positions
                .checked_add(source.total_positions)
                .ok_or_else(|| {
                    io::Error::new(
                        io::ErrorKind::InvalidData,
                        "merged position cursor overflow",
                    )
                })?;
        }
        let mut stream: Vec<u8> = Vec::new();
        let mut l0_buf: Vec<u8> = Vec::new();
        let mut l1_docs: Vec<u32> = Vec::new();
        let mut l0_count = 0usize;
        let mut ratios = sources
            .iter()
            .any(|(s, _)| s.has_ratio_bounds())
            .then(Vec::new);
        let mut impacts = if sources.iter().any(|(s, _)| s.has_impact_bounds()) {
            let blocks = sources
                .iter()
                .try_fold(0usize, |n, (s, _)| n.checked_add(s.num_blocks()))
                .ok_or_else(|| {
                    io::Error::new(io::ErrorKind::InvalidData, "impact block count overflow")
                })?;
            Some(ImpactBuilder::with_groups(blocks)?)
        } else {
            None
        };
        let mut max_tf = 0u32;
        let all_cursors = sources.iter().all(|(s, _)| s.has_position_cursors());
        if !all_cursors && sources.iter().any(|(s, _)| s.has_position_cursors()) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "cannot concatenate posting lists with and without position cursors",
            ));
        }
        let mut cursors: Vec<u8> = Vec::new();
        let mut positions_before = 0u64;
        let mut min_len = u32::MAX;

        for (source, doc_offset) in sources {
            max_tf = max_tf.max(source.max_tf);
            min_len = min_len.min(source.min_len().unwrap_or(1));
            for block_idx in 0..source.num_blocks() {
                if let Some(impacts) = &mut impacts {
                    impacts.append(
                        source
                            .impacts
                            .as_ref()
                            .and_then(|t| t.record(block_idx))
                            .unwrap_or(&[]),
                    )?;
                }
                if let Some(ratios) = &mut ratios {
                    ratios.extend_from_slice(&source.block_length_ratio(block_idx).to_le_bytes());
                }
                if all_cursors {
                    let cursor = source.pos_cursor(block_idx).unwrap_or(0) + positions_before;
                    cursors.extend_from_slice(&cursor.to_le_bytes());
                }
                let (first_doc, last_doc, _, word) = source.read_l0_entry(block_idx);
                let (block_max_tf, block_min_len) = unpack_bounds(word, source.len_bounds);
                let bounds = pack_bounds(block_max_tf, block_min_len.unwrap_or(1));
                let header = source.block_header(block_idx);
                let count = u16::from_le_bytes(header[..2].try_into().unwrap());
                if stream.len() > u32::MAX as usize {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "posting list stream exceeds u32::MAX bytes during concatenation",
                    ));
                }
                let new_offset = stream.len() as u32;

                // Write patched header + copy packed arrays verbatim
                stream.write_u16::<LittleEndian>(count)?;
                stream.write_u32::<LittleEndian>(first_doc + doc_offset)?;
                stream.extend_from_slice(&header[6..]);
                stream.extend_from_slice(source.block_payload(block_idx));

                let new_last = last_doc + doc_offset;
                write_l0(
                    &mut l0_buf,
                    first_doc + doc_offset,
                    new_last,
                    new_offset,
                    bounds,
                );
                l0_count += 1;

                if l0_count.is_multiple_of(L1_INTERVAL) {
                    l1_docs.push(new_last);
                }
            }
            positions_before += source.total_positions;
        }

        // Final L1 entry for partial group
        if !l0_count.is_multiple_of(L1_INTERVAL) && l0_count > 0 {
            let (_, last_doc, _, _) = read_l0(&l0_buf, l0_count - 1);
            l1_docs.push(last_doc);
        }
        let l1_bounds = group_bounds_from_l0(&l0_buf, l0_count);
        if let Some(ratios) = &mut ratios {
            append_ratio_groups(ratios, l0_count);
        }

        Ok(Self {
            compact_headers: false,
            short_cursors: false,
            verify_content: true,
            content_error: None,
            stream: OwnedBytes::new(stream),
            l0_bytes: OwnedBytes::new(l0_buf),
            l0_count,
            l1_docs: l1_docs.into(),
            l1_bounds: l1_bounds.into(),
            ratios: ratios.map(OwnedBytes::new),
            impacts: if let Some(mut impacts) = impacts {
                let mut source = 0;
                let mut base = 0;
                impacts.append_groups_with(l0_count, |range| {
                    while base + sources[source].0.num_blocks() <= range.start {
                        base += sources[source].0.num_blocks();
                        source += 1;
                    }
                    let list = &sources[source].0;
                    let local = range.start - base;
                    let record = if local.is_multiple_of(L1_INTERVAL)
                        && range.end - base == (local + L1_INTERVAL).min(list.num_blocks())
                    {
                        list.impacts
                            .as_ref()
                            .and_then(|t| t.record(list.l0_count + local / L1_INTERVAL))
                    } else {
                        None
                    };
                    Ok(record)
                })?;
                impacts.finish()
            } else {
                None
            },
            doc_count: total_docs,
            max_tf,
            pos_cursors: all_cursors.then(|| OwnedBytes::new(cursors)),
            total_positions: if all_cursors { total_positions } else { 0 },
            len_bounds: true,
            min_len: if min_len == u32::MAX { 1 } else { min_len },
        })
    }

    /// Streaming merge: write blocks directly to output writer (bounded memory).
    ///
    /// **Zero-materializing**: reads L0 entries directly from source bytes
    /// (mmap or &[u8]) without parsing into Vecs. Block sizes come from the
    /// L0 offsets, so blocks of any codec are copied verbatim.
    ///
    /// Output L0 + L1 are buffered (bounded O(total_blocks × 16 + total_blocks/8 × 4)).
    /// Block data flows source → output writer without intermediate buffering.
    ///
    /// Returns `(doc_count, bytes_written)`.
    ///
    /// Preflights all source headers/directories and remapped document ranges
    /// before writing. Corrupt sources, overlapping ranges, or arithmetic
    /// overflow return `Error::Corruption`, including the single-source copy path.
    pub fn concatenate_streaming<W: Write>(
        sources: &[(&[u8], u32)], // (serialized_bytes, doc_offset)
        writer: &mut W,
    ) -> crate::Result<(u32, usize)> {
        let mut metas: Vec<Footer> = Vec::with_capacity(sources.len());
        let mut total_docs = 0u32;
        let mut merged_max_tf = 0u32;
        let mut merged_min_len = u32::MAX;
        let mut previous_last = None;
        let mut total_positions = 0u64;

        for (source_index, (raw, offset)) in sources.iter().enumerate() {
            let invalid_source = |error: io::Error| {
                crate::Error::Corruption(format!(
                    "posting list source {source_index} is invalid: {error}"
                ))
            };
            // The zero-offset copy shortcut also needs validated payloads.
            // This checks headers/directories; it never decodes postings.
            let footer = Self::validate_bytes(raw).map_err(invalid_source)?;
            validation::validate_remap(
                &raw[footer.l0_start()..footer.l0_end()],
                footer.l0_count,
                *offset,
                &mut previous_last,
            )
            .map_err(invalid_source)?;
            total_docs = total_docs
                .checked_add(footer.doc_count)
                .ok_or_else(|| crate::Error::Corruption("merged posting count overflow".into()))?;
            total_positions = total_positions
                .checked_add(footer.total_positions)
                .ok_or_else(|| {
                    crate::Error::Corruption("merged position cursor overflow".into())
                })?;
            merged_max_tf = merged_max_tf.max(footer.max_tf);
            merged_min_len = merged_min_len.min(if footer.len_bounds { footer.min_len } else { 1 });
            metas.push(footer);
        }

        // The common single-source term in the first segment needs no doc-id
        // rebasing and already has a valid index/footer. Copy it wholesale.
        if sources.len() == 1 && sources[0].1 == 0 {
            writer.write_all(sources[0].0)?;
            return Ok((metas[0].doc_count, sources[0].0.len()));
        }

        let compact_output = !metas.is_empty() && metas.iter().all(|meta| meta.compact_headers);
        let short_cursors = compact_output && total_positions <= u32::MAX as u64;
        let mut out_headers = Vec::new();
        let all_cursors = metas.iter().all(|m| m.has_cursors);
        if !all_cursors && metas.iter().any(|m| m.has_cursors) {
            return Err(crate::Error::Corruption(
                "cannot concatenate posting lists with and without position cursors".into(),
            ));
        }

        // Phase 1: Stream block data, reading L0 entries on-the-fly.
        // Accumulate output L0 + L1 + cursors (bounded).
        let mut out_impacts = if metas.iter().any(|m| m.impact_bounds) {
            let blocks = metas
                .iter()
                .try_fold(0usize, |n, m| n.checked_add(m.l0_count))
                .ok_or_else(|| crate::Error::Corruption("impact block count overflow".into()))?;
            Some(ImpactBuilder::with_groups(blocks)?)
        } else {
            None
        };
        let mut out_ratios = metas.iter().any(|m| m.ratio_bounds).then(Vec::new);
        let mut out_l0: Vec<u8> = Vec::new();
        let mut out_l1_docs: Vec<u32> = Vec::new();
        let mut out_cursors: Vec<u8> = Vec::new();
        let mut positions_before = 0u64;
        let mut out_l0_count = 0usize;
        let mut stream_written = 0u64;
        let mut patch_buf = [0u8; 8];

        for (src_idx, meta) in metas.iter().enumerate() {
            let (raw, doc_offset) = &sources[src_idx];
            let l0_base = meta.l0_start(); // L0 entries start right after stream
            let src_stream = &raw[..meta.stream_len];
            let cursors_base = meta.l1_bounds_end();

            for i in 0..meta.l0_count {
                if let Some(impacts) = &mut out_impacts {
                    let record = if meta.impact_bounds {
                        ImpactTable::record_from_validated(
                            &raw[meta.ratios_end()..raw.len() - FOOTER_V2_SIZE],
                            meta.impact_record_count(),
                            i,
                        )
                    } else {
                        &[]
                    };
                    impacts.append(record)?;
                }
                if let Some(ratios) = &mut out_ratios {
                    let ratio = if meta.ratio_bounds {
                        read_ratio(&raw[meta.cursors_end()..], i)
                    } else {
                        0.0
                    };
                    ratios.extend_from_slice(&ratio.to_le_bytes());
                }
                // Read source L0 entry directly from raw bytes
                let (first_doc, last_doc, offset, word) = read_l0(&raw[l0_base..], i);
                let (block_max_tf, block_min_len) = unpack_bounds(word, meta.len_bounds);
                let bounds = pack_bounds(block_max_tf, block_min_len.unwrap_or(1));
                if all_cursors {
                    let size = meta.cursor_size();
                    let p = cursors_base + i * size;
                    let cursor = if meta.short_cursors {
                        u64::from(u32::from_le_bytes(raw[p..p + size].try_into().unwrap()))
                    } else {
                        u64::from_le_bytes(raw[p..p + size].try_into().unwrap())
                    };
                    if short_cursors {
                        out_cursors
                            .extend_from_slice(&((cursor + positions_before) as u32).to_le_bytes());
                    } else {
                        out_cursors.extend_from_slice(&(cursor + positions_before).to_le_bytes());
                    }
                }

                // Block size from the neighbouring L0 offset (codec-independent)
                let blk_size =
                    block_len_from_l0(&raw[l0_base..], meta.l0_count, meta.stream_len, i);
                let block = &src_stream[offset as usize..offset as usize + blk_size];

                // Write output L0 entry
                let new_last = last_doc + doc_offset;
                if stream_written > u32::MAX as u64 {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "posting list stream exceeds u32::MAX bytes during streaming merge",
                    )
                    .into());
                }
                write_l0(
                    &mut out_l0,
                    first_doc + doc_offset,
                    new_last,
                    stream_written as u32,
                    bounds,
                );
                out_l0_count += 1;

                // L1 entry at group boundary
                if out_l0_count.is_multiple_of(L1_INTERVAL) {
                    out_l1_docs.push(new_last);
                }

                // Patch 8-byte header: [count: u16][first_doc: u32][bits: 2 bytes]
                let payload = if meta.compact_headers {
                    let header = &raw[meta.l0_end() + i * 4..meta.l0_end() + i * 4 + 4];
                    patch_buf[..2].copy_from_slice(&header[..2]);
                    patch_buf[6..].copy_from_slice(&header[2..]);
                    block
                } else {
                    patch_buf.copy_from_slice(&block[..8]);
                    &block[8..]
                };
                patch_buf[2..6].copy_from_slice(&(first_doc + doc_offset).to_le_bytes());
                if compact_output {
                    out_headers.extend_from_slice(&patch_buf[..2]);
                    out_headers.extend_from_slice(&patch_buf[6..]);
                } else {
                    writer.write_all(&patch_buf)?;
                }
                writer.write_all(payload)?;
                stream_written += (if compact_output { 0 } else { 8 } + payload.len()) as u64;
            }
            positions_before += meta.total_positions;
        }

        // Final L1 entry for partial group
        if !out_l0_count.is_multiple_of(L1_INTERVAL) && out_l0_count > 0 {
            let (_, last_doc, _, _) = read_l0(&out_l0, out_l0_count - 1);
            out_l1_docs.push(last_doc);
        }

        // Phase 2: Write L0 + L1 + L1 bounds + cursors + footer
        let out_l1_bounds = group_bounds_from_l0(&out_l0, out_l0_count);
        writer.write_all(&out_l0)?;
        writer.write_all(&out_headers)?;
        for &doc in &out_l1_docs {
            writer.write_u32::<LittleEndian>(doc)?;
        }
        for &bounds in &out_l1_bounds {
            writer.write_u32::<LittleEndian>(bounds)?;
        }
        writer.write_all(&out_cursors)?;
        if let Some(ratios) = &mut out_ratios {
            append_ratio_groups(ratios, out_l0_count);
            writer.write_all(ratios)?;
        }
        let out_impacts = if let Some(mut impacts) = out_impacts {
            let mut source = 0;
            let mut base = 0;
            impacts.append_groups_with(out_l0_count, |range| {
                while base + metas[source].l0_count <= range.start {
                    base += metas[source].l0_count;
                    source += 1;
                }
                let meta = &metas[source];
                let local = range.start - base;
                let record = if meta.group_impact_bounds
                    && local.is_multiple_of(L1_INTERVAL)
                    && range.end - base == (local + L1_INTERVAL).min(meta.l0_count)
                {
                    let raw = sources[source].0;
                    Some(ImpactTable::record_from_validated(
                        &raw[meta.ratios_end()..raw.len() - FOOTER_V2_SIZE],
                        meta.impact_record_count(),
                        meta.l0_count + local / L1_INTERVAL,
                    ))
                } else {
                    None
                };
                Ok(record)
            })?;
            impacts.finish()
        } else {
            None
        };
        if let Some(impacts) = &out_impacts {
            writer.write_all(impacts.bytes())?;
        }
        Self::write_footer(
            writer,
            stream_written,
            out_l0_count,
            out_l1_docs.len(),
            total_docs,
            merged_max_tf,
            if all_cursors { total_positions } else { 0 },
            all_cursors,
            Some(if merged_min_len == u32::MAX {
                1
            } else {
                merged_min_len
            }),
            true,
            out_ratios.is_some(),
            out_impacts.is_some(),
            out_impacts.is_some(),
            if compact_output {
                FLAG_COMPACT_HEADERS
            } else {
                0
            } | if short_cursors && all_cursors {
                FLAG_SHORT_CURSORS
            } else {
                0
            },
        )?;

        let l1_bytes_len = out_l1_docs.len() * L1_SIZE + out_l1_bounds.len() * 4;
        let total_bytes = stream_written as usize
            + out_l0.len()
            + out_headers.len()
            + l1_bytes_len
            + out_cursors.len()
            + out_ratios.as_ref().map_or(0, Vec::len)
            + out_impacts.as_ref().map_or(0, |t| t.bytes().len())
            + FOOTER_V2_SIZE;
        Ok((total_docs, total_bytes))
    }

    /// Decode a specific block into caller-provided buffers.
    ///
    /// Returns `true` if the block was decoded, `false` if `block_idx` is out of range.
    /// Reuses `doc_ids` and `tfs` buffers (cleared before filling).
    ///
    /// Uses SIMD-accelerated unpack for 8/16/32-bit packed arrays.
    pub fn decode_block_into(
        &self,
        block_idx: usize,
        doc_ids: &mut Vec<u32>,
        tfs: &mut Vec<u32>,
    ) -> bool {
        if let Some((offset, tf_start, count)) = self.decode_block_doc_ids_only(block_idx, doc_ids)
        {
            self.decode_block_tfs_deferred(offset, tf_start, count, tfs);
            true
        } else {
            false
        }
    }

    /// Decode only doc IDs from a block (no TF decoding).
    ///
    /// Returns `(block_data_offset, tf_start_within_block, count)` for deferred TF decode,
    /// or `None` if block_idx is out of range. A block whose decoded ids
    /// disagree with the L0 directory (content corruption that structural
    /// admission cannot see) also yields `None`, after an error log naming
    /// the block; callers treat it as the end of the list.
    pub fn decode_block_doc_ids_only(
        &self,
        block_idx: usize,
        doc_ids: &mut Vec<u32>,
    ) -> Option<(usize, usize, usize)> {
        match self.decode_block_doc_ids_checked(block_idx, doc_ids) {
            Ok(state) => state,
            Err(error) => {
                log::error!(
                    "posting block {block_idx} of {} is corrupt; the cursor ends here: {error}",
                    self.l0_count
                );
                None
            }
        }
    }

    /// `Ok(None)` is out of range; `Err` is a block whose payload does not
    /// match its directory entry. On error `doc_ids` is left empty.
    fn decode_block_doc_ids_checked(
        &self,
        block_idx: usize,
        doc_ids: &mut Vec<u32>,
    ) -> io::Result<Option<(usize, usize, usize)>> {
        if block_idx >= self.l0_count {
            return Ok(None);
        }
        let decoded = (|| {
            let (first, last, offset, _) = self.read_l0_entry(block_idx);
            let header = self.block_header(block_idx);
            let payload = self.block_payload(block_idx);
            let state = self.decode_block_doc_ids_unchecked(
                offset as usize,
                block_idx,
                header,
                payload,
                doc_ids,
            )?;
            if self.verify_content {
                // Header 8 denotes Rounded with 8-bit raw gaps (no codec tag).
                let byte_gaps = (header[6] == 8).then(|| &payload[..doc_ids.len() - 1]);
                if header[6] >> PostingCodec::HEADER_SHIFT == PostingCodec::Simd4x as u8
                    && doc_ids.len() == BLOCK_SIZE
                    && !bitpacking4x::first_gap_is_zero(
                        payload,
                        header[6] & PostingCodec::WIDTH_MASK,
                    )
                {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "SIMD posting first gap must be zero",
                    ));
                }
                let strict_gap_width = (header[6] >> PostingCodec::HEADER_SHIFT
                    == PostingCodec::Simd4x as u8)
                    .then_some(header[6] & PostingCodec::WIDTH_MASK);
                if !verify_block_docs(doc_ids, first, last, byte_gaps, strict_gap_width) {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!("decoded doc ids leave the directory range {first}..={last}"),
                    ));
                }
            }
            crate::observe::search_work!(
                doc_blocks += 1,
                doc_values += doc_ids.len(),
                doc_payload_bytes += state.1 - if self.compact_headers { 0 } else { 8 }
            );
            Ok(Some(state))
        })();
        if decoded.is_err() {
            doc_ids.clear();
            if let Some(error) = &self.content_error {
                // Write-once: no query may erase another query's failure.
                error.record(block_idx);
            }
        }
        decoded
    }

    fn decode_block_doc_ids_unchecked(
        &self,
        pos: usize,
        block_idx: usize,
        header: [u8; 8],
        payload: &[u8],
        doc_ids: &mut Vec<u32>,
    ) -> io::Result<(usize, usize, usize)> {
        let invalid = |message: &str| io::Error::new(io::ErrorKind::InvalidData, message);
        let count = u16::from_le_bytes(header[..2].try_into().unwrap()) as usize;
        let first_doc = u32::from_le_bytes(header[2..6].try_into().unwrap());
        let (codec, doc_width) = PostingCodec::from_header_byte(header[6])?;
        let header_len = if self.compact_headers { 0 } else { 8 };
        let state = if self.compact_headers { block_idx } else { pos };
        if count == 0 || count > BLOCK_SIZE {
            return Err(invalid("invalid posting block count"));
        }

        // Every decoder overwrites the complete output; retain initialized
        // storage so equal-sized blocks do not need a redundant zero pass.
        doc_ids.resize(count, 0);
        doc_ids[0] = first_doc;

        if codec == PostingCodec::Simd4x {
            let values = if count == BLOCK_SIZE {
                count
            } else {
                count - 1
            };
            let bytes = bitpacking4x::encoded_len(values, doc_width);
            bitpacking4x::decode_docs(&payload[..bytes], doc_width, first_doc, doc_ids);
            return Ok((state, header_len + bytes, count));
        }
        let deltas_bytes = if count > 1 {
            match codec {
                PostingCodec::Rounded => {
                    let rounded = simd::RoundedBitWidth::try_from_u8(doc_width)
                        .ok_or_else(|| invalid("invalid rounded posting width"))?;
                    let bytes = (count - 1) * rounded.bytes_per_value();
                    simd::unpack_rounded_raw_delta_decode(
                        &payload[..bytes],
                        rounded,
                        doc_ids,
                        first_doc,
                        count,
                    );
                    return Ok((state, header_len + bytes, count));
                }
                PostingCodec::Packed => {
                    let bytes = packed_bytes(count - 1, doc_width);
                    unpack_bits(&payload[..bytes], doc_width, &mut doc_ids[1..], count - 1);
                    bytes
                }
                PostingCodec::Pfor => {
                    let bytes = pfor_payload_len(payload, count - 1, doc_width)?;
                    unpack_pfor(&payload[..bytes], doc_width, &mut doc_ids[1..], count - 1)?;
                    bytes
                }
                PostingCodec::Simd4x => unreachable!("SIMD documents were decoded above"),
            }
        } else {
            0
        };
        for i in 1..count {
            doc_ids[i] = doc_ids[i].wrapping_add(doc_ids[i - 1]);
        }

        let tfs_start = header_len + deltas_bytes;
        Ok((state, tfs_start, count))
    }

    /// Decode TFs from a previously loaded block (deferred decode).
    ///
    /// `block_offset` and `tf_start` are returned by `decode_block_doc_ids_only`.
    pub fn decode_block_tfs_deferred(
        &self,
        block_offset: usize,
        tf_start: usize,
        count: usize,
        tfs: &mut Vec<u32>,
    ) {
        tfs.resize(count, 0);
        self.decode_block_tfs_slice(block_offset, tf_start, tfs);
    }

    /// Shared frequency decoder for vector and fixed-block consumers.
    fn decode_block_tfs_slice(&self, block_offset: usize, tf_start: usize, tfs: &mut [u32]) {
        let count = tfs.len();
        let (header, block_data) = if self.compact_headers {
            (
                self.block_header(block_offset),
                self.block_payload(block_offset),
            )
        } else {
            (
                self.stream[block_offset..block_offset + 8]
                    .try_into()
                    .unwrap(),
                &self.stream[block_offset..],
            )
        };
        let (codec, _) = PostingCodec::from_header_byte(header[6]).expect("admitted posting codec");
        let tf_bits = header[7];
        let payload = &block_data[tf_start..];
        crate::observe::search_work!(
            tf_blocks += 1,
            tf_values += count,
            tf_payload_bytes += match codec {
                PostingCodec::Pfor =>
                    pfor_payload_len(payload, count, tf_bits).expect("admitted frequency payload"),
                _ => packed_bytes(count, tf_bits),
            }
        );
        match codec {
            PostingCodec::Simd4x => {
                bitpacking4x::decode(&payload[..packed_bytes(count, tf_bits)], tf_bits, tfs);
            }
            PostingCodec::Rounded => {
                let rounded = simd::RoundedBitWidth::try_from_u8(tf_bits)
                    .expect("invalid rounded posting frequency width");
                simd::unpack_rounded(
                    &payload[..count * rounded.bytes_per_value()],
                    rounded,
                    tfs,
                    count,
                );
            }
            PostingCodec::Packed => {
                unpack_bits(
                    &payload[..packed_bytes(count, tf_bits)],
                    tf_bits,
                    tfs,
                    count,
                );
            }
            PostingCodec::Pfor => {
                let len = pfor_payload_len(payload, count, tf_bits)
                    .expect("invalid patched posting frequency payload");
                unpack_pfor(&payload[..len], tf_bits, tfs, count)
                    .expect("invalid patched posting frequency table");
            }
        }
    }

    /// Byte length of block `block_idx` (from the L0 offsets).
    #[inline]
    fn block_len(&self, block_idx: usize) -> usize {
        block_len_from_l0(&self.l0_bytes, self.l0_count, self.stream.len(), block_idx)
    }

    /// Codec of block `block_idx` (diagnostics).
    pub fn block_codec(&self, block_idx: usize) -> Option<PostingCodec> {
        if block_idx >= self.l0_count {
            return None;
        }
        PostingCodec::from_header_byte(self.block_header(block_idx)[6])
            .ok()
            .map(|(codec, _)| codec)
    }

    /// First doc_id of a block (from L0 skip entry). Returns `None` if out of range.
    #[inline]
    pub fn block_first_doc(&self, block_idx: usize) -> Option<DocId> {
        if block_idx >= self.l0_count {
            return None;
        }
        let (first_doc, _, _, _) = self.read_l0_entry(block_idx);
        Some(first_doc)
    }

    /// Last doc_id of a block (from L0 skip entry). Returns `None` if out of range.
    #[inline]
    pub fn block_last_doc(&self, block_idx: usize) -> Option<DocId> {
        if block_idx >= self.l0_count {
            return None;
        }
        let (_, last_doc, _, _) = self.read_l0_entry(block_idx);
        Some(last_doc)
    }

    /// Find the first block whose `last_doc >= target`, starting from `from_block`.
    ///
    /// Checks the current block first, then gallops over the L1 group ends
    /// before a bounded L0 search. Near seeks are constant time; distant
    /// seeks inspect logarithmically many groups rather than scanning the gap.
    ///
    /// Returns `None` if no block contains `target`.
    pub fn seek_block(&self, target: DocId, from_block: usize) -> Option<usize> {
        if from_block >= self.l0_count {
            return None;
        }

        if self
            .block_last_doc(from_block)
            .is_some_and(|last| last >= target)
        {
            return Some(from_block);
        }
        let from_l1 = from_block / L1_INTERVAL;
        let groups = self.l1_docs.words().get(from_l1..)?;
        let first = u32::from_le_bytes(*groups.first()?);
        let offset = if first >= target {
            0
        } else {
            let mut bound = 1usize;
            while bound < groups.len() && u32::from_le_bytes(groups[bound]) < target {
                bound = bound.saturating_mul(2);
            }
            let lo = bound / 2;
            let hi = bound.saturating_add(1).min(groups.len());
            lo + groups[lo..hi].partition_point(|&last| u32::from_le_bytes(last) < target)
        };
        let l1_idx = from_l1 + offset;
        if l1_idx >= self.l1_docs.len() {
            return None;
        }

        // Search the validated entries in place instead of gathering every
        // strided last ID in the group before finding the lower bound.
        let start = (l1_idx * L1_INTERVAL).max(from_block + 1);
        let end = ((l1_idx + 1) * L1_INTERVAL).min(self.l0_count);
        let entries = self.l0_bytes.as_slice().as_chunks::<L0_SIZE>().0;
        let within = entries[start..end].partition_point(|entry| {
            u32::from_le_bytes([entry[4], entry[5], entry[6], entry[7]]) < target
        });
        let block_idx = start + within;

        if block_idx < self.l0_count {
            Some(block_idx)
        } else {
            None
        }
    }

    /// Create an iterator with skip support
    pub fn iterator(&self) -> BlockPostingIterator<'_> {
        BlockPostingIterator::new(self)
    }

    /// Create an owned iterator that doesn't borrow self
    pub fn into_iterator(self) -> BlockPostingIterator<'static> {
        BlockPostingIterator::owned(self)
    }

    /// Point probes start at their first requested block and reuse decode
    /// storage. Ordinary iteration still starts at the first posting.
    pub(crate) fn into_candidate_iterator(
        self,
        first_target: DocId,
        scratch: &mut PostingDecodeScratch,
    ) -> BlockPostingIterator<'static> {
        let first_block = self.seek_block(first_target, 0);
        let PostingDecodeScratch {
            doc_ids,
            term_freqs,
        } = std::mem::take(scratch);
        let mut block_tfs = term_freqs.unwrap_or_default();
        block_tfs.take();
        let mut iterator = BlockPostingIterator {
            block_list: std::borrow::Cow::Owned(self),
            current_block: first_block.unwrap_or(0),
            block_doc_ids: doc_ids,
            block_tfs,
            tf_state: (0, 0, 0),
            position_in_block: 0,
            position_offsets: None,
            position_offsets_ready: false,
            block_position_cursor: 0,
            exhausted: first_block.is_none(),
        };
        if let Some(block) = first_block {
            iterator.load_block(block);
        }
        iterator
    }
}

#[derive(Default)]
pub(crate) struct PostingDecodeScratch {
    doc_ids: Vec<u32>,
    term_freqs: Option<std::sync::OnceLock<[u32; BLOCK_SIZE]>>,
}

/// Iterator over block posting list with skip support
/// Can be either borrowed or owned via Cow
///
/// Document IDs and frequencies have separate bounded decode buffers. Document
/// movement does not initialize frequencies; score and position consumers do.
/// Only the current block is retained, including during scratch recycling.
pub struct BlockPostingIterator<'a> {
    block_list: std::borrow::Cow<'a, BlockPostingList>,
    current_block: usize,
    block_doc_ids: Vec<u32>,
    /// Fixed inline scratch, reused across blocks and initialized only by
    /// frequency/position consumers. OnceLock preserves immutable frequency
    /// access and the iterator's native Send+Sync contract; keeping it inline
    /// avoids a heap allocation per cursor per query.
    block_tfs: std::sync::OnceLock<[u32; BLOCK_SIZE]>,
    tf_state: (usize, usize, usize),
    position_in_block: usize,
    /// Lazily computed absolute position offsets, including the one-past-end
    /// offset. One bounded block (1 KiB) replaces per-document prefix reductions.
    position_offsets: Option<Box<[u64; BLOCK_SIZE + 1]>>,
    position_offsets_ready: bool,
    block_position_cursor: u64,
    exhausted: bool,
}

impl<'a> BlockPostingIterator<'a> {
    pub(crate) fn recycle(self, scratch: &mut PostingDecodeScratch) {
        *scratch = PostingDecodeScratch {
            doc_ids: self.block_doc_ids,
            term_freqs: Some(self.block_tfs),
        };
    }

    fn new(block_list: &'a BlockPostingList) -> Self {
        let exhausted = block_list.l0_count == 0;
        let mut iter = Self {
            block_list: std::borrow::Cow::Borrowed(block_list),
            current_block: 0,
            block_doc_ids: Vec::with_capacity(BLOCK_SIZE),
            block_tfs: std::sync::OnceLock::new(),
            tf_state: (0, 0, 0),
            position_in_block: 0,
            position_offsets: None,
            position_offsets_ready: false,
            block_position_cursor: 0,
            exhausted,
        };
        if !iter.exhausted {
            iter.load_block(0);
        }
        iter
    }

    fn owned(block_list: BlockPostingList) -> BlockPostingIterator<'static> {
        let exhausted = block_list.l0_count == 0;
        let mut iter = BlockPostingIterator {
            block_list: std::borrow::Cow::Owned(block_list),
            current_block: 0,
            block_doc_ids: Vec::with_capacity(BLOCK_SIZE),
            block_tfs: std::sync::OnceLock::new(),
            tf_state: (0, 0, 0),
            position_in_block: 0,
            position_offsets: None,
            position_offsets_ready: false,
            block_position_cursor: 0,
            exhausted,
        };
        if !iter.exhausted {
            iter.load_block(0);
        }
        iter
    }

    fn load_block(&mut self, block_idx: usize) {
        if block_idx >= self.block_list.l0_count {
            self.exhausted = true;
            return;
        }

        self.current_block = block_idx;
        self.position_in_block = 0;
        self.position_offsets_ready = false;
        self.block_position_cursor = self.block_list.pos_cursor(block_idx).unwrap_or(0);

        self.block_tfs.take();
        match self
            .block_list
            .decode_block_doc_ids_checked(block_idx, &mut self.block_doc_ids)
        {
            Ok(Some(state)) => self.tf_state = state,
            Ok(None) => unreachable!("block index was range-checked above"),
            Err(error) => {
                // The cursor API is infallible: end the list here rather than
                // expose ids outside the directory, and say so.
                log::error!(
                    "posting block {block_idx} of {} is corrupt; the cursor ends here: {error}",
                    self.block_list.l0_count
                );
                self.block_doc_ids.clear();
                self.tf_state = (0, 0, 0);
                self.exhausted = true;
            }
        }
    }

    fn frequencies(&self) -> &[u32] {
        let (offset, start, count) = self.tf_state;
        if count == 0 {
            return &[];
        }
        &self.block_tfs.get_or_init(|| {
            let mut tfs = [0; BLOCK_SIZE];
            self.block_list
                .decode_block_tfs_slice(offset, start, &mut tfs[..count]);
            tfs
        })[..count]
    }

    /// Offset of the current posting's positions in the term's position
    /// stream (see `structures::postings::positions_v2`): the block's cursor
    /// plus the term frequencies of the postings before it in the block.
    /// Meaningful only for lists built with position cursors.
    #[inline]
    pub fn position_cursor(&self) -> u64 {
        if self.position_offsets_ready {
            self.position_offsets.as_ref().unwrap()[self.position_in_block]
        } else {
            self.block_position_cursor
                + self.frequencies()[..self.position_in_block]
                    .iter()
                    .map(|&tf| u64::from(tf))
                    .sum::<u64>()
        }
    }

    pub(crate) fn position_cursor_mut(&mut self) -> u64 {
        if !self.position_offsets_ready {
            self.initialize_position_offsets();
        }
        self.position_offsets.as_ref().unwrap()[self.position_in_block]
    }

    /// Address and length of the current posting's positions. Prefixes are
    /// prepared once per block; document-only iteration does not initialize them.
    #[inline]
    pub(crate) fn position_range(&mut self) -> (u64, u32) {
        let cursor = self.position_cursor_mut();
        (cursor, self.frequencies()[self.position_in_block])
    }

    fn initialize_position_offsets(&mut self) {
        self.frequencies();
        let offsets = self
            .position_offsets
            .get_or_insert_with(|| Box::new([0; BLOCK_SIZE + 1]));
        let mut total = self.block_position_cursor;
        offsets[0] = total;
        if let Some(frequencies) = self.block_tfs.get() {
            for (offset, &tf) in offsets[1..].iter_mut().zip(&frequencies[..self.tf_state.2]) {
                total += u64::from(tf);
                *offset = total;
            }
        }
        self.position_offsets_ready = true;
    }

    pub fn doc(&self) -> DocId {
        if self.exhausted {
            TERMINATED
        } else if self.position_in_block < self.block_doc_ids.len() {
            self.block_doc_ids[self.position_in_block]
        } else {
            TERMINATED
        }
    }

    pub fn term_freq(&self) -> u32 {
        if self.exhausted || self.position_in_block >= self.block_doc_ids.len() {
            0
        } else {
            self.frequencies()[self.position_in_block]
        }
    }

    pub fn advance(&mut self) -> DocId {
        if self.exhausted {
            return TERMINATED;
        }

        self.position_in_block += 1;
        if self.position_in_block >= self.block_doc_ids.len() {
            self.load_block(self.current_block + 1);
        }
        self.doc()
    }

    #[inline]
    pub fn seek(&mut self, target: DocId) -> DocId {
        crate::observe::search_work!(posting_seeks += 1);
        if self.exhausted {
            return TERMINATED;
        }
        let current = self.block_doc_ids[self.position_in_block];
        if target <= current {
            return current;
        }
        if target > *self.block_doc_ids.last().unwrap() {
            return self.seek_later_block(target);
        }
        // Nearby intersection probes often need just one step. Distant probes
        // use logarithmic comparisons instead of scanning the decoded gap.
        let next = self.position_in_block + 1;
        self.position_in_block = if self.block_doc_ids[next] >= target {
            next
        } else {
            let remaining = &self.block_doc_ids[next + 1..];
            let mut bound = 1;
            while bound < remaining.len() && remaining[bound] < target {
                bound *= 2;
            }
            let lo = bound / 2;
            let hi = (bound + 1).min(remaining.len());
            next + 1 + lo + remaining[lo..hi].partition_point(|&doc| doc < target)
        };
        self.block_doc_ids[self.position_in_block]
    }

    /// Select a posting from a bounded decoded suffix, leaving it parked for
    /// ordinary frequency/position reads. None yields after consuming a block.
    pub(crate) fn find_in_block(
        &mut self,
        mut find: impl FnMut(&[u32], &[u32]) -> Option<usize>,
    ) -> Option<DocId> {
        if self.exhausted {
            return Some(TERMINATED);
        }
        let start = self.position_in_block;
        if let Some(offset) = find(&self.block_doc_ids[start..], &self.frequencies()[start..]) {
            self.position_in_block += offset;
            return Some(self.block_doc_ids[self.position_in_block]);
        }
        self.load_block(self.current_block + 1);
        None
    }

    /// Reposition a physical probe without discarding its bounded decode buffers.
    /// Logical document order can move backwards through an RGB permutation.
    pub(crate) fn seek_physical(&mut self, target: DocId) -> DocId {
        if !self.exhausted && target >= self.doc() {
            return self.seek(target);
        }
        let Some(block) = self.block_list.seek_block(target, 0) else {
            self.exhausted = true;
            return TERMINATED;
        };
        self.exhausted = false;
        if block != self.current_block || self.block_doc_ids.is_empty() {
            self.load_block(block);
        }
        if self.exhausted {
            return TERMINATED;
        }
        self.position_in_block = self.block_doc_ids.partition_point(|&doc| doc < target);
        self.doc()
    }

    fn seek_later_block(&mut self, target: DocId) -> DocId {
        let Some(block_idx) = self.block_list.seek_block(target, self.current_block + 1) else {
            self.exhausted = true;
            return TERMINATED;
        };
        self.load_block(block_idx);
        if self.exhausted {
            return TERMINATED;
        }
        // Verified content: the block's last id is at least `target`.
        self.position_in_block = self.block_doc_ids.partition_point(|&doc| doc < target);
        self.block_doc_ids[self.position_in_block]
    }

    /// Copy a bounded prefix without decoding frequencies or positions.
    pub(crate) fn fill_doc_batch(&mut self, docs: &mut [DocId]) -> usize {
        self.fill_batch::<false>(docs, &mut [])
    }

    pub(crate) fn fill_scored_doc_batch(&mut self, docs: &mut [DocId], tfs: &mut [u32]) -> usize {
        assert!(tfs.len() >= docs.len());
        self.fill_batch::<true>(docs, tfs)
    }

    fn fill_batch<const WITH_FREQUENCIES: bool>(
        &mut self,
        docs: &mut [DocId],
        tfs: &mut [u32],
    ) -> usize {
        assert!(docs.len() <= BLOCK_SIZE);
        let mut count = 0;
        while count < docs.len() && !self.exhausted {
            let remaining = &self.block_doc_ids[self.position_in_block..];
            let take = remaining.len().min(docs.len() - count);
            docs[count..count + take].copy_from_slice(&remaining[..take]);
            if WITH_FREQUENCIES {
                tfs[count..count + take].copy_from_slice(
                    &self.frequencies()[self.position_in_block..self.position_in_block + take],
                );
            }
            count += take;
            self.position_in_block += take;
            if self.position_in_block == self.block_doc_ids.len() {
                self.load_block(self.current_block + 1);
            }
        }
        count
    }

    /// Probe sorted IDs within decoded blocks, amortizing directory checks.
    pub(crate) fn retain_doc_batch(&mut self, docs: &mut [DocId]) -> usize {
        self.retain_batch::<false>(docs, &mut [])
    }

    pub(crate) fn retain_scored_doc_batch(&mut self, docs: &mut [DocId], tfs: &mut [u32]) -> usize {
        assert!(tfs.len() >= docs.len());
        self.retain_batch::<true>(docs, tfs)
    }

    fn retain_batch<const WITH_FREQUENCIES: bool>(
        &mut self,
        docs: &mut [DocId],
        tfs: &mut [u32],
    ) -> usize {
        assert!(docs.len() <= BLOCK_SIZE);
        let mut input = 0;
        let mut kept = 0;
        while input < docs.len() {
            if self.seek(docs[input]) == TERMINATED {
                break;
            }
            let last = *self.block_doc_ids.last().unwrap();
            while input < docs.len() && docs[input] <= last {
                let doc = docs[input];
                self.position_in_block = simd::find_first_ge_block_from(
                    &self.block_doc_ids,
                    self.position_in_block,
                    doc,
                );
                if self.block_doc_ids[self.position_in_block] == doc {
                    docs[kept] = doc;
                    if WITH_FREQUENCIES {
                        tfs[kept] = self.frequencies()[self.position_in_block];
                    }
                    kept += 1;
                }
                input += 1;
            }
        }
        kept
    }

    /// Consume a bounded ID range into caller-owned membership words. Retain the
    /// term-frequency prefix so subsequent scoring and position reads stay valid.
    pub(crate) fn fill_doc_window(&mut self, base: DocId, bits: &mut [u64]) {
        let span = u32::try_from(bits.len()).unwrap().checked_mul(64).unwrap();
        let end = base.saturating_add(span);
        bits.fill(0);
        self.seek(base);
        let list = self.block_list.as_ref();
        let dense = list.doc_count() >= 16
            && list
                .block_first_doc(0)
                .zip(list.block_last_doc(list.num_blocks().saturating_sub(1)))
                .is_some_and(|(first, last)| {
                    u64::from(last)
                        .checked_sub(u64::from(first))
                        .is_some_and(|span| span < u64::from(list.doc_count()) * 2)
                });
        if dense {
            self.fill_doc_words::<true>(base, end, bits);
        } else {
            self.fill_doc_words::<false>(base, end, bits);
        }
    }

    fn fill_doc_words<const GROUPED: bool>(&mut self, base: DocId, end: DocId, bits: &mut [u64]) {
        self.visit_until::<false>(end, |docs, _| {
            // Decide once per window, outside the hot decoded-run loop.
            if !GROUPED {
                for &doc in docs {
                    let offset = (doc - base) as usize;
                    bits[offset / 64] |= 1u64 << (offset % 64);
                }
                return true;
            }
            let mut index = 0;
            while index < docs.len() {
                let offset = (docs[index] - base) as usize;
                let word_index = offset / 64;
                let mut mask = 1u64 << (offset % 64);
                index += 1;
                while index < docs.len() {
                    let offset = (docs[index] - base) as usize;
                    if offset / 64 != word_index {
                        break;
                    }
                    mask |= 1u64 << (offset % 64);
                    index += 1;
                }
                bits[word_index] |= mask;
            }
            true
        });
    }

    /// Visit already decoded posting runs before `end` (exclusive). Each run
    /// contains at most one block. Returning false leaves that run unconsumed;
    /// callers can check cancellation without changing storage-layer policy.
    /// Preserve the TF prefix so subsequent position reads remain valid.
    pub(crate) fn visit_postings_until(
        &mut self,
        end: DocId,
        mut visit: impl FnMut(&[u32], &[u32]) -> bool,
    ) {
        self.visit_until::<true>(end, |docs, tfs| visit(docs, tfs.unwrap()));
    }

    /// Consume bounded decoded ID runs without initializing frequencies.
    pub(crate) fn visit_doc_ids_until(
        &mut self,
        end: DocId,
        mut visit: impl FnMut(&[u32]) -> bool,
    ) {
        self.visit_until::<false>(end, |docs, _| visit(docs));
    }

    fn visit_until<const WITH_FREQUENCIES: bool>(
        &mut self,
        end: DocId,
        mut visit: impl FnMut(&[u32], Option<&[u32]>) -> bool,
    ) {
        while self.doc() < end {
            let start = self.position_in_block;
            let count = self.block_doc_ids[start..].partition_point(|&doc| doc < end);
            let tfs = WITH_FREQUENCIES.then(|| &self.frequencies()[start..start + count]);
            if !visit(&self.block_doc_ids[start..start + count], tfs) {
                return;
            }
            self.position_in_block += count;
            if self.position_in_block == self.block_doc_ids.len() {
                self.load_block(self.current_block + 1);
            }
        }
    }

    /// Skip to the next block, returning the first doc_id in the new block
    /// This is used for block-max pruning when the current block's
    /// max score can't beat the threshold.
    pub fn skip_to_next_block(&mut self) -> DocId {
        if self.exhausted {
            return TERMINATED;
        }
        self.load_block(self.current_block + 1);
        self.doc()
    }

    /// Get the current block index
    #[inline]
    pub fn current_block_idx(&self) -> usize {
        self.current_block
    }

    /// Get total number of blocks
    #[inline]
    pub fn num_blocks(&self) -> usize {
        self.block_list.l0_count
    }

    /// Borrow immutable metadata for the currently decoded posting block.
    pub(crate) fn current_block_metadata(&self) -> Option<(&BlockPostingList, usize)> {
        (!self.exhausted).then_some((&self.block_list, self.current_block))
    }

    /// Get the current block's max term frequency for block-max pruning
    #[inline]
    pub fn current_block_max_tf(&self) -> u32 {
        if self.exhausted || self.current_block >= self.block_list.l0_count {
            0
        } else {
            self.block_list
                .block_max_tf(self.current_block)
                .unwrap_or(0)
        }
    }
}

/// Bounded intersection scratch for a fixed pair of monotonically advancing
/// posting iterators. Cursor positions stay on a match while its TF and positions
/// are consumed; the SIMD kernel runs once per overlapping block pair.
pub(crate) struct PostingIntersection {
    seek_driven: bool,
    left_is_rare: bool,
    blocks: (usize, usize),
    pairs: [(u8, u8); BLOCK_SIZE],
    next: usize,
    count: usize,
    ends: (usize, usize),
}

impl Default for PostingIntersection {
    fn default() -> Self {
        Self {
            seek_driven: false,
            left_is_rare: true,
            blocks: (usize::MAX, usize::MAX),
            pairs: [(0, 0); BLOCK_SIZE],
            next: 0,
            count: 0,
            ends: (0, 0),
        }
    }
}

impl PostingIntersection {
    pub(crate) fn with_costs(left: u32, right: u32) -> Self {
        Self {
            seek_driven: left.min(right).saturating_mul(4) < left.max(right),
            left_is_rare: left <= right,
            ..Self::default()
        }
    }

    /// Invalidate cached pairs before a physical rewind through a document map.
    pub(crate) fn reset(&mut self) {
        self.blocks = (usize::MAX, usize::MAX);
    }

    /// None yields at a block boundary so the caller can check cancellation.
    pub(crate) fn intersect_block(
        &mut self,
        left: &mut BlockPostingIterator<'_>,
        right: &mut BlockPostingIterator<'_>,
    ) -> Option<DocId> {
        if left.exhausted || right.exhausted {
            return Some(TERMINATED);
        }
        if left.doc() == right.doc() {
            return Some(left.doc());
        }
        if self.seek_driven {
            fn align(
                lead: &mut BlockPostingIterator<'_>,
                other: &mut BlockPostingIterator<'_>,
            ) -> Option<DocId> {
                let candidate = lead.doc();
                let next = other.seek(candidate);
                if next == candidate {
                    return Some(candidate);
                }
                lead.seek(next);
                None
            }
            return if self.left_is_rare {
                align(left, right)
            } else {
                align(right, left)
            };
        }
        if *left.block_doc_ids.last().unwrap() < right.doc() {
            left.seek_later_block(right.doc());
            return None;
        }
        if *right.block_doc_ids.last().unwrap() < left.doc() {
            right.seek_later_block(left.doc());
            return None;
        }
        let blocks = (left.current_block, right.current_block);
        if self.blocks != blocks {
            let mut a = left.position_in_block;
            let mut b = right.position_in_block;
            self.count = simd::intersect_posting_blocks(
                &left.block_doc_ids,
                &mut a,
                &right.block_doc_ids,
                &mut b,
                &mut self.pairs,
            );
            self.blocks = blocks;
            self.ends = (a, b);
            self.next = 0;
        }
        while self.next < self.count {
            let (a, b) = self.pairs[self.next];
            self.next += 1;
            let (a, b) = (usize::from(a), usize::from(b));
            if a >= left.position_in_block && b >= right.position_in_block {
                left.position_in_block = a;
                right.position_in_block = b;
                return Some(left.doc());
            }
        }
        left.position_in_block = left.position_in_block.max(self.ends.0);
        right.position_in_block = right.position_in_block.max(self.ends.1);
        if left.position_in_block == left.block_doc_ids.len() {
            left.load_block(left.current_block + 1);
        }
        if right.position_in_block == right.block_doc_ids.len() {
            right.load_block(right.current_block + 1);
        }
        None
    }
}

#[cfg(test)]
mod compact_layout_tests {
    use super::*;
    #[test]
    fn batched_intersection_preserves_monotone_seeks_frequencies_and_position_offsets() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let make = |divisor| {
                let mut postings = PostingList::new();
                for doc in 0..3001 {
                    if doc % divisor != 1 {
                        postings.push(doc, 1 + doc % 7);
                    }
                }
                BlockPostingList::from_posting_list_with_options(&postings, true, None, codec)
                    .unwrap()
            };
            let a = make(3);
            let b = make(5);
            for (seek_driven, left_is_rare) in [(false, true), (true, true), (true, false)] {
                let mut left = a.iterator();
                let mut right = b.iterator();
                let mut reference_left = a.iterator();
                let mut reference_right = b.iterator();
                let mut intersection = PostingIntersection {
                    seek_driven,
                    left_is_rare,
                    ..Default::default()
                };
                let mut target = 0;
                loop {
                    left.seek(target);
                    right.seek(target);
                    let doc = loop {
                        if let Some(doc) = intersection.intersect_block(&mut left, &mut right) {
                            break doc;
                        }
                    };
                    let expected = (target..3001)
                        .find(|doc| doc % 3 != 1 && doc % 5 != 1)
                        .unwrap_or(TERMINATED);
                    assert_eq!(doc, expected);
                    assert_eq!(
                        intersection.intersect_block(&mut left, &mut right),
                        Some(doc)
                    );
                    if doc == TERMINATED {
                        break;
                    }
                    reference_left.seek(doc);
                    reference_right.seek(doc);
                    assert_eq!(left.term_freq(), reference_left.term_freq());
                    assert_eq!(right.term_freq(), reference_right.term_freq());
                    assert_eq!(left.position_cursor(), reference_left.position_cursor());
                    assert_eq!(right.position_cursor(), reference_right.position_cursor());
                    target = doc + if doc % 7 == 0 { 19 } else { 1 };
                }
                for target in [0, 200, 129, 3, 2048, 3000, 2] {
                    intersection.reset();
                    left.seek_physical(target);
                    right.seek_physical(target);
                    let doc = loop {
                        if let Some(doc) = intersection.intersect_block(&mut left, &mut right) {
                            break doc;
                        }
                    };
                    let expected = (target..3001)
                        .find(|doc| doc % 3 != 1 && doc % 5 != 1)
                        .unwrap_or(TERMINATED);
                    assert_eq!(doc, expected);
                    reference_left.seek_physical(doc);
                    reference_right.seek_physical(doc);
                    assert_eq!(left.position_cursor(), reference_left.position_cursor());
                    assert_eq!(right.position_cursor(), reference_right.position_cursor());
                }
            }
        }
    }

    #[test]
    fn compact_postings_preserve_payload_scores_and_copy_merges() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Simd4x,
            PostingCodec::Pfor,
        ] {
            for count in [1, 2, 127, 128, 129, 1025] {
                let mut postings = PostingList::new();
                for doc in 0..count {
                    postings.push(doc * 3, doc % 13);
                }
                let list = BlockPostingList::from_posting_list_with_ratio_bounds(
                    &postings,
                    true,
                    Some(&|doc| doc % 100 + 1),
                    codec,
                )
                .unwrap();
                let mut old = Vec::new();
                list.serialize(&mut old).unwrap();
                let mut bytes = Vec::new();
                list.serialize_compact(&mut bytes).unwrap();
                let compact = BlockPostingList::deserialize(&bytes).unwrap();
                assert_eq!(compact.compact_headers, codec != PostingCodec::Pfor);
                if codec != PostingCodec::Pfor {
                    assert_eq!(old.len() - bytes.len(), list.num_blocks() * 8);
                }
                let mut a = Vec::new();
                let mut b = Vec::new();
                for i in 0..list.num_blocks() {
                    assert_eq!(list.block_payload(i), compact.block_payload(i));
                    assert_eq!(list.pos_cursor(i), compact.pos_cursor(i));
                    assert!(compact.decode_block_into(i, &mut a, &mut b));
                    assert_eq!(
                        a,
                        postings.postings
                            [i * BLOCK_SIZE..(i * BLOCK_SIZE + BLOCK_SIZE).min(count as usize)]
                            .iter()
                            .map(|p| p.doc_id)
                            .collect::<Vec<_>>()
                    );
                    assert_eq!(b, a.iter().map(|doc| (doc / 3) % 13).collect::<Vec<_>>());
                }
                let mut repeated = Vec::new();
                compact.serialize(&mut repeated).unwrap();
                assert_eq!(bytes, repeated);
                for second in [&old, &bytes] {
                    let mut merged = Vec::new();
                    let (docs, len) = BlockPostingList::concatenate_streaming(
                        &[(&bytes, 0), (second, count * 3)],
                        &mut merged,
                    )
                    .unwrap();
                    assert_eq!(len, merged.len());
                    assert_eq!(docs, count * 2);
                    let merged = BlockPostingList::deserialize(&merged).unwrap();
                    for block in 0..merged.num_blocks() {
                        assert_eq!(
                            merged.block_payload(block),
                            list.block_payload(block % list.num_blocks())
                        );
                        assert!(merged.decode_block_into(block, &mut a, &mut b));
                    }
                }
            }
        }
    }

    #[test]
    fn compact_posting_metadata_rejects_bad_descriptors_and_content_checks_remain_observable() {
        let mut postings = PostingList::new();
        for doc in 0..257 {
            postings.push(doc * 2, 1);
        }
        let list = BlockPostingList::from_posting_list_with_options(
            &postings,
            true,
            None,
            PostingCodec::Rounded,
        )
        .unwrap();
        let mut bytes = Vec::new();
        list.serialize_compact(&mut bytes).unwrap();
        let footer = Footer::parse(&bytes).unwrap();
        for (at, value) in [
            (footer.l0_end(), 0),
            (footer.l0_end() + 1, 1),
            (footer.l0_end() + 2, 33),
            (footer.l0_end() + 3, 33),
            (footer.l1_bounds_end(), 1),
        ] {
            let mut corrupt = bytes.clone();
            corrupt[at] = value;
            assert!(
                BlockPostingList::deserialize(&corrupt).is_err(),
                "byte {at}"
            );
        }
        let mut corrupt = bytes.clone();
        corrupt[0] = 0;
        let compact = BlockPostingList::deserialize(&corrupt).unwrap();
        let mut docs = Vec::new();
        assert!(compact.decode_block_doc_ids_checked(0, &mut docs).is_err());
        assert!(docs.is_empty());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn block_decoding_overwrites_stale_values_across_lengths_and_codecs() {
        let mut docs = vec![u32::MAX; BLOCK_SIZE * 2];
        let mut tfs = docs.clone();
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            for count in [128, 1, 127, 128, 17, 256, 257] {
                for freq in [0, 1, 255, 65536] {
                    let mut postings = PostingList::new();
                    for i in 0..count {
                        postings.push(i * 3 + 7, freq);
                    }
                    let list = BlockPostingList::from_posting_list_with_options(
                        &postings, true, None, codec,
                    )
                    .unwrap();
                    for compact in [false, true] {
                        let mut bytes = Vec::new();
                        if compact {
                            list.serialize_compact(&mut bytes).unwrap();
                        } else {
                            list.serialize(&mut bytes).unwrap();
                        }
                        let list = BlockPostingList::deserialize(&bytes).unwrap();
                        for block in (0..list.num_blocks()).rev().chain(0..list.num_blocks()) {
                            docs.fill(u32::MAX);
                            tfs.fill(u32::MAX);
                            assert!(list.decode_block_into(block, &mut docs, &mut tfs));
                            let expected: Vec<_> = postings.postings[block * BLOCK_SIZE
                                ..postings.postings.len().min((block + 1) * BLOCK_SIZE)]
                                .iter()
                                .map(|p| p.doc_id)
                                .collect();
                            assert_eq!(docs, expected);
                            assert_eq!(tfs, vec![freq; expected.len()]);
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn compact_membership_batches_preserve_resume_frequencies_and_position_prefixes() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let mut postings = PostingList::new();
            let mut expected = Vec::new();
            let mut prefix = 0u64;
            for i in 0..701u32 {
                let tf = i % 19 + 1;
                let doc = i * 13 + 5;
                postings.push(doc, tf);
                expected.push((doc, tf, prefix));
                prefix += u64::from(tf);
            }
            let list =
                BlockPostingList::from_posting_list_with_options(&postings, true, None, codec)
                    .unwrap();
            let bytes = serialize_bpl(&list);
            let mut cursor = list.iterator();
            let mut docs = [0; BLOCK_SIZE];
            let mut consumed = 0;
            while cursor.doc() != TERMINATED {
                let count = cursor.fill_doc_batch(&mut docs);
                assert_eq!(
                    &docs[..count],
                    &expected[consumed..consumed + count]
                        .iter()
                        .map(|e| e.0)
                        .collect::<Vec<_>>()
                );
                consumed += count;
                if consumed < expected.len() {
                    assert!(cursor.block_tfs.get().is_none(), "copy initialized TFs");
                    assert_eq!(cursor.doc(), expected[consumed].0);
                    assert_eq!(cursor.term_freq(), expected[consumed].1);
                    assert_eq!(cursor.position_cursor_mut(), expected[consumed].2);
                }
            }
            assert_eq!(consumed, expected.len());
            assert_eq!(cursor.fill_doc_batch(&mut docs), 0);
            let mut cursor = list.iterator();
            for start in (0..10_000u32).step_by(BLOCK_SIZE) {
                let mut candidates: Vec<_> = (start..start + BLOCK_SIZE as u32).collect();
                let wanted: Vec<_> = candidates
                    .iter()
                    .copied()
                    .filter(|doc| expected.iter().any(|e| e.0 == *doc))
                    .collect();
                let kept = cursor.retain_doc_batch(&mut candidates);
                assert_eq!(&candidates[..kept], wanted, "{codec:?} start={start}");
                if cursor.doc() != TERMINATED {
                    let entry = expected.iter().find(|e| e.0 == cursor.doc()).unwrap();
                    assert_eq!(cursor.term_freq(), entry.1);
                    assert_eq!(cursor.position_cursor_mut(), entry.2);
                }
            }
            assert_eq!(cursor.doc(), TERMINATED);
            assert_eq!(serialize_bpl(&list), bytes);
        }
    }

    #[test]
    fn membership_words_preserve_unaligned_dense_sparse_windows_and_resume() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            for stride in [1, 2, 3, 67, 129] {
                let docs: Vec<u32> = (0..10_000).step_by(stride).collect();
                let mut postings = PostingList::new();
                for &doc in &docs {
                    postings.push(doc, 1 + doc % 7);
                }
                let list =
                    BlockPostingList::from_posting_list_with_options(&postings, true, None, codec)
                        .unwrap();
                for start in [0, 1, 17, 63, 64, 127, 511] {
                    let mut cursor = list.iterator();
                    for base in [start, start + 4096, start + 8192] {
                        let mut bits = [u64::MAX; 64];
                        cursor.fill_doc_window(base, &mut bits);
                        let mut expected = [0u64; 64];
                        for &doc in &docs {
                            if (base..base + 4096).contains(&doc) {
                                let offset = (doc - base) as usize;
                                expected[offset / 64] |= 1u64 << (offset % 64);
                            }
                        }
                        assert_eq!(bits, expected, "{codec:?}, stride={stride}, base={base}");
                        let next = docs
                            .iter()
                            .copied()
                            .find(|&doc| doc >= base + 4096)
                            .unwrap_or(TERMINATED);
                        assert_eq!(cursor.doc(), next);
                        if next != TERMINATED {
                            assert_eq!(cursor.term_freq(), 1 + next % 7);
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn document_only_navigation_defers_frequencies_and_resumes_exact_position_reads() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let mut postings = PostingList::new();
            let mut expected = Vec::new();
            let mut prefix = 0u64;
            for i in 0..701u32 {
                let tf = if i % 37 == 0 { u32::MAX } else { i % 23 + 1 };
                let doc = i * 13 + 5;
                postings.push(doc, tf);
                expected.push((doc, tf, prefix));
                prefix += u64::from(tf);
            }
            let list =
                BlockPostingList::from_posting_list_with_options(&postings, true, None, codec)
                    .unwrap();
            let bytes = serialize_bpl(&list);
            for owned in [false, true] {
                let mut cursor = if owned {
                    list.clone().into_iterator()
                } else {
                    list.iterator()
                };
                assert!(
                    cursor.block_tfs.get().is_none(),
                    "{codec:?}: ID-only open decoded frequencies"
                );
                for target in [20, 205, 1800, 3900] {
                    let entry = expected.iter().find(|e| e.0 >= target).unwrap();
                    assert_eq!(cursor.seek(target), entry.0);
                    assert!(
                        cursor.block_tfs.get().is_none(),
                        "ID-only seek decoded frequencies"
                    );
                }
                let entry = expected.iter().find(|e| e.0 == cursor.doc()).unwrap();
                #[cfg(feature = "native")]
                std::thread::scope(|scope| {
                    for _ in 0..4 {
                        let shared = &cursor;
                        scope.spawn(move || {
                            assert_eq!(shared.term_freq(), entry.1);
                            assert_eq!(shared.position_cursor(), entry.2);
                        });
                    }
                });
                assert_eq!(cursor.position_cursor(), entry.2);
                assert_eq!(cursor.term_freq(), entry.1);
                assert_eq!(cursor.position_cursor_mut(), entry.2);
                let mut bits = [0u64; 64];
                cursor.fill_doc_window(4096, &mut bits);
                for &(doc, _, _) in &expected {
                    if (4096..8192).contains(&doc) {
                        assert_ne!(bits[(doc as usize - 4096) / 64] & (1 << (doc % 64)), 0);
                    }
                }
                assert_eq!(
                    bits.iter().map(|word| word.count_ones()).sum::<u32>(),
                    expected
                        .iter()
                        .filter(|e| (4096..8192).contains(&e.0))
                        .count() as u32
                );
                assert!(
                    cursor.block_tfs.get().is_none(),
                    "membership windows decoded frequencies"
                );
                let entry = expected.iter().find(|e| e.0 == cursor.doc()).unwrap();
                assert_eq!(cursor.position_cursor_mut(), entry.2);
                assert_eq!(cursor.term_freq(), entry.1);
                let parked = cursor.doc();
                cursor.visit_postings_until(TERMINATED, |docs, tfs| {
                    for (&doc, &tf) in docs.iter().zip(tfs) {
                        assert_eq!(tf, expected.iter().find(|e| e.0 == doc).unwrap().1);
                    }
                    false
                });
                assert_eq!(cursor.doc(), parked);
                assert_eq!(cursor.position_cursor(), entry.2);
                let mut seen = Vec::new();
                cursor.visit_postings_until(TERMINATED, |docs, tfs| {
                    seen.extend(docs.iter().copied().zip(tfs.iter().copied()));
                    true
                });
                assert_eq!(
                    seen,
                    expected
                        .iter()
                        .filter(|e| e.0 >= parked)
                        .map(|e| (e.0, e.1))
                        .collect::<Vec<_>>()
                );
                assert_eq!(cursor.doc(), TERMINATED);
                assert_eq!(cursor.term_freq(), 0);
                assert_eq!(cursor.seek(0), TERMINATED);
            }
            assert_eq!(serialize_bpl(&list), bytes);
        }
    }

    #[test]
    fn document_navigation_defers_position_accounting_without_changing_cursors() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let mut postings = PostingList::new();
            let mut expected = Vec::new();
            let mut total = 0u64;
            for doc in 0..701u32 {
                expected.push(total);
                let tf = doc % 23 + 1;
                postings.push(doc * 7, tf);
                total += u64::from(tf);
            }
            let list =
                BlockPostingList::from_posting_list_with_options(&postings, true, None, codec)
                    .unwrap();
            let mut cursor = list.iterator();
            for target in [7, 70, 777, 896, 1400, 3500, 4900] {
                assert_eq!(cursor.seek(target), target);
                assert!(
                    cursor.position_offsets.is_none(),
                    "navigation must not prepare unused position offsets"
                );
                assert_eq!(cursor.position_cursor(), expected[target as usize / 7]);
                assert_eq!(cursor.term_freq(), target / 7 % 23 + 1);
                assert_eq!(cursor.seek(target - 1), target);
            }
            assert_eq!(cursor.advance(), TERMINATED);
        }
    }

    #[test]
    fn deferred_position_accounting_resumes_after_reads_windows_and_stopped_runs() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let docs: Vec<_> = (0..600u32).map(|doc| (doc * 11, u32::MAX - doc)).collect();
            let prefixes = expected_cursors(&docs);
            let mut input = PostingList::new();
            for &(doc, tf) in &docs {
                input.push(doc, tf);
            }
            let list = BlockPostingList::build(&input, true, None, codec, false, false).unwrap();
            let before = serialize_bpl(&list);
            let mut cursor = list.iterator();
            for at in [13, 25, 127, 128, 199] {
                cursor.seek(docs[at].0);
                assert_eq!(cursor.position_range(), (prefixes[at], docs[at].1));
                assert_eq!(cursor.position_range(), (prefixes[at], docs[at].1));
                assert_eq!(cursor.position_cursor_mut(), prefixes[at]);
                assert_eq!(cursor.position_cursor_mut(), prefixes[at]);
                assert_eq!(cursor.position_cursor(), prefixes[at]);
                cursor.seek(docs[at].0 - 1);
                assert_eq!(cursor.position_cursor_mut(), prefixes[at]);
            }
            cursor.visit_postings_until(docs[220].0, |_, _| true);
            assert_eq!(cursor.position_cursor_mut(), prefixes[220]);
            cursor.visit_postings_until(docs[250].0, |_, _| false);
            assert_eq!(cursor.position_cursor_mut(), prefixes[220]);
            let mut bits = [0u64; 4];
            cursor.fill_doc_window(docs[220].0, &mut bits);
            let at = docs.partition_point(|&(doc, _)| doc < docs[220].0 + 256);
            assert_eq!(cursor.position_cursor_mut(), prefixes[at]);
            cursor.skip_to_next_block();
            assert_eq!(cursor.position_cursor_mut(), prefixes[256]);
            assert_eq!(serialize_bpl(&list), before);
        }
    }

    #[test]
    fn posting_runs_preserve_frequencies_positions_and_resumption_after_stop() {
        let docs: Vec<_> = (0..1027u32).map(|i| (i * 7, i % 31 + 1)).collect();
        let prefixes = expected_cursors(&docs);
        let mut list = PostingList::new();
        for &(doc, tf) in &docs {
            list.push(doc, tf);
        }
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let postings = BlockPostingList::build(&list, true, None, codec, false, false).unwrap();
            let bytes = serialize_bpl(&postings);
            let mut cursor = postings.iterator();
            cursor.seek(docs[17].0);
            let mut actual = Vec::new();
            let mut calls = 0;
            cursor.visit_postings_until(TERMINATED, |ids, tfs| {
                calls += 1;
                assert_eq!(ids.len(), tfs.len());
                assert!(ids.len() <= BLOCK_SIZE);
                if calls == 3 {
                    return false;
                }
                actual.extend(ids.iter().copied().zip(tfs.iter().copied()));
                true
            });
            assert_eq!(actual, docs[17..256]);
            assert_eq!(cursor.doc(), docs[256].0);
            assert_eq!(cursor.position_cursor(), prefixes[256]);
            cursor.visit_postings_until(docs[331].0, |ids, tfs| {
                actual.extend(ids.iter().copied().zip(tfs.iter().copied()));
                true
            });
            assert_eq!(actual, docs[17..331]);
            assert_eq!(cursor.doc(), docs[331].0);
            assert_eq!(cursor.term_freq(), docs[331].1);
            assert_eq!(cursor.position_cursor(), prefixes[331]);
            cursor.visit_postings_until(TERMINATED, |ids, tfs| {
                actual.extend(ids.iter().copied().zip(tfs.iter().copied()));
                true
            });
            assert_eq!(actual, docs[17..]);
            cursor.visit_postings_until(TERMINATED, |_, _| panic!("already exhausted"));
            assert_eq!(cursor.doc(), TERMINATED);
            assert_eq!(serialize_bpl(&postings), bytes);
        }
    }

    #[test]
    fn document_windows_preserve_posting_frequencies_positions_and_bytes() {
        for near_end in [false, true] {
            let base = if near_end { TERMINATED - 30_000 } else { 0 };
            let docs: Vec<_> = (0..5000u32).map(|i| (base + i * 5, i % 31 + 1)).collect();
            let prefixes = expected_cursors(&docs);
            let mut list = PostingList::new();
            for &(doc, tf) in &docs {
                list.push(doc, tf);
            }
            for codec in [
                PostingCodec::Rounded,
                PostingCodec::Packed,
                PostingCodec::Pfor,
                PostingCodec::Simd4x,
            ] {
                let postings =
                    BlockPostingList::build(&list, true, None, codec, false, false).unwrap();
                let bytes = serialize_bpl(&postings);
                let mut cursor = postings.iterator();
                let mut actual = Vec::new();
                while cursor.doc() != TERMINATED {
                    let base = cursor.doc();
                    let mut bits = [u64::MAX; 64];
                    cursor.fill_doc_window(base, &mut bits);
                    for (index, word) in bits.into_iter().enumerate() {
                        for bit in 0..64 {
                            if word & (1 << bit) != 0 {
                                actual.push(base + index as u32 * 64 + bit);
                            }
                        }
                    }
                    let at = docs.partition_point(|&(doc, _)| doc < base.saturating_add(4096));
                    assert_eq!(cursor.doc(), docs.get(at).map_or(TERMINATED, |p| p.0));
                    if at < docs.len() {
                        assert_eq!(cursor.term_freq(), docs[at].1);
                        assert_eq!(cursor.position_cursor(), prefixes[at]);
                    }
                }
                assert_eq!(actual, docs.iter().map(|p| p.0).collect::<Vec<_>>());
                assert_eq!(serialize_bpl(&postings), bytes);
            }
        }
    }

    #[test]
    fn nearby_and_distant_seeks_preserve_postings_position_cursors_and_bytes() {
        let docs: Vec<_> = (0..32_769u32)
            .map(|i| (i * 5 + i % 3, i % 31 + 1))
            .collect();
        let prefixes = expected_cursors(&docs);
        let mut list = PostingList::new();
        for &(doc, tf) in &docs {
            list.push(doc, tf);
        }
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let postings = BlockPostingList::build(&list, true, None, codec, false, false).unwrap();
            let bytes = serialize_bpl(&postings);
            let lasts: Vec<_> = (0..postings.num_blocks())
                .map(|i| postings.block_last_doc(i).unwrap())
                .collect();
            for from in [0, 1, 7, 8, 64, 200, 256, 257] {
                for target in [0, 1, 100, 639, 640, 641, 700, 160_000, 163_840, TERMINATED] {
                    let expected = (from..lasts.len()).find(|&i| lasts[i] >= target);
                    assert_eq!(
                        postings.seek_block(target, from),
                        expected,
                        "{codec:?} from={from} target={target}"
                    );
                }
            }
            let mut cursor = postings.iterator();
            let mut at = 0;
            for target in [
                0,
                1,
                20,
                19,
                639,
                640,
                641,
                700,
                160_000,
                161_000,
                160_000,
                docs.last().unwrap().0 - 1,
                docs.last().unwrap().0,
                TERMINATED,
                0,
            ] {
                while at < docs.len() && docs[at].0 < target {
                    at += 1;
                }
                let expected = docs.get(at).map_or(TERMINATED, |&(doc, _)| doc);
                assert_eq!(cursor.seek(target), expected, "{codec:?} target={target}");
                if at < docs.len() {
                    assert_eq!(cursor.term_freq(), docs[at].1);
                    assert_eq!(cursor.position_cursor(), prefixes[at]);
                }
            }
            assert_eq!(serialize_bpl(&postings), bytes);
        }
    }

    #[test]
    fn candidate_posting_probes_reuse_buffers_and_preserve_seek_and_position_cursors() {
        let mut list = PostingList::new();
        for i in 0..700 {
            list.push(i * 3, i % 7 + 1);
        }
        let postings = BlockPostingList::from_posting_list(&list).unwrap();
        let mut scratch = PostingDecodeScratch::default();
        let mut doc_buffer = None;
        for first in [0, 385, 900, 1800, 2100, 100, 2097] {
            let mut reference = postings.clone().into_iterator();
            let mut selected = postings
                .clone()
                .into_candidate_iterator(first, &mut scratch);
            assert_eq!(
                selected.current_block_idx(),
                postings.seek_block(first, 0).unwrap_or(0)
            );
            for target in (first..2200).step_by(17) {
                assert_eq!(selected.seek(target), reference.seek(target));
                assert_eq!(selected.term_freq(), reference.term_freq());
                if selected.doc() != TERMINATED {
                    assert_eq!(selected.position_cursor(), reference.position_cursor());
                }
            }
            selected.recycle(&mut scratch);
            assert!(scratch.doc_ids.capacity() >= BLOCK_SIZE);
            // The frequency scratch is inline (no heap allocation to reuse);
            // the document buffer is the one heap allocation and must be.
            assert!(scratch.term_freqs.is_some());
            let address = scratch.doc_ids.as_ptr() as usize;
            if let Some(previous) = doc_buffer {
                assert_eq!(
                    address, previous,
                    "document scratch allocation must be reused"
                );
            }
            doc_buffer = Some(address);
        }
    }

    #[test]
    fn test_posting_list_basic() {
        let mut list = PostingList::new();
        list.push(1, 2);
        list.push(5, 1);
        list.push(10, 3);

        assert_eq!(list.len(), 3);

        let mut iter = PostingListIterator::new(&list);
        assert_eq!(iter.doc(), 1);
        assert_eq!(iter.term_freq(), 2);

        assert_eq!(iter.advance(), 5);
        assert_eq!(iter.term_freq(), 1);

        assert_eq!(iter.advance(), 10);
        assert_eq!(iter.term_freq(), 3);

        assert_eq!(iter.advance(), TERMINATED);
    }

    #[test]
    fn test_posting_list_seek() {
        let mut list = PostingList::new();
        for i in 0..100 {
            list.push(i * 2, 1);
        }

        let mut iter = PostingListIterator::new(&list);

        assert_eq!(iter.seek(50), 50);
        assert_eq!(iter.seek(51), 52);
        assert_eq!(iter.seek(200), TERMINATED);
    }

    #[test]
    fn test_block_posting_list() {
        let mut list = PostingList::new();
        for i in 0..500 {
            list.push(i * 2, (i % 10) + 1);
        }

        let block_list = BlockPostingList::from_posting_list(&list).unwrap();
        assert_eq!(block_list.doc_count(), 500);

        let mut iter = block_list.iterator();
        assert_eq!(iter.doc(), 0);
        assert_eq!(iter.term_freq(), 1);

        // Test seek across blocks
        assert_eq!(iter.seek(500), 500);
        assert_eq!(iter.seek(998), 998);
        assert_eq!(iter.seek(1000), TERMINATED);
    }

    #[test]
    fn test_block_posting_list_serialization() {
        let mut list = PostingList::new();
        for i in 0..300 {
            list.push(i * 3, i + 1);
        }

        let block_list = BlockPostingList::from_posting_list(&list).unwrap();

        let mut buffer = Vec::new();
        block_list.serialize(&mut buffer).unwrap();

        let deserialized = BlockPostingList::deserialize(&buffer[..]).unwrap();
        assert_eq!(deserialized.doc_count(), block_list.doc_count());

        // Verify iteration produces same results
        let mut iter1 = block_list.iterator();
        let mut iter2 = deserialized.iterator();

        while iter1.doc() != TERMINATED {
            assert_eq!(iter1.doc(), iter2.doc());
            assert_eq!(iter1.term_freq(), iter2.term_freq());
            iter1.advance();
            iter2.advance();
        }
        assert_eq!(iter2.doc(), TERMINATED);
    }

    /// Helper: collect all (doc_id, tf) from a BlockPostingIterator
    fn collect_postings(bpl: &BlockPostingList) -> Vec<(u32, u32)> {
        let mut result = Vec::new();
        let mut it = bpl.iterator();
        while it.doc() != TERMINATED {
            result.push((it.doc(), it.term_freq()));
            it.advance();
        }
        result
    }

    /// Helper: build a BlockPostingList from (doc_id, tf) pairs
    #[test]
    fn deserialization_rejects_unsupported_width_instead_of_empty_results() {
        let list = build_bpl(&[(0, 1), (1, 2)]);
        let mut bytes = Vec::new();
        list.serialize(&mut bytes).unwrap();
        bytes[6] = 0xe1;
        let error = BlockPostingList::deserialize(&bytes).unwrap_err();
        assert_eq!(error.kind(), io::ErrorKind::InvalidData);
        assert!(error.to_string().contains("exceeds 32 bits"));
    }

    fn build_bpl(postings: &[(u32, u32)]) -> BlockPostingList {
        let mut pl = PostingList::new();
        for &(doc_id, tf) in postings {
            pl.push(doc_id, tf);
        }
        BlockPostingList::from_posting_list(&pl).unwrap()
    }

    /// Helper: serialize a BlockPostingList to bytes
    fn serialize_bpl(bpl: &BlockPostingList) -> Vec<u8> {
        let mut buf = Vec::new();
        bpl.serialize(&mut buf).unwrap();
        buf
    }

    #[test]
    fn test_concatenate_blocks_two_segments() {
        // Segment A: docs 0,2,4,...,198 (100 docs, tf=1..100)
        let a: Vec<(u32, u32)> = (0..100).map(|i| (i * 2, i + 1)).collect();
        let bpl_a = build_bpl(&a);

        // Segment B: docs 0,3,6,...,297 (100 docs, tf=2..101)
        let b: Vec<(u32, u32)> = (0..100).map(|i| (i * 3, i + 2)).collect();
        let bpl_b = build_bpl(&b);

        // Merge: segment B starts at doc_offset=200
        let merged =
            BlockPostingList::concatenate_blocks(&[(bpl_a.clone(), 0), (bpl_b.clone(), 200)])
                .unwrap();

        assert_eq!(merged.doc_count(), 200);

        let postings = collect_postings(&merged);
        assert_eq!(postings.len(), 200);

        // First 100 from A (unchanged)
        for (i, p) in postings.iter().enumerate().take(100) {
            assert_eq!(*p, (i as u32 * 2, i as u32 + 1));
        }
        // Next 100 from B (doc_id += 200)
        for i in 0..100 {
            assert_eq!(postings[100 + i], (i as u32 * 3 + 200, i as u32 + 2));
        }
    }

    #[test]
    fn test_concatenate_streaming_matches_blocks() {
        // Build 3 segments with different doc distributions
        let seg_a: Vec<(u32, u32)> = (0..250).map(|i| (i * 2, (i % 7) + 1)).collect();
        let seg_b: Vec<(u32, u32)> = (0..180).map(|i| (i * 5, (i % 3) + 1)).collect();
        let seg_c: Vec<(u32, u32)> = (0..90).map(|i| (i * 10, (i % 11) + 1)).collect();

        let bpl_a = build_bpl(&seg_a);
        let bpl_b = build_bpl(&seg_b);
        let bpl_c = build_bpl(&seg_c);

        let offset_b = 1000u32;
        let offset_c = 2000u32;

        // Method 1: concatenate_blocks (in-memory reference)
        let ref_merged = BlockPostingList::concatenate_blocks(&[
            (bpl_a.clone(), 0),
            (bpl_b.clone(), offset_b),
            (bpl_c.clone(), offset_c),
        ])
        .unwrap();
        let mut ref_buf = Vec::new();
        ref_merged.serialize(&mut ref_buf).unwrap();

        // Method 2: concatenate_streaming (footer-based, writes to output)
        let bytes_a = serialize_bpl(&bpl_a);
        let bytes_b = serialize_bpl(&bpl_b);
        let bytes_c = serialize_bpl(&bpl_c);

        let sources: Vec<(&[u8], u32)> =
            vec![(&bytes_a, 0), (&bytes_b, offset_b), (&bytes_c, offset_c)];
        let mut stream_buf = Vec::new();
        let (doc_count, bytes_written) =
            BlockPostingList::concatenate_streaming(&sources, &mut stream_buf).unwrap();

        assert_eq!(doc_count, 520); // 250 + 180 + 90
        assert_eq!(bytes_written, stream_buf.len());

        // Deserialize both and verify identical postings
        let ref_postings = collect_postings(&BlockPostingList::deserialize(&ref_buf).unwrap());
        let stream_postings =
            collect_postings(&BlockPostingList::deserialize(&stream_buf).unwrap());

        assert_eq!(ref_postings.len(), stream_postings.len());
        for (i, (r, s)) in ref_postings.iter().zip(stream_postings.iter()).enumerate() {
            assert_eq!(r, s, "mismatch at posting {}", i);
        }
    }

    #[test]
    fn test_concatenate_streaming_short_source_returns_corruption() {
        // A source shorter than the 24-byte footer (e.g. a corrupt TermInfo
        // (offset, len) pointing at truncated bytes) must fail loudly.
        // Silently skipping it pairs every later source with the wrong
        // metadata (metas[i] vs sources[i]) — panicking or emitting garbage.
        let seg_a: Vec<(u32, u32)> = (0..250).map(|i| (i * 2, (i % 7) + 1)).collect();
        let seg_c: Vec<(u32, u32)> = (0..90).map(|i| (i * 10, (i % 11) + 1)).collect();
        let bytes_a = serialize_bpl(&build_bpl(&seg_a));
        let bytes_c = serialize_bpl(&build_bpl(&seg_c));
        let short = vec![0u8; FOOTER_SIZE - 1]; // corrupt: shorter than footer

        let sources: Vec<(&[u8], u32)> = vec![(&bytes_a, 0), (&short, 1000), (&bytes_c, 2000)];
        let mut out = Vec::new();
        let result = BlockPostingList::concatenate_streaming(&sources, &mut out);
        assert!(
            matches!(result, Err(crate::Error::Corruption(_))),
            "short/corrupt source must be a Corruption error, not silently skipped: {:?}",
            result.map(|r| r.0)
        );
    }

    #[test]
    fn test_multi_round_merge() {
        // Simulate 3 rounds of merging (like tiered merge policy)
        //
        // Round 0: 4 small segments built independently
        // Round 1: merge pairs → 2 medium segments
        // Round 2: merge those → 1 large segment

        let segments: Vec<Vec<(u32, u32)>> = (0..4)
            .map(|seg| (0..200).map(|i| (i * 3, (i + seg * 7) % 10 + 1)).collect())
            .collect();

        let bpls: Vec<BlockPostingList> = segments.iter().map(|s| build_bpl(s)).collect();
        let serialized: Vec<Vec<u8>> = bpls.iter().map(serialize_bpl).collect();

        // Round 1: merge seg0+seg1 (offset=0,600), seg2+seg3 (offset=0,600)
        let mut merged_01 = Vec::new();
        let sources_01: Vec<(&[u8], u32)> = vec![(&serialized[0], 0), (&serialized[1], 600)];
        let (dc_01, _) =
            BlockPostingList::concatenate_streaming(&sources_01, &mut merged_01).unwrap();
        assert_eq!(dc_01, 400);

        let mut merged_23 = Vec::new();
        let sources_23: Vec<(&[u8], u32)> = vec![(&serialized[2], 0), (&serialized[3], 600)];
        let (dc_23, _) =
            BlockPostingList::concatenate_streaming(&sources_23, &mut merged_23).unwrap();
        assert_eq!(dc_23, 400);

        // Round 2: merge the two intermediate results (offset=0, 1200)
        let mut final_merged = Vec::new();
        let sources_final: Vec<(&[u8], u32)> = vec![(&merged_01, 0), (&merged_23, 1200)];
        let (dc_final, _) =
            BlockPostingList::concatenate_streaming(&sources_final, &mut final_merged).unwrap();
        assert_eq!(dc_final, 800);

        // Verify final result has all 800 postings with correct doc_ids
        let final_bpl = BlockPostingList::deserialize(&final_merged).unwrap();
        let postings = collect_postings(&final_bpl);
        assert_eq!(postings.len(), 800);

        // Verify doc_id ordering (must be monotonically non-decreasing within segments,
        // and segment boundaries at 0, 600, 1200, 1800)
        // Seg0: 0..597, Seg1: 600..1197, Seg2: 1200..1797, Seg3: 1800..2397
        assert_eq!(postings[0].0, 0); // first doc of seg0
        assert_eq!(postings[199].0, 597); // last doc of seg0 (199*3)
        assert_eq!(postings[200].0, 600); // first doc of seg1 (0+600)
        assert_eq!(postings[399].0, 1197); // last doc of seg1 (597+600)
        assert_eq!(postings[400].0, 1200); // first doc of seg2
        assert_eq!(postings[799].0, 2397); // last doc of seg3

        // Verify TFs preserved through two rounds of merging
        // Creation formula: tf = (i + seg * 7) % 10 + 1
        for seg in 0u32..4 {
            for i in 0u32..200 {
                let idx = (seg * 200 + i) as usize;
                assert_eq!(
                    postings[idx].1,
                    (i + seg * 7) % 10 + 1,
                    "seg{} tf[{}]",
                    seg,
                    i
                );
            }
        }

        // Verify seek works on final merged result
        let mut it = final_bpl.iterator();
        assert_eq!(it.seek(600), 600);
        assert_eq!(it.seek(1200), 1200);
        assert_eq!(it.seek(2397), 2397);
        assert_eq!(it.seek(2398), TERMINATED);
    }

    #[test]
    fn test_large_scale_merge() {
        // 5 segments × 2000 docs each = 10,000 total docs
        // Each segment has 16 blocks (2000/128 = 15.6 → 16 blocks)
        let num_segments = 5;
        let docs_per_segment = 2000;
        let docs_gap = 3; // doc_ids: 0, 3, 6, ...

        let segments: Vec<Vec<(u32, u32)>> = (0..num_segments)
            .map(|seg| {
                (0..docs_per_segment)
                    .map(|i| (i as u32 * docs_gap, (i as u32 + seg as u32) % 20 + 1))
                    .collect()
            })
            .collect();

        let bpls: Vec<BlockPostingList> = segments.iter().map(|s| build_bpl(s)).collect();

        // Verify each segment has multiple blocks
        for bpl in &bpls {
            assert!(
                bpl.num_blocks() >= 15,
                "expected >=15 blocks, got {}",
                bpl.num_blocks()
            );
        }

        let serialized: Vec<Vec<u8>> = bpls.iter().map(serialize_bpl).collect();

        // Compute offsets: each segment occupies max_doc+1 doc_id space
        let max_doc_per_seg = (docs_per_segment as u32 - 1) * docs_gap;
        let offsets: Vec<u32> = (0..num_segments)
            .map(|i| i as u32 * (max_doc_per_seg + 1))
            .collect();

        let sources: Vec<(&[u8], u32)> = serialized
            .iter()
            .zip(offsets.iter())
            .map(|(b, o)| (b.as_slice(), *o))
            .collect();

        let mut merged = Vec::new();
        let (doc_count, _) =
            BlockPostingList::concatenate_streaming(&sources, &mut merged).unwrap();
        assert_eq!(doc_count, (num_segments * docs_per_segment) as u32);

        // Deserialize and verify
        let merged_bpl = BlockPostingList::deserialize(&merged).unwrap();
        let postings = collect_postings(&merged_bpl);
        assert_eq!(postings.len(), num_segments * docs_per_segment);

        // Verify all doc_ids are strictly monotonically increasing across segment boundaries
        for i in 1..postings.len() {
            assert!(
                postings[i].0 > postings[i - 1].0 || (i % docs_per_segment == 0), // new segment can have lower absolute ID
                "doc_id not increasing at {}: {} vs {}",
                i,
                postings[i - 1].0,
                postings[i].0,
            );
        }

        // Verify seek across all block boundaries
        let mut it = merged_bpl.iterator();
        for (seg, &expected_first) in offsets.iter().enumerate() {
            assert_eq!(
                it.seek(expected_first),
                expected_first,
                "seek to segment {} start",
                seg
            );
        }
    }

    #[test]
    fn test_merge_edge_cases() {
        // Single doc per segment
        let bpl_a = build_bpl(&[(0, 5)]);
        let bpl_b = build_bpl(&[(0, 3)]);

        let merged =
            BlockPostingList::concatenate_blocks(&[(bpl_a.clone(), 0), (bpl_b.clone(), 1)])
                .unwrap();
        assert_eq!(merged.doc_count(), 2);
        let p = collect_postings(&merged);
        assert_eq!(p, vec![(0, 5), (1, 3)]);

        // Exactly BLOCK_SIZE docs (single full block)
        let exact_block: Vec<(u32, u32)> = (0..BLOCK_SIZE as u32).map(|i| (i, i % 5 + 1)).collect();
        let bpl_exact = build_bpl(&exact_block);
        assert_eq!(bpl_exact.num_blocks(), 1);

        let bytes = serialize_bpl(&bpl_exact);
        let mut out = Vec::new();
        let sources: Vec<(&[u8], u32)> = vec![(&bytes, 0), (&bytes, BLOCK_SIZE as u32)];
        let (dc, _) = BlockPostingList::concatenate_streaming(&sources, &mut out).unwrap();
        assert_eq!(dc, BLOCK_SIZE as u32 * 2);

        let merged = BlockPostingList::deserialize(&out).unwrap();
        let postings = collect_postings(&merged);
        assert_eq!(postings.len(), BLOCK_SIZE * 2);
        // Second segment's docs offset by BLOCK_SIZE
        assert_eq!(postings[BLOCK_SIZE].0, BLOCK_SIZE as u32);

        // BLOCK_SIZE + 1 docs (two blocks: 128 + 1)
        let over_block: Vec<(u32, u32)> = (0..BLOCK_SIZE as u32 + 1).map(|i| (i * 2, 1)).collect();
        let bpl_over = build_bpl(&over_block);
        assert_eq!(bpl_over.num_blocks(), 2);
    }

    #[test]
    fn test_streaming_roundtrip_single_source() {
        // Streaming merge with a single source should produce equivalent output to serialize
        let docs: Vec<(u32, u32)> = (0..500).map(|i| (i * 7, i % 15 + 1)).collect();
        let bpl = build_bpl(&docs);
        let direct = serialize_bpl(&bpl);

        let sources: Vec<(&[u8], u32)> = vec![(&direct, 0)];
        let mut streamed = Vec::new();
        BlockPostingList::concatenate_streaming(&sources, &mut streamed).unwrap();

        // Both should deserialize to identical postings
        let p1 = collect_postings(&BlockPostingList::deserialize(&direct).unwrap());
        let p2 = collect_postings(&BlockPostingList::deserialize(&streamed).unwrap());
        assert_eq!(p1, p2);
    }

    #[test]
    fn test_max_tf_preserved_through_merge() {
        // Segment A: max_tf = 50
        let mut a = Vec::new();
        for i in 0..200 {
            a.push((i * 2, if i == 100 { 50 } else { 1 }));
        }
        let bpl_a = build_bpl(&a);
        assert_eq!(bpl_a.max_tf(), 50);

        // Segment B: max_tf = 30
        let mut b = Vec::new();
        for i in 0..200 {
            b.push((i * 2, if i == 50 { 30 } else { 2 }));
        }
        let bpl_b = build_bpl(&b);
        assert_eq!(bpl_b.max_tf(), 30);

        // After merge, max_tf should be max(50, 30) = 50
        let bytes_a = serialize_bpl(&bpl_a);
        let bytes_b = serialize_bpl(&bpl_b);
        let sources: Vec<(&[u8], u32)> = vec![(&bytes_a, 0), (&bytes_b, 1000)];
        let mut out = Vec::new();
        BlockPostingList::concatenate_streaming(&sources, &mut out).unwrap();

        let merged = BlockPostingList::deserialize(&out).unwrap();
        assert_eq!(merged.max_tf(), 50);
        assert_eq!(merged.doc_count(), 400);
    }

    // ── 2-level skip list format tests ──────────────────────────────────

    #[test]
    fn test_l0_l1_counts() {
        // 1 block (< L1_INTERVAL) → 1 L1 entry (partial group)
        let bpl = build_bpl(&(0..50u32).map(|i| (i, 1)).collect::<Vec<_>>());
        assert_eq!(bpl.num_blocks(), 1);
        assert_eq!(bpl.l1_docs.len(), 1);

        // Exactly L1_INTERVAL blocks → 1 L1 entry (full group)
        let n = BLOCK_SIZE * L1_INTERVAL;
        let bpl = build_bpl(&(0..n as u32).map(|i| (i * 2, 1)).collect::<Vec<_>>());
        assert_eq!(bpl.num_blocks(), L1_INTERVAL);
        assert_eq!(bpl.l1_docs.len(), 1);

        // L1_INTERVAL + 1 blocks → 2 L1 entries
        let n = BLOCK_SIZE * L1_INTERVAL + 1;
        let bpl = build_bpl(&(0..n as u32).map(|i| (i * 2, 1)).collect::<Vec<_>>());
        assert_eq!(bpl.num_blocks(), L1_INTERVAL + 1);
        assert_eq!(bpl.l1_docs.len(), 2);

        // 3 × L1_INTERVAL blocks → 3 L1 entries (all full groups)
        let n = BLOCK_SIZE * L1_INTERVAL * 3;
        let bpl = build_bpl(&(0..n as u32).map(|i| (i, 1)).collect::<Vec<_>>());
        assert_eq!(bpl.num_blocks(), L1_INTERVAL * 3);
        assert_eq!(bpl.l1_docs.len(), 3);
    }

    #[test]
    fn test_l1_last_doc_values() {
        // 20 blocks: 2 full L1 groups (8+8) + 1 partial (4) → 3 L1 entries
        let n = BLOCK_SIZE * 20;
        let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 3, 1)).collect();
        let bpl = build_bpl(&docs);
        assert_eq!(bpl.num_blocks(), 20);
        assert_eq!(bpl.l1_docs.len(), 3); // ceil(20/8) = 3

        // L1[0] = last_doc of block 7 (end of first group)
        let expected_l1_0 = bpl.block_last_doc(7).unwrap();
        assert_eq!(bpl.l1_docs.get(0).unwrap(), expected_l1_0);

        // L1[1] = last_doc of block 15 (end of second group)
        let expected_l1_1 = bpl.block_last_doc(15).unwrap();
        assert_eq!(bpl.l1_docs.get(1).unwrap(), expected_l1_1);

        // L1[2] = last_doc of block 19 (end of partial group)
        let expected_l1_2 = bpl.block_last_doc(19).unwrap();
        assert_eq!(bpl.l1_docs.get(2).unwrap(), expected_l1_2);
    }

    #[test]
    fn test_seek_block_basic() {
        // 20 blocks spanning large doc ID range
        let n = BLOCK_SIZE * 20;
        let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 10, 1)).collect();
        let bpl = build_bpl(&docs);

        // Seek to doc 0 → block 0
        assert_eq!(bpl.seek_block(0, 0), Some(0));

        // Seek to the first doc of each block
        for blk in 0..20 {
            let first = bpl.block_first_doc(blk).unwrap();
            assert_eq!(
                bpl.seek_block(first, 0),
                Some(blk),
                "seek to block {} first_doc",
                blk
            );
        }

        // Seek to the last doc of each block
        for blk in 0..20 {
            let last = bpl.block_last_doc(blk).unwrap();
            assert_eq!(
                bpl.seek_block(last, 0),
                Some(blk),
                "seek to block {} last_doc",
                blk
            );
        }

        // Seek past all docs
        let max_doc = bpl.block_last_doc(19).unwrap();
        assert_eq!(bpl.seek_block(max_doc + 1, 0), None);

        // Seek with from_block > 0 (skip early blocks)
        let mid_doc = bpl.block_first_doc(10).unwrap();
        assert_eq!(bpl.seek_block(mid_doc, 10), Some(10));
        assert_eq!(
            bpl.seek_block(mid_doc, 11),
            Some(11).or(bpl.seek_block(mid_doc, 11))
        );
    }

    #[test]
    fn test_seek_block_across_l1_boundaries() {
        // 24 blocks = 3 L1 groups of 8
        let n = BLOCK_SIZE * 24;
        let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 5, 1)).collect();
        let bpl = build_bpl(&docs);
        assert_eq!(bpl.l1_docs.len(), 3);

        // Seek into each L1 group
        for group in 0..3 {
            let blk = group * L1_INTERVAL;
            let target = bpl.block_first_doc(blk).unwrap();
            assert_eq!(
                bpl.seek_block(target, 0),
                Some(blk),
                "seek to group {} block {}",
                group,
                blk
            );
        }

        // Seek to doc in the middle of group 2 (block 20)
        let target = bpl.block_first_doc(20).unwrap() + 1;
        assert_eq!(bpl.seek_block(target, 0), Some(20));
    }

    #[test]
    fn block_len_matches_l0_offsets() {
        // Block lengths derive from neighbouring L0 offsets and add up to the stream.
        let bpl = build_bpl(&(0..1000).map(|i| (i * 3, 1 + i % 4)).collect::<Vec<_>>());
        let mut total = 0usize;
        for b in 0..bpl.num_blocks() {
            let (_, _, offset, _) = bpl.read_l0_entry(b);
            assert_eq!(offset as usize, total, "block {b} offset");
            total += bpl.block_len(b);
        }
        assert_eq!(total, bpl.stream.len());
    }

    /// Every codec round-trips doc ids and tfs exactly, `seek` agrees with
    /// `Rounded`, and `Rounded` output is byte-identical to the historic
    /// layout (codec id 0, widths 0/8/16/32).
    #[test]
    fn every_codec_round_trips_and_seeks() {
        let mut postings: Vec<(u32, u32)> = Vec::new();
        let mut doc = 0u32;
        for i in 0..5000u32 {
            // Mostly small gaps with rare huge ones (forces exceptions / wide
            // blocks), tfs mostly 1-3 with rare outliers.
            doc += if i % 97 == 0 { 100_000 } else { 1 + i % 7 };
            let tf = if i % 131 == 0 { 5000 } else { 1 + i % 3 };
            postings.push((doc, tf));
        }
        let mut list = PostingList::new();
        for &(d, tf) in &postings {
            list.push(d, tf);
        }
        let rounded = BlockPostingList::from_posting_list(&list).unwrap();
        let mut sizes = Vec::new();
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let bpl = BlockPostingList::from_posting_list_with_codec(&list, codec).unwrap();
            assert_eq!(collect_postings(&bpl), postings, "{codec}");
            for b in 0..bpl.num_blocks() {
                let count = (postings.len() - b * BLOCK_SIZE).min(BLOCK_SIZE);
                let expected_codec = if codec == PostingCodec::Simd4x && count < BLOCK_SIZE {
                    PostingCodec::Rounded
                } else {
                    codec
                };
                assert_eq!(bpl.block_codec(b), Some(expected_codec));
                assert_eq!(bpl.block_max_tf(b), rounded.block_max_tf(b));
            }
            // Serialized round trip (both copying and zero-copy paths).
            let bytes = serialize_bpl(&bpl);
            let back = BlockPostingList::deserialize(&bytes).unwrap();
            assert_eq!(collect_postings(&back), postings, "{codec} deserialize");
            let back =
                BlockPostingList::deserialize_zero_copy(OwnedBytes::new(bytes.clone())).unwrap();
            assert_eq!(collect_postings(&back), postings, "{codec} zero-copy");
            // Seeks land on the same docs as the reference layout.
            let mut a = rounded.iterator();
            let mut b = back.iterator();
            for target in (0..postings.last().unwrap().0 + 10).step_by(2_003) {
                assert_eq!(a.seek(target), b.seek(target), "{codec} seek {target}");
                assert_eq!(a.term_freq(), b.term_freq());
            }
            sizes.push((codec, bytes.len()));
        }
        let rounded_bytes = serialize_bpl(&rounded);
        assert_eq!(
            serialize_bpl(
                &BlockPostingList::from_posting_list_with_codec(&list, PostingCodec::Rounded)
                    .unwrap()
            ),
            rounded_bytes,
            "Rounded must stay byte-identical"
        );
        // Header byte of a Rounded block: codec 0, rounded width.
        assert!(matches!(rounded.stream[6], 0 | 8 | 16 | 32));
        let size = |c: PostingCodec| sizes.iter().find(|(k, _)| *k == c).unwrap().1;
        assert!(size(PostingCodec::Packed) < size(PostingCodec::Rounded));
        assert!(size(PostingCodec::Pfor) < size(PostingCodec::Packed));
    }

    /// Blocks of different codecs merge by verbatim copy and decode correctly.
    #[test]
    fn mixed_codec_sources_concatenate() {
        let a: Vec<(u32, u32)> = (0..300u32).map(|i| (i * 5, 1 + i % 9)).collect();
        let b: Vec<(u32, u32)> = (0..300u32).map(|i| (i * 11 + 3, 2 + i % 5)).collect();
        let list_a = {
            let mut l = PostingList::new();
            a.iter().for_each(|&(d, t)| l.push(d, t));
            BlockPostingList::from_posting_list_with_codec(&l, PostingCodec::Pfor).unwrap()
        };
        let list_b = {
            let mut l = PostingList::new();
            b.iter().for_each(|&(d, t)| l.push(d, t));
            BlockPostingList::from_posting_list_with_codec(&l, PostingCodec::Packed).unwrap()
        };
        let offset_b = a.last().unwrap().0 + 1;
        let expected: Vec<(u32, u32)> = a
            .iter()
            .copied()
            .chain(b.iter().map(|&(d, t)| (d + offset_b, t)))
            .collect();

        let merged = BlockPostingList::concatenate_blocks(&[
            (list_a.clone(), 0),
            (list_b.clone(), offset_b),
        ])
        .unwrap();
        assert_eq!(collect_postings(&merged), expected);

        let bytes_a = serialize_bpl(&list_a);
        let bytes_b = serialize_bpl(&list_b);
        let mut out = Vec::new();
        let (docs, written) = BlockPostingList::concatenate_streaming(
            &[(bytes_a.as_slice(), 0), (bytes_b.as_slice(), offset_b)],
            &mut out,
        )
        .unwrap();
        assert_eq!(docs, 600);
        assert_eq!(written, out.len());
        let streamed = BlockPostingList::deserialize(&out).unwrap();
        assert_eq!(collect_postings(&streamed), expected);
        assert_eq!(streamed.block_codec(0), Some(PostingCodec::Pfor));
        assert_eq!(
            streamed.block_codec(streamed.num_blocks() - 1),
            Some(PostingCodec::Packed)
        );
    }

    #[test]
    fn test_l0_entry_roundtrip() {
        // Verify L0 entries survive serialize → deserialize
        let docs: Vec<(u32, u32)> = (0..1000u32).map(|i| (i * 3, (i % 10) + 1)).collect();
        let bpl = build_bpl(&docs);

        let bytes = serialize_bpl(&bpl);
        let bpl2 = BlockPostingList::deserialize(&bytes).unwrap();

        assert_eq!(bpl.num_blocks(), bpl2.num_blocks());
        for blk in 0..bpl.num_blocks() {
            assert_eq!(
                bpl.read_l0_entry(blk),
                bpl2.read_l0_entry(blk),
                "L0 entry mismatch at block {}",
                blk
            );
        }

        // Verify L1 docs match
        assert_eq!(bpl.l1_docs.bytes(), bpl2.l1_docs.bytes());
    }

    #[test]
    fn posting_open_borrows_unaligned_group_directories_and_preserves_bytes_and_seeks() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let mut postings = PostingList::new();
            for i in 0..3457u32 {
                postings.push(i * 7 + 3, i % 13 + 1);
            }
            let built = BlockPostingList::from_posting_list_with_codec(&postings, codec).unwrap();
            let encoded = serialize_bpl(&built);
            for prefix in [1, 3, 7] {
                let mut padded = vec![0; prefix];
                padded.extend_from_slice(&encoded);
                let owner = OwnedBytes::new(padded);
                let raw = owner.slice(prefix..owner.len());
                let footer = Footer::parse(&raw).unwrap();
                let docs_start = raw[footer.l1_start()..].as_ptr();
                let bounds_start = raw[footer.l1_end()..].as_ptr();
                let opened = BlockPostingList::deserialize_zero_copy(raw).unwrap();
                assert_eq!(
                    opened.l1_docs.bytes().as_ptr(),
                    docs_start,
                    "opening must borrow group document metadata"
                );
                assert_eq!(
                    opened.l1_bounds.bytes().as_ptr(),
                    bounds_start,
                    "opening must borrow group bound metadata"
                );
                assert_eq!(serialize_bpl(&opened), encoded);
                for from in 0..=built.num_blocks() {
                    for target in (0..=3457 * 7 + 4).step_by(37) {
                        let expected = (from..built.num_blocks())
                            .find(|&block| built.block_last_doc(block).unwrap() >= target);
                        assert_eq!(opened.seek_block(target, from), expected);
                    }
                }
                assert_eq!(collect_postings(&opened), collect_postings(&built));
            }
        }
    }

    #[test]
    fn test_zero_copy_deserialize_matches() {
        let docs: Vec<(u32, u32)> = (0..2000u32).map(|i| (i * 2, (i % 5) + 1)).collect();
        let bpl = build_bpl(&docs);
        let bytes = serialize_bpl(&bpl);

        let copied = BlockPostingList::deserialize(&bytes).unwrap();
        let zero_copy =
            BlockPostingList::deserialize_zero_copy(OwnedBytes::new(bytes.clone())).unwrap();

        // Same structure
        assert_eq!(copied.l0_count, zero_copy.l0_count);
        assert_eq!(copied.l1_docs.bytes(), zero_copy.l1_docs.bytes());
        assert_eq!(copied.doc_count, zero_copy.doc_count);
        assert_eq!(copied.max_tf, zero_copy.max_tf);

        // Same iteration
        let p1 = collect_postings(&copied);
        let p2 = collect_postings(&zero_copy);
        assert_eq!(p1, p2);
    }

    #[test]
    fn test_l1_preserved_through_streaming_merge() {
        // Merge 3 segments, verify L1 is correctly rebuilt
        let seg_a = build_bpl(&(0..1000u32).map(|i| (i * 2, 1)).collect::<Vec<_>>());
        let seg_b = build_bpl(&(0..800u32).map(|i| (i * 3, 2)).collect::<Vec<_>>());
        let seg_c = build_bpl(&(0..500u32).map(|i| (i * 5, 3)).collect::<Vec<_>>());

        let bytes_a = serialize_bpl(&seg_a);
        let bytes_b = serialize_bpl(&seg_b);
        let bytes_c = serialize_bpl(&seg_c);

        let sources: Vec<(&[u8], u32)> = vec![(&bytes_a, 0), (&bytes_b, 10000), (&bytes_c, 20000)];
        let mut out = Vec::new();
        BlockPostingList::concatenate_streaming(&sources, &mut out).unwrap();

        let merged = BlockPostingList::deserialize(&out).unwrap();
        let expected_l1_count = merged.num_blocks().div_ceil(L1_INTERVAL);
        assert_eq!(merged.l1_docs.len(), expected_l1_count);

        // Verify L1 values are correct
        for (i, word) in merged.l1_docs.words().iter().enumerate() {
            let l1_doc = u32::from_le_bytes(*word);
            let last_block_in_group = ((i + 1) * L1_INTERVAL - 1).min(merged.num_blocks() - 1);
            let expected = merged.block_last_doc(last_block_in_group).unwrap();
            assert_eq!(l1_doc, expected, "L1[{}] mismatch", i);
        }

        // Verify seek_block works on merged result
        for blk in 0..merged.num_blocks() {
            let first = merged.block_first_doc(blk).unwrap();
            assert_eq!(merged.seek_block(first, 0), Some(blk));
        }
    }

    #[test]
    fn test_seek_block_single_block() {
        // Edge case: single block (< L1_INTERVAL)
        let bpl = build_bpl(&[(0, 1), (10, 2), (20, 3)]);
        assert_eq!(bpl.num_blocks(), 1);
        assert_eq!(bpl.l1_docs.len(), 1);

        assert_eq!(bpl.seek_block(0, 0), Some(0));
        assert_eq!(bpl.seek_block(10, 0), Some(0));
        assert_eq!(bpl.seek_block(20, 0), Some(0));
        assert_eq!(bpl.seek_block(21, 0), None);
    }

    #[test]
    fn test_footer_size() {
        // Verify serialized size = stream + L0 + L1 + FOOTER_SIZE
        let docs: Vec<(u32, u32)> = (0..500u32).map(|i| (i * 2, 1)).collect();
        let bpl = build_bpl(&docs);
        let bytes = serialize_bpl(&bpl);

        let expected = bpl.stream.len()
            + bpl.l0_count * L0_SIZE
            + bpl.l1_docs.len() * (L1_SIZE + 4)
            + FOOTER_V2_SIZE;
        assert_eq!(bytes.len(), expected);
    }

    fn build_bpl_with_positions(postings: &[(u32, u32)]) -> BlockPostingList {
        let mut list = PostingList::new();
        for &(doc, tf) in postings {
            list.push(doc, tf);
        }
        BlockPostingList::from_posting_list_with(&list, true, None).unwrap()
    }

    /// Expected cursor of every posting: the cumulative tf before it.
    fn expected_cursors(postings: &[(u32, u32)]) -> Vec<u64> {
        let mut acc = 0u64;
        postings
            .iter()
            .map(|&(_, tf)| {
                let c = acc;
                acc += tf as u64;
                c
            })
            .collect()
    }

    fn iterator_cursors(bpl: &BlockPostingList) -> Vec<u64> {
        let mut it = bpl.iterator();
        let mut out = Vec::new();
        while it.doc() != TERMINATED {
            out.push(it.position_cursor());
            it.advance();
        }
        out
    }

    #[test]
    fn position_cursors_survive_serialization_and_seeks() {
        let docs: Vec<(u32, u32)> = (0..700u32).map(|i| (i * 3, i % 5 + 1)).collect();
        let bpl = build_bpl_with_positions(&docs);
        assert!(bpl.has_position_cursors());
        assert_eq!(
            bpl.total_positions(),
            docs.iter().map(|&(_, tf)| tf as u64).sum::<u64>()
        );
        assert_eq!(bpl.pos_cursor(0), Some(0));
        assert_eq!(
            bpl.pos_cursor(1),
            Some(docs[..128].iter().map(|&(_, tf)| tf as u64).sum::<u64>())
        );
        assert_eq!(iterator_cursors(&bpl), expected_cursors(&docs));

        let bytes = serialize_bpl(&bpl);
        assert_eq!(
            bytes.len(),
            bpl.stream.len()
                + bpl.l0_count * (L0_SIZE + CURSOR_SIZE)
                + bpl.l1_docs.len() * (L1_SIZE + 4)
                + FOOTER_V2_SIZE
        );
        assert!(BlockPostingList::has_cursors_bytes(&bytes));
        let decoded =
            BlockPostingList::deserialize_zero_copy(OwnedBytes::new(bytes.clone())).unwrap();
        assert_eq!(iterator_cursors(&decoded), expected_cursors(&docs));
        assert_eq!(decoded.total_positions(), bpl.total_positions());

        // Seeking within and across blocks keeps the cursor exact.
        let mut it = decoded.iterator();
        let expected = expected_cursors(&docs);
        for (i, &(doc, _)) in docs.iter().enumerate().step_by(37) {
            assert_eq!(it.seek(doc), doc);
            assert_eq!(it.position_cursor(), expected[i], "cursor at doc {doc}");
        }
        let mut it = decoded.iterator();
        assert_eq!(it.seek(docs[600].0 + 1), docs[601].0);
        assert_eq!(it.position_cursor(), expected[601]);

        // Lists without positions carry no cursors (the iterator's prefix
        // sum is then relative to nothing and never consulted).
        let plain = build_bpl(&docs);
        assert!(!plain.has_position_cursors());
        assert_eq!(plain.pos_cursor(0), None);
        assert_eq!(plain.total_positions(), 0);
    }

    #[test]
    fn length_bounds_are_packed_per_block_and_survive_merges() {
        let docs: Vec<(u32, u32)> = (0..300u32).map(|i| (i, i % 3 + 1)).collect();
        let length_of = |doc: u32| 10 + (doc % 50) * 7;
        let mut list = PostingList::new();
        for &(doc, tf) in &docs {
            list.push(doc, tf);
        }
        let bpl = BlockPostingList::from_posting_list_with(&list, true, Some(&length_of)).unwrap();
        assert_eq!(bpl.min_len(), Some(10));
        assert_eq!(bpl.block_bounds(0), Some((3, Some(10))));
        // Block 2 covers docs 256..300: min length there is doc 256 (256 % 50 = 6 → 52).
        assert_eq!(bpl.block_bounds(2), Some((3, Some(52))));
        assert_eq!(bpl.block_max_tf(2), Some(3));

        let bytes = serialize_bpl(&bpl);
        let decoded = BlockPostingList::deserialize(&bytes).unwrap();
        assert_eq!(decoded.min_len(), Some(10));
        assert_eq!(decoded.block_bounds(2), Some((3, Some(52))));
        // Superblock bounds: one group of three blocks here, max tf 3 and
        // the smallest length of the whole list.
        assert_eq!(decoded.group_bounds(0), Some((3, 10)));
        assert_eq!(decoded.group_bounds(2), Some((3, 10)));
        assert_eq!(decoded.group_bounds(3), None);
        assert_eq!(decoded.group_last_doc(1), Some(299));
        assert_eq!(decoded.next_group_block(1), 3);

        // Without lengths the minimum is 1, which every real unit satisfies.
        let plain = build_bpl(&docs);
        assert_eq!(plain.min_len(), Some(1));
        assert_eq!(plain.block_bounds(0), Some((3, Some(1))));

        // Streaming merge keeps per-block bounds and takes the list minimum.
        let mut out = Vec::new();
        BlockPostingList::concatenate_streaming(&[(&bytes, 0), (&bytes, 1000)], &mut out).unwrap();
        let merged = BlockPostingList::deserialize(&out).unwrap();
        assert_eq!(merged.min_len(), Some(10));
        assert_eq!(merged.block_bounds(2), Some((3, Some(52))));
        assert_eq!(merged.block_bounds(3), Some((3, Some(10))));
        assert_eq!(merged.block_max_tf(5), Some(3));
        // Six blocks: one full group of eight would need more; here both
        // lists' blocks share group 0.
        assert_eq!(merged.group_bounds(5), Some((3, 10)));
        assert_eq!(merged.group_last_doc(5), Some(1299));
        assert_eq!(merged.next_group_block(5), 6);
    }

    #[test]
    fn legacy_footer_without_magic_still_deserializes() {
        let docs: Vec<(u32, u32)> = (0..300u32).map(|i| (i * 2, 1 + i % 3)).collect();
        let bpl = build_bpl(&docs);
        let bytes = serialize_bpl(&bpl);
        // A real pre-magic layout has no L1 bounds or footer extension, and
        // stores f32 maxima rather than packed TF/length L0 words.
        let footer = Footer::parse(&bytes).unwrap();
        let mut legacy = bytes[..footer.l1_end()].to_vec();
        for block in 0..bpl.num_blocks() {
            let at = footer.l0_start() + block * L0_SIZE + 12;
            legacy[at..at + 4]
                .copy_from_slice(&(bpl.block_max_tf(block).unwrap() as f32).to_le_bytes());
        }
        legacy.extend_from_slice(
            &bytes[bytes.len() - FOOTER_V2_SIZE..bytes.len() - (FOOTER_V2_SIZE - FOOTER_SIZE)],
        );
        assert!(!BlockPostingList::has_cursors_bytes(&legacy));
        // Legacy lists carry an f32 max tf per block and no lengths.
        let decoded = BlockPostingList::deserialize(&legacy).unwrap();
        assert_eq!(collect_postings(&decoded), docs);
        assert_eq!(decoded.max_tf(), 3);
        assert!(!decoded.has_position_cursors());
        assert_eq!(decoded.min_len(), None);
        assert_eq!(decoded.group_bounds(0), None);
        // And the legacy bytes concatenate into a current-format list.
        let mut out = Vec::new();
        let (count, written) =
            BlockPostingList::concatenate_streaming(&[(&legacy, 0), (&legacy, 1000)], &mut out)
                .unwrap();
        assert_eq!(count, 600);
        assert_eq!(written, out.len());
        let merged = BlockPostingList::deserialize(&out).unwrap();
        assert_eq!(merged.doc_count(), 600);
        assert!(!merged.has_position_cursors());
    }

    #[test]
    fn streaming_merge_rebases_position_cursors() {
        let a: Vec<(u32, u32)> = (0..200u32).map(|i| (i, i % 4 + 1)).collect();
        let b: Vec<(u32, u32)> = (0..150u32).map(|i| (i * 2, 2)).collect();
        let bytes_a = serialize_bpl(&build_bpl_with_positions(&a));
        let bytes_b = serialize_bpl(&build_bpl_with_positions(&b));
        let mut out = Vec::new();
        let (count, written) =
            BlockPostingList::concatenate_streaming(&[(&bytes_a, 0), (&bytes_b, 1000)], &mut out)
                .unwrap();
        assert_eq!(count, 350);
        assert_eq!(written, out.len());
        let merged = BlockPostingList::deserialize(&out).unwrap();
        assert!(merged.has_position_cursors());
        let all: Vec<(u32, u32)> = a
            .iter()
            .copied()
            .chain(b.iter().map(|&(d, tf)| (d + 1000, tf)))
            .collect();
        assert_eq!(collect_postings(&merged), all);
        assert_eq!(iterator_cursors(&merged), expected_cursors(&all));
        assert_eq!(
            merged.total_positions(),
            all.iter().map(|&(_, tf)| tf as u64).sum::<u64>()
        );
        // The in-memory reference agrees.
        let reference = BlockPostingList::concatenate_blocks(&[
            (build_bpl_with_positions(&a), 0),
            (build_bpl_with_positions(&b), 1000),
        ])
        .unwrap();
        assert_eq!(iterator_cursors(&reference), expected_cursors(&all));
        // Mixing lists with and without cursors is refused.
        let plain = serialize_bpl(&build_bpl(&b));
        assert!(
            BlockPostingList::concatenate_streaming(
                &[(&bytes_a, 0), (&plain, 1000)],
                &mut Vec::new()
            )
            .is_err()
        );
    }

    #[test]
    fn test_seek_block_from_block_skips_earlier() {
        // 16 blocks: seek with from_block should skip earlier blocks
        let n = BLOCK_SIZE * 16;
        let docs: Vec<(u32, u32)> = (0..n as u32).map(|i| (i * 3, 1)).collect();
        let bpl = build_bpl(&docs);

        // Target is in block 5, but from_block=8 → should find block >= 8
        let target_in_5 = bpl.block_first_doc(5).unwrap() + 1;
        // from_block=8 means we only look at blocks 8+
        // target_in_5 < last_doc of block 8, so seek_block(target, 8) should return 8
        let result = bpl.seek_block(target_in_5, 8);
        assert!(result.is_some());
        assert!(result.unwrap() >= 8);
    }
    #[test]
    fn saturated_tf_bounds_remain_upper_bounds_without_changing_encoded_bytes() {
        let docs: Vec<_> = (0..(BLOCK_SIZE * 18) as u32)
            .map(|doc| {
                (
                    doc,
                    if doc == (BLOCK_SIZE * 17) as u32 {
                        100_000
                    } else {
                        1
                    },
                )
            })
            .collect();
        let list = build_bpl(&docs);
        let bytes = serialize_bpl(&list);
        assert_eq!(list.max_tf(), 100_000);
        assert!(list.block_bounds(17).unwrap().0 >= 100_000);
        assert!(list.group_bounds(17).unwrap().0 >= 100_000);
        assert_eq!(list.block_bounds(0).unwrap().0, 1);
        let decoded = BlockPostingList::deserialize(&bytes).unwrap();
        assert!(decoded.block_bounds(17).unwrap().0 >= 100_000);
        assert!(decoded.group_bounds(17).unwrap().0 >= 100_000);
        assert_eq!(serialize_bpl(&decoded), bytes);
        assert_eq!(collect_postings(&decoded), docs);
    }
    #[test]
    fn ratio_bounds_preserve_payload_bytes_and_copy_merge_with_legacy_blocks() {
        for codec in [
            PostingCodec::Rounded,
            PostingCodec::Packed,
            PostingCodec::Pfor,
            PostingCodec::Simd4x,
        ] {
            let mut postings = PostingList::new();
            for i in 0..1100 {
                postings.push(i * 3, 1 + i % 97);
            }
            let length = |id| 10 + id % 997;
            let plain = BlockPostingList::from_posting_list_with_options(
                &postings,
                true,
                Some(&length),
                codec,
            )
            .unwrap();
            let tight = BlockPostingList::from_posting_list_with_ratio_bounds(
                &postings,
                true,
                Some(&length),
                codec,
            )
            .unwrap();
            assert_eq!(plain.stream.as_slice(), tight.stream.as_slice());
            assert_eq!(plain.l0_bytes.as_slice(), tight.l0_bytes.as_slice());
            assert_eq!(
                plain.pos_cursors.as_ref().map(OwnedBytes::as_slice),
                tight.pos_cursors.as_ref().map(OwnedBytes::as_slice)
            );
            let mut raw = Vec::new();
            tight.serialize(&mut raw).unwrap();
            let decoded = BlockPostingList::deserialize(&raw).unwrap();
            assert!(decoded.has_ratio_bounds());
            for block in 0..tight.num_blocks() {
                assert!(decoded.block_length_ratio(block) > 0.0);
                for posting in &postings.postings
                    [block * BLOCK_SIZE..((block + 1) * BLOCK_SIZE).min(postings.len())]
                {
                    assert!(
                        decoded.block_length_ratio(block) as f64
                            <= length(posting.doc_id) as f64 / posting.term_freq as f64
                    );
                    assert!(decoded.group_length_ratio(block) <= decoded.block_length_ratio(block));
                }
            }
            let merged = BlockPostingList::concatenate_blocks(&[
                (tight.clone(), 0),
                (plain.clone(), 10_000),
            ])
            .unwrap();
            let mut plain_raw = Vec::new();
            plain.serialize(&mut plain_raw).unwrap();
            let mut streaming = Vec::new();
            let (_, size) = BlockPostingList::concatenate_streaming(
                &[(&raw, 0), (&plain_raw, 10_000)],
                &mut streaming,
            )
            .unwrap();
            let mut expected = Vec::new();
            merged.serialize(&mut expected).unwrap();
            assert_eq!(streaming, expected);
            assert_eq!(streaming.len(), size);
            for block in 0..tight.num_blocks() {
                assert_eq!(
                    merged.block_length_ratio(block),
                    tight.block_length_ratio(block)
                );
                assert_eq!(merged.block_length_ratio(block + tight.num_blocks()), 0.0);
                let mut before = Vec::new();
                let mut after = Vec::new();
                let mut tfs_before = Vec::new();
                let mut tfs_after = Vec::new();
                tight.decode_block_into(block, &mut before, &mut tfs_before);
                merged.decode_block_into(block, &mut after, &mut tfs_after);
                assert_eq!((before, tfs_before), (after, tfs_after));
            }
            // Model an actual list without the optional extension: remove
            // its bytes as well as its flag. Unaddressed trailers are corrupt.
            let footer = Footer::parse(&raw).unwrap();
            raw.drain(footer.cursors_end()..footer.ratios_end());
            let flags = raw.len() - 12;
            raw[flags] &= !(FLAG_RATIO_BOUNDS as u8);
            let legacy_view = BlockPostingList::deserialize(&raw).unwrap();
            assert!(!legacy_view.has_ratio_bounds());
            assert_eq!(legacy_view.stream.as_slice(), tight.stream.as_slice());
        }
    }

    #[test]
    fn ratio_bounds_reject_truncation_nonfinite_negative_and_unknown_flags() {
        let mut postings = PostingList::new();
        postings.push(1, 3);
        let list = BlockPostingList::from_posting_list_with_ratio_bounds(
            &postings,
            false,
            Some(&|_| 7),
            PostingCodec::Rounded,
        )
        .unwrap();
        let mut raw = Vec::new();
        list.serialize(&mut raw).unwrap();
        let footer = Footer::parse(&raw).unwrap();
        for bad in [f32::NAN, f32::INFINITY, -1.0] {
            let mut corrupt = raw.clone();
            corrupt[footer.cursors_end()..footer.cursors_end() + 4]
                .copy_from_slice(&bad.to_le_bytes());
            assert!(BlockPostingList::deserialize(&corrupt).is_err());
            assert!(
                BlockPostingList::concatenate_streaming(&[(&corrupt, 0)], &mut Vec::new()).is_err()
            );
        }
        let mut short = raw.clone();
        short.remove(footer.cursors_end());
        assert!(BlockPostingList::deserialize(&short).is_err());
        let at = raw.len() - 12;
        raw[at] |= 128;
        assert!(BlockPostingList::deserialize(&raw).is_err());
    }

    const ALL_CODECS: [PostingCodec; 4] = [
        PostingCodec::Rounded,
        PostingCodec::Packed,
        PostingCodec::Pfor,
        PostingCodec::Simd4x,
    ];

    /// Byte offset of the first document delta of `block` in the stream.
    fn first_delta_byte(list: &BlockPostingList, block: usize) -> usize {
        let (_, _, offset, _) = list.read_l0_entry(block);
        let header = offset as usize;
        // Pfor arrays start with their exception count.
        header + 8 + usize::from(list.block_codec(block) == Some(PostingCodec::Pfor))
    }

    /// Structurally valid bytes whose block content disagrees with the L0
    /// directory must end the cursor explicitly, never index out of range.
    #[test]
    fn content_corrupt_block_never_panics_or_truncates_silently() {
        for codec in ALL_CODECS {
            // Block 0: docs 0..128 (full); block 1: [200, 300] (a tail, so
            // Simd4x falls back to Rounded there).
            let mut postings = PostingList::new();
            for doc in 0..128 {
                postings.push(doc, doc % 5 + 1);
            }
            postings.push(200, 2);
            postings.push(300, 3);
            let list =
                BlockPostingList::from_posting_list_with_options(&postings, true, None, codec)
                    .unwrap();
            let bytes = serialize_bpl(&list);
            let at = first_delta_byte(&list, 1);
            assert_eq!(
                bytes[at], 100,
                "{codec}: delta 300-200 at the first payload byte"
            );
            let mut corrupt = bytes.clone();
            corrupt[at] = 1; // block 1 now decodes to [200, 201]; L0 still says 200..=300
            let admitted = BlockPostingList::deserialize(&corrupt)
                .unwrap_or_else(|e| panic!("{codec}: structural admission must pass: {e}"));
            // Before the content check this indexed past the decoded block.
            let mut cursor = admitted.iterator();
            assert_eq!(cursor.seek(250), TERMINATED, "{codec}");
            assert_eq!(cursor.term_freq(), 0);
            assert_eq!(cursor.advance(), TERMINATED);
            let mut decoded = Vec::new();
            assert!(
                admitted
                    .decode_block_doc_ids_only(0, &mut decoded)
                    .is_some()
            );
            assert_eq!(decoded.len(), 128);
            assert!(
                admitted
                    .decode_block_doc_ids_only(1, &mut decoded)
                    .is_none(),
                "{codec}: a content-corrupt block must be reported, not decoded"
            );
            assert!(decoded.is_empty(), "no ids escape a corrupt block");
            assert!(!admitted.decode_block_into(1, &mut decoded, &mut Vec::new()));
            // Sequential traversal stops at the corrupt block instead of
            // yielding ids outside the directory range.
            assert_eq!(
                collect_postings(&admitted),
                collect_postings(&list)[..128],
                "{codec}"
            );
            let mut window = admitted.iterator();
            let mut bits = [0u64; 8];
            window.fill_doc_window(0, &mut bits);
            assert_eq!(bits[..2], [u64::MAX, u64::MAX]);
            assert_eq!(window.doc(), TERMINATED);
            // Untouched bytes still decode completely.
            assert_eq!(
                collect_postings(&BlockPostingList::deserialize(&bytes).unwrap()).len(),
                130
            );
        }
        // A full Simd4x block: corrupt one lane word after the reserved zero
        // first gap so the decoded ids drift below the directory's last id.
        let mut postings = PostingList::new();
        for doc in 0..128 {
            postings.push(doc, 1);
        }
        for i in 0..128 {
            postings.push(200 + i * 2, 1);
        }
        let list = BlockPostingList::from_posting_list_with_codec(&postings, PostingCodec::Simd4x)
            .unwrap();
        assert_eq!(list.block_codec(1), Some(PostingCodec::Simd4x));
        let mut bytes = serialize_bpl(&list);
        let at = first_delta_byte(&list, 1) + 1;
        assert_ne!(bytes[at], 0);
        bytes[at] = 0;
        let admitted = BlockPostingList::deserialize(&bytes).unwrap();
        let mut cursor = admitted.iterator();
        assert_eq!(cursor.seek(450), TERMINATED);
        assert!(
            admitted
                .decode_block_doc_ids_only(1, &mut Vec::new())
                .is_none()
        );
        assert_eq!(collect_postings(&admitted).len(), 128);
    }

    /// Single-block list whose tail (count < 128) is encoded with codec id 3,
    /// as the first Simd4x prototype wrote; the builder now emits Rounded
    /// tails but readers keep accepting these bytes.
    fn simd_exact_tail_bytes(docs: &[(u32, u32)]) -> Vec<u8> {
        assert!(!docs.is_empty() && docs.len() < BLOCK_SIZE);
        let count = docs.len();
        let (first, last) = (docs[0].0, docs[count - 1].0);
        let deltas: Vec<u32> = docs.windows(2).map(|w| w[1].0 - w[0].0).collect();
        let tfs: Vec<u32> = docs.iter().map(|d| d.1).collect();
        let max_tf = tfs.iter().copied().max().unwrap();
        let mut stream = Vec::new();
        stream.write_u16::<LittleEndian>(count as u16).unwrap();
        stream.write_u32::<LittleEndian>(first).unwrap();
        let header_at = stream.len();
        stream.extend_from_slice(&[0, 0]);
        let doc_bits = bitpacking4x::encode_gaps(&deltas, &mut stream);
        let tf_bits = bitpacking4x::encode(&tfs, &mut stream);
        stream[header_at] = PostingCodec::Simd4x.header_byte(doc_bits);
        stream[header_at + 1] = tf_bits;
        let mut bytes = stream.clone();
        write_l0(&mut bytes, first, last, 0, pack_bounds(max_tf, 1));
        bytes.extend_from_slice(&last.to_le_bytes());
        bytes.extend_from_slice(&pack_bounds(max_tf, 1).to_le_bytes());
        BlockPostingList::write_footer(
            &mut bytes,
            stream.len() as u64,
            1,
            1,
            count as u32,
            max_tf,
            0,
            false,
            Some(1),
            true,
            false,
            false,
            false,
            0,
        )
        .unwrap();
        bytes
    }

    #[test]
    fn simd4x_exact_tail_blocks_decode_seek_and_copy_through_merge() {
        for count in [1usize, 2, 3, 17, 127] {
            let docs: Vec<(u32, u32)> = (0..count as u32)
                .map(|i| (i * 3 + 7, i % 4 + 1 + (i == 5) as u32 * 900))
                .collect();
            let bytes = simd_exact_tail_bytes(&docs);
            let list = BlockPostingList::deserialize(&bytes).unwrap();
            assert_eq!(
                list.block_codec(0),
                Some(PostingCodec::Simd4x),
                "count={count}"
            );
            assert_eq!(collect_postings(&list), docs, "count={count}");
            let mut cursor = list.iterator();
            for &(doc, tf) in &docs {
                assert_eq!(cursor.seek(doc.saturating_sub(1)), doc);
                assert_eq!(cursor.term_freq(), tf);
            }
            assert_eq!(cursor.seek(docs[count - 1].0 + 1), TERMINATED);
            assert_eq!(serialize_bpl(&list), bytes, "byte-identical round trip");
            // Merges copy the tail verbatim, keeping codec id 3.
            let mut out = Vec::new();
            let (merged_count, written) =
                BlockPostingList::concatenate_streaming(&[(&bytes, 0), (&bytes, 1000)], &mut out)
                    .unwrap();
            assert_eq!((merged_count as usize, written), (2 * count, out.len()));
            let merged = BlockPostingList::deserialize(&out).unwrap();
            assert_eq!(merged.block_codec(1), Some(PostingCodec::Simd4x));
            let expected: Vec<(u32, u32)> = docs
                .iter()
                .copied()
                .chain(docs.iter().map(|&(d, t)| (d + 1000, t)))
                .collect();
            assert_eq!(collect_postings(&merged), expected, "count={count}");
            let typed =
                BlockPostingList::concatenate_blocks(&[(list.clone(), 0), (list.clone(), 1000)])
                    .unwrap();
            assert_eq!(serialize_bpl(&typed), out);
            // Header corruption of the tail is still caught structurally.
            let mut short = bytes.clone();
            short[0] = (count + 1) as u8;
            assert!(BlockPostingList::deserialize(&short).is_err());
        }
    }

    /// A legacy 24-byte footer ending in a `max_tf` equal to the extended
    /// footer magic is ambiguous; it must be rejected, never parsed as the
    /// extended layout.
    #[test]
    fn legacy_footer_whose_max_tf_equals_the_magic_is_rejected_not_misread() {
        for count in [1u32, 2, 130, 700] {
            let mut postings = PostingList::new();
            for i in 0..count {
                postings.push(i * 2, if i == 0 { FOOTER_MAGIC } else { 1 });
            }
            let bpl = BlockPostingList::from_posting_list(&postings).unwrap();
            assert_eq!(bpl.max_tf(), FOOTER_MAGIC);
            let bytes = serialize_bpl(&bpl);
            let footer = Footer::parse(&bytes).unwrap();
            let mut legacy = bytes[..footer.l1_end()].to_vec();
            for block in 0..bpl.num_blocks() {
                let at = footer.l0_start() + block * L0_SIZE + 12;
                legacy[at..at + 4]
                    .copy_from_slice(&(bpl.block_max_tf(block).unwrap() as f32).to_le_bytes());
            }
            legacy.extend_from_slice(
                &bytes[bytes.len() - FOOTER_V2_SIZE..bytes.len() - (FOOTER_V2_SIZE - FOOTER_SIZE)],
            );
            assert_eq!(
                u32::from_le_bytes(legacy[legacy.len() - 4..].try_into().unwrap()),
                FOOTER_MAGIC
            );
            assert!(
                BlockPostingList::deserialize(&legacy).is_err(),
                "count={count}: ambiguous footer must not be read as either layout"
            );
            assert!(!BlockPostingList::has_cursors_bytes(&legacy));
            let mut out = vec![0xab];
            assert!(BlockPostingList::concatenate_streaming(&[(&legacy, 0)], &mut out).is_err());
            assert_eq!(out, [0xab]);
            // The same postings with the extended footer read back exactly.
            assert_eq!(
                BlockPostingList::deserialize(&bytes).unwrap().max_tf(),
                FOOTER_MAGIC
            );
        }
    }

    #[test]
    fn mixed_cursor_and_cursorless_sources_fail_loudly_before_output() {
        let docs: Vec<(u32, u32)> = (0..300u32).map(|i| (i * 2, i % 3 + 1)).collect();
        let with = build_bpl_with_positions(&docs);
        let without = build_bpl(&docs);
        assert!(with.has_position_cursors() && !without.has_position_cursors());
        for sources in [
            [(with.clone(), 0), (without.clone(), 1000)],
            [(without.clone(), 0), (with.clone(), 1000)],
        ] {
            let error = BlockPostingList::concatenate_blocks(&sources).unwrap_err();
            assert!(
                error
                    .to_string()
                    .contains("with and without position cursors"),
                "{error}"
            );
            let bytes: Vec<Vec<u8>> = sources.iter().map(|(s, _)| serialize_bpl(s)).collect();
            let mut out = vec![0xab];
            let result = BlockPostingList::concatenate_streaming(
                &[(&bytes[0], 0), (&bytes[1], 1000)],
                &mut out,
            );
            match result {
                Err(crate::Error::Corruption(message)) => {
                    assert!(
                        message.contains("with and without position cursors"),
                        "{message}"
                    )
                }
                other => panic!("expected a loud corruption error, got {other:?}"),
            }
            assert_eq!(out, [0xab], "nothing is written for a rejected source set");
        }
    }
}

#[cfg(test)]
#[path = "posting/merge_admission_tests.rs"]
mod merge_admission_tests;

#[cfg(test)]
mod byte_gap_validation_tests {
    use super::*;

    #[test]
    fn strict_gap_validation_agrees_with_full_scan_at_width_and_wrap_boundaries() {
        for count in 1..=BLOCK_SIZE {
            for width in 0..=32 {
                let mask = if width == 0 {
                    0
                } else {
                    u32::MAX >> (32 - width)
                };
                for first in [0u32, 17, u32::MAX / 2, u32::MAX - 1] {
                    for pattern in 0..3 {
                        let mut docs = vec![first];
                        for i in 1..count {
                            let encoded = match pattern {
                                0 => mask,
                                1 => 0,
                                _ => (i as u32).wrapping_mul(0x9e3779b9) & mask,
                            };
                            docs.push(docs.last().unwrap().wrapping_add(encoded).wrapping_add(1));
                        }
                        let end = *docs.last().unwrap();
                        for last in [end, end.wrapping_add(1), end.wrapping_sub(1)] {
                            assert_eq!(
                                verify_block_docs(&docs, first, last, None, Some(width)),
                                verify_block_docs(&docs, first, last, None, None),
                                "count={count}, width={width}, first={first}, pattern={pattern}, last={last}"
                            );
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn byte_gap_validation_agrees_with_decoded_order_for_tails_and_unsigned_wraps() {
        for count in 1..=BLOCK_SIZE {
            for first in [0u32, 17, u32::MAX - 400, u32::MAX - 1] {
                for pattern in 0..5 {
                    let gaps: Vec<u8> = (1..count)
                        .map(|i| match pattern {
                            0 => 1,
                            1 => 255,
                            2 => {
                                if i == count / 2 {
                                    0
                                } else {
                                    1
                                }
                            }
                            3 => (i * 71) as u8,
                            _ => ((i * 71) as u8).max(1),
                        })
                        .collect();
                    let mut docs = vec![first];
                    for &gap in &gaps {
                        docs.push(docs.last().unwrap().wrapping_add(u32::from(gap)));
                    }
                    let end = *docs.last().unwrap();
                    for last in [end, end.wrapping_add(1), end.wrapping_sub(1)] {
                        assert_eq!(
                            verify_block_docs(&docs, first, last, Some(&gaps), None),
                            verify_block_docs(&docs, first, last, None, None),
                            "count={count}, first={first}, pattern={pattern}, last={last}"
                        );
                    }
                }
            }
        }
    }
}