infino 0.5.5

A fast retrieval engine that stores data on object storage and runs SQL, full-text search, and vector search over it from a single system — search-on-Parquet.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Infino Authors

//! Vector blob reader. Multi-column kNN search via IVF + 1-bit RaBitQ
//! shortlist + full-precision rerank.
//!
//! Opens the unified-blob byte layout produced by
//! [`super::builder::VectorBuilder::finish`] and exposes per-column
//! kNN search.
//!
//! Self-contained: owns its `Bytes`. Per-column metadata is parsed
//! eagerly at `open()`; per-query work happens on demand.

use std::{
    collections::HashMap,
    fmt,
    ops::Range,
    sync::{
        Arc, Mutex, OnceLock, PoisonError,
        atomic::{AtomicBool, Ordering as AtomicOrdering},
    },
    thread,
};

use bytes::Bytes;
use futures::{StreamExt, TryStreamExt, future::try_join_all, stream};
use rayon::{ThreadPool, prelude::*};
use roaring::RoaringBitmap;
use serde::Deserialize;
use tokio::sync::oneshot;

pub(crate) use crate::superfile::lazy_source::Source;
use crate::{
    memory::{ConnectionMemoryBudget, Reservation},
    runtime_bridge::{run_on_pool, spawn_on},
    runtime_metrics::{
        cpu::{thread_cpu_delta_ns, thread_cpu_ns},
        op_stats::{metering_active, timed_section},
    },
    storage::io_counters,
    superfile::{
        BuildError, ReadError,
        error::VectorError,
        format::{
            checksum::crc32c,
            vec::{
                CELL_DIR_ENTRY_SIZE, CLUSTER_IDX_COUNT_OFFSET, CLUSTER_IDX_ENTRY_BYTES,
                MAGIC_BYTES, U32_BYTES, U64_BYTES, cell_dir_entry, dir_entry, outer_hdr, sub_hdr,
            },
            {self},
        },
        lazy_source::{LazyByteSource, LazyByteSourceError, PrefetchedSource, RangeCoalescePlan},
        vector::{
            cell_posting::{EncodedCellRow, MaterializedIvfRow},
            distance::{
                Metric, Sq8ResidualKernel, Sq16Kernel, decode_f32_le_into, distance_bytes_codec,
                nearest_k_centroids_bytes, sum_f32,
            },
            ivf_merge::Sq8IvfMergeInput,
            quant::{
                BitQuantizer, LUT_BLOCK_ROWS, LutQuery, build_transposed_code_cache,
                for_each_code_block_scores, lut_scan_supported,
            },
            rerank_codec::{EncodedRowParts, RerankCodec, SQ8_FIXED_OFFSET, SQ8_FIXED_SCALE},
            rotation::RandomRotation,
        },
    },
};

/// Bytes per fp32 lane in the on-disk centroid region.
const F32_BYTES: usize = 4;
const OUTER_HEADER_SIZE: usize = format::vec::OUTER_HEADER_SIZE;
const DOC_ID_BYTES: usize = format::vec::DOC_ID_BYTES;
const STABLE_ID_BYTES: usize = format::vec::STABLE_ID_BYTES;
const DIR_ENTRY_SIZE: usize = format::vec::DIR_ENTRY_SIZE;
const SUB_HEADER_SIZE: usize = format::vec::SUB_HEADER_SIZE;

/// JSON-deserialized form of one vector-index entry in the legacy `inf.vec.columns` metadata. The KV
/// value is a JSON array of these in declaration order.
#[derive(Debug, Clone, Deserialize)]
pub struct VectorColumnConfig {
    pub column: String,
    pub dim: usize,
    pub rot_seed: u64,
    /// `"l2sq"`, `"cosine"`, or `"negdot"`.
    pub metric: String,
}

#[derive(Debug, Clone)]
pub(super) enum Sq8ColumnMeta {
    Eager {
        scale: Vec<f32>,
        offset: Vec<f32>,
        per_doc_norms: Option<Arc<[f32]>>,
    },
    Lazy {
        scale_abs_off: usize,
        offset_abs_off: usize,
        norms_abs_off: Option<usize>,
    },
}

#[derive(Debug)]
struct Sq8ParsedMeta {
    scale: Vec<f32>,
    offset: Vec<f32>,
    per_doc_norms: Option<Arc<[f32]>>,
}

/// Per-column reader state; cached at open time.
#[derive(Debug)]
pub struct ColumnReader {
    /// Lazily built transposed code blocks for the FastScan LUT scan
    /// (shared `Arc` so rayon scan tasks can hold it past the reader
    /// borrow). See [`TransposedCodeCache`].
    transposed_codes: Arc<TransposedCodeCache>,
    pub name: String,
    pub dim: usize,
    pub n_cent: u32,
    pub n_docs: u32,
    pub metric: Metric,
    pub rot_seed: u64,
    /// — on-disk rerank codec for this column. Today
    /// admits Fp32, Sq8, and RabitqOnly; the parser rejects
    /// every other codec at open time with a `MalformedVersion`
    /// until support for it is added (the `None` codec is not yet
    /// implemented).
    pub rerank_codec: RerankCodec,
    /// `Sq8`-only quantizer metadata, materialised
    /// at open time from the `codec_meta` region. `None` for
    /// every other codec (Fp32 / RabitqOnly). At dim=384 the
    /// scale + offset arrays are 3 KB total; for L2Sq columns
    /// the per-doc norms add `n_docs × 4` bytes (4 MB at 1M
    /// docs / column). Materialising here amortizes the parse
    /// across every search call.
    /// Per-doc-norm carrier, shared by the residual family (`Sq8*`) and
    /// the flat `Sq16` codec. For `Sq16` the `scale`/`offset` arrays are
    /// empty (fixed grid) and only `per_doc_norms` is populated, so the
    /// reader's `cand.pos` norm indexing is byte-for-byte the residual
    /// family's. `None` for `Fp32` / `RabitqOnly`.
    pub(super) sq8_meta: Option<Sq8ColumnMeta>,
    lazy_sq8_parsed: OnceLock<Arc<Sq8ParsedMeta>>,
    /// Byte range of this column's subsection within the outer blob.
    subsection_range: Range<usize>,
    /// Offsets relative to the subsection start.
    summary_off: usize,
    centroids_off: usize,
    cluster_idx_off: usize,
    /// relative offset of the per-column
    /// `codec_meta` region inside the subsection. `0` means
    /// "no codec_meta" (Fp32 / RabitqOnly); non-zero is only
    /// produced by codecs whose `codec_meta_bytes(...) > 0`
    /// (`Sq8` is the only one today). In the current layout
    /// `codec_meta` sits between `cluster_idx` and the
    /// per-cluster blocks (inside the open-time region).
    #[allow(dead_code)]
    codec_meta_off: usize,
    /// Relative offset of the per-cluster blocks region. Each
    /// cluster `c` lives at
    /// `per_cluster_blocks_off + doc_off[c] * stride` for
    /// `count[c] * stride` bytes, where `stride = code_bytes + 4
    /// + per_vec_bytes`, formatted as `[codes_chunk:
    /// count*code_bytes][doc_ids_chunk: count*4][full_chunk:
    /// count*per_vec_bytes]`. The full-precision rerank vectors
    /// are interleaved into each block (no separate `full[]`
    /// region) so one range GET per probed cluster covers the
    /// estimate codes, doc-ids, and rerank vectors together.
    per_cluster_blocks_off: usize,
    /// Relative offset of the inline stable-`_id` region — one i128 per doc,
    /// indexed by `local_doc_id` — present only on materialized (hidden-cell)
    /// subsections. `None` when the subsection carries no region (every
    /// streaming/merge build). The region sits *between* the codec-meta region
    /// and the per-cluster blocks; its presence/size are derived at parse time
    /// from the offset gap `per_cluster_blocks_off − codec_meta_end` (no header
    /// flag). Read via [`Self::stable_ids_region_range`] /
    /// [`VectorReader::inline_stable_ids_for_locals`] so an id+score query (and
    /// the drain) skips resolving the stable `_id` through a scalar `_id` column.
    stable_ids_off: Option<usize>,
    quant: BitQuantizer,
    /// Cached random rotation built once at open from `(dim, rot_seed)`.
    /// Construction is `O(dim³)` for Gram-Schmidt — at dim=384 that's
    /// ~7.9 ms, dominant over every other per-query stage if rebuilt
    /// per `search()`. Build once, reuse forever.
    rot: RandomRotation,
}

/// Shared context threaded through the probe → shortlist → score pipeline.
struct ProbeCtx<'a> {
    q_rot: &'a [f32],
    k: usize,
    rerank_mult: usize,
    allow: Option<Arc<RoaringBitmap>>,
    /// Deny-set of local doc-ids to EXCLUDE before the coarse heap — tombstoned
    /// rows on the hidden vector path. Distinct from `allow` (keep-only): a
    /// candidate is scored iff it is in `allow` (when set) AND not in `deny`.
    /// Applying it pre-heap (not as a post-filter) preserves recall@k under
    /// deletes — each cell's top-k is selected from live rows only.
    deny: Option<Arc<RoaringBitmap>>,
    /// Rayon pool for CPU work. `None` falls back to the global pool.
    pool: Option<Arc<ThreadPool>>,
    /// Connection memory budget for the cold cluster-block fetch. See [`reserve_cold_fetch`].
    budget: Option<Arc<ConnectionMemoryBudget>>,
}

impl ColumnReader {
    /// byte range covering one cluster's
    /// `[codes_chunk + doc_ids_chunk]` block as a single
    /// contiguous span. Pulled in **one** range fetch per
    /// probed cluster; the cold-first-search budget collapses
    /// to `nprobe + 1` range GETs (nprobe cluster blocks + 1
    /// rerank run) on a freshly-opened lazy reader, down from
    /// `2 × nprobe + 1` on the older split-range path.
    ///
    /// Block layout: each cluster's block is
    /// `count * (code_bytes + 4)` bytes formatted as
    /// `[codes: count*code_bytes][doc_ids: count*4]`. The
    /// per-cluster `(doc_off, count)` entry recorded in
    /// `cluster_idx` addresses both halves with no extra
    /// lookup: byte offset = `per_cluster_blocks_off +
    /// doc_off * (code_bytes + 4)`.
    /// Full per-cluster block range `[codes][doc_ids][full]`. The
    /// production search now fetches only the codes+doc_ids prefix
    /// (`cluster_codes_doc_ids_range`) plus survivor `full[]` rows
    /// (`cluster_rerank_row_range`), so this whole-block range is
    /// retained for the layout-invariant test that pins the on-disk
    /// shape.
    pub(super) fn cluster_block_range(
        &self,
        cluster_doc_off: u32,
        cluster_count: u32,
    ) -> Range<usize> {
        let sub_start = self.subsection_range.start;
        let stride = self.per_cluster_doc_stride();
        let start = sub_start + self.per_cluster_blocks_off + (cluster_doc_off as usize) * stride;
        let len = (cluster_count as usize) * stride;
        start..start + len
    }

    pub(super) fn cluster_codes_doc_ids_range(
        &self,
        cluster_doc_off: u32,
        cluster_count: u32,
    ) -> Range<usize> {
        let sub_start = self.subsection_range.start;
        let start = sub_start
            + self.per_cluster_blocks_off
            + (cluster_doc_off as usize) * self.per_cluster_doc_stride();
        let len = (cluster_count as usize) * (self.quant.code_bytes() + format::vec::DOC_ID_BYTES);
        start..start + len
    }

    pub(super) fn cluster_rerank_row_range(
        &self,
        cluster_doc_off: u32,
        cluster_count: u32,
        local_idx: usize,
    ) -> Range<usize> {
        let sub_start = self.subsection_range.start;
        let block_start = sub_start
            + self.per_cluster_blocks_off
            + (cluster_doc_off as usize) * self.per_cluster_doc_stride();
        let prefix_len =
            (cluster_count as usize) * (self.quant.code_bytes() + format::vec::DOC_ID_BYTES);
        let start =
            block_start + prefix_len + local_idx * self.rerank_codec.per_vector_bytes(self.dim);
        start..start + self.rerank_codec.per_vector_bytes(self.dim)
    }

    /// Per-doc byte stride inside a cluster block:
    /// `code_bytes + 4 (doc_id) + per_vec_bytes (full rerank)`.
    /// A cluster's block packs `cnt` docs at this stride as
    /// `[codes_chunk][doc_ids_chunk][full_chunk]`.
    pub(super) fn per_cluster_doc_stride(&self) -> usize {
        self.quant.code_bytes()
            + format::vec::DOC_ID_BYTES
            + self.rerank_codec.per_vector_bytes(self.dim)
    }

    /// `true` when this subsection carries an inline stable-`_id` region
    /// (materialized/hidden-cell builds). When so, a hidden hit's positional
    /// `local_doc_id` resolves straight to its stable `_id` via
    /// [`Self::stable_id_at`] — no scalar `_id` column read.
    pub(super) fn has_inline_stable_ids(&self) -> bool {
        self.stable_ids_off.is_some()
    }

    /// Absolute byte range of the whole inline stable-`_id` region, for a
    /// single batched fetch before resolving many locals. `None` when absent.
    pub(super) fn stable_ids_region_range(&self) -> Option<Range<usize>> {
        let off = self.stable_ids_off?;
        let start = self.subsection_range.start + off;
        let len = (self.n_docs as usize) * format::vec::STABLE_ID_BYTES;
        Some(start..start + len)
    }
}

/// Per-open knobs for [`VectorReader::open_with`] and
/// [`VectorReader::open_lazy`]. `Default` is the safe choice
/// (CRC verification on). The argumentless [`VectorReader::open`]
/// takes the default; the lazy path uses
/// [`Self::for_object_store`] which turns CRC off (a full-blob
/// scan would defeat the cold-open byte budget).
///
#[derive(Debug, Clone, Copy)]
pub struct OpenOptions {
    /// Verify the per-subsection CRC during open. Each subsection is
    /// scanned in full (≈1.5 GiB at 1M × 384, single column), so this
    /// dominates cold-open time when on. Defaults to `true`; the
    /// argumentless [`VectorReader::open`] uses this default.
    /// Flip to `false` when storage is already trusted (content-
    /// addressed object store, checksummed filesystem) to skip
    /// the scan.
    pub verify_crc: bool,
}

impl Default for OpenOptions {
    fn default() -> Self {
        Self { verify_crc: true }
    }
}

impl OpenOptions {
    /// defaults tuned for an object-store-backed
    /// `Source::Lazy` open: `verify_crc = false` (a full-blob
    /// scan would defeat every cold-open byte-budget number in
    /// the plan; deployments that need CRC verification opt
    /// back in and accept the cost).
    pub fn for_object_store() -> Self {
        Self { verify_crc: false }
    }
}

/// Multi-index vector blob reader. `Send + Sync`; concurrent
/// searches share the underlying [`Source`] (refcount-shared via
/// `Bytes` / `Arc<dyn LazyByteSource>`).
#[derive(Debug)]
pub struct VectorReader {
    source: Source,
    n_docs: u64,
    columns: Vec<ColumnReader>,
    column_id_by_name: HashMap<String, u32>,
    /// Global cell ids parallel to [`Self::columns`] for multi-cell (v2)
    /// blobs. Empty for the single-column v1 layout.
    cell_ids: Vec<u32>,
    /// Prefix sums of per-cell `n_cent` for flat cluster routing on
    /// multi-cell blobs: flat id `c` maps to cell `i` where
    /// `flat_cluster_base[i] <= c < flat_cluster_base[i+1]` (with a
    /// trailing total at the end). Empty for v1.
    flat_cluster_base: Vec<u32>,
    /// Cold-path stash of the inline stable-`_id` region bytes, prefetched
    /// in the same fan-out wave as the cluster blocks (see
    /// [`Self::probe_clusters_async`]). The remap step resolves hidden→user
    /// `_id` from this — via the sync [`Self::inline_stable_ids_for_locals`]
    /// at the fan-out tag site — instead of a trailing region GET. Empty on
    /// warm (the region is already resident) and on any reader whose column
    /// has no inline region.
    cold_stable_id_region: std::sync::Mutex<Option<Bytes>>,
}

impl VectorReader {
    /// Test-only accessor: the per-doc dequantized-norm table this
    /// reader loaded from a column's `codec_meta` region (residual
    /// family or `Sq16`). Returns `None` for a codec that carries no
    /// norms, an unknown column, or a lazily-open source that has not
    /// materialised the norms eagerly. Used by the Sq16 store/read
    /// localization test to compare the loaded table against the norms
    /// recomputed from the on-disk `u16` codes.
    #[cfg(test)]
    pub(crate) fn per_doc_norms_for_test(&self, column: &str) -> Option<std::sync::Arc<[f32]>> {
        let col = self.columns.iter().find(|c| c.name == column)?;
        match col.sq8_meta.as_ref()? {
            Sq8ColumnMeta::Eager { per_doc_norms, .. } => per_doc_norms.clone(),
            Sq8ColumnMeta::Lazy { .. } => None,
        }
    }

    /// Open the reader. `columns_json` is the value of the
    /// legacy `inf.vec.columns` Parquet KV key (a JSON array of
    /// [`VectorColumnConfig`]).
    /// Open the reader with default options (CRC verification on).
    pub fn open(blob: Bytes, columns_json: &str) -> Result<Self, VectorError> {
        Self::open_with(blob, columns_json, OpenOptions::default())
    }

    /// Open with explicit options. The fast path is
    /// `OpenOptions { verify_crc: false }` which skips both the
    /// outer (whole-blob) CRC and the per-subsection CRC scans —
    /// at 1M × 384 cold open drops from ~132 ms to ~2 ms. Use this
    /// when the underlying storage is trusted (e.g. local disk after
    /// a successful file integrity check) or when CRC verification
    /// is performed elsewhere in the stack.
    pub fn open_with(
        blob: Bytes,
        columns_json: &str,
        opts: OpenOptions,
    ) -> Result<Self, VectorError> {
        // every byte fetch routes through `Source::try_get_range_sync`
        // so a future lazy variant can intercept the same call sites
        // without a second rewrite. `InMemory` returns zero-copy
        // `Bytes::slice` views; refcount bumps only.
        Self::open_with_source(Source::InMemory(blob), columns_json, opts)
    }

    /// Async open against a [`LazyByteSource`].
    ///
    /// The lazy open path fetches exactly the bytes the structural
    /// decode reads:
    ///   - outer header (`32 B`);
    ///   - directory + directory CRC;
    ///   - each subsection header (`56 B`);
    ///   - Sq8 `codec_meta` only (scale/offset/norm tables).
    ///
    /// Centroids, cluster indexes, and per-cluster blocks are search-time
    /// data, not open-time data. They stay lazy so cold open is governed
    /// by metadata bytes and serial dependency depth instead of a large
    /// speculative slab.
    ///
    /// `opts.verify_crc = true` is honored, but it forces every
    /// subsection to be fetched in full and defeats the cold-open
    /// cold-open byte budget — only set it when the
    /// underlying storage is untrusted and CRC verification is
    /// load-bearing. The convenience constructor
    /// [`OpenOptions::for_object_store`] sets it to `false`
    /// (the load-bearing default; see the `verify_crc` trade-off
    /// documented on `OpenOptions`).
    pub async fn open_lazy(
        source: Arc<dyn LazyByteSource>,
        columns_json: &str,
        opts: OpenOptions,
    ) -> Result<Self, VectorError> {
        let blob_size = source.size() as usize;
        if blob_size < OUTER_HEADER_SIZE + 4 {
            return Err(VectorError::Read(ReadError::MissingKv(
                "vector blob header",
            )));
        }

        let header_bytes = source
            .range(0, OUTER_HEADER_SIZE as u64)
            .await
            .map_err(|e| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "lazy open: outer header fetch: {e}"
                )))
            })?;
        if &header_bytes[0..MAGIC_BYTES] != format::vec::OUTER_MAGIC {
            return Err(VectorError::Read(ReadError::BadMagic {
                section: "vector",
                expected: format::vec::OUTER_MAGIC,
                actual: header_bytes[0..MAGIC_BYTES].to_vec(),
            }));
        }
        let version =
            read_u32_le(&header_bytes[outer_hdr::VERSION_OFF..outer_hdr::VERSION_OFF + U32_BYTES]);
        if version != format::vec::VERSION && version != format::vec::VERSION_MULTI_CELL {
            return Err(VectorError::Read(ReadError::UnsupportedVersion(format!(
                "vector blob version {version}"
            ))));
        }
        if version == format::vec::VERSION_MULTI_CELL {
            return Self::open_lazy_multi_cell(source, columns_json, header_bytes, blob_size, opts)
                .await;
        }
        let n_columns = read_u32_le(
            &header_bytes[outer_hdr::N_COLUMNS_OFF..outer_hdr::N_COLUMNS_OFF + U32_BYTES],
        ) as usize;
        let dir_offset = read_u64_le(
            &header_bytes[outer_hdr::DIR_OFFSET_OFF..outer_hdr::DIR_OFFSET_OFF + U64_BYTES],
        ) as usize;
        let (dir_size, dir_end) = checked_dir_bounds(dir_offset, n_columns, DIR_ENTRY_SIZE)?;
        if dir_end > blob_size {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "lazy open: directory end {dir_end} exceeds blob size {blob_size}",
            ))));
        }

        let dir_prefetch = source
            .range(dir_offset as u64, (dir_end - dir_offset) as u64)
            .await
            .map_err(|e| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "lazy open: directory fetch: {e}"
                )))
            })?;

        // Validate directory CRC against the prefetched bytes
        // before walking subsection metadata. A directory-CRC
        // mismatch on the lazy path is the closest thing we
        // have to an end-to-end integrity check when
        // `verify_crc = false`.
        let dir_bytes_slice = &dir_prefetch[0..dir_size];
        let dir_crc_expected = read_u32_le(&dir_prefetch[dir_size..dir_size + format::CRC_BYTES]);
        let dir_crc_actual = crc32c(dir_bytes_slice);
        if dir_crc_expected != dir_crc_actual {
            return Err(VectorError::Read(ReadError::ChecksumMismatch {
                section: "vector/directory",
                column: String::new(),
            }));
        }

        // Stage the overlay with the exact header and directory bytes.
        let mut overlay = PrefetchedSource::new(Arc::clone(&source));
        overlay.install(0, header_bytes.clone());
        overlay.install(dir_offset as u64, dir_prefetch.clone());

        let mut subsection_meta = Vec::with_capacity(n_columns);
        let mut subheader_ranges = Vec::with_capacity(n_columns);
        for i in 0..n_columns {
            let entry_off = i * DIR_ENTRY_SIZE;
            let subsection_off = read_u64_le(
                &dir_bytes_slice[entry_off + dir_entry::SUBSECTION_OFF_OFF
                    ..entry_off + dir_entry::SUBSECTION_OFF_OFF + U64_BYTES],
            ) as usize;
            let subsection_len = read_u64_le(
                &dir_bytes_slice[entry_off + dir_entry::SUBSECTION_LEN_OFF
                    ..entry_off + dir_entry::SUBSECTION_LEN_OFF + U64_BYTES],
            ) as usize;
            let dir_codec_meta_off = read_u32_le(
                &dir_bytes_slice[entry_off + dir_entry::CODEC_META_OFF_OFF
                    ..entry_off + dir_entry::CODEC_META_OFF_OFF + U32_BYTES],
            ) as usize;
            let dir_codec_meta_size = read_u32_le(
                &dir_bytes_slice[entry_off + dir_entry::CODEC_META_SIZE_OFF
                    ..entry_off + dir_entry::CODEC_META_SIZE_OFF + U32_BYTES],
            ) as usize;
            if subsection_len < SUB_HEADER_SIZE + format::CRC_BYTES {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "subsection {i} too short ({subsection_len} bytes)"
                ))));
            }
            let sub_end = subsection_off + subsection_len;
            if sub_end > blob_size {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "subsection {i} runs past blob",
                ))));
            }
            if dir_codec_meta_size > 0 {
                let meta_end = dir_codec_meta_off + dir_codec_meta_size;
                if dir_codec_meta_off < SUB_HEADER_SIZE || meta_end > subsection_len - 4 {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "subsection {i} directory codec_meta range [{dir_codec_meta_off}..\
                         {meta_end}) outside subsection body length {}",
                        subsection_len - 4
                    ))));
                }
            }
            subsection_meta.push((
                i,
                entry_off,
                subsection_off,
                subsection_len,
                sub_end,
                dir_codec_meta_off,
                dir_codec_meta_size,
            ));
            subheader_ranges.push((i, subsection_off));
        }

        let subheaders_fut =
            futures::future::try_join_all(subheader_ranges.iter().map(|&(i, subsection_off)| {
                let source = Arc::clone(&source);
                async move {
                    let bytes = source
                        .range(subsection_off as u64, SUB_HEADER_SIZE as u64)
                        .await
                        .map_err(|e| {
                            VectorError::Read(ReadError::MalformedVersion(format!(
                                "lazy open: subsection {i} sub-header fetch: {e}"
                            )))
                        })?;
                    Ok::<_, VectorError>((i, subsection_off, bytes))
                }
            }));
        let subheaders = subheaders_fut.await?;

        for (i, subsection_off, sub_header) in subheaders {
            if &sub_header[0..MAGIC_BYTES] != format::vec::SUB_MAGIC {
                return Err(VectorError::Read(ReadError::BadMagic {
                    section: "vector/subsection",
                    expected: format::vec::SUB_MAGIC,
                    actual: sub_header[0..MAGIC_BYTES].to_vec(),
                }));
            }
            overlay.install(subsection_off as u64, sub_header.clone());
            let (_, entry_off, _, subsection_len, sub_end, dir_codec_meta_off, dir_codec_meta_size) =
                subsection_meta[i];
            let per_cluster_blocks_off = read_u64_le(
                &sub_header[sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF
                    ..sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF + U64_BYTES],
            ) as usize;
            let open_time_abs_end = subsection_off + per_cluster_blocks_off;
            if open_time_abs_end > sub_end {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "subsection {i} per_cluster_blocks_off {per_cluster_blocks_off} \
                     exceeds subsection length {subsection_len}",
                ))));
            }
            let codec_meta_size = read_u32_le(
                &sub_header[sub_hdr::CODEC_META_SIZE_OFF..sub_hdr::CODEC_META_SIZE_OFF + U32_BYTES],
            ) as usize;

            // Codec_meta lives at `[cluster_idx_off + n_cent*8 ..
            // per_cluster_blocks_off]`. We only need it for Sq8
            // columns (non-Sq8 declares codec_meta_size = 0).
            //
            // Exact-open path: fetch only the codec_meta bytes,
            // not the centroids / cluster_idx prefix that precedes
            // them in the subsection.
            if codec_meta_size > 0 {
                let cluster_idx_off = read_u64_le(
                    &sub_header
                        [sub_hdr::CLUSTER_IDX_OFF_OFF..sub_hdr::CLUSTER_IDX_OFF_OFF + U64_BYTES],
                ) as usize;
                let n_cent = read_u32_le(
                    &dir_bytes_slice[entry_off + dir_entry::N_CENT_OFF
                        ..entry_off + dir_entry::N_CENT_OFF + U32_BYTES],
                ) as usize;
                let codec_meta_off = cluster_idx_off + n_cent * CLUSTER_IDX_ENTRY_BYTES;
                let codec_meta_abs_off = subsection_off + codec_meta_off;
                // codec_meta ends at or before per_cluster_blocks_off; any gap
                // is the inline stable-`_id` region (one i128 per doc). The full
                // parse validates the exact size against n_docs — here we only
                // require a well-formed (non-negative, 16-aligned) gap.
                let codec_meta_abs_end = codec_meta_abs_off + codec_meta_size;
                let stable_ids_gap = open_time_abs_end.checked_sub(codec_meta_abs_end);
                if !stable_ids_gap
                    .is_some_and(|gap| gap.is_multiple_of(format::vec::STABLE_ID_BYTES))
                {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "subsection {i} codec_meta_size {codec_meta_size} does not end at or a \
                         whole number of stable-`_id`s before per_cluster_blocks_off \
                         {per_cluster_blocks_off}"
                    ))));
                }
                if dir_codec_meta_off != codec_meta_off || dir_codec_meta_size != codec_meta_size {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "subsection {i} directory codec_meta range \
                         off={dir_codec_meta_off} len={dir_codec_meta_size} does not match \
                         subheader-derived off={codec_meta_off} len={codec_meta_size}"
                    ))));
                }
                let _ = subsection_len;
            } else if dir_codec_meta_size != 0 || dir_codec_meta_off != 0 {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "subsection {i} has zero codec_meta_size but directory declares \
                     off={dir_codec_meta_off} len={dir_codec_meta_size}"
                ))));
            }
        }

        Self::open_with_source(Source::Lazy(Arc::new(overlay)), columns_json, opts)
    }

    /// open over an arbitrary [`Source`].
    ///
    /// The structural decode path is the same as
    /// [`Self::open_with`]; this entry just accepts a pre-built
    /// `Source`. Used by:
    /// - Test helpers that need to wire a counting / mock
    ///   [`LazyByteSource`] under a `Source::Lazy` (e.g. the
    ///   range-counting integration test).
    /// - [`Self::open_lazy`], which pre-fetches the
    ///   open-time region into a [`PrefetchedSource`] overlay
    ///   and hands the overlay through as `Source::Lazy`.
    ///
    /// Contract on `Source::Lazy`: the lazy source's
    /// `try_get_range_sync` must resolve every range request
    /// the structural decode path issues — sub-header (56 B per
    /// column) and codec_meta tail (Sq8 columns only). The
    /// `open_lazy` path guarantees this via the overlay; callers
    /// constructing a `Source::Lazy` directly (tests, mmap-
    /// backed sources) must arrange equivalent residency.
    pub(crate) fn open_with_source(
        source: Source,
        columns_json: &str,
        opts: OpenOptions,
    ) -> Result<Self, VectorError> {
        if source.len() < OUTER_HEADER_SIZE + format::CRC_BYTES {
            return Err(VectorError::Read(ReadError::MissingKv(
                "vector blob header",
            )));
        }

        // Pull the fixed-size outer header in one fetch — five small
        // reads collapse into one `Bytes::slice`.
        let header = fetch_sync(&source, 0..OUTER_HEADER_SIZE, "outer header")?;
        if &header[0..MAGIC_BYTES] != format::vec::OUTER_MAGIC {
            return Err(VectorError::Read(ReadError::BadMagic {
                section: "vector",
                expected: format::vec::OUTER_MAGIC,
                actual: header[0..MAGIC_BYTES].to_vec(),
            }));
        }

        let version =
            read_u32_le(&header[outer_hdr::VERSION_OFF..outer_hdr::VERSION_OFF + U32_BYTES]);
        if version != format::vec::VERSION && version != format::vec::VERSION_MULTI_CELL {
            return Err(VectorError::Read(ReadError::UnsupportedVersion(format!(
                "vector blob version {version}"
            ))));
        }
        if version == format::vec::VERSION_MULTI_CELL {
            return Self::open_multi_cell_with_source(source, columns_json, opts, header);
        }

        let n_columns =
            read_u32_le(&header[outer_hdr::N_COLUMNS_OFF..outer_hdr::N_COLUMNS_OFF + U32_BYTES])
                as usize;
        let n_docs = read_u64_le(&header[outer_hdr::N_DOCS_OFF..outer_hdr::N_DOCS_OFF + U64_BYTES]);
        let dir_offset =
            read_u64_le(&header[outer_hdr::DIR_OFFSET_OFF..outer_hdr::DIR_OFFSET_OFF + U64_BYTES])
                as usize;

        // Verify directory CRC (cheap, needed before we can parallelize
        // subsection CRCs since we walk dir entries to find them).
        let (dir_size, dir_end) = checked_dir_bounds(dir_offset, n_columns, DIR_ENTRY_SIZE)?;
        if dir_end > source.len() {
            return Err(VectorError::Read(ReadError::MalformedVersion(
                "vector directory runs past blob".into(),
            )));
        }
        let dir_bytes = fetch_sync(&source, dir_offset..dir_offset + dir_size, "directory")?;
        let dir_crc_bytes = fetch_sync(
            &source,
            dir_offset + dir_size..dir_offset + dir_size + 4,
            "directory crc",
        )?;
        let dir_crc_expected = read_u32_le(&dir_crc_bytes);
        let dir_crc_actual = crc32c(&dir_bytes);
        if dir_crc_expected != dir_crc_actual {
            return Err(VectorError::Read(ReadError::ChecksumMismatch {
                section: "vector/directory",
                column: String::new(),
            }));
        }

        // CRC verification: the outer-blob scan and per-subsection scans
        // each touch ~1.5 GiB at 1M × 384; together they're the bulk of
        // cold-open cost when `verify_crc=true`. Two stacked optimizations:
        //
        // 1. CLMUL-vectorized CRC32C via `crc-fast` in the checksum
        //    module — folds 8 lanes in vector regs instead of one
        //    serial dependency chain on `_mm_crc32_u64`, ~10× single-
        //    thread throughput on the boxes we measure.
        // 2. Run outer + per-subsection scans in parallel via rayon —
        //    each subsection's CRC is independent. The outer scan
        //    overlaps with the largest subsection on its own thread.
        //
        // Skip the whole pass via `OpenOptions { verify_crc: false }`
        // if upstream storage is trusted (content-addressed object
        // store, etc.); that path is ~12 ms regardless.
        if opts.verify_crc {
            // `Bytes` instead of `&'a [u8]` so the par_iter doesn't need
            // a lifetime parameter — each job owns a refcount-shared view
            // into the source, which is itself shared via the outer
            // `Source` for the duration of `open_with`.
            struct CrcJob {
                idx: i32,
                bytes: Bytes,
                expected: u32,
            }

            let mut jobs: Vec<CrcJob> = Vec::with_capacity(n_columns + 1);

            let outer_total = source.len();
            let outer_crc_pos = outer_total - format::CRC_BYTES;
            let outer_body = fetch_sync(&source, 0..outer_crc_pos, "outer body")?;
            let outer_crc_bytes = fetch_sync(&source, outer_crc_pos..outer_total, "outer crc")?;
            jobs.push(CrcJob {
                idx: -1,
                bytes: outer_body,
                expected: read_u32_le(&outer_crc_bytes),
            });

            for i in 0..n_columns {
                let entry_off = i * DIR_ENTRY_SIZE;
                let subsection_off = read_u64_le(
                    &dir_bytes[entry_off + dir_entry::SUBSECTION_OFF_OFF
                        ..entry_off + dir_entry::SUBSECTION_OFF_OFF + U64_BYTES],
                ) as usize;
                let subsection_len = read_u64_le(
                    &dir_bytes[entry_off + dir_entry::SUBSECTION_LEN_OFF
                        ..entry_off + dir_entry::SUBSECTION_LEN_OFF + U64_BYTES],
                ) as usize;
                let sub_end = subsection_off + subsection_len;
                if sub_end > source.len() {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "subsection {i} runs past blob"
                    ))));
                }
                let sub = fetch_sync(&source, subsection_off..sub_end, "subsection")?;
                if sub.len() < SUB_HEADER_SIZE + format::CRC_BYTES {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "subsection {i} too short"
                    ))));
                }
                let sub_crc_pos = sub.len() - format::CRC_BYTES;
                // `Bytes::slice` is zero-copy — no second `try_get_range_sync`
                // round-trip needed since we already hold the subsection.
                let sub_body = sub.slice(0..sub_crc_pos);
                let sub_crc_bytes = sub.slice(sub_crc_pos..sub.len());
                jobs.push(CrcJob {
                    idx: i as i32,
                    bytes: sub_body,
                    expected: read_u32_le(&sub_crc_bytes),
                });
            }

            // Serial CRC verify over the (handful of) subsections — a
            // one-time open cost, not query-hot, so it stays serial,
            // off the rayon scan path.
            let mismatch = jobs.iter().find_map(|job| {
                if crc32c(&job.bytes) != job.expected {
                    Some(job.idx)
                } else {
                    None
                }
            });
            if let Some(idx) = mismatch {
                if idx < 0 {
                    return Err(VectorError::Read(ReadError::ChecksumMismatch {
                        section: "vector",
                        column: String::new(),
                    }));
                } else {
                    let i = idx as usize;
                    return Err(VectorError::Read(ReadError::ChecksumMismatch {
                        section: "vector/subsection",
                        column: format!(" (column index {i})"),
                    }));
                }
            }
        }

        // Parse JSON.
        let cols_json: Vec<VectorColumnConfig> =
            serde_json::from_str(columns_json).map_err(|e| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "inf.vec.columns JSON: {e}"
                )))
            })?;
        if cols_json.len() != n_columns {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "inf.vec.columns has {} entries, header says {n_columns}",
                cols_json.len()
            ))));
        }

        // Parse each directory entry, build ColumnReader.
        let mut columns = Vec::with_capacity(n_columns);
        let mut column_id_by_name = HashMap::with_capacity(n_columns);
        for (i, cfg) in cols_json.iter().enumerate() {
            let entry_off = i * DIR_ENTRY_SIZE;
            let column_id = read_u32_le(&dir_bytes[entry_off..entry_off + U32_BYTES]);
            if column_id != i as u32 {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "vector dir entry {i} has column_id {column_id}"
                ))));
            }
            let dim = read_u32_le(
                &dir_bytes
                    [entry_off + dir_entry::DIM_OFF..entry_off + dir_entry::DIM_OFF + U32_BYTES],
            ) as usize;
            let n_cent = read_u32_le(
                &dir_bytes[entry_off + dir_entry::N_CENT_OFF
                    ..entry_off + dir_entry::N_CENT_OFF + U32_BYTES],
            );
            let metric_id = read_u32_le(
                &dir_bytes[entry_off + dir_entry::METRIC_ID_OFF
                    ..entry_off + dir_entry::METRIC_ID_OFF + U32_BYTES],
            );
            let rot_seed = read_u64_le(
                &dir_bytes[entry_off + dir_entry::ROT_SEED_OFF
                    ..entry_off + dir_entry::ROT_SEED_OFF + U64_BYTES],
            );
            let subsection_off = read_u64_le(
                &dir_bytes[entry_off + dir_entry::SUBSECTION_OFF_OFF
                    ..entry_off + dir_entry::SUBSECTION_OFF_OFF + U64_BYTES],
            ) as usize;
            let subsection_len = read_u64_le(
                &dir_bytes[entry_off + dir_entry::SUBSECTION_LEN_OFF
                    ..entry_off + dir_entry::SUBSECTION_LEN_OFF + U64_BYTES],
            ) as usize;
            // bytes [40..48] = summary_offset (absolute), [48..52] = summary_length,
            // [52..56] = codec_id (1) + reserved (3)
            let _summary_off_abs = read_u64_le(
                &dir_bytes[entry_off + dir_entry::SUMMARY_ABS_OFF
                    ..entry_off + dir_entry::SUMMARY_ABS_OFF + U64_BYTES],
            );
            let codec_id = dir_bytes[entry_off + dir_entry::CODEC_ID_OFF];
            let rerank_codec = RerankCodec::from_codec_id(codec_id).ok_or_else(|| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' has unknown rerank-codec id {codec_id} \
                     (known ids: 0=fp32, 1=sq8_residual, 2=rabitq_only, \
                      3=sq8_fixed_residual, 4=sq16, 5=sq16_adaptive)",
                    cfg.column
                )))
            })?;
            if !rerank_codec.is_implemented() {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' uses rerank codec {} which is not implemented yet \
                     (`fp32`, `sq8_residual`, `sq8_fixed_residual`, `sq16`, \
                      `sq16_adaptive`, `rabitq_only` are the supported codecs)",
                    cfg.column,
                    rerank_codec.name()
                ))));
            }

            // Validate against JSON.
            if dim != cfg.dim {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' dim mismatch: dir={dim} json={}",
                    cfg.column, cfg.dim
                ))));
            }
            if rot_seed != cfg.rot_seed {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' rot_seed mismatch",
                    cfg.column
                ))));
            }
            let metric = match metric_id {
                format::vec::METRIC_ID_L2SQ => Metric::L2Sq,
                format::vec::METRIC_ID_COSINE => Metric::Cosine,
                format::vec::METRIC_ID_NEGDOT => Metric::NegDot,
                _ => {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "unknown metric_id {metric_id} for column '{}'",
                        cfg.column
                    ))));
                }
            };
            if !rerank_codec.supports_metric(metric) {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' codec {} supports cosine metric only",
                    cfg.column,
                    rerank_codec.name()
                ))));
            }

            // Validate subsection bounds + magic.
            //
            // Open-time region fetch. The reader's
            // open path only reads the sub-header + (when present)
            // codec_meta from the subsection. Per-cluster blocks,
            // full[], and the trailing CRC are search-time concerns.
            //
            // To minimize cold-open byte volume against an object-
            // store-backed `Source::Lazy`, fetch in two phases:
            //   Phase A — sub-header (56 B) at `[subsection_off..
            //     subsection_off + SUB_HEADER_SIZE]`. Carries
            //     codec_meta_size and per_cluster_blocks_off, which
            //     together fix the open-time region's end offset.
            //   Phase B — codec_meta tail at `[subsection_off +
            //     cluster_idx_off + n_cent*8 .. subsection_off +
            //     per_cluster_blocks_off]` (Sq8 columns only;
            //     skipped when codec_meta_size == 0).
            //
            // On `Source::InMemory` both fetches are zero-copy
            // `Bytes::slice` views — identical cost to the previous
            // single full-subsection slice. On `Source::Lazy` they
            // resolve through the `PrefetchedSource` overlay
            // installed by `open_lazy` (zero underlying GETs) when
            // the caller pre-fetched the open-time region; on a
            // bare `Source::Lazy` they fall through to the
            // underlying async `range` and round-trip per fetch.
            let sub_end = subsection_off + subsection_len;
            if sub_end > source.len() {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "subsection {i} runs past blob"
                ))));
            }
            if subsection_len < SUB_HEADER_SIZE + format::CRC_BYTES {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "subsection {i} too short"
                ))));
            }
            let sub_header = fetch_sync(
                &source,
                subsection_off..subsection_off + SUB_HEADER_SIZE,
                "sub_header",
            )?;
            if &sub_header[0..MAGIC_BYTES] != format::vec::SUB_MAGIC {
                return Err(VectorError::Read(ReadError::BadMagic {
                    section: "vector/subsection",
                    expected: format::vec::SUB_MAGIC,
                    actual: sub_header[0..MAGIC_BYTES].to_vec(),
                }));
            }
            // CRC was either already verified above in the parallel
            // pre-pass (when `opts.verify_crc` is true) or skipped on
            // the lazy fast path. Either way `sub_crc_pos` is derived
            // from `subsection_len` (directory entry), not from a
            // resident buffer.
            let sub_crc_pos = subsection_len - format::CRC_BYTES;

            // Sub-header parse. Only one layout version is
            // accepted; any other value is rejected as malformed.
            // See `format::vec::SUBSECTION_VERSION` for the
            // byte-level spec.
            //   [ 8..12] SUBSECTION_VERSION
            //   [12..16] codec_meta_size (u32 LE)
            //   [16..24] summary_centroid_offset (u64 LE)
            //   [24..32] reserved (u64)
            //   [32..40] centroids_off (u64 LE)
            //   [40..48] cluster_idx_off (u64 LE)
            //   [48..56] per_cluster_blocks_off (u64 LE)
            let subsection_version =
                read_u32_le(&sub_header[sub_hdr::VERSION_OFF..sub_hdr::VERSION_OFF + U32_BYTES]);
            if subsection_version != format::vec::SUBSECTION_VERSION {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' has unsupported subsection layout version \
                     {subsection_version}; this build supports only {}",
                    cfg.column,
                    format::vec::SUBSECTION_VERSION
                ))));
            }

            let quant = BitQuantizer::new(dim);
            let code_bytes = quant.code_bytes();
            if code_bytes == 0 {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' dim={dim} yields code_bytes=0",
                    cfg.column
                ))));
            }
            let per_vec_bytes = rerank_codec.per_vector_bytes(dim);
            // Sq16 carries a per-doc norm table for cosine/L2Sq (0 for
            // NegDot), so it is not unconditionally zero-meta — its exact
            // size is validated below against `codec_meta_bytes`.
            let codec_meta_required_zero =
                matches!(rerank_codec, RerankCodec::Fp32 | RerankCodec::RabitqOnly);

            let codec_meta_size = read_u32_le(
                &sub_header[sub_hdr::CODEC_META_SIZE_OFF..sub_hdr::CODEC_META_SIZE_OFF + U32_BYTES],
            ) as usize;
            let summary_off = read_u64_le(
                &sub_header[sub_hdr::SUMMARY_OFF_OFF..sub_hdr::SUMMARY_OFF_OFF + U64_BYTES],
            ) as usize;
            let centroids_off = read_u64_le(
                &sub_header[sub_hdr::CENTROIDS_OFF_OFF..sub_hdr::CENTROIDS_OFF_OFF + U64_BYTES],
            ) as usize;
            let cluster_idx_off = read_u64_le(
                &sub_header[sub_hdr::CLUSTER_IDX_OFF_OFF..sub_hdr::CLUSTER_IDX_OFF_OFF + U64_BYTES],
            ) as usize;
            let per_cluster_blocks_off = read_u64_le(
                &sub_header[sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF
                    ..sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF + U64_BYTES],
            ) as usize;

            if codec_meta_required_zero && codec_meta_size != 0 {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' has codec_meta_size={codec_meta_size} for codec {}; \
                     fp32/rabitq_only must write codec_meta_size=0",
                    cfg.column,
                    rerank_codec.name()
                ))));
            }

            // codec_meta sits immediately after cluster_idx (when
            // non-empty); 0 means "no codec_meta" and skips the
            // sq8_meta parse below.
            let cluster_idx_size = (n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES;
            let codec_meta_off = if codec_meta_size == 0 {
                0
            } else {
                cluster_idx_off + cluster_idx_size
            };
            // End of the last fixed region before the per-cluster blocks: the
            // codec_meta region (Sq8), else the cluster index (Fp32/RabitqOnly).
            let preceding_end = if codec_meta_size == 0 {
                cluster_idx_off + cluster_idx_size
            } else {
                codec_meta_off + codec_meta_size
            };
            // Anything between `preceding_end` and `per_cluster_blocks_off` is
            // the inline stable-`_id` region (materialized/hidden-cell builds);
            // `0` means none. Validated against n_docs below. Self-describing
            // from the offsets — no header flag.
            let stable_ids_region_bytes =
                per_cluster_blocks_off.checked_sub(preceding_end).ok_or_else(|| {
                    VectorError::Read(ReadError::MalformedVersion(format!(
                        "column '{}' regions before per_cluster_blocks_off={per_cluster_blocks_off} \
                         overrun it (preceding_end={preceding_end})",
                        cfg.column
                    )))
                })?;

            // Per-cluster blocks fill [per_cluster_blocks_off..sub_crc_pos) —
            // the trailing data region. Each doc contributes
            // `code_bytes + 4 (doc_id) + per_vec_bytes (full)` — codes, doc-id,
            // and rerank vector interleaved per cluster. Solve for n_docs from
            // the region size (the stable-`_id` region, if any, is *before*
            // per_cluster_blocks_off and so does not perturb this).
            let blocks_region_size = sub_crc_pos - per_cluster_blocks_off;
            let per_doc_stride = code_bytes + format::vec::DOC_ID_BYTES + per_vec_bytes;
            if per_doc_stride == 0 || !blocks_region_size.is_multiple_of(per_doc_stride) {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' per_cluster_blocks region {blocks_region_size} bytes \
                     not divisible by per-doc stride {per_doc_stride}",
                    cfg.column
                ))));
            }
            let col_n_docs = (blocks_region_size / per_doc_stride) as u32;
            // The stable-`_id` region, when present, is exactly one i128 per doc.
            let expected_stable_ids_bytes = (col_n_docs as usize) * format::vec::STABLE_ID_BYTES;
            if stable_ids_region_bytes != 0 && stable_ids_region_bytes != expected_stable_ids_bytes
            {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' gap before per_cluster_blocks_off is {stable_ids_region_bytes} \
                     bytes; expected 0 or n_docs×16 = {expected_stable_ids_bytes}",
                    cfg.column
                ))));
            }
            // Relative offset of the stable-`_id` region (start of the i128s).
            let stable_ids_off = (stable_ids_region_bytes != 0).then_some(preceding_end);
            let actual_codec_meta_size = codec_meta_size;

            // Sq8 + L2Sq adds the per-doc norms tail to codec_meta
            // (`n_docs * 4` bytes); now that `col_n_docs` is known
            // we can validate the declared size against the codec's
            // exact expectation.
            let expected_codec_meta_size =
                rerank_codec.codec_meta_bytes(dim, col_n_docs as usize, n_cent as usize, metric);
            if actual_codec_meta_size != expected_codec_meta_size {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' codec_meta_size={actual_codec_meta_size} on disk but \
                     codec {} / metric {metric:?} expects {expected_codec_meta_size} bytes",
                    cfg.column,
                    rerank_codec.name()
                ))));
            }

            let sq8_meta = if rerank_codec.carries_cluster_quant_meta() {
                let meta_abs_start = subsection_off + codec_meta_off;
                let meta_abs_end = meta_abs_start + actual_codec_meta_size;
                let so_block_bytes = (n_cent as usize) * dim * 4;
                let scale_end = so_block_bytes;
                let offset_end = scale_end + so_block_bytes;
                if let Some(meta_bytes) = source.try_get_range_sync(meta_abs_start..meta_abs_end) {
                    let scale = parse_f32_le_vec(&meta_bytes[0..scale_end]);
                    let offset = parse_f32_le_vec(&meta_bytes[scale_end..offset_end]);
                    validate_quantizer_meta(rerank_codec, &scale, &offset, cfg.column.as_str())?;
                    let per_doc_norms: Option<Arc<[f32]>> =
                        if matches!(metric, Metric::L2Sq | Metric::Cosine) {
                            let norms_end = offset_end + (col_n_docs as usize) * 4;
                            debug_assert_eq!(norms_end, actual_codec_meta_size);
                            Some(Arc::from(parse_f32_le_vec(
                                &meta_bytes[offset_end..norms_end],
                            )))
                        } else {
                            None
                        };
                    Some(Sq8ColumnMeta::Eager {
                        scale,
                        offset,
                        per_doc_norms,
                    })
                } else {
                    Some(Sq8ColumnMeta::Lazy {
                        scale_abs_off: meta_abs_start,
                        offset_abs_off: meta_abs_start + scale_end,
                        norms_abs_off: matches!(metric, Metric::L2Sq | Metric::Cosine)
                            .then_some(meta_abs_start + offset_end),
                    })
                }
            } else if rerank_codec.is_sq16() && matches!(metric, Metric::L2Sq | Metric::Cosine) {
                // Sq16 reuses the residual family's `Sq8ColumnMeta` norm
                // carrier + `cand.pos` indexing verbatim; the only layout
                // difference is that the per-doc norm table starts at the
                // codec_meta head (no scale/offset arrays precede it), so
                // `scale`/`offset` stay empty.
                let meta_abs_start = subsection_off + codec_meta_off;
                let norms_end = meta_abs_start + (col_n_docs as usize) * 4;
                debug_assert_eq!(norms_end - meta_abs_start, actual_codec_meta_size);
                if let Some(meta_bytes) = source.try_get_range_sync(meta_abs_start..norms_end) {
                    Some(Sq8ColumnMeta::Eager {
                        scale: Vec::new(),
                        offset: Vec::new(),
                        per_doc_norms: Some(Arc::from(parse_f32_le_vec(&meta_bytes))),
                    })
                } else {
                    Some(Sq8ColumnMeta::Lazy {
                        scale_abs_off: meta_abs_start,
                        offset_abs_off: meta_abs_start,
                        norms_abs_off: Some(meta_abs_start),
                    })
                }
            } else {
                None
            };

            // Structural bounds. cluster_idx fits before the
            // per-cluster blocks region. The
            // `blocks_region_size.is_multiple_of(...)` check
            // above already pinned n_docs and that the per-cluster
            // blocks region tiles exactly to the CRC; this check
            // guards an off-by-one in the cluster_idx slot.
            let cluster_idx_end = cluster_idx_off + cluster_idx_size;
            if cluster_idx_end > sub_crc_pos {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' cluster index runs past subsection",
                    cfg.column
                ))));
            }

            // Soft cross-check: cfg.metric matches blob's metric.
            let cfg_metric = match cfg.metric.as_str() {
                "l2sq" => Some(Metric::L2Sq),
                "cosine" => Some(Metric::Cosine),
                "negdot" => Some(Metric::NegDot),
                _ => None,
            };
            if let Some(m) = cfg_metric
                && m != metric
            {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' metric mismatch: dir={metric:?} json={}",
                    cfg.column, cfg.metric
                ))));
            }

            columns.push(ColumnReader {
                transposed_codes: Arc::new(TransposedCodeCache::default()),
                name: cfg.column.clone(),
                dim,
                n_cent,
                n_docs: col_n_docs,
                metric,
                rot_seed,
                rerank_codec,
                sq8_meta,
                lazy_sq8_parsed: OnceLock::new(),
                subsection_range: subsection_off..sub_end,
                summary_off,
                centroids_off,
                cluster_idx_off,
                codec_meta_off,
                per_cluster_blocks_off,
                stable_ids_off,
                quant,
                rot: RandomRotation::new(dim, rot_seed),
            });
            column_id_by_name.insert(cfg.column.clone(), i as u32);
        }

        Ok(VectorReader {
            source,
            n_docs,
            columns,
            column_id_by_name,
            cell_ids: Vec::new(),
            flat_cluster_base: Vec::new(),
            cold_stable_id_region: std::sync::Mutex::new(None),
        })
    }

    /// Sync open for a v2 multi-cell blob (cell directory + complete cell IVFs).
    fn open_multi_cell_with_source(
        source: Source,
        columns_json: &str,
        opts: OpenOptions,
        header: Bytes,
    ) -> Result<Self, VectorError> {
        let n_cells =
            read_u32_le(&header[outer_hdr::N_CELLS_OFF..outer_hdr::N_CELLS_OFF + U32_BYTES])
                as usize;
        let n_docs = read_u64_le(&header[outer_hdr::N_DOCS_OFF..outer_hdr::N_DOCS_OFF + U64_BYTES]);
        let dir_offset =
            read_u64_le(&header[outer_hdr::DIR_OFFSET_OFF..outer_hdr::DIR_OFFSET_OFF + U64_BYTES])
                as usize;
        let (dir_size, dir_end) = checked_dir_bounds(dir_offset, n_cells, CELL_DIR_ENTRY_SIZE)?;
        if dir_end > source.len() {
            return Err(VectorError::Read(ReadError::MalformedVersion(
                "multi-cell directory runs past blob".into(),
            )));
        }
        let dir_bytes = fetch_sync(&source, dir_offset..dir_offset + dir_size, "cell directory")?;
        let dir_crc_bytes = fetch_sync(
            &source,
            dir_offset + dir_size..dir_offset + dir_size + format::CRC_BYTES,
            "cell directory crc",
        )?;
        let dir_crc_expected = read_u32_le(&dir_crc_bytes);
        if dir_crc_expected != crc32c(&dir_bytes) {
            return Err(VectorError::Read(ReadError::ChecksumMismatch {
                section: "vector/cell_directory",
                column: String::new(),
            }));
        }

        let cols_json: Vec<VectorColumnConfig> =
            serde_json::from_str(columns_json).map_err(|e| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "inf.vec.columns JSON: {e}"
                )))
            })?;
        if cols_json.len() != 1 {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "multi-cell blob requires exactly one logical column in inf.vec.columns, got {}",
                cols_json.len()
            ))));
        }
        let cfg = &cols_json[0];
        let metric = match cfg.metric.as_str() {
            "l2sq" => Metric::L2Sq,
            "cosine" => Metric::Cosine,
            "negdot" => Metric::NegDot,
            other => {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "unknown metric {other}"
                ))));
            }
        };
        let mut columns = Vec::with_capacity(n_cells);
        let mut cell_ids = Vec::with_capacity(n_cells);
        let mut flat_cluster_base = Vec::with_capacity(n_cells + 1);
        flat_cluster_base.push(0);
        let mut flat_total = 0u32;
        let mut packed_codec = None;

        for i in 0..n_cells {
            let entry_off = i * CELL_DIR_ENTRY_SIZE;
            let cell_id = read_u32_le(
                &dir_bytes[entry_off + cell_dir_entry::CELL_ID_OFF
                    ..entry_off + cell_dir_entry::CELL_ID_OFF + U32_BYTES],
            );
            let subsection_off = read_u64_le(
                &dir_bytes[entry_off + cell_dir_entry::SUBSECTION_OFF_OFF
                    ..entry_off + cell_dir_entry::SUBSECTION_OFF_OFF + U64_BYTES],
            ) as usize;
            let subsection_len = read_u64_le(
                &dir_bytes[entry_off + cell_dir_entry::SUBSECTION_LEN_OFF
                    ..entry_off + cell_dir_entry::SUBSECTION_LEN_OFF + U64_BYTES],
            ) as usize;
            let raw_codec = read_u32_le(
                &dir_bytes[entry_off + cell_dir_entry::CODEC_ID_OFF
                    ..entry_off + cell_dir_entry::CODEC_ID_OFF + U32_BYTES],
            );
            let rerank_codec = match raw_codec {
                value if value == u32::from(RerankCodec::Sq8Residual.codec_id()) => {
                    RerankCodec::Sq8Residual
                }
                value if value == u32::from(RerankCodec::Sq8FixedResidual.codec_id()) => {
                    RerankCodec::Sq8FixedResidual
                }
                value if value == u32::from(RerankCodec::Sq16.codec_id()) => RerankCodec::Sq16,
                value if value == u32::from(RerankCodec::Sq16Adaptive.codec_id()) => {
                    RerankCodec::Sq16Adaptive
                }
                _ => {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "multi-cell directory has unknown rerank codec id {raw_codec}"
                    ))));
                }
            };
            if let Some(expected) = packed_codec
                && expected != rerank_codec
            {
                return Err(VectorError::Read(ReadError::MalformedVersion(
                    "multi-cell directory mixes rerank codecs".into(),
                )));
            }
            packed_codec = Some(rerank_codec);
            if !rerank_codec.supports_metric(metric) {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "multi-cell codec {} supports cosine metric only",
                    rerank_codec.name()
                ))));
            }
            if i > 0 && cell_id <= cell_ids[i - 1] {
                return Err(VectorError::Read(ReadError::MalformedVersion(
                    "multi-cell directory cell_ids must be strictly ascending".into(),
                )));
            }
            let col = Self::parse_cell_subsection(
                &source,
                cfg,
                metric,
                rerank_codec,
                subsection_off,
                subsection_len,
                opts.verify_crc,
            )?;
            // Checked, not saturating: a malformed directory whose summed
            // cluster count overflows u32 would otherwise saturate and alias
            // every later cell onto the same flat base in
            // `resolve_flat_cluster`.
            flat_total = flat_total.checked_add(col.n_cent).ok_or_else(|| {
                VectorError::Read(ReadError::MalformedVersion(
                    "multi-cell directory total cluster count exceeds u32".into(),
                ))
            })?;
            flat_cluster_base.push(flat_total);
            columns.push(col);
            cell_ids.push(cell_id);
        }

        // The outer header is NOT covered by the directory CRC, so cross-check
        // its `n_docs` against the summed per-cell doc counts (derived from
        // the validated subsection geometry). A corrupt header — e.g. a
        // bit-flipped `n_docs` of 0 — must fail the open instead of opening
        // clean and silently returning empty results.
        let summed_docs: u64 = columns.iter().map(|col| u64::from(col.n_docs)).sum();
        // File-local ids are u32 throughout the read paths; a blob whose
        // summed cell docs exceed that space would silently wrap routing
        // and bitmap remaps.
        if summed_docs > u64::from(u32::MAX) {
            return Err(VectorError::Read(ReadError::MalformedVersion(
                "multi-cell blob doc count exceeds u32 local-doc-id space".into(),
            )));
        }
        if n_docs != summed_docs {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "multi-cell outer header n_docs={n_docs} != summed cell docs {summed_docs}"
            ))));
        }
        // Trailing whole-blob CRC (header + directory + dir CRC + every cell
        // subsection), verified under the same opt-in flag the per-subsection
        // CRCs use — v1 verifies its outer CRC on this flag, and v2 must not
        // regress that integrity check on eager opens.
        if opts.verify_crc {
            let blob_len = source.len();
            if blob_len < format::CRC_BYTES {
                return Err(VectorError::Read(ReadError::MalformedVersion(
                    "multi-cell blob shorter than its trailing CRC".into(),
                )));
            }
            let crc_pos = blob_len - format::CRC_BYTES;
            let body = fetch_sync(&source, 0..crc_pos, "multi-cell outer crc body")?;
            let expected = read_u32_le(&fetch_sync(
                &source,
                crc_pos..blob_len,
                "multi-cell outer crc",
            )?);
            if expected != crc32c(&body) {
                return Err(VectorError::Read(ReadError::ChecksumMismatch {
                    section: "vector/multi_cell_outer",
                    column: cfg.column.clone(),
                }));
            }
        }

        let mut column_id_by_name = HashMap::new();
        column_id_by_name.insert(cfg.column.clone(), 0);

        Ok(VectorReader {
            source,
            n_docs,
            columns,
            column_id_by_name,
            cell_ids,
            flat_cluster_base,
            cold_stable_id_region: std::sync::Mutex::new(None),
        })
    }

    /// Lazy open for a v2 multi-cell blob: prefetch header + cell directory,
    /// then hand off to the sync multi-cell open over a prefetched overlay.
    ///
    /// Honors `opts.verify_crc`: when true, the full blob is fetched so the
    /// outer + per-subsection CRC checks in [`Self::open_multi_cell_with_source`]
    /// can run (same contract as v1 [`Self::open_lazy`]).
    async fn open_lazy_multi_cell(
        source: Arc<dyn LazyByteSource>,
        columns_json: &str,
        header_bytes: Bytes,
        blob_size: usize,
        opts: OpenOptions,
    ) -> Result<Self, VectorError> {
        let n_cells =
            read_u32_le(&header_bytes[outer_hdr::N_CELLS_OFF..outer_hdr::N_CELLS_OFF + U32_BYTES])
                as usize;
        let dir_offset = read_u64_le(
            &header_bytes[outer_hdr::DIR_OFFSET_OFF..outer_hdr::DIR_OFFSET_OFF + U64_BYTES],
        ) as usize;
        let (dir_size, dir_end) = checked_dir_bounds(dir_offset, n_cells, CELL_DIR_ENTRY_SIZE)?;
        if dir_end > blob_size {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "lazy multi-cell: directory end {dir_end} exceeds blob size {blob_size}",
            ))));
        }

        // CRC-on: one full-blob GET so sync CRC verification can
        // `fetch_sync` contiguous ranges (PrefetchedSource does not stitch).
        if opts.verify_crc {
            let full = source.range(0, blob_size as u64).await.map_err(|e| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "lazy multi-cell: full-blob CRC fetch: {e}"
                )))
            })?;
            let mut overlay = PrefetchedSource::new(Arc::clone(&source));
            overlay.install(0, full);
            return Self::open_multi_cell_with_source(
                Source::Lazy(Arc::new(overlay)),
                columns_json,
                opts,
                header_bytes,
            );
        }

        let dir_prefetch = source
            .range(dir_offset as u64, (dir_end - dir_offset) as u64)
            .await
            .map_err(|e| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "lazy multi-cell: directory fetch: {e}"
                )))
            })?;
        let dir_bytes_slice = &dir_prefetch[0..dir_size];
        let dir_crc_expected = read_u32_le(&dir_prefetch[dir_size..dir_size + format::CRC_BYTES]);
        if dir_crc_expected != crc32c(dir_bytes_slice) {
            return Err(VectorError::Read(ReadError::ChecksumMismatch {
                section: "vector/cell_directory",
                column: String::new(),
            }));
        }

        // Prefetch each cell's open-time region (sub-header through
        // per_cluster_blocks_off) so structural decode is resident.
        let mut overlay = PrefetchedSource::new(Arc::clone(&source));
        overlay.install(0, header_bytes.clone());
        overlay.install(dir_offset as u64, dir_prefetch.clone());

        // v1 open discipline: fetch each cell's sub-header + cluster index
        // only. Centroids, Sq8 meta/norms, and the stable-id region stay on
        // disk and are read per probed cell through the block cache (the
        // parse below takes its lazy Sq8-meta arm when the bytes are not
        // resident). The multi-cell contract is one logical column, whose
        // dim bounds the cluster index.
        let cols: Vec<VectorColumnConfig> = serde_json::from_str(columns_json).map_err(|e| {
            VectorError::Read(ReadError::MalformedVersion(format!(
                "inf.vec.columns JSON: {e}"
            )))
        })?;
        let dim = match cols.as_slice() {
            [only] if only.dim > 0 => only.dim,
            _ => {
                return Err(VectorError::Read(ReadError::MalformedVersion(
                    "multi-cell blob requires exactly one logical column".into(),
                )));
            }
        };
        for i in 0..n_cells {
            let entry_off = i * CELL_DIR_ENTRY_SIZE;
            let subsection_off = read_u64_le(
                &dir_bytes_slice[entry_off + cell_dir_entry::SUBSECTION_OFF_OFF
                    ..entry_off + cell_dir_entry::SUBSECTION_OFF_OFF + U64_BYTES],
            );
            let subsection_len = read_u64_le(
                &dir_bytes_slice[entry_off + cell_dir_entry::SUBSECTION_LEN_OFF
                    ..entry_off + cell_dir_entry::SUBSECTION_LEN_OFF + U64_BYTES],
            );
            if subsection_len < SUB_HEADER_SIZE as u64 + format::CRC_BYTES as u64 {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "multi-cell subsection {i} too short ({subsection_len} bytes)"
                ))));
            }
            let sub_hdr_bytes = source
                .range(subsection_off, SUB_HEADER_SIZE as u64)
                .await
                .map_err(|e| {
                    VectorError::Read(ReadError::MalformedVersion(format!(
                        "lazy multi-cell: sub-header {i}: {e}"
                    )))
                })?;
            overlay.install(subsection_off, sub_hdr_bytes.clone());
            let centroids_off = read_u64_le(
                &sub_hdr_bytes[sub_hdr::CENTROIDS_OFF_OFF..sub_hdr::CENTROIDS_OFF_OFF + U64_BYTES],
            );
            let cluster_idx_off = read_u64_le(
                &sub_hdr_bytes
                    [sub_hdr::CLUSTER_IDX_OFF_OFF..sub_hdr::CLUSTER_IDX_OFF_OFF + U64_BYTES],
            );
            let centroids_span = cluster_idx_off.checked_sub(centroids_off).ok_or_else(|| {
                VectorError::Read(ReadError::MalformedVersion(format!(
                    "multi-cell subsection {i}: cluster_idx_off precedes centroids_off"
                )))
            })?;
            if !centroids_span.is_multiple_of(dim as u64 * 4) {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "multi-cell subsection {i}: centroids region not divisible by dim*4"
                ))));
            }
            let n_cent = centroids_span / (dim as u64 * 4);
            let idx_len = n_cent * CLUSTER_IDX_ENTRY_BYTES as u64;
            if idx_len > 0 {
                let idx_bytes = source
                    .range(subsection_off + cluster_idx_off, idx_len)
                    .await
                    .map_err(|e| {
                        VectorError::Read(ReadError::MalformedVersion(format!(
                            "lazy multi-cell: cluster index {i}: {e}"
                        )))
                    })?;
                overlay.install(subsection_off + cluster_idx_off, idx_bytes);
            }
        }

        Self::open_multi_cell_with_source(
            Source::Lazy(Arc::new(overlay)),
            columns_json,
            opts,
            header_bytes,
        )
    }

    /// Parse one complete cell-IVF subsection into a [`ColumnReader`].
    fn parse_cell_subsection(
        source: &Source,
        cfg: &VectorColumnConfig,
        metric: Metric,
        rerank_codec: RerankCodec,
        subsection_off: usize,
        subsection_len: usize,
        verify_crc: bool,
    ) -> Result<ColumnReader, VectorError> {
        let dim = cfg.dim;
        let rot_seed = cfg.rot_seed;
        if !rerank_codec.supports_metric(metric) {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "cell codec {} supports cosine metric only",
                rerank_codec.name()
            ))));
        }
        // Checked: both values come from on-disk metadata, so the sum can
        // overflow before the bounds check and wrap to a bogus in-range end.
        let sub_end = subsection_off.checked_add(subsection_len).ok_or_else(|| {
            VectorError::Read(ReadError::MalformedVersion(
                "cell subsection offset + length overflows".into(),
            ))
        })?;
        if sub_end > source.len() {
            return Err(VectorError::Read(ReadError::MalformedVersion(
                "cell subsection runs past blob".into(),
            )));
        }
        if subsection_len < SUB_HEADER_SIZE + format::CRC_BYTES {
            return Err(VectorError::Read(ReadError::MalformedVersion(
                "cell subsection too short".into(),
            )));
        }
        let sub_header = fetch_sync(
            source,
            subsection_off..subsection_off + SUB_HEADER_SIZE,
            "cell sub_header",
        )?;
        if &sub_header[0..MAGIC_BYTES] != format::vec::SUB_MAGIC {
            return Err(VectorError::Read(ReadError::BadMagic {
                section: "vector/subsection",
                expected: format::vec::SUB_MAGIC,
                actual: sub_header[0..MAGIC_BYTES].to_vec(),
            }));
        }
        let subsection_version =
            read_u32_le(&sub_header[sub_hdr::VERSION_OFF..sub_hdr::VERSION_OFF + U32_BYTES]);
        if subsection_version != format::vec::SUBSECTION_VERSION {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "cell subsection unsupported layout version {subsection_version}"
            ))));
        }
        if verify_crc {
            let body = fetch_sync(source, subsection_off..sub_end, "cell subsection crc")?;
            let crc_pos = subsection_len - format::CRC_BYTES;
            let expected = read_u32_le(&body[crc_pos..crc_pos + format::CRC_BYTES]);
            if expected != crc32c(&body[..crc_pos]) {
                return Err(VectorError::Read(ReadError::ChecksumMismatch {
                    section: "vector/subsection",
                    column: cfg.column.clone(),
                }));
            }
        }

        let quant = BitQuantizer::new(dim);
        let code_bytes = quant.code_bytes();
        let per_vec_bytes = rerank_codec.per_vector_bytes(dim);
        let codec_meta_size = read_u32_le(
            &sub_header[sub_hdr::CODEC_META_SIZE_OFF..sub_hdr::CODEC_META_SIZE_OFF + U32_BYTES],
        ) as usize;
        let summary_off = read_u64_le(
            &sub_header[sub_hdr::SUMMARY_OFF_OFF..sub_hdr::SUMMARY_OFF_OFF + U64_BYTES],
        ) as usize;
        let centroids_off = read_u64_le(
            &sub_header[sub_hdr::CENTROIDS_OFF_OFF..sub_hdr::CENTROIDS_OFF_OFF + U64_BYTES],
        ) as usize;
        let cluster_idx_off = read_u64_le(
            &sub_header[sub_hdr::CLUSTER_IDX_OFF_OFF..sub_hdr::CLUSTER_IDX_OFF_OFF + U64_BYTES],
        ) as usize;
        let per_cluster_blocks_off = read_u64_le(
            &sub_header[sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF
                ..sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF + U64_BYTES],
        ) as usize;

        // Untrusted sub-header offsets: validate ordering with checked
        // arithmetic — inverted offsets must surface as MalformedVersion,
        // not an integer underflow, because the CRC-off lazy open path
        // (object-store reads) skips the checksum that would otherwise
        // catch the corruption.
        let centroids_span = cluster_idx_off.checked_sub(centroids_off).ok_or_else(|| {
            VectorError::Read(ReadError::MalformedVersion(
                "cell subsection offsets inverted: cluster_idx_off precedes centroids_off".into(),
            ))
        })?;
        if dim == 0 || !centroids_span.is_multiple_of(dim * 4) {
            return Err(VectorError::Read(ReadError::MalformedVersion(
                "cell subsection centroids region not divisible by dim*4".into(),
            )));
        }
        let n_cent = centroids_span / (dim * 4);
        let n_cent_u32 = n_cent as u32;
        let cluster_idx_size = n_cent * CLUSTER_IDX_ENTRY_BYTES;
        let codec_meta_off = if codec_meta_size == 0 {
            0
        } else {
            cluster_idx_off + cluster_idx_size
        };
        let preceding_end = if codec_meta_size == 0 {
            cluster_idx_off + cluster_idx_size
        } else {
            codec_meta_off + codec_meta_size
        };
        let sub_crc_pos = subsection_len - format::CRC_BYTES;
        let stable_ids_region_bytes = per_cluster_blocks_off
            .checked_sub(preceding_end)
            .ok_or_else(|| {
                VectorError::Read(ReadError::MalformedVersion(
                    "cell subsection regions overrun per_cluster_blocks_off".into(),
                ))
            })?;
        let blocks_region_size =
            sub_crc_pos
                .checked_sub(per_cluster_blocks_off)
                .ok_or_else(|| {
                    VectorError::Read(ReadError::MalformedVersion(
                        "cell subsection offsets inverted: per_cluster_blocks_off past the \
                     subsection CRC"
                            .into(),
                    ))
                })?;
        let per_doc_stride = code_bytes + format::vec::DOC_ID_BYTES + per_vec_bytes;
        if per_doc_stride == 0 || !blocks_region_size.is_multiple_of(per_doc_stride) {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "cell subsection blocks region {blocks_region_size} not divisible by stride {per_doc_stride}"
            ))));
        }
        let col_n_docs = (blocks_region_size / per_doc_stride) as u32;
        let expected_stable_ids_bytes = (col_n_docs as usize) * format::vec::STABLE_ID_BYTES;
        if stable_ids_region_bytes != 0 && stable_ids_region_bytes != expected_stable_ids_bytes {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "cell subsection stable-id gap {stable_ids_region_bytes}, expected 0 or {expected_stable_ids_bytes}"
            ))));
        }
        let stable_ids_off = (stable_ids_region_bytes != 0).then_some(preceding_end);
        let expected_codec_meta_size =
            rerank_codec.codec_meta_bytes(dim, col_n_docs as usize, n_cent, metric);
        if codec_meta_size != expected_codec_meta_size {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "cell subsection codec_meta_size={codec_meta_size}, expected {expected_codec_meta_size}"
            ))));
        }

        let sq8_meta = if rerank_codec.carries_cluster_quant_meta() {
            let meta_abs_start = subsection_off + codec_meta_off;
            let meta_abs_end = meta_abs_start + codec_meta_size;
            let so_block_bytes = n_cent * dim * 4;
            let scale_end = so_block_bytes;
            let offset_end = scale_end + so_block_bytes;
            if let Some(meta_bytes) = source.try_get_range_sync(meta_abs_start..meta_abs_end) {
                let scale = parse_f32_le_vec(&meta_bytes[0..scale_end]);
                let offset = parse_f32_le_vec(&meta_bytes[scale_end..offset_end]);
                validate_quantizer_meta(rerank_codec, &scale, &offset, cfg.column.as_str())?;
                let per_doc_norms: Option<Arc<[f32]>> =
                    if matches!(metric, Metric::L2Sq | Metric::Cosine) {
                        let norms_end = offset_end + (col_n_docs as usize) * 4;
                        Some(Arc::from(parse_f32_le_vec(
                            &meta_bytes[offset_end..norms_end],
                        )))
                    } else {
                        None
                    };
                Some(Sq8ColumnMeta::Eager {
                    scale,
                    offset,
                    per_doc_norms,
                })
            } else {
                Some(Sq8ColumnMeta::Lazy {
                    scale_abs_off: meta_abs_start,
                    offset_abs_off: meta_abs_start + scale_end,
                    norms_abs_off: matches!(metric, Metric::L2Sq | Metric::Cosine)
                        .then_some(meta_abs_start + offset_end),
                })
            }
        } else if rerank_codec.is_sq16() && matches!(metric, Metric::L2Sq | Metric::Cosine) {
            // Sq16: per-doc norm table only (no scale/offset), carried in
            // the shared `Sq8ColumnMeta` so norm indexing matches the
            // residual family. See the main open path for the rationale.
            let meta_abs_start = subsection_off + codec_meta_off;
            let norms_end = meta_abs_start + (col_n_docs as usize) * 4;
            if let Some(meta_bytes) = source.try_get_range_sync(meta_abs_start..norms_end) {
                Some(Sq8ColumnMeta::Eager {
                    scale: Vec::new(),
                    offset: Vec::new(),
                    per_doc_norms: Some(Arc::from(parse_f32_le_vec(&meta_bytes))),
                })
            } else {
                Some(Sq8ColumnMeta::Lazy {
                    scale_abs_off: meta_abs_start,
                    offset_abs_off: meta_abs_start,
                    norms_abs_off: Some(meta_abs_start),
                })
            }
        } else {
            None
        };

        Ok(ColumnReader {
            transposed_codes: Arc::new(TransposedCodeCache::default()),
            name: cfg.column.clone(),
            dim,
            n_cent: n_cent_u32,
            n_docs: col_n_docs,
            metric,
            rot_seed,
            rerank_codec,
            sq8_meta,
            lazy_sq8_parsed: OnceLock::new(),
            subsection_range: subsection_off..sub_end,
            summary_off,
            centroids_off,
            cluster_idx_off,
            codec_meta_off,
            per_cluster_blocks_off,
            stable_ids_off,
            quant,
            rot: RandomRotation::new(dim, rot_seed),
        })
    }

    /// Packed global cell ids for a multi-cell blob (empty for v1).
    pub(crate) fn packed_cell_ids(&self) -> &[u32] {
        &self.cell_ids
    }

    /// Whether this reader is a v2 multi-cell pack.
    pub(crate) fn is_multi_cell(&self) -> bool {
        !self.cell_ids.is_empty()
    }

    /// Map a flat cluster id (manifest / query fan-out) to
    /// `(cell_column_index, local_cluster)` for multi-cell blobs.
    pub(crate) fn resolve_flat_cluster(&self, flat: u32) -> Option<(usize, u32)> {
        if self.flat_cluster_base.is_empty() {
            // v1 single IVF: bound against the only column's cluster count so
            // a stale/corrupt routing id can't leak an out-of-range cluster
            // into downstream indexing. (The multi-cell path below is bounded
            // by the prefix sums, which are built from the same n_cents.)
            let col = self.columns.first()?;
            return (flat < col.n_cent).then_some((0, flat));
        }
        // flat_cluster_base is prefix sums of length n_cells+1.
        let bases = &self.flat_cluster_base;
        if flat >= *bases.last()? {
            return None;
        }
        let mut lo = 0usize;
        let mut hi = bases.len() - 1;
        while lo + 1 < hi {
            let mid = (lo + hi) / 2;
            if bases[mid] <= flat {
                lo = mid;
            } else {
                hi = mid;
            }
        }
        Some((lo, flat - bases[lo]))
    }

    pub fn n_docs(&self) -> u64 {
        self.n_docs
    }

    pub fn vector_columns(&self) -> impl Iterator<Item = &str> {
        self.columns.iter().map(|c| c.name.as_str())
    }
    pub fn vector_columns_config(&self) -> impl Iterator<Item = &ColumnReader> {
        self.columns.iter()
    }

    pub(crate) fn public_rerank_mult(&self, _column: &str, base: usize) -> usize {
        base
    }

    /// Per-column summary centroid, copied into the manifest's
    /// per-superfile [`VectorSummary`] at commit time.
    ///
    /// For multi-cell (v2) blobs this is the **doc-weighted mean** of each
    /// packed cell's IVF summary centroid (weights = cell `n_docs`), so
    /// superfile skip ordering sees the whole packed object — not only the
    /// first cell in the directory.
    pub fn summary(&self, column: &str) -> Option<Vec<f32>> {
        if !self.column_id_by_name.contains_key(column) {
            return None;
        }
        if self.is_multi_cell() {
            let dim = self.columns.first()?.dim;
            let mut acc = vec![0.0f64; dim];
            let mut total_docs = 0u64;
            for col in &self.columns {
                let sub = self
                    .source
                    .try_get_range_sync(col.subsection_range.clone())?;
                let mut cell_summary = vec![0f32; dim];
                decode_f32_le_into(
                    &sub[col.summary_off..col.summary_off + dim * 4],
                    &mut cell_summary,
                );
                let w = f64::from(col.n_docs);
                if w > 0.0 {
                    for (a, &v) in acc.iter_mut().zip(cell_summary.iter()) {
                        *a += f64::from(v) * w;
                    }
                    total_docs = total_docs.saturating_add(u64::from(col.n_docs));
                }
            }
            if total_docs == 0 {
                return Some(vec![0.0; dim]);
            }
            let inv = 1.0 / total_docs as f64;
            return Some(acc.iter().map(|&a| (a * inv) as f32).collect());
        }
        let cid = *self.column_id_by_name.get(column)?;
        let col = &self.columns[cid as usize];
        // byte access routed through `Source::try_get_range_sync`
        // — zero-copy on `InMemory`, lazy on `Source::Lazy`.
        let sub = self
            .source
            .try_get_range_sync(col.subsection_range.clone())?;
        let off = col.summary_off;
        let dim = col.dim;
        let mut centroid = vec![0f32; dim];
        decode_f32_le_into(&sub[off..off + dim * 4], &mut centroid);
        Some(centroid)
    }

    /// The column's per-cluster IVF centroids (fp32, cluster-major,
    /// `n_cent * dim`) plus each cluster's indexed doc count. Returns
    /// `(n_cent, dim, centroids, counts)`. Used by the writer to stage
    /// quantized cluster centroids into the manifest for cross-superfile
    /// global cluster selection. `None` if the column is unknown or the
    /// centroid/cluster_idx bytes aren't resident.
    ///
    /// For multi-cell (v2) blobs, concatenates fine centroids across all
    /// packed cells in cell-directory order so flat cluster ids match
    /// [`Self::resolve_flat_cluster`] / [`Self::search_clusters_async`].
    pub fn cluster_centroids(&self, column: &str) -> Option<(u32, u32, Vec<f32>, Vec<u32>)> {
        if !self.column_id_by_name.contains_key(column) {
            return None;
        }
        if self.is_multi_cell() {
            let dim = self.columns.first()?.dim;
            let mut centroids = Vec::new();
            let mut counts = Vec::new();
            for col in &self.columns {
                let sub = self
                    .source
                    .try_get_range_sync(col.subsection_range.clone())?;
                let n_cent = col.n_cent as usize;
                let stride = dim * 4;
                for c in 0..n_cent {
                    let base = col.centroids_off + c * stride;
                    let mut buf = vec![0f32; dim];
                    decode_f32_le_into(&sub[base..base + stride], &mut buf);
                    centroids.extend_from_slice(&buf);
                    let b = col.cluster_idx_off
                        + c * CLUSTER_IDX_ENTRY_BYTES
                        + CLUSTER_IDX_COUNT_OFFSET;
                    counts.push(u32::from_le_bytes([
                        sub[b],
                        sub[b + 1],
                        sub[b + 2],
                        sub[b + 3],
                    ]));
                }
            }
            let n_cent = counts.len() as u32;
            return Some((n_cent, dim as u32, centroids, counts));
        }
        let cid = *self.column_id_by_name.get(column)?;
        let col = &self.columns[cid as usize];
        let sub = self
            .source
            .try_get_range_sync(col.subsection_range.clone())?;
        let n_cent = col.n_cent as usize;
        let dim = col.dim;
        let stride = dim * 4;

        // Centroids: fp32, cluster-major, at `centroids_off`.
        let mut centroids = vec![0f32; n_cent * dim];
        for c in 0..n_cent {
            let base = col.centroids_off + c * stride;
            decode_f32_le_into(
                &sub[base..base + stride],
                &mut centroids[c * dim..(c + 1) * dim],
            );
        }

        // cluster_idx: `n_cent` × `(doc_off: u32, count: u32)`; we want
        // the count (second u32 of each 8-byte entry).
        let mut counts = Vec::with_capacity(n_cent);
        for c in 0..n_cent {
            let b = col.cluster_idx_off + c * CLUSTER_IDX_ENTRY_BYTES + CLUSTER_IDX_COUNT_OFFSET;
            counts.push(u32::from_le_bytes([
                sub[b],
                sub[b + 1],
                sub[b + 2],
                sub[b + 3],
            ]));
        }

        Some((col.n_cent, dim as u32, centroids, counts))
    }

    /// Per-cell fine centroids without flattening away global-cell ownership.
    /// Legacy single-IVF columns return one unscoped (`None`) group.
    pub(crate) fn cluster_centroids_by_cell(
        &self,
        column: &str,
    ) -> Option<Vec<(Option<u32>, u32, u32, Vec<f32>, Vec<u32>)>> {
        if !self.column_id_by_name.contains_key(column) {
            return None;
        }
        if !self.is_multi_cell() {
            let (n_cent, dim, centroids, counts) = self.cluster_centroids(column)?;
            return Some(vec![(None, n_cent, dim, centroids, counts)]);
        }
        let mut out = Vec::with_capacity(self.columns.len());
        for (index, col) in self.columns.iter().enumerate() {
            let sub = self
                .source
                .try_get_range_sync(col.subsection_range.clone())?;
            let n_cent = col.n_cent as usize;
            let dim = col.dim;
            let stride = dim * 4;
            let mut centroids = vec![0f32; n_cent * dim];
            let mut counts = Vec::with_capacity(n_cent);
            for cluster in 0..n_cent {
                let base = col.centroids_off + cluster * stride;
                decode_f32_le_into(
                    &sub[base..base + stride],
                    &mut centroids[cluster * dim..(cluster + 1) * dim],
                );
                let count_offset = col.cluster_idx_off
                    + cluster * CLUSTER_IDX_ENTRY_BYTES
                    + CLUSTER_IDX_COUNT_OFFSET;
                counts.push(u32::from_le_bytes([
                    sub[count_offset],
                    sub[count_offset + 1],
                    sub[count_offset + 2],
                    sub[count_offset + 3],
                ]));
            }
            out.push((
                self.cell_ids.get(index).copied(),
                col.n_cent,
                dim as u32,
                centroids,
                counts,
            ));
        }
        Some(out)
    }

    /// One inline stable id, indexed by cell-local doc id. The single
    /// owner of the region's 16-byte-le indexed read (the docs already
    /// referred to this helper before it existed); `None` past the region
    /// end.
    pub(crate) fn stable_id_at(region: &[u8], local: usize) -> Option<i128> {
        let start = local.checked_mul(STABLE_ID_BYTES)?;
        let end = start.checked_add(STABLE_ID_BYTES)?;
        Some(i128::from_le_bytes(
            region.get(start..end)?.try_into().ok()?,
        ))
    }

    /// Per-cell view for depth calibration: the raw fp32 centroid-region
    /// bytes (ranked by the shared [`nearest_k_centroids_bytes`] scan —
    /// no decode here) plus a stable-id -> fine-cluster map covering every
    /// row of the cell, walked with the shared [`read_cluster_entry`] /
    /// [`Self::stable_id_at`] parsers. Resident-only by design — the drain
    /// observes freshly built shard bytes, where every range resolves
    /// synchronously; `None` on any non-resident range (never a fetch), and
    /// on single-cell v1 blobs (no cell ids to observe; the hidden drain
    /// never produces them).
    pub(crate) fn cell_fine_calibration_views(
        &self,
        column: &str,
    ) -> Option<Vec<CellFineCalibrationView>> {
        // In the packed multi-cell layout each subsection entry IS one grid
        // cell of the SINGLE indexed vector column (`cell_ids` is parallel
        // to `columns` — see the round-trip in the drain's cell build), so
        // walking every entry is walking that column's cells, not mixing
        // columns. The name gate rejects readers that don't carry the
        // requested column; a second vector column cannot reach this path
        // (the hidden index is single-column by construction, resolved by
        // name at the calibration entry).
        if !self.is_multi_cell() || !self.column_id_by_name.contains_key(column) {
            return None;
        }
        let mut out = Vec::with_capacity(self.columns.len());
        for (index, col) in self.columns.iter().enumerate() {
            if col.n_docs == 0 || col.n_cent == 0 {
                continue;
            }
            let sub = self
                .source
                .try_get_range_sync(col.subsection_range.clone())?;
            let n_fine = col.n_cent as usize;
            // Corrupt-offset hardening: every region bound is validated
            // before slicing — observation degrades to `None` (the caller's
            // documented keep-previous-law fallback) instead of panicking
            // on a malformed subsection.
            let centroids_len = n_fine.checked_mul(col.dim)?.checked_mul(F32_BYTES)?;
            let centroids_end = col.centroids_off.checked_add(centroids_len)?;
            if centroids_end > sub.len() {
                return None;
            }
            let fine_centroids_bytes = sub.slice(col.centroids_off..centroids_end);
            // Stable ids are indexed by cell-local doc id; each cluster's
            // doc-id block names its member rows' cell-local ids — walking
            // both yields stable id -> fine cluster for every row.
            let region = self
                .source
                .try_get_range_sync(col.stable_ids_region_range()?)?;
            let idx_end = col
                .cluster_idx_off
                .checked_add(n_fine.checked_mul(CLUSTER_IDX_ENTRY_BYTES)?)?;
            let idx_slice = sub.get(col.cluster_idx_off..idx_end)?;
            let mut cluster_of_stable = HashMap::with_capacity(col.n_docs as usize);
            for cluster in 0..n_fine {
                let (off, cnt) = read_cluster_entry(idx_slice, cluster);
                if cnt == 0 {
                    continue;
                }
                let block = self
                    .source
                    .try_get_range_sync(col.cluster_codes_doc_ids_range(off, cnt))?;
                let ids_start = (cnt as usize).checked_mul(col.quant.code_bytes())?;
                for i in 0..cnt as usize {
                    let p = ids_start.checked_add(i.checked_mul(DOC_ID_BYTES)?)?;
                    let id_bytes = block.get(p..p.checked_add(DOC_ID_BYTES)?)?;
                    let did = u32::from_le_bytes(id_bytes.try_into().ok()?);
                    let stable = Self::stable_id_at(&region, did as usize)?;
                    // One fine cluster per row, by construction — a stable
                    // id claimed by two cluster blocks is an inconsistent
                    // index, and calibrating depth from it would silently
                    // under-stamp the laws. Degrade the whole read.
                    if cluster_of_stable.insert(stable, cluster as u32).is_some() {
                        return None;
                    }
                }
            }
            // Completeness: every row of the cell must be mapped (inserts
            // are all-unique above, so len == the summed cluster counts).
            // A partial view is partial depth evidence — degrade instead
            // of calibrating from it.
            if cluster_of_stable.len() != col.n_docs as usize {
                return None;
            }
            // This walk is multi-cell-only (gated above), so every subsection
            // must have a cell-id entry — a missing one is inconsistent
            // metadata, and observations mapped without a cell would be
            // silently dropped downstream. Degrade the whole read instead.
            out.push(CellFineCalibrationView {
                cell_id: Some(self.cell_ids.get(index).copied()?),
                dim: col.dim,
                n_fine,
                fine_centroids_bytes,
                cluster_of_stable,
            });
        }
        Some(out)
    }

    /// Remap a file-local allow/deny bitmap onto one packed cell's local
    /// id space (`0..n_docs`). IVF cluster blocks store cell-local ids;
    /// callers pass file-local bitmaps (parquet / packed-shard space).
    fn cell_local_filter_bitmap(
        &self,
        file_local: Option<&RoaringBitmap>,
        cell_idx: usize,
        doc_base: u32,
    ) -> Option<Arc<RoaringBitmap>> {
        let bm = file_local?;
        let n_docs = self.columns.get(cell_idx)?.n_docs;
        if n_docs == 0 {
            return Some(Arc::new(RoaringBitmap::new()));
        }
        let end = doc_base.saturating_add(n_docs);
        let mut local = RoaringBitmap::new();
        for file_id in bm.range(doc_base..end) {
            local.insert(file_id - doc_base);
        }
        Some(Arc::new(local))
    }

    /// Resolve the stable `_id` for each `local_doc_id` straight from the inline
    /// stable-`_id` region — no scalar `_id` column read. `None` when no column
    /// carries a region, or the region bytes are not resident (a lazy reader the
    /// caller hasn't warmed); the caller then falls back to the scalar column.
    ///
    /// For single-cell (v1) blobs the region is indexed by cell-local
    /// `local_doc_id`. For multi-cell (v2) packed shards, search / parquet use
    /// **file-local** ids (running sum of prior cells' `n_docs`); this maps
    /// each id onto the owning cell's region before indexing.
    pub(crate) fn inline_stable_ids_for_locals(&self, locals: &[u32]) -> Option<Vec<i128>> {
        if self.is_multi_cell() {
            // Per-cell regions; do not use `cold_stable_id_region` (that stash
            // holds at most one cell from the last probe wave).
            //
            // Batched cell mapping: prefix sums once, then a moving cursor —
            // callers pass locals in ascending runs (token_match order), so
            // the common case is O(1) per hit; out-of-order locals fall back
            // to a binary search. A per-hit linear `file_local_to_cell` walk
            // costs more than the entire posting scan at 1M hits. Regions are
            // fetched lazily per cell (once each) as the cursor enters them.
            let mut bases: Vec<u32> = Vec::with_capacity(self.columns.len() + 1);
            let mut running = 0u32;
            bases.push(0);
            for col in &self.columns {
                running = running.checked_add(col.n_docs)?;
                bases.push(running);
            }
            let mut out = vec![0i128; locals.len()];
            let mut cell_idx = 0usize;
            let mut region: Option<Bytes> = None;
            for (output_idx, &file_local) in locals.iter().enumerate() {
                if file_local >= running {
                    return None;
                }
                if file_local < bases[cell_idx] || file_local >= bases[cell_idx + 1] {
                    // partition_point: first base > file_local, minus one.
                    cell_idx = bases.partition_point(|&b| b <= file_local) - 1;
                    region = None;
                }
                if region.is_none() {
                    let col = &self.columns[cell_idx];
                    if !col.has_inline_stable_ids() {
                        return None;
                    }
                    region = Some(
                        self.source
                            .try_get_range_sync(col.stable_ids_region_range()?)?,
                    );
                }
                let region = region.as_ref()?;
                let cell_local = file_local - bases[cell_idx];
                out[output_idx] = Self::stable_id_at(region, cell_local as usize)?;
            }
            return Some(out);
        }
        let col = self.columns.iter().find(|c| c.has_inline_stable_ids())?;
        // Prefer the cold-path region stashed during the fan-out wave
        // (`cold_stable_id_region`); otherwise serve a resident (warm) slice.
        // Both are the full `i128 × n_docs` region, indexed identically below.
        let region = match self
            .cold_stable_id_region
            .lock()
            .ok()
            .and_then(|g| g.clone())
        {
            Some(bytes) => bytes,
            None => self
                .source
                .try_get_range_sync(col.stable_ids_region_range()?)?,
        };
        let mut out = Vec::with_capacity(locals.len());
        for &local in locals {
            out.push(Self::stable_id_at(&region, local as usize)?);
        }
        Some(out)
    }

    /// Async sibling of [`Self::inline_stable_ids_for_locals`] for the COLD
    /// path: when the inline `_id` region is present but **not resident** (a
    /// freshly-opened lazy reader — the search fetches centroids/cluster_idx
    /// and the per-cluster blocks, but never the region that sits between
    /// codec_meta and those blocks), the sync version returns `None` and the
    /// caller falls back to a scalar `_id` parquet decode. This variant
    /// `range_async`-fetches the region (one small contiguous read,
    /// `i128 × n_docs`) and indexes it — far cheaper than decoding the scalar
    /// `_id` pages, and the path the inline-`_id` region was built for.
    /// Returns `Ok(None)` when the column has no region (caller then uses the
    /// scalar column).
    pub(crate) async fn inline_stable_ids_for_locals_async(
        &self,
        locals: &[u32],
    ) -> Result<Option<Vec<i128>>, VectorError> {
        if self.is_multi_cell() {
            if !self.columns.iter().any(|c| c.has_inline_stable_ids()) {
                return Ok(None);
            }
            // Batched cell mapping — prefix sums + moving cursor, same as the
            // sync variant: a per-hit linear cell walk dominates everything
            // else at million-hit scale. Locals arrive in ascending runs;
            // out-of-order falls back to a binary search.
            let mut bases: Vec<u32> = Vec::with_capacity(self.columns.len() + 1);
            let mut running = 0u32;
            bases.push(0);
            for col in &self.columns {
                running = running.checked_add(col.n_docs).ok_or_else(|| {
                    VectorError::Read(ReadError::MalformedVersion(
                        "inline stable_id region: cell doc counts overflow".into(),
                    ))
                })?;
                bases.push(running);
            }
            let mut grouped: Vec<Vec<(usize, u32)>> = vec![Vec::new(); self.columns.len()];
            let mut cell_idx = 0usize;
            for (output_idx, &file_local) in locals.iter().enumerate() {
                if file_local >= running {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "inline stable_id region: file-local {file_local} out of range \
                         (n_docs={})",
                        self.n_docs
                    ))));
                }
                if file_local < bases[cell_idx] || file_local >= bases[cell_idx + 1] {
                    cell_idx = bases.partition_point(|&b| b <= file_local) - 1;
                }
                grouped[cell_idx].push((output_idx, file_local - bases[cell_idx]));
            }
            let mut requests = Vec::new();
            for (cell_idx, positions) in grouped.into_iter().enumerate() {
                if positions.is_empty() {
                    continue;
                }
                let col = &self.columns[cell_idx];
                let Some(range) = col.stable_ids_region_range() else {
                    return Ok(None);
                };
                requests.push((cell_idx, range, positions));
            }
            let fetched = try_join_all(requests.into_iter().map(
                |(cell_idx, range, positions)| async move {
                    let region = self
                        .source
                        .range_async(range)
                        .await
                        .map_err(|e| VectorError::LazySource(e.to_string()))?;
                    Ok::<_, VectorError>((cell_idx, positions, region))
                },
            ))
            .await?;
            let mut out = vec![0i128; locals.len()];
            for (cell_idx, positions, region) in fetched {
                for (output_idx, cell_local) in positions {
                    let p = (cell_local as usize) * format::vec::STABLE_ID_BYTES;
                    let end = p + format::vec::STABLE_ID_BYTES;
                    if end > region.len() {
                        return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                            "inline stable_id region: cell-local {cell_local} out of range \
                             ({} bytes, cell_idx={cell_idx})",
                            region.len()
                        ))));
                    }
                    let arr: [u8; format::vec::STABLE_ID_BYTES] =
                        region[p..end].try_into().map_err(|_| {
                            VectorError::Read(ReadError::MalformedVersion(
                                "inline stable_id region slice".into(),
                            ))
                        })?;
                    out[output_idx] = i128::from_le_bytes(arr);
                }
            }
            return Ok(Some(out));
        }
        let Some(col) = self.columns.iter().find(|c| c.has_inline_stable_ids()) else {
            return Ok(None);
        };
        let Some(range) = col.stable_ids_region_range() else {
            return Ok(None);
        };
        let region = self
            .source
            .range_async(range)
            .await
            .map_err(|e| VectorError::LazySource(e.to_string()))?;
        let mut out = Vec::with_capacity(locals.len());
        for &local in locals {
            let p = (local as usize) * format::vec::STABLE_ID_BYTES;
            let end = p + format::vec::STABLE_ID_BYTES;
            if end > region.len() {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "inline stable_id region: local {local} out of range ({} bytes)",
                    region.len()
                ))));
            }
            let arr: [u8; format::vec::STABLE_ID_BYTES] =
                region[p..end].try_into().map_err(|_| {
                    VectorError::Read(ReadError::MalformedVersion(
                        "inline stable_id region slice".into(),
                    ))
                })?;
            out.push(i128::from_le_bytes(arr));
        }
        Ok(Some(out))
    }

    /// Load one column's subsection and Sq8 meta for byte-splice compaction merge.
    pub(crate) fn sq8_ivf_merge_input(
        &self,
        column: &str,
        doc_id_offset: u32,
    ) -> Result<Sq8IvfMergeInput, BuildError> {
        // Fail closed on multi-cell blobs: `column_id_by_name` pins slot 0
        // there, so the name-based path would silently merge one packed cell
        // and drop the rest. Maintenance must address cells explicitly via
        // [`Self::sq8_ivf_merge_input_at`].
        if self.is_multi_cell() {
            return Err(BuildError::VectorSchemaMismatch(format!(
                "multi-cell blob for column {column}: use sq8_ivf_merge_input_at per packed cell"
            )));
        }
        let cid = *self
            .column_id_by_name
            .get(column)
            .ok_or_else(|| BuildError::VectorSchemaMismatch(format!("unknown column {column}")))?;
        self.sq8_ivf_merge_input_at(cid as usize, doc_id_offset)
    }

    /// Like [`Self::sq8_ivf_merge_input`], but addresses a packed cell by its
    /// column-slot index (multi-cell v2: one [`ColumnReader`] per cell).
    pub(crate) fn sq8_ivf_merge_input_at(
        &self,
        col_idx: usize,
        doc_id_offset: u32,
    ) -> Result<Sq8IvfMergeInput, BuildError> {
        let col = self.columns.get(col_idx).ok_or_else(|| {
            BuildError::VectorSchemaMismatch(format!("cell column index {col_idx} out of range"))
        })?;
        if !col.rerank_codec.is_ivf_mergeable() {
            return Err(BuildError::VectorRerankCodecUnimplemented {
                column: col.name.clone(),
                codec: col.rerank_codec.name(),
            });
        }
        let dim = col.dim;
        let so_block_bytes = (col.n_cent as usize) * dim * 4;
        let (scale, offset) = match &col.sq8_meta {
            Some(Sq8ColumnMeta::Eager { scale, offset, .. }) => (scale.clone(), offset.clone()),
            Some(Sq8ColumnMeta::Lazy {
                scale_abs_off,
                offset_abs_off,
                ..
            }) => {
                let scale_bytes = self
                    .source
                    .try_get_range_sync(*scale_abs_off..*scale_abs_off + so_block_bytes)
                    .ok_or(BuildError::VectorReadError)?;
                let offset_bytes = self
                    .source
                    .try_get_range_sync(*offset_abs_off..*offset_abs_off + so_block_bytes)
                    .ok_or(BuildError::VectorReadError)?;
                (
                    parse_f32_le_vec(scale_bytes.as_ref()),
                    parse_f32_le_vec(offset_bytes.as_ref()),
                )
            }
            _ => return Err(BuildError::VectorReadError),
        };
        let sub = self
            .source
            .try_get_range_sync(col.subsection_range.clone())
            .ok_or(BuildError::VectorReadError)?;
        // Checked reads: this path serves maintenance (compaction / drain
        // merge) over bytes whose CRC verification is opt-in, so a
        // truncated stable-id region must surface as a read error, not an
        // out-of-bounds panic.
        let stable_ids = col
            .stable_ids_off
            .map(|so| {
                (0..col.n_docs as usize)
                    .map(|local| {
                        let p = so + local * format::vec::STABLE_ID_BYTES;
                        sub.as_ref()
                            .get(p..p + format::vec::STABLE_ID_BYTES)
                            .and_then(|b| b.try_into().ok())
                            .map(i128::from_le_bytes)
                            .ok_or(BuildError::VectorReadError)
                    })
                    .collect::<Result<Vec<_>, BuildError>>()
            })
            .transpose()?;
        Ok(Sq8IvfMergeInput {
            sub: sub.as_ref().to_vec(),
            dim,
            n_cent: col.n_cent as usize,
            n_docs: col.n_docs,
            metric: col.metric,
            rerank_codec: col.rerank_codec,
            doc_id_offset,
            cluster_idx_off: col.cluster_idx_off,
            centroids_off: col.centroids_off,
            per_cluster_blocks_off: col.per_cluster_blocks_off,
            code_bytes: col.quant.code_bytes(),
            per_vec_bytes: col.rerank_codec.per_vector_bytes(dim),
            stride: col.per_cluster_doc_stride(),
            scale,
            offset,
            stable_ids,
        })
    }

    /// Sync materialize of one packed cell's IVF rows (eager / resident
    /// sources only — compaction opens inputs eagerly).
    pub(crate) fn materialized_cell_rows_at(
        &self,
        col_idx: usize,
    ) -> Result<Vec<MaterializedIvfRow>, BuildError> {
        let col = self.columns.get(col_idx).ok_or_else(|| {
            BuildError::VectorSchemaMismatch(format!("cell column index {col_idx} out of range"))
        })?;
        if !col.rerank_codec.is_ivf_mergeable() {
            return Err(BuildError::VectorRerankCodecUnimplemented {
                column: col.name.clone(),
                codec: col.rerank_codec.name(),
            });
        }
        let dim = col.dim;
        let so_block_bytes = (col.n_cent as usize) * dim * 4;
        let (scale, offset) = match &col.sq8_meta {
            Some(Sq8ColumnMeta::Eager { scale, offset, .. }) => (scale.clone(), offset.clone()),
            Some(Sq8ColumnMeta::Lazy {
                scale_abs_off,
                offset_abs_off,
                ..
            }) => {
                let scale_bytes = self
                    .source
                    .try_get_range_sync(*scale_abs_off..*scale_abs_off + so_block_bytes)
                    .ok_or(BuildError::VectorReadError)?;
                let offset_bytes = self
                    .source
                    .try_get_range_sync(*offset_abs_off..*offset_abs_off + so_block_bytes)
                    .ok_or(BuildError::VectorReadError)?;
                (
                    parse_f32_le_vec(scale_bytes.as_ref()),
                    parse_f32_le_vec(offset_bytes.as_ref()),
                )
            }
            _ => return Err(BuildError::VectorReadError),
        };
        let sub = self
            .source
            .try_get_range_sync(col.subsection_range.clone())
            .ok_or(BuildError::VectorReadError)?;
        Self::parse_materialized_index_rows(col, sub.as_ref(), &scale, &offset)
    }

    /// Async materialize of one cell column slot (v1 single column or one
    /// packed-cell subsection). Fetches via `range_async` so cold maintenance
    /// works when the subsection is not resident.
    pub(crate) async fn materialized_cell_rows_async_at(
        &self,
        col_idx: usize,
    ) -> Option<Vec<MaterializedIvfRow>> {
        let col = self.columns.get(col_idx)?;
        if !col.rerank_codec.is_ivf_mergeable() {
            return None;
        }
        let dim = col.dim;
        let so_block_bytes = (col.n_cent as usize) * dim * 4;
        let (scale_buf, offset_buf) = match &col.sq8_meta {
            Some(Sq8ColumnMeta::Eager { scale, offset, .. }) => (scale.clone(), offset.clone()),
            Some(Sq8ColumnMeta::Lazy {
                scale_abs_off,
                offset_abs_off,
                ..
            }) => {
                let scale_bytes = self
                    .source
                    .range_async(*scale_abs_off..*scale_abs_off + so_block_bytes)
                    .await
                    .ok()?;
                let offset_bytes = self
                    .source
                    .range_async(*offset_abs_off..*offset_abs_off + so_block_bytes)
                    .await
                    .ok()?;
                (
                    parse_f32_le_vec(scale_bytes.as_ref()),
                    parse_f32_le_vec(offset_bytes.as_ref()),
                )
            }
            _ => return None,
        };
        let sub = self
            .source
            .range_async(col.subsection_range.clone())
            .await
            .ok()?;
        Self::parse_materialized_index_rows(col, sub.as_ref(), &scale_buf, &offset_buf).ok()
    }

    /// Materialize packed cells as `(global_cell_id, rows)`.
    ///
    /// - `only_cells = None` → every cell in the directory (v1: one synthetic
    ///   cell using column 0; callers that need a cell id should prefer the
    ///   entry's `partition_hint`).
    /// - `only_cells = Some(&[…])` → only those global cell ids present in
    ///   this blob's cell directory.
    ///
    /// Row `local_doc_id` values stay **cell-local** (`0..n_docs` within that
    /// cell IVF). Callers that need file-local ids must offset by prior cells.
    pub(crate) async fn materialized_cells_rows_async(
        &self,
        only_cells: Option<&[u32]>,
    ) -> Option<Vec<(u32, Vec<MaterializedIvfRow>)>> {
        if self.is_multi_cell() {
            let mut out = Vec::new();
            for (ci, &cell_id) in self.cell_ids.iter().enumerate() {
                if only_cells.is_some_and(|want| !want.contains(&cell_id)) {
                    continue;
                }
                let rows = self.materialized_cell_rows_async_at(ci).await?;
                out.push((cell_id, rows));
            }
            Some(out)
        } else {
            // v1: single IVF. `only_cells` of length 1 names that cell; otherwise 0.
            let cell_id = only_cells.and_then(|c| c.first().copied()).unwrap_or(0);
            if let Some(want) = only_cells
                && !want.is_empty()
                && !want.contains(&cell_id)
            {
                return Some(Vec::new());
            }
            let rows = self.materialized_cell_rows_async_at(0).await?;
            Some(vec![(cell_id, rows)])
        }
    }

    /// Per packed cell: `(global_cell_id, inline stable ids in cell-local
    /// order)`. Diagnostic read for tests/benches auditing which cell the
    /// drain stored each row in. Multi-cell (v2) blobs only — returns `None`
    /// for v1 blobs or when any cell lacks an inline stable-id region.
    pub(crate) async fn packed_cell_stable_ids_async(
        &self,
    ) -> Result<Option<Vec<(u32, Vec<i128>)>>, VectorError> {
        if !self.is_multi_cell() {
            return Ok(None);
        }
        let mut out = Vec::with_capacity(self.cell_ids.len());
        for (ci, &cell_id) in self.cell_ids.iter().enumerate() {
            let col = &self.columns[ci];
            let Some(range) = col.stable_ids_region_range() else {
                return Ok(None);
            };
            let region = self
                .source
                .range_async(range)
                .await
                .map_err(|e| VectorError::LazySource(e.to_string()))?;
            // Exact-size check: a truncated region would silently yield fewer
            // ids than rows (partial mapping); trailing bytes mean the offsets
            // are wrong. Both are corruption — fail fast.
            let expected_len = (col.n_docs as usize) * format::vec::STABLE_ID_BYTES;
            if region.len() != expected_len {
                return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "inline stable_id region for cell {cell_id}: {} bytes, expected {expected_len}",
                    region.len()
                ))));
            }
            let mut ids = Vec::with_capacity(col.n_docs as usize);
            for chunk in region.as_ref().chunks_exact(format::vec::STABLE_ID_BYTES) {
                let arr: [u8; format::vec::STABLE_ID_BYTES] = chunk.try_into().map_err(|_| {
                    VectorError::Read(ReadError::MalformedVersion(
                        "inline stable_id region slice".into(),
                    ))
                })?;
                ids.push(i128::from_le_bytes(arr));
            }
            out.push((cell_id, ids));
        }
        Ok(Some(out))
    }

    /// Doc count for packed cell `cell_id`, or `None` if this blob does not
    /// contain that cell (v1 always returns column-0 `n_docs` when `cell_id`
    /// matches the caller's expected single cell — use [`Self::n_docs`] for
    /// the whole blob).
    pub(crate) fn packed_cell_n_docs(&self, cell_id: u32) -> Option<u32> {
        if self.is_multi_cell() {
            let ci = self.cell_ids.iter().position(|&c| c == cell_id)?;
            Some(self.columns.get(ci)?.n_docs)
        } else {
            Some(self.columns.first()?.n_docs)
        }
    }

    /// Read Sq8+ε rerank rows plus preserved 1-bit RaBitQ codes for maintenance
    /// rebuilds through the normal IVF writer (no fp32 reconstruction).
    ///
    /// Async because this is the OPANN drain/maintenance read-back: the hidden
    /// incoming superfile it reads is routinely evicted from the disk cache by
    /// the pre-drain search, so it must fetch-on-miss — and the drain's source
    /// (`StorageRangeSource`) has no sync-resident tier, so a resident-only read
    /// would spuriously fail. It fetches the subsection (and any non-resident
    /// Sq8 meta) via `range_async`, awaited directly on the caller's runtime,
    /// then parses the rows from those bytes. It deliberately avoids the sync
    /// `get_range` bridge, whose nested `block_in_place` + `block_on` deadlocks
    /// when called inside the drain's async task.
    ///
    /// For multi-cell (v2) blobs this concatenates **every** packed cell's rows
    /// in cell-directory order and remaps `local_doc_id` to file-local offsets
    /// (matching parquet id-column order). Prefer
    /// [`Self::materialized_cells_rows_async`] when only some cells are needed.
    pub(crate) async fn materialized_index_rows_async(
        &self,
        index_name: &str,
    ) -> Option<Vec<MaterializedIvfRow>> {
        if !self.column_id_by_name.contains_key(index_name) {
            return None;
        }
        if self.is_multi_cell() {
            let cells = self.materialized_cells_rows_async(None).await?;
            let mut out = Vec::new();
            let mut file_doc_base = 0u32;
            for (_cell_id, rows) in cells {
                let n = rows.len() as u32;
                for mut row in rows {
                    row.local_doc_id += file_doc_base;
                    out.push(row);
                }
                file_doc_base = file_doc_base.saturating_add(n);
            }
            return Some(out);
        }
        self.materialized_cell_rows_async_at(0).await
    }

    /// Decode every IVF row from `sub` (the full subsection bytes) using the
    /// column's per-cluster Sq8 `scale`/`offset`, carrying the inline stable
    /// `_id` when the subsection has the region. Pure/sync — fed pre-fetched
    /// bytes by [`Self::materialized_index_rows_async`].
    ///
    /// Fallible: every offset derived from the directory is bounds-checked
    /// against `sub`, so a truncated or corrupted subsection surfaces
    /// [`BuildError::VectorReadError`] instead of panicking the maintenance
    /// path (CRC verification is opt-in there).
    fn parse_materialized_index_rows(
        col: &ColumnReader,
        sub: &[u8],
        scale: &[f32],
        offset: &[f32],
    ) -> Result<Vec<MaterializedIvfRow>, BuildError> {
        let dim = col.dim;
        let code_bytes = col.quant.code_bytes();
        let stride = col.per_cluster_doc_stride();
        let id_bytes = format::vec::DOC_ID_BYTES;
        let per_vec = col.rerank_codec.per_vector_bytes(dim);
        let n_cent = col.n_cent as usize;
        let store_norm = matches!(col.metric, Metric::L2Sq | Metric::Cosine);
        let ops = col.rerank_codec.ops().ok_or(BuildError::VectorReadError)?;
        let u32_at = |p: usize| -> Result<u32, BuildError> {
            let bytes: [u8; 4] = sub
                .get(p..p + 4)
                .and_then(|s| s.try_into().ok())
                .ok_or(BuildError::VectorReadError)?;
            Ok(u32::from_le_bytes(bytes))
        };

        // Inline stable-`_id` region (relative offset into `sub`), when this is
        // a materialized/hidden-cell subsection. Lets the read-back carry the
        // stable `_id` straight from the blob instead of a `0` placeholder the
        // caller later overlays from a scalar `_id` column.
        let stable_ids_rel = col.stable_ids_off;

        let mut out = Vec::with_capacity(col.n_docs as usize);
        for c in 0..n_cent {
            let e = col.cluster_idx_off + c * CLUSTER_IDX_ENTRY_BYTES;
            let doc_off = u32_at(e)? as usize;
            let count = u32_at(e + CLUSTER_IDX_COUNT_OFFSET)? as usize;
            if count == 0 {
                continue;
            }
            let block = col.per_cluster_blocks_off + doc_off * stride;
            let doc_ids_at = block + count * code_bytes;
            let full_at = block + count * (code_bytes + id_bytes);
            // Shared per-cluster backing: each row clones the Arc (refcount bump),
            // not the dim-length scale/offset buffers. Only the residual family
            // carries per-cluster scale/offset (`n_cent * dim` each); single-plane
            // fixed-grid codecs (Sq16) decode off the fixed grid and store empty
            // scale/offset, so there is nothing to slice per cluster.
            let (sc, of): (std::sync::Arc<[f32]>, std::sync::Arc<[f32]>) =
                if col.rerank_codec.carries_cluster_quant_meta() {
                    (
                        std::sync::Arc::from(
                            scale
                                .get(c * dim..c * dim + dim)
                                .ok_or(BuildError::VectorReadError)?,
                        ),
                        std::sync::Arc::from(
                            offset
                                .get(c * dim..c * dim + dim)
                                .ok_or(BuildError::VectorReadError)?,
                        ),
                    )
                } else {
                    (
                        std::sync::Arc::from(Vec::new()),
                        std::sync::Arc::from(Vec::new()),
                    )
                };
            for i in 0..count {
                let local_id = u32_at(doc_ids_at + i * id_bytes)?;
                let rabitq = sub
                    .get(block + i * code_bytes..block + (i + 1) * code_bytes)
                    .ok_or(BuildError::VectorReadError)?
                    .to_vec();
                let rowb = full_at + i * per_vec;
                let row = sub
                    .get(rowb..rowb + per_vec)
                    .ok_or(BuildError::VectorReadError)?;
                let EncodedRowParts {
                    codes,
                    residuals,
                    norm_sq,
                } = ops.parse_materialized_row(row, dim, &sc, &of, store_norm);
                let stable_id = match stable_ids_rel {
                    Some(so) => {
                        let p = so + (local_id as usize) * format::vec::STABLE_ID_BYTES;
                        let bytes: [u8; format::vec::STABLE_ID_BYTES] = sub
                            .get(p..p + format::vec::STABLE_ID_BYTES)
                            .and_then(|s| s.try_into().ok())
                            .ok_or(BuildError::VectorReadError)?;
                        i128::from_le_bytes(bytes)
                    }
                    None => 0,
                };
                out.push(MaterializedIvfRow {
                    local_doc_id: local_id,
                    stable_id,
                    cluster: c as u32,
                    rabitq_code: rabitq,
                    encoded: EncodedCellRow {
                        stable_id,
                        rerank_codec: col.rerank_codec,
                        scale: sc.clone(),
                        offset: of.clone(),
                        codes,
                        residuals,
                        norm_sq,
                    },
                });
            }
        }
        Ok(out)
    }

    /// Single-column kNN search. Returns `(local_doc_id,
    /// distance)` sorted ascending by distance (smaller = closer
    /// for every metric).
    ///
    /// Sync — every public surface in `src/` is sync. Routes
    /// per-region byte
    /// access through [`Source::get_range`], which is itself
    /// sync and bridges to the underlying async
    /// `LazyByteSource::range` only on a cold `Source::Lazy`
    /// miss (via `block_in_place + Handle::block_on`, same
    /// pattern as `supertable::query::superfile_reader`). On
    /// `Source::InMemory` and on `Source::Lazy` warm caches
    /// (`BytesLazyByteSource`, mmap-backed) every fetch resolves
    /// zero-copy on the sync fast path.
    ///
    /// Range count per cold first search at `nprobe = 8` on the
    /// v0 layout:
    ///
    /// - 1 range for centroids (`n_cent × dim × 4` bytes)
    /// - 1 range for the cluster_idx header (`n_cent × 8` bytes)
    /// - `nprobe` ranges for per-cluster codes
    /// - `nprobe` ranges for per-cluster doc_ids
    /// - 1 fat range covering the rerank batch in `full[]` from
    ///   `min(pos)` to `max(pos) + 1`
    ///
    /// At `nprobe = 8`: 2 + 16 + 1 = **19 ranges**. Rerank `pos`
    /// is captured inline in the shortlist tuple at code-scoring
    /// time (each candidate's position is `off + i` where
    /// `(off, cnt)` is the cluster's entry and `i` is the
    /// in-cluster index), so there is no `doc_to_pos` lookup
    /// table at all — that 4 MB / 1M-doc allocation was deleted
    /// once an audit confirmed zero external readers.
    pub async fn search(
        &self,
        column: &str,
        query: &[f32],
        k: usize,
        nprobe: usize,
        rerank_mult: usize,
    ) -> Result<Vec<(u32, f32)>, VectorError> {
        let (col, validated) = self.resolve_column(column, query, k)?;
        if !validated {
            return Ok(Vec::new());
        }
        // Centroids are always fp32 (4 bytes/dim) regardless of codec.
        let centroid_stride = col.dim * 4;
        let sub_start = col.subsection_range.start;

        // 1. Centroids + cluster_idx region. These are contiguous
        //    in the subsection, and search needs both before it can
        //    issue per-cluster range requests. Fetching them as one
        //    span saves one request and one foreground RTT batch on
        //    cold object-store search.
        let centroids_start = sub_start + col.centroids_off;
        let centroids_end = centroids_start + (col.n_cent as usize) * centroid_stride;
        let idx_start = sub_start + col.cluster_idx_off;
        let idx_end = idx_start + (col.n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES;
        let centroid_idx_region = self
            .source
            .get_range(centroids_start..idx_end)
            .map_err(|e| VectorError::LazySource(e.to_string()))?;
        let centroids = centroid_idx_region.slice(0..centroids_end - centroids_start);
        let cluster_idx =
            centroid_idx_region.slice(idx_start - centroids_start..idx_end - centroids_start);

        let nprobe_eff = nprobe.min(col.n_cent as usize).max(1);
        // 2. Score centroids → top `nprobe` clusters. Only the
        // retained probe set is fully sorted; the tail centroids are
        // partitioned away with `select_nth_unstable_by`.
        let centroid_scores = score_centroids(&centroids, col, query, nprobe_eff);

        // 3. Rotate query once for the 1-bit code estimator.
        let mut q_rot = vec![0f32; col.dim];
        col.rot.apply(query, &mut q_rot);

        // 4. Per-cluster fetches and shortlist build. Shortlist
        //    tuple is (doc_id, estimate, pos, cluster_id);
        //    pos = off + i and cluster_id are captured inline at
        //    no extra fetch cost. cluster_id is consumed by the
        //    Sq8PerCluster rerank dispatch to pick each
        //    candidate's quantizer; Fp32/RabitqOnly rerank paths
        //    ignore it.
        //
        //    codes and doc_ids per cluster live in
        //    one contiguous block on disk (`per-cluster blocks`
        //    region under the v1 layout), so each cluster pulls
        //    in **one** `get_range` call. those
        //    `nprobe` per-cluster GETs fire **concurrently**
        //    via [`Source::get_ranges_parallel`] instead of
        //    serially via per-call [`Source::get_range`]. On a
        //    `Source::Lazy` backed by object storage the cold
        //    first-search wall-clock collapses from
        //    `sum_c RTT(c)` to `max_c RTT(c)` (one HTTP/2
        //    multiplexed batch). On warm/in-memory paths the
        //    requests resolve through the sync zero-copy
        //    fast path with no extra cost.
        let _ = sub_start; // retained for downstream offset math below
        let cb = col.quant.code_bytes();
        let mut cluster_meta: Vec<(usize, u32, u32)> = Vec::with_capacity(nprobe_eff);
        let mut cluster_prefix_ranges: Vec<Range<usize>> = Vec::with_capacity(nprobe_eff);
        for &(c, _) in &centroid_scores {
            let (off, cnt) = read_cluster_entry(&cluster_idx, c);
            if cnt == 0 {
                continue;
            }
            cluster_prefix_ranges.push(col.cluster_codes_doc_ids_range(off, cnt));
            cluster_meta.push((c, off, cnt));
        }
        let lazy_sq8_meta_range = lazy_sq8_meta_range(col);
        let prefix_blocks_sync: Option<Vec<Bytes>> = cluster_prefix_ranges
            .iter()
            .map(|range| self.source.try_get_range_sync(range.clone()))
            .collect();
        // Survivor-only rerank fetch on BOTH the warm and cold paths.
        // Coarse-score off the cheap `[codes][doc_ids]` prefix, then
        // pull the full rerank vectors ONLY for the survivors:
        //   * warm — the prefix is already resident (the sync probe
        //     above hits), and survivor rows are sliced from the
        //     resident superfile; zero GETs either wave.
        //   * cold — fetch the prefixes over the wire in one coalesced
        //     RTT batch, score, then fetch the survivor rows in a
        //     second small batch. The dominant per-candidate `full[]`
        //     bytes (~3.4 MiB/superfile — the volume that saturates S3
        //     read throughput on a 256-way cold fan-out) are never
        //     moved for non-survivors.
        // The scoring math is identical to the old full-block path —
        // same codes, same coarse shortlist, same fp32/Sq8 rerank — so
        // recall is unchanged; only *which* bytes are fetched differs.
        let survivor_only_rerank_fetch = true;
        let (cluster_blocks, lazy_sq8_meta_bytes) = if let Some(prefix_blocks) = prefix_blocks_sync
        {
            let meta_bytes = if let Some(range) = lazy_sq8_meta_range {
                let mut fetched = self
                    .source
                    .get_ranges_parallel(&[range])
                    .map_err(|e| VectorError::LazySource(e.to_string()))?;
                fetched.pop()
            } else {
                None
            };
            (prefix_blocks, meta_bytes)
        } else {
            // Cold: fetch only the codes+doc_ids prefixes (coalesced)
            // plus the Sq8 meta in one batch. Full vectors are fetched
            // later, for survivors only.
            let extras: Vec<Range<usize>> = lazy_sq8_meta_range.clone().into_iter().collect();
            let (blocks, mut extra_bytes) = get_cluster_ranges_coalesced_with_extras(
                &self.source,
                &cluster_prefix_ranges,
                &extras,
            )
            .map_err(|e| VectorError::LazySource(e.to_string()))?;
            (blocks, extra_bytes.pop())
        };
        debug_assert_eq!(cluster_blocks.len(), cluster_meta.len());

        // Score the 1-bit shortlist and build rerank references — the
        // pure-CPU stage shared with `search_async` (see
        // [`build_shortlist`]). Each cluster block is
        // `[codes][doc_ids][full?]`; scoring reads the prefix, and the
        // survivor `full[]` rows are fetched below — the only step
        // that differs from the async path.
        let ctx = ProbeCtx {
            q_rot: &q_rot,
            k,
            rerank_mult,
            allow: None,
            deny: None,
            pool: None,
            // `search` is a test/bench-only entry (production vector search goes
            // through the async paths); its cold fetch is not budget-gated.
            budget: None,
        };
        // Superfile-tier direct API: no collector at this layer, ns dropped.
        let (outcome, _scan_kernel_ns) = build_shortlist(
            col,
            cb,
            &cluster_meta,
            &cluster_blocks,
            survivor_only_rerank_fetch,
            &ctx,
        )
        .await?;
        let (candidates, survivor_full_ranges) = match outcome {
            ShortlistOutcome::Done(out) => return Ok(out),
            ShortlistOutcome::Rerank {
                candidates,
                survivor_full_ranges,
            } => (candidates, survivor_full_ranges),
        };
        // Coalesce the survivor rows (scattered single-vector ranges
        // inside each cluster's `full[]` region) into a small second
        // wave; warm ranges resolve sync/zero-copy, so this is a cheap
        // sort.
        let survivor_full_rows = match survivor_full_ranges {
            Some(ranges) => Some(
                get_survivor_ranges_coalesced(&self.source, &ranges)
                    .map_err(|e| VectorError::LazySource(e.to_string()))?,
            ),
            None => None,
        };

        // 8. CPU-only rerank using the true metric. Sq8 columns
        //    pre-build a per-query kernel that folds the per-dim
        //    scale/offset into the query (one `dim/8` SIMD pass);
        //    the per-doc inner step is then a plain u8→f32 widen
        //    + SIMD dot. Fp32 takes the flat dispatch.
        // Superfile-tier direct API: no collector reaches this layer, so
        // the bracketed ns are dropped (the supertable paths carry them).
        Ok(rerank_candidates_from_blocks(
            &self.source,
            lazy_sq8_meta_bytes.as_ref(),
            &cluster_blocks,
            survivor_full_rows.as_deref(),
            &candidates,
            col,
            query,
            None,
            k,
        )
        .await?
        .0)
    }

    /// Async sibling of [`Self::search`]. Byte-for-byte the same IVF
    /// kernel — identical centroid scoring, coarse 1-bit shortlist,
    /// survivor-only rerank, and the same coalesced range plans, so
    /// recall is identical — but the three fetch waves (centroid+idx
    /// region, per-cluster code prefixes + Sq8 meta, survivor rerank
    /// rows) are `await`ed on the caller's runtime instead of bridged
    /// through a per-call throwaway runtime. This is what lets the
    /// supertable vector fan-out drive every superfile concurrently on
    /// the shared query runtime — mirroring the FTS
    /// `bm25_search_pretokenized` path — rather than serializing cold
    /// object-store GETs. The CPU steps (centroid/code scoring,
    /// rerank) call the same helpers as the sync path and parallelize
    /// on the global rayon pool; warm/in-memory ranges still resolve
    /// sync/zero-copy via `try_get_range_sync` with no `await`.
    pub async fn search_async(
        &self,
        column: &str,
        query: &[f32],
        k: usize,
        nprobe: usize,
        rerank_mult: usize,
        // Filtered search allow-set (per-superfile matching doc-ids).
        // `None` = unfiltered; threaded to the coarse shortlist so the
        // top-k is the true k-nearest among matching rows.
        allow: Option<Arc<RoaringBitmap>>,
        deny: Option<Arc<RoaringBitmap>>,
        pool: Option<Arc<ThreadPool>>,
        budget: Option<Arc<ConnectionMemoryBudget>>,
    ) -> Result<(Vec<(u32, f32)>, ProbeTally), VectorError> {
        let (col, validated) = self.resolve_column(column, query, k)?;
        if !validated {
            return Ok((Vec::new(), ProbeTally::default()));
        }
        let centroid_stride = col.dim * 4;
        let sub_start = col.subsection_range.start;

        // 1. Centroids + cluster_idx region (one contiguous span).
        let centroids_start = sub_start + col.centroids_off;
        let centroids_end = centroids_start + (col.n_cent as usize) * centroid_stride;
        let idx_start = sub_start + col.cluster_idx_off;
        let idx_end = idx_start + (col.n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES;
        let centroid_idx_region = self
            .source
            .range_async(centroids_start..idx_end)
            .await
            .map_err(|e| VectorError::LazySource(e.to_string()))?;
        let centroids = centroid_idx_region.slice(0..centroids_end - centroids_start);
        let cluster_idx =
            centroid_idx_region.slice(idx_start - centroids_start..idx_end - centroids_start);

        // Filtered search: boost nprobe and rerank_mult inversely with
        // selectivity so probed clusters and the rerank shortlist cover
        // enough eligible rows. Capped at [`MAX_FILTER_SELECTIVITY_MULT`]
        // on the selectivity side and [`MAX_EFFECTIVE_FILTERED_RERANK_MULT`]
        // on the effective rerank width.
        let Some((nprobe_eff, rerank_mult_eff)) =
            effective_filtered_params(&allow, col.n_docs, col.n_cent, nprobe, rerank_mult)
        else {
            return Ok((Vec::new(), ProbeTally::default()));
        };
        // 2. Score centroids → top `nprobe` clusters.
        let centroid_scores = score_centroids(&centroids, col, query, nprobe_eff);

        // 3. Rotate query once for the 1-bit code estimator.
        let mut q_rot = vec![0f32; col.dim];
        col.rot.apply(query, &mut q_rot);

        // 4. Probe the centroid-scored clusters through the shared tail
        //    (also used by the externally-selected
        //    `search_clusters_async` path).
        let _ = sub_start;
        let chosen: Vec<usize> = centroid_scores.iter().map(|&(c, _)| c).collect();
        let ctx = ProbeCtx {
            q_rot: &q_rot,
            k,
            rerank_mult: rerank_mult_eff,
            allow,
            deny,
            pool,
            budget,
        };
        let (hits, mut tally) = self
            .probe_clusters_async(col, query, &ctx, &cluster_idx, &chosen)
            .await?;
        // The centroid + cluster-index span above was one planned range.
        tally.ranges_requested += 1;
        Ok((hits, tally))
    }

    /// Async IVF probe over an **externally chosen** set of cluster ids.
    /// The cross-superfile global selector picks these from the manifest's
    /// per-cluster centroids, so this skips the superfile's own centroid
    /// scoring entirely — it fetches just the cluster index, then probes
    /// exactly `clusters` (ids ≥ `n_cent` and empty clusters are
    /// ignored). The shortlist + rerank are byte-for-byte the same as
    /// [`Self::search_async`].
    pub async fn search_clusters_async(
        &self,
        column: &str,
        query: &[f32],
        k: usize,
        clusters: &[u32],
        rerank_mult: usize,
        // Filtered search allow-set (per-superfile matching doc-ids).
        // `None` = unfiltered; threaded to the coarse shortlist so the
        // top-k is the true k-nearest among matching rows.
        allow: Option<Arc<RoaringBitmap>>,
        // Tombstone deny-set (per-superfile deleted local doc-ids), excluded
        // before the coarse heap so the top-k is selected from live rows.
        deny: Option<Arc<RoaringBitmap>>,
        pool: Option<Arc<ThreadPool>>,
        budget: Option<Arc<ConnectionMemoryBudget>>,
    ) -> Result<(Vec<(u32, f32)>, ProbeTally), VectorError> {
        if self.is_multi_cell() {
            return self
                .search_clusters_async_multi_cell(
                    column,
                    query,
                    k,
                    clusters,
                    rerank_mult,
                    allow,
                    deny,
                    pool,
                    budget,
                )
                .await;
        }
        let (col, validated) = self.resolve_column(column, query, k)?;
        if !validated {
            return Ok((Vec::new(), ProbeTally::default()));
        }
        let sub_start = col.subsection_range.start;
        let idx_start = sub_start + col.cluster_idx_off;
        let idx_end = idx_start + (col.n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES;
        let cluster_idx = self
            .source
            .range_async(idx_start..idx_end)
            .await
            .map_err(|e| VectorError::LazySource(e.to_string()))?;
        let mut q_rot = vec![0f32; col.dim];
        col.rot.apply(query, &mut q_rot);
        let chosen: Vec<usize> = clusters.iter().map(|&c| c as usize).collect();
        // No inverse-selectivity rerank inflation on the fan path: the
        // shortlist heap admits MATCHING rows only (the allow test runs
        // before a candidate can take a slot), so `k × rerank_mult`
        // already buys the standard rerank contract over matching
        // candidates — post-filter underflow cannot happen. The old ×10
        // boost multiplied the exact-rerank volume by selectivity on
        // every probed fragment (measured 70 ms survivor gather + 52 ms
        // Sq8 rerank per filtered query at 1M).
        if allow.as_ref().is_some_and(|bm| bm.is_empty()) {
            return Ok((Vec::new(), ProbeTally::default()));
        }
        let ctx = ProbeCtx {
            q_rot: &q_rot,
            k,
            rerank_mult,
            allow,
            deny,
            pool,
            budget,
        };
        let (hits, mut tally) = self
            .probe_clusters_async(col, query, &ctx, &cluster_idx, &chosen)
            .await?;
        // The cluster-index fetch above was one planned range.
        tally.ranges_requested += 1;
        Ok((hits, tally))
    }

    /// Map flat cluster ids to per-cell probe work on a multi-cell
    /// reader: `(cell index, doc-id base, cell-local cluster ids)`.
    /// Doc-id bases follow parquet row order (cells concatenated in
    /// directory order). The single shared resolution step of the
    /// immediate probe ([`Self::search_clusters_async_multi_cell`]) and
    /// the deferred scan ([`Self::search_clusters_scan_async`]), so the
    /// two paths cannot drift.
    fn resolve_cells_for_clusters(&self, clusters: &[u32]) -> Vec<(usize, u32, Vec<usize>)> {
        let mut by_cell: HashMap<usize, Vec<usize>> = HashMap::new();
        for &flat in clusters {
            let Some((cell_idx, local)) = self.resolve_flat_cluster(flat) else {
                continue;
            };
            by_cell.entry(cell_idx).or_default().push(local as usize);
        }
        let mut doc_base = vec![0u32; self.columns.len()];
        let mut running = 0u32;
        for (i, col) in self.columns.iter().enumerate() {
            doc_base[i] = running;
            running = running.saturating_add(col.n_docs);
        }
        by_cell
            .into_iter()
            .map(|(cell_idx, locals)| (cell_idx, doc_base[cell_idx], locals))
            .collect()
    }

    /// Multi-cell probe: map flat cluster ids → (cell, local), probe each
    /// touched cell, merge hits. Local doc ids are unique within a cell
    /// subsection; across cells they may collide, so hits are tagged with
    /// a cell-local id space by offsetting with a running base equal to
    /// the sum of prior cells' `n_docs` (matching parquet id-column order
    /// when cells are concatenated in directory order).
    #[allow(clippy::too_many_arguments)]
    async fn search_clusters_async_multi_cell(
        &self,
        column: &str,
        query: &[f32],
        k: usize,
        clusters: &[u32],
        rerank_mult: usize,
        allow: Option<Arc<RoaringBitmap>>,
        deny: Option<Arc<RoaringBitmap>>,
        pool: Option<Arc<ThreadPool>>,
        budget: Option<Arc<ConnectionMemoryBudget>>,
    ) -> Result<(Vec<(u32, f32)>, ProbeTally), VectorError> {
        if !self.column_id_by_name.contains_key(column) {
            return Err(VectorError::UnknownColumn(column.to_string()));
        }
        let dim = self
            .columns
            .first()
            .map(|c| c.dim)
            .ok_or_else(|| VectorError::UnknownColumn(column.to_string()))?;
        if query.len() != dim {
            return Err(VectorError::DimensionMismatch {
                expected: dim,
                got: query.len(),
            });
        }
        if k == 0 || self.n_docs == 0 {
            return Ok((Vec::new(), ProbeTally::default()));
        }

        let per_cell = self.resolve_cells_for_clusters(clusters);
        if per_cell.is_empty() {
            return Ok((Vec::new(), ProbeTally::default()));
        }

        // Probe the selected cells CONCURRENTLY — the same wave shape the
        // supertable's cross-superfile fan-out (and FTS) already uses, one
        // level down. A width sweep selects many cells inside one packed
        // shard; awaiting them serially stacked their fetch+scan+rerank
        // walls (measured ~1.2 ms x width per query at w=54 on Cohere-1M).
        // Each cell's probe is independent: own ProbeCtx, own blocks, own
        // rerank; the merge below is order-independent (re-sorted).
        let cell_probes = per_cell.into_iter().filter_map(|(cell_idx, base, locals)| {
            let col = &self.columns[cell_idx];
            if col.n_docs == 0 {
                return None;
            }
            // Allow/deny are file-local; each cell IVF checks cell-local ids.
            let cell_allow = self.cell_local_filter_bitmap(allow.as_deref(), cell_idx, base);
            let cell_deny = self.cell_local_filter_bitmap(deny.as_deref(), cell_idx, base);
            // No inverse-selectivity rerank inflation — same reasoning as
            // [`Self::search_clusters_async`]: the shortlist is allow-first
            // (matching rows only), so the standard `k × rerank_mult`
            // contract holds unchanged under a filter.
            if cell_allow.as_ref().is_some_and(|bm| bm.is_empty()) {
                return None;
            }
            let pool = pool.clone();
            let budget = budget.clone();
            Some(async move {
                let sub_start = col.subsection_range.start;
                let idx_start = sub_start + col.cluster_idx_off;
                let idx_end = idx_start + (col.n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES;
                let cluster_idx = self
                    .source
                    .range_async(idx_start..idx_end)
                    .await
                    .map_err(|e| VectorError::LazySource(e.to_string()))?;
                let mut q_rot = vec![0f32; col.dim];
                col.rot.apply(query, &mut q_rot);
                let ctx = ProbeCtx {
                    q_rot: &q_rot,
                    k,
                    rerank_mult,
                    allow: cell_allow,
                    deny: cell_deny,
                    pool,
                    budget,
                };
                let (hits, mut tally) = self
                    .probe_clusters_async(col, query, &ctx, &cluster_idx, &locals)
                    .await?;
                // Each probed cell fetched its own cluster index above.
                tally.ranges_requested += 1;
                Ok::<(Vec<(u32, f32)>, ProbeTally), VectorError>((
                    hits.into_iter()
                        .map(|(local_id, score)| (base.saturating_add(local_id), score))
                        .collect(),
                    tally,
                ))
            })
        });
        // Wave-cap the concurrent probes to the pool width (the same
        // bound every other fan-out here uses): a wide sweep over a
        // many-celled shard must not open unbounded simultaneous
        // range reads / cold fetches.
        let max_in_flight = pool_wave_cap(pool.as_deref());
        let per_cell: Vec<(Vec<(u32, f32)>, ProbeTally)> = stream::iter(cell_probes)
            .buffer_unordered(max_in_flight)
            .try_collect()
            .await?;
        let mut tally = ProbeTally::default();
        for (_, cell_tally) in &per_cell {
            tally.cells_scanned += cell_tally.cells_scanned;
            tally.candidates_scanned += cell_tally.candidates_scanned;
            tally.ranges_requested += cell_tally.ranges_requested;
            tally.rows_reranked += cell_tally.rows_reranked;
            tally.kernel_cpu_ns += cell_tally.kernel_cpu_ns;
        }
        let mut merged: Vec<(u32, f32)> = per_cell.into_iter().flat_map(|(hits, _)| hits).collect();
        // Distance ascending (smaller = closer), matching every other vector
        // search path. Descending here kept the farthest k hits and collapsed
        // packed-shard recall to ~0.
        merged.sort_by(|a, b| a.1.total_cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
        merged.truncate(k);
        Ok((merged, tally))
    }

    /// Deferred-rerank scan for the supertable's GLOBAL shortlist selection
    /// (unfiltered width sweeps). Per probed cell:
    ///
    /// - WARM (every `[codes][doc_ids]` prefix resident): run only the
    ///   1-bit scan, heap-capped at the UNDIVIDED `k × rerank_mult`, and
    ///   surface the survivors with their estimates — the supertable pools
    ///   them across cells and superfiles, selects the global top
    ///   `k × rerank_mult`, and calls [`Self::rerank_selected_async`] with
    ///   the winners. No survivor fetch, no rerank here.
    /// - COLD (or `RabitqOnly`, which has no rerank stage): probe exactly
    ///   as [`Self::search_clusters_async`] does, under the width-divided
    ///   `cold_rerank_mult` — deferring a cold cell would hold its fetched
    ///   blocks and budget reservation across the whole fan-out.
    #[allow(clippy::too_many_arguments)]
    pub(crate) async fn search_clusters_scan_async(
        &self,
        column: &str,
        query: &[f32],
        k: usize,
        clusters: &[u32],
        rerank_mult: usize,
        cold_rerank_mult: usize,
        allow: Option<Arc<RoaringBitmap>>,
        deny: Option<Arc<RoaringBitmap>>,
        pool: Option<Arc<ThreadPool>>,
        budget: Option<Arc<ConnectionMemoryBudget>>,
    ) -> Result<ScanOutcome, VectorError> {
        let rot_seed = self
            .columns
            .first()
            .map(|c| c.rot_seed)
            .ok_or_else(|| VectorError::UnknownColumn(column.to_string()))?;
        let mut outcome = ScanOutcome {
            hits: Vec::new(),
            candidates: Vec::new(),
            rot_seed,
            cells_scanned: 0,
            candidates_scanned: 0,
            ranges_requested: 0,
            rows_reranked: 0,
            kernel_cpu_ns: 0,
        };
        if k == 0 || self.n_docs == 0 {
            return Ok(outcome);
        }

        // (cell_idx, base, cell-local chosen clusters): multi-cell files
        // resolve flat ids per cell; single-subsection files are the
        // degenerate one-cell case.
        let mut per_cell: Vec<(usize, u32, Vec<usize>)> = Vec::new();
        if self.is_multi_cell() {
            if !self.column_id_by_name.contains_key(column) {
                return Err(VectorError::UnknownColumn(column.to_string()));
            }
            let dim = self
                .columns
                .first()
                .map(|c| c.dim)
                .ok_or_else(|| VectorError::UnknownColumn(column.to_string()))?;
            if query.len() != dim {
                return Err(VectorError::DimensionMismatch {
                    expected: dim,
                    got: query.len(),
                });
            }
            per_cell = self.resolve_cells_for_clusters(clusters);
        } else {
            let (_, validated) = self.resolve_column(column, query, k)?;
            if !validated {
                return Ok(outcome);
            }
            // v1 layout: `column_id_by_name` indexes straight into `columns`.
            let cell_idx = self
                .column_id_by_name
                .get(column)
                .copied()
                .ok_or_else(|| VectorError::UnknownColumn(column.to_string()))?
                as usize;
            per_cell.push((cell_idx, 0, clusters.iter().map(|&c| c as usize).collect()));
        }

        if per_cell.is_empty() {
            return Ok(outcome);
        }
        // Estimates pool cross-superfile keyed by this seed; report the
        // REQUESTED column's seed (every cell of one column shares it),
        // not `columns[0]`'s — a multi-column file's first column can be
        // a different column with a different rotation.
        outcome.rot_seed = self.columns[per_cell[0].0].rot_seed;
        // Rotate the query once per superfile: every cell of a column
        // shares the table-wide rotation seed (cross-cell estimate pooling
        // depends on it), so per-cell re-rotation is duplicate work — at a
        // ~60-cell width sweep the repeated 768x768 applies measured
        // ~1.3ms of wall per query.
        let first_col = &self.columns[per_cell[0].0];
        let mut q_rot_shared = vec![0f32; first_col.dim];
        first_col.rot.apply(query, &mut q_rot_shared);
        let q_rot_shared = &q_rot_shared;
        let cell_scans = per_cell.into_iter().filter_map(|(cell_idx, base, locals)| {
            let col = &self.columns[cell_idx];
            if col.n_docs == 0 {
                return None;
            }
            let cell_allow = self.cell_local_filter_bitmap(allow.as_deref(), cell_idx, base);
            let cell_deny = self.cell_local_filter_bitmap(deny.as_deref(), cell_idx, base);
            if cell_allow.as_ref().is_some_and(|bm| bm.is_empty()) {
                return None;
            }
            let pool = pool.clone();
            let budget = budget.clone();
            Some(async move {
                let sub_start = col.subsection_range.start;
                let idx_start = sub_start + col.cluster_idx_off;
                let idx_end = idx_start + (col.n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES;
                let cluster_idx = self
                    .source
                    .range_async(idx_start..idx_end)
                    .await
                    .map_err(|e| VectorError::LazySource(e.to_string()))?;
                let cb = col.quant.code_bytes();
                let (cluster_meta, prefix_ranges) = chosen_cluster_meta(col, &cluster_idx, &locals);
                if cluster_meta.is_empty() {
                    // The cluster-index read itself was one planned range.
                    let tally = ProbeTally {
                        ranges_requested: 1,
                        ..ProbeTally::default()
                    };
                    return Ok((Vec::new(), Vec::new(), tally));
                }
                // Work-stats tallies, taken before the warm/cold branch so
                // both arms count the codes their clusters hold. Ranges:
                // the cluster index plus one per prefix span (the warm
                // arm's fetches; the cold arm's whole-cluster blocks are
                // one range per chosen cluster, the same count).
                let mut tally = ProbeTally {
                    cells_scanned: 1,
                    candidates_scanned: cluster_meta
                        .iter()
                        .map(|(_, _, cnt)| u64::from(*cnt))
                        .sum(),
                    ranges_requested: 1 + prefix_ranges.len() as u64,
                    rows_reranked: 0,
                    kernel_cpu_ns: 0,
                };
                // Warm fast path: every prefix resident -> defer rerank.
                let prefix_blocks: Option<Vec<Bytes>> = prefix_ranges
                    .iter()
                    .map(|range| self.source.try_get_range_sync(range.clone()))
                    .collect();
                let rabitq_only = matches!(col.rerank_codec, RerankCodec::RabitqOnly);
                if let (Some(blocks), false) = (prefix_blocks, rabitq_only) {
                    let ctx = ProbeCtx {
                        q_rot: q_rot_shared,
                        k,
                        rerank_mult,
                        allow: cell_allow,
                        deny: cell_deny,
                        pool,
                        budget,
                    };
                    let coarse_limit = k.saturating_mul(rerank_mult);
                    let (shortlist, scan_kernel_ns) =
                        scan_shortlist(col, cb, &cluster_meta, &blocks, true, coarse_limit, &ctx)
                            .await?;
                    tally.kernel_cpu_ns += scan_kernel_ns;
                    let cands = shortlist
                        .into_iter()
                        .map(|(did, estimate, pos, cluster_id)| ScanCandidate {
                            did: base.saturating_add(did),
                            estimate,
                            pos,
                            cluster_id,
                            cell_idx,
                        })
                        .collect();
                    return Ok::<(Vec<(u32, f32)>, Vec<ScanCandidate>, ProbeTally), VectorError>((
                        Vec::new(),
                        cands,
                        tally,
                    ));
                }
                // Cold (or RabitqOnly): probe-and-rerank now, width-divided.
                let ctx = ProbeCtx {
                    q_rot: q_rot_shared,
                    k,
                    rerank_mult: cold_rerank_mult,
                    allow: cell_allow,
                    deny: cell_deny,
                    pool,
                    budget,
                };
                let (hits, probe_tally) = self
                    .probe_clusters_async(col, query, &ctx, &cluster_idx, &locals)
                    .await?;
                // Only the rerank rows come from the probe: this scan
                // already counted the cell, its candidates, and its
                // cluster-index + prefix ranges before the branch, and the
                // probe's own tallies cover the same clusters.
                tally.rows_reranked = probe_tally.rows_reranked;
                tally.kernel_cpu_ns += probe_tally.kernel_cpu_ns;
                Ok((
                    hits.into_iter()
                        .map(|(local_id, score)| (base.saturating_add(local_id), score))
                        .collect(),
                    Vec::new(),
                    tally,
                ))
            })
        });
        let max_in_flight = pool_wave_cap(pool.as_deref());
        let per_cell_results: Vec<(Vec<(u32, f32)>, Vec<ScanCandidate>, ProbeTally)> =
            stream::iter(cell_scans)
                .buffer_unordered(max_in_flight)
                .try_collect()
                .await?;
        for (hits, cands, tally) in per_cell_results {
            outcome.hits.extend(hits);
            outcome.candidates.extend(cands);
            outcome.cells_scanned += tally.cells_scanned;
            outcome.candidates_scanned += tally.candidates_scanned;
            outcome.ranges_requested += tally.ranges_requested;
            outcome.rows_reranked += tally.rows_reranked;
            outcome.kernel_cpu_ns += tally.kernel_cpu_ns;
        }
        // Cold hits merge to this file's top-k exactly as the immediate
        // path does; candidates stay unbounded here (per-cell heaps
        // already cap them) — the supertable's global selection truncates.
        outcome
            .hits
            .sort_by(|a, b| a.1.total_cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
        outcome.hits.truncate(k);
        Ok(outcome)
    }

    /// Rerank the GLOBAL shortlist winners that live in this superfile —
    /// phase C of the deferred-rerank path. Every selected candidate came
    /// from a WARM cell scan ([`Self::search_clusters_scan_async`]), so the
    /// survivor rows and Sq8 meta resolve from resident bytes; per cell the
    /// flow is exactly the warm arm of the immediate probe (survivor-only
    /// gather -> exact rerank -> ascending top-k), minus the 1-bit scan
    /// already done.
    pub(crate) async fn rerank_selected_async(
        &self,
        column: &str,
        query: &[f32],
        k: usize,
        selected: Vec<ScanCandidate>,
        pool: Option<Arc<ThreadPool>>,
    ) -> Result<(Vec<(u32, f32)>, u64), VectorError> {
        if selected.is_empty() || k == 0 {
            return Ok((Vec::new(), 0));
        }
        if !self.column_id_by_name.contains_key(column) {
            return Err(VectorError::UnknownColumn(column.to_string()));
        }
        let mut by_cell: HashMap<usize, Vec<ScanCandidate>> = HashMap::new();
        for cand in selected {
            by_cell.entry(cand.cell_idx).or_default().push(cand);
        }
        let cell_reranks = by_cell.into_iter().filter_map(|(cell_idx, cands)| {
            let col = self.columns.get(cell_idx)?;
            let pool = pool.clone();
            Some(async move {
                let sub_start = col.subsection_range.start;
                let idx_start = sub_start + col.cluster_idx_off;
                let idx_end = idx_start + (col.n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES;
                let cluster_idx = self
                    .source
                    .range_async(idx_start..idx_end)
                    .await
                    .map_err(|e| VectorError::LazySource(e.to_string()))?;
                // Distinct probed clusters -> (off, cnt), for survivor
                // row addressing.
                let mut extent_by_cid: HashMap<u32, (u32, u32)> = HashMap::new();
                for cand in &cands {
                    extent_by_cid.entry(cand.cluster_id).or_insert_with(|| {
                        read_cluster_entry(&cluster_idx, cand.cluster_id as usize)
                    });
                }
                let mut ranges = Vec::with_capacity(cands.len());
                let mut rerank_cands = Vec::with_capacity(cands.len());
                for cand in &cands {
                    let (off, cnt) = extent_by_cid[&cand.cluster_id];
                    // `pos = off + i` was captured from the same immutable
                    // superfile this rerank reads, so the subtraction cannot
                    // wrap — make any future layout drift loud instead of a
                    // ~4e9 "local" and an out-of-range byte range.
                    debug_assert!(
                        cand.pos >= off && cand.pos - off < cnt,
                        "candidate pos {} outside cluster extent (off {off}, cnt {cnt})",
                        cand.pos
                    );
                    let local = (cand.pos - off) as usize;
                    let full_idx = Some(ranges.len());
                    ranges.push(col.cluster_rerank_row_range(off, cnt, local));
                    // `did` passes through rerank untouched (norms are keyed
                    // by `pos`), so the file-local id stays as selected.
                    rerank_cands.push(RerankCandidate {
                        did: cand.did,
                        pos: cand.pos,
                        cluster_id: cand.cluster_id,
                        block_idx: 0,
                        full_off: 0,
                        full_idx,
                    });
                }
                let meta_bytes = if let Some(range) = lazy_sq8_meta_range(col) {
                    let mut fetched = self
                        .source
                        .get_ranges_parallel_async(&[range])
                        .await
                        .map_err(|e| VectorError::LazySource(e.to_string()))?;
                    fetched.pop()
                } else {
                    None
                };
                let survivor_t0 = io_counters::phase_start();
                // Residency-aware gather: the globally selected winners are
                // FEW and SCATTERED (~k x rerank_mult / width per cell), so
                // the coalesce plan's gap window — correct for remote
                // fetches, where round trips dominate — merges them into
                // ~whole-cell spans that a resident cache then materializes
                // (measured: ~300 MB touched per query to deliver ~10 MB of
                // rows, 60% of warm query wall). When the bytes are
                // resident, resolve ONE zero-copy span per probed cluster
                // (min..max survivor row) and slice each row out of it —
                // resident spans are mmap/`Bytes` views, so pages fault in
                // only for rows actually read, and the per-range source
                // lookups drop from one per survivor to one per cluster
                // (measured on Cohere-1M defaults: 25.3K sync lookups
                // ≈ 8.5 ms of a 22 ms warm query). Any missing span falls
                // back to the coalesced remote plan, as before.
                let span_by_cluster = survivor_cluster_spans(&rerank_cands, &ranges);
                let spans: Option<HashMap<u32, (usize, Bytes)>> = span_by_cluster
                    .iter()
                    .map(|(&cid, span)| {
                        self.source
                            .try_get_range_sync(span.clone())
                            .map(|bytes| (cid, (span.start, bytes)))
                    })
                    .collect();
                let survivor_rows = match spans {
                    Some(spans) => rerank_cands
                        .iter()
                        .zip(&ranges)
                        .map(|(cand, range)| {
                            let (base, region) = &spans[&cand.cluster_id];
                            region.slice(range.start - base..range.end - base)
                        })
                        .collect(),
                    None => get_survivor_ranges_coalesced_async(&self.source, &ranges)
                        .await
                        .map_err(|e| VectorError::LazySource(e.to_string()))?,
                };
                if let Some(t0) = survivor_t0 {
                    io_counters::phase_record(
                        "vec.survivor_fetch",
                        t0.elapsed().as_micros() as u64,
                    );
                }
                io_counters::phase_timed_async("vec.rerank", async {
                    rerank_candidates_from_blocks(
                        &self.source,
                        meta_bytes.as_ref(),
                        &[],
                        Some(&survivor_rows),
                        &rerank_cands,
                        col,
                        query,
                        pool,
                        k,
                    )
                    .await
                })
                .await
            })
        });
        let max_in_flight = pool_wave_cap(pool.as_deref());
        let per_cell: Vec<(Vec<(u32, f32)>, u64)> = stream::iter(cell_reranks)
            .buffer_unordered(max_in_flight)
            .try_collect()
            .await?;
        let kernel_ns: u64 = per_cell.iter().map(|(_, ns)| ns).sum();
        let mut merged: Vec<(u32, f32)> = per_cell.into_iter().flat_map(|(hits, _)| hits).collect();
        merged.sort_by(|a, b| a.1.total_cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
        merged.truncate(k);
        Ok((merged, kernel_ns))
    }

    /// Shared async tail of the IVF probe: given a chosen set of cluster
    /// ids plus the already-fetched cluster index, fetch each non-empty
    /// cluster's block, build the 1-bit shortlist, and rerank to top-k.
    /// Used by [`Self::search_async`] (clusters from this superfile's
    /// centroid scoring) and [`Self::search_clusters_async`] (clusters
    /// from the global cross-superfile selector).
    /// Returns the hits plus the number of rows the probe reranked at
    /// full precision (0 on the `RabitqOnly` and empty-shortlist paths),
    /// for the per-query work stats.
    async fn probe_clusters_async(
        &self,
        col: &ColumnReader,
        query: &[f32],
        ctx: &ProbeCtx<'_>,
        cluster_idx: &[u8],
        chosen: &[usize],
    ) -> Result<(Vec<(u32, f32)>, ProbeTally), VectorError> {
        let cb = col.quant.code_bytes();
        let (cluster_meta, cluster_prefix_ranges) = chosen_cluster_meta(col, cluster_idx, chosen);
        if cluster_meta.is_empty() {
            return Ok((Vec::new(), ProbeTally::default()));
        }
        // Plan tallies, fixed before the warm/cold fetch branch: both arms
        // scan the same clusters and request one prefix/block range each
        // (the caller counts the cluster-index range it fetched).
        let lazy_sq8_meta_range = lazy_sq8_meta_range(col);
        let mut tally = ProbeTally {
            cells_scanned: 1,
            candidates_scanned: cluster_meta.iter().map(|&(_, _, cnt)| u64::from(cnt)).sum(),
            // One range per chosen cluster, plus the lazy Sq8 meta table
            // when the column stores one — the warm arm fetches it and
            // the cold arm rides it on the coalesce plan, so the planned
            // count is identical at either temperature.
            ranges_requested: cluster_meta.len() as u64 + u64::from(lazy_sq8_meta_range.is_some()),
            rows_reranked: 0,
            kernel_cpu_ns: 0,
        };
        // Warm fast path: every prefix already resident → sync zero-copy.
        let prefix_blocks_sync: Option<Vec<Bytes>> = cluster_prefix_ranges
            .iter()
            .map(|range| self.source.try_get_range_sync(range.clone()))
            .collect();

        // Reserve the cold fetch against the connection budget before it fires;
        // held for the rest of the probe (covers the cluster blocks). Warm slices
        // reserve nothing.
        let mut _cold_guard: Option<Reservation> = None;

        let (cluster_blocks, lazy_sq8_meta_bytes, survivor_only_rerank_fetch) =
            if let Some(prefix_blocks) = prefix_blocks_sync {
                // Warm: prefixes resident. Keep the survivor-only rerank
                // split — the survivor `full[]` rows resolve sync/zero-copy
                // below (no round-trip), so there is nothing to coalesce and
                // we avoid touching the unneeded rerank bytes.
                let meta_bytes = if let Some(range) = lazy_sq8_meta_range {
                    let mut fetched = self
                        .source
                        .get_ranges_parallel_async(&[range])
                        .await
                        .map_err(|e| VectorError::LazySource(e.to_string()))?;
                    fetched.pop()
                } else {
                    None
                };
                (prefix_blocks, meta_bytes, true)
            } else {
                // Cold: fetch the **full** per-cluster blocks
                // (`[codes][doc_ids][full]`) + Sq8 meta in one coalesced
                // batch, so the survivor rerank rows arrive *with* the codes
                // — collapsing the dependent rerank round-trip (wave 3) into
                // this wave. Cold latency is RTT/wave-bound and the
                // background cache-fill is already downloading the whole
                // cell, so the extra rerank bytes here are bytes we'd pull
                // regardless; we just front-load them to save a serial S3
                // round-trip. `survivor_only_rerank_fetch = false` tells
                // `build_shortlist` the rerank rows are in-block (no second
                // fetch).
                let cluster_full_ranges: Vec<Range<usize>> = cluster_meta
                    .iter()
                    .map(|&(_, off, cnt)| col.cluster_block_range(off, cnt))
                    .collect();

                _cold_guard =
                    reserve_cold_fetch(&self.source, &cluster_full_ranges, ctx.budget.as_ref())?;

                // The metadata legs (lazy Sq8 meta + inline stable-`_id`
                // region) ride the SAME coalesce plan as the cluster
                // blocks. On multi-cell (pre-drain user) files the probed
                // cell's subsection is contiguous — subheader, meta, ids,
                // blocks within ~100 KiB — so all three legs merge into
                // ONE GET per file instead of three; on packed files the
                // regions are megabytes apart and the plan naturally
                // keeps them as separate ranges in the same concurrent
                // round-trip envelope. The remap step then resolves
                // hidden→user `_id` from the stash (sync, at the fan-out
                // tag site) instead of issuing a trailing region GET.
                let region_range = col.stable_ids_region_range();
                let mut extras: Vec<Range<usize>> = Vec::new();
                let meta_slot = lazy_sq8_meta_range.clone().map(|r| {
                    extras.push(r);
                    extras.len() - 1
                });
                let region_slot = region_range.map(|r| {
                    extras.push(r);
                    extras.len() - 1
                });
                let (blocks, extra_bytes) = get_cluster_ranges_coalesced_with_extras_async(
                    &self.source,
                    &cluster_full_ranges,
                    &extras,
                )
                .await
                .map_err(|e| VectorError::LazySource(e.to_string()))?;
                let meta = meta_slot.map(|i| extra_bytes[i].clone());
                if let Some(bytes) = region_slot.map(|i| extra_bytes[i].clone())
                    && let Ok(mut slot) = self.cold_stable_id_region.lock()
                {
                    *slot = Some(bytes);
                }
                (blocks, meta, false)
            };
        debug_assert_eq!(cluster_blocks.len(), cluster_meta.len());

        // Shared pure-CPU shortlist + candidate-build stage (see
        // [`build_shortlist`]); only the survivor-row fetch below
        // diverges from the sync path.
        // Phase names (INFINO_TRACE_VECTOR_WARM_PHASES): shortlist = 1-bit
        // RaBitQ heap; survivor_fetch = warm/cold full-row gather; rerank =
        // Sq8/fp32 refine. Concurrent fan-out units each emit their own spans.
        let shortlist_t0 = io_counters::phase_start();
        let (outcome, scan_kernel_ns) = build_shortlist(
            col,
            cb,
            &cluster_meta,
            &cluster_blocks,
            survivor_only_rerank_fetch,
            ctx,
        )
        .await?;
        tally.kernel_cpu_ns += scan_kernel_ns;
        let (candidates, survivor_full_ranges) = match outcome {
            ShortlistOutcome::Done(out) => {
                if let Some(t0) = shortlist_t0 {
                    io_counters::phase_record("vec.shortlist", t0.elapsed().as_micros() as u64);
                }
                return Ok((out, tally));
            }
            ShortlistOutcome::Rerank {
                candidates,
                survivor_full_ranges,
            } => (candidates, survivor_full_ranges),
        };
        if let Some(t0) = shortlist_t0 {
            io_counters::phase_record("vec.shortlist", t0.elapsed().as_micros() as u64);
        }
        // Survivor rerank rows in one concurrent batch on the caller's
        // runtime; warm ranges resolve sync/zero-copy with no await.
        let survivor_t0 = io_counters::phase_start();
        let survivor_full_rows = match survivor_full_ranges {
            Some(ranges) => Some(
                get_survivor_ranges_coalesced_async(&self.source, &ranges)
                    .await
                    .map_err(|e| VectorError::LazySource(e.to_string()))?,
            ),
            None => None,
        };
        if let Some(t0) = survivor_t0 {
            io_counters::phase_record("vec.survivor_fetch", t0.elapsed().as_micros() as u64);
        }

        tally.rows_reranked = candidates.len() as u64;
        let (hits, rerank_ns) = io_counters::phase_timed_async("vec.rerank", async {
            rerank_candidates_from_blocks(
                &self.source,
                lazy_sq8_meta_bytes.as_ref(),
                &cluster_blocks,
                survivor_full_rows.as_deref(),
                &candidates,
                col,
                query,
                ctx.pool.clone(),
                ctx.k,
            )
            .await
        })
        .await?;
        tally.kernel_cpu_ns += rerank_ns;
        Ok((hits, tally))
    }

    /// Look up the column by name and validate `query.len() == col.dim`
    /// + the "empty work" short-circuit (`k == 0` or `n_docs == 0`).
    /// `Ok((col, true))` = real search to follow; `Ok((col, false))`
    /// = empty-result short circuit, caller returns `Ok(Vec::new())`.
    #[inline]
    /// Retrieve original vectors in their insertion order for fp32-encoded columns.
    /// Returns an error if the column uses a different encoding (Sq8Residual or RabitqOnly).
    pub fn get_vectors_fp32(&self, column: &str) -> Result<Vec<Vec<f32>>, VectorError> {
        let cid = *self
            .column_id_by_name
            .get(column)
            .ok_or_else(|| VectorError::UnknownColumn(column.to_string()))?;
        let col = &self.columns[cid as usize];

        if col.rerank_codec != RerankCodec::Fp32 {
            return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "column '{}' uses rerank codec {} instead of Fp32",
                col.name,
                col.rerank_codec.name()
            ))));
        }

        if col.n_docs == 0 {
            return Ok(Vec::new());
        }

        let sub_start = col.subsection_range.start;
        let idx_start = sub_start + col.cluster_idx_off;
        let idx_end = idx_start + (col.n_cent as usize) * 8;
        let cluster_idx = self
            .source
            .get_range(idx_start..idx_end)
            .map_err(|e| VectorError::LazySource(e.to_string()))?;

        let cb = col.quant.code_bytes();
        let per_vec_bytes = col.rerank_codec.per_vector_bytes(col.dim);

        // Collect all cluster ranges needed for fetching
        let mut cluster_ranges: Vec<Range<usize>> = Vec::new();
        let mut cluster_meta: Vec<(usize, u32, u32)> = Vec::new();

        for c in 0..col.n_cent as usize {
            let (off, cnt) = read_cluster_entry(&cluster_idx, c);
            if cnt == 0 {
                continue;
            }
            cluster_ranges.push(col.cluster_block_range(off, cnt));
            cluster_meta.push((c, off, cnt));
        }

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

        // Fetch all cluster blocks
        let cluster_blocks = self
            .source
            .get_ranges_parallel(&cluster_ranges)
            .map_err(|e| VectorError::LazySource(e.to_string()))?;

        // Allocate output vector with doc_id -> vector mapping
        let mut result: Vec<Option<Vec<f32>>> = vec![None; col.n_docs as usize];

        // Process each cluster block
        for (bi, block) in cluster_blocks.iter().enumerate() {
            let (_, _off, cnt) = cluster_meta[bi];
            let cnt_usize = cnt as usize;

            // Layout within the block: [codes_chunk][doc_ids_chunk][full_chunk]
            let codes_len = cnt_usize * cb;
            let doc_ids_len = cnt_usize * 4;
            let full_start = codes_len + doc_ids_len;

            // Extract doc_ids from the block
            let doc_ids_slice = block.slice(codes_len..codes_len + doc_ids_len);

            // Extract and reconstruct vectors
            for i in 0..cnt_usize {
                let doc_id = u32::from_le_bytes([
                    doc_ids_slice[i * 4],
                    doc_ids_slice[i * 4 + 1],
                    doc_ids_slice[i * 4 + 2],
                    doc_ids_slice[i * 4 + 3],
                ]) as usize;

                let vec_start = full_start + i * per_vec_bytes;
                let vec_end = vec_start + per_vec_bytes;
                let vec_bytes = block.slice(vec_start..vec_end);

                // Convert bytes to f32 vector
                // For Fp32 codec, per_vec_bytes = dim * 4, so we expect dim f32s
                let vec_f32: Vec<f32> = vec_bytes
                    .as_ref()
                    .chunks_exact(4)
                    .map(|chunk| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
                    .collect();

                if vec_f32.len() != col.dim {
                    return Err(VectorError::Read(ReadError::MalformedVersion(format!(
                        "vector size mismatch: got {}, expected {}",
                        vec_f32.len(),
                        col.dim
                    ))));
                }

                if doc_id < col.n_docs as usize {
                    result[doc_id] = Some(vec_f32);
                }
            }
        }

        // Convert to final result, checking all vectors were found
        result
            .into_iter()
            .enumerate()
            .map(|(idx, vec_opt)| {
                vec_opt.ok_or_else(|| {
                    VectorError::Read(ReadError::MalformedVersion(format!(
                        "missing vector for doc_id {}",
                        idx
                    )))
                })
            })
            .collect()
    }

    /// Decode vectors for superfile merge/rebuild. Fp32 columns use the stored
    /// rerank payload; Sq8+ε columns decode at the merge boundary only.
    pub(crate) fn get_vectors_for_merge(&self, column: &str) -> Result<Vec<Vec<f32>>, VectorError> {
        let cid = *self
            .column_id_by_name
            .get(column)
            .ok_or_else(|| VectorError::UnknownColumn(column.to_string()))?;
        let col = &self.columns[cid as usize];
        if col.n_docs == 0 {
            return Ok(Vec::new());
        }
        match col.rerank_codec {
            RerankCodec::Fp32 => self.get_vectors_fp32(column),
            RerankCodec::Sq8Residual | RerankCodec::Sq8FixedResidual => {
                Err(VectorError::Read(ReadError::MalformedVersion(format!(
                    "column '{}' uses {} — merge via build_from_sq8_ivf_readers",
                    col.name,
                    col.rerank_codec.name()
                ))))
            }
            other => Err(VectorError::Read(ReadError::MalformedVersion(format!(
                "column '{}' uses rerank codec {} which cannot be merged",
                col.name,
                other.name()
            )))),
        }
    }

    fn resolve_column(
        &self,
        column: &str,
        query: &[f32],
        k: usize,
    ) -> Result<(&ColumnReader, bool), VectorError> {
        let cid = *self
            .column_id_by_name
            .get(column)
            .ok_or_else(|| VectorError::UnknownColumn(column.to_string()))?;
        let col = &self.columns[cid as usize];
        if query.len() != col.dim {
            return Err(VectorError::DimensionMismatch {
                expected: col.dim,
                got: query.len(),
            });
        }
        if k == 0 || col.n_docs == 0 {
            return Ok((col, false));
        }
        Ok((col, true))
    }
}

/// Outcome of the 1-bit shortlist + candidate-build stage shared by
/// [`VectorReader::search`] and [`VectorReader::search_async`].
enum ShortlistOutcome {
    /// Final result — no rerank fetch needed: empty shortlist,
    /// `coarse_limit == 0`, or a `RabitqOnly` column whose 1-bit
    /// shortlist *is* the ranking.
    Done(Vec<(u32, f32)>),
    /// Survivors to rerank against the true metric.
    /// `survivor_full_ranges` (when `Some`) are the per-survivor
    /// `full[]` rows the caller fetches — sync or async, the only
    /// step that differs between the two search paths.
    Rerank {
        candidates: Vec<RerankCandidate>,
        survivor_full_ranges: Option<Vec<Range<usize>>>,
    },
}

/// Pure-CPU stage shared by the sync and async vector search paths.
///
/// Scores the probed clusters' 1-bit codes into a bounded shortlist,
/// short-circuits `RabitqOnly` columns (whose shortlist is the final
/// ranking), and otherwise builds the rerank references plus the
/// survivor `full[]` ranges to fetch. Holds no I/O: the caller does
/// the survivor-row fetch (sync vs async — the sole divergence) and
/// then runs [`rerank_candidates_from_blocks`]. Factoring this out
/// keeps `search` / `search_async` down to their fetch waves around a
/// single shared kernel, so the two can't drift in scoring/recall.
/// Resolve the chosen cluster ids against a cell's cluster index:
/// `(cluster_id, doc_off, count)` for every non-empty in-range cluster,
/// plus each cluster's `[codes][doc_ids]` prefix range.
fn chosen_cluster_meta(
    col: &ColumnReader,
    cluster_idx: &[u8],
    chosen: &[usize],
) -> (Vec<(usize, u32, u32)>, Vec<Range<usize>>) {
    let mut cluster_meta = Vec::with_capacity(chosen.len());
    let mut prefix_ranges = Vec::with_capacity(chosen.len());
    for &c in chosen {
        if c >= col.n_cent as usize {
            continue;
        }
        let (off, cnt) = read_cluster_entry(cluster_idx, c);
        if cnt == 0 {
            continue;
        }
        prefix_ranges.push(col.cluster_codes_doc_ids_range(off, cnt));
        cluster_meta.push((c, off, cnt));
    }
    (cluster_meta, prefix_ranges)
}

/// Wave cap for concurrent per-cell probes: the pool's width — the same
/// bound every fan-out here uses, so a wide sweep cannot open unbounded
/// simultaneous range reads / cold fetches.
fn pool_wave_cap(pool: Option<&ThreadPool>) -> usize {
    pool.map(ThreadPool::current_num_threads)
        .unwrap_or_else(rayon::current_num_threads)
        .max(1)
}

/// Per-cell cache of cluster codes in transposed (position-major)
/// layout for the FastScan LUT scan — the codes counterpart of the
/// routing layer's transposed centroid cache. Built lazily per cluster
/// on the first scan that touches it (every later query reuses the
/// blocks) and dropped with the reader, so its lifetime and footprint
/// ride the reader cache. Each entry holds the RAII budget reservation
/// that admitted it; a denied reservation keeps that cluster on the
/// row-major estimator instead of evicting anything.
#[derive(Default)]
pub(super) struct TransposedCodeCache {
    clusters: Mutex<HashMap<u32, TransposedCluster>>,
    /// Sticky once the budget declines a build: warm queries must not
    /// keep knocking on the gate (the budget's denial counter is an
    /// operator diagnostic, and warm search's contract is to reserve
    /// nothing at steady state). Already-built entries keep serving;
    /// a reader-cache reopen retries naturally.
    budget_denied: AtomicBool,
}

impl fmt::Debug for TransposedCodeCache {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let clusters = self
            .clusters
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .len();
        f.debug_struct("TransposedCodeCache")
            .field("clusters", &clusters)
            .finish()
    }
}

struct TransposedCluster {
    bytes: Arc<Vec<u8>>,
    /// Held for the entry's lifetime; releases when the reader drops.
    _reservation: Option<Reservation>,
}

impl TransposedCodeCache {
    /// Return the cluster's transposed code blocks, building them on
    /// first touch. `None` = the memory budget declined the bytes; the
    /// caller falls back to the row-major scan for this cluster.
    fn get_or_build(
        &self,
        off: u32,
        codes: &[u8],
        cnt: usize,
        cb: usize,
        budget: Option<&Arc<ConnectionMemoryBudget>>,
    ) -> Option<Arc<Vec<u8>>> {
        {
            let map = self.clusters.lock().unwrap_or_else(PoisonError::into_inner);
            if let Some(hit) = map.get(&off) {
                return Some(Arc::clone(&hit.bytes));
            }
        }
        if self.budget_denied.load(AtomicOrdering::Relaxed) {
            return None;
        }
        // Build outside the lock: concurrent chunks of one cell may race
        // to build the same cluster; the first insert wins and the
        // loser's work (and reservation) is discarded — bounded waste,
        // no lock held across the transpose.
        let transposed_len = cnt.div_ceil(LUT_BLOCK_ROWS) * cb * LUT_BLOCK_ROWS;
        let reservation = match budget {
            Some(b) => match b.try_reserve(transposed_len) {
                Ok(r) => Some(r),
                Err(_) => {
                    self.budget_denied.store(true, AtomicOrdering::Relaxed);
                    tracing::warn!(
                        transposed_len,
                        "vector scan: connection memory budget declined the \
                         transposed-code cache; this reader keeps the exact \
                         row-major estimator"
                    );
                    return None;
                }
            },
            // No budget attached: measure-only, same as the cold path.
            None => None,
        };
        let bytes = Arc::new(build_transposed_code_cache(codes, cnt, cb));
        let mut map = self.clusters.lock().unwrap_or_else(PoisonError::into_inner);
        let entry = map.entry(off).or_insert(TransposedCluster {
            bytes,
            _reservation: reservation,
        });
        Some(Arc::clone(&entry.bytes))
    }
}

/// Scan one cluster through the transposed LUT kernels, pushing
/// `(did, estimate, pos, cluster_id)` for every row — the fast-path
/// body of [`scan_shortlist`]'s per-cluster loop (unfiltered scans
/// only; filtered scans keep the row-major estimator, whose per-row
/// allow/deny checks precede scoring).
#[allow(clippy::too_many_arguments)]
fn scan_cluster_transposed(
    transposed: &[u8],
    doc_ids: &[u8],
    cnt: usize,
    off: u32,
    cluster_id: u32,
    cb: usize,
    lut: &LutQuery,
    acc: &mut Vec<(u32, f32, u32, u32)>,
) {
    for_each_code_block_scores(transposed, cb, lut, |base_r, scores| {
        let live = cnt.saturating_sub(base_r).min(LUT_BLOCK_ROWS);
        for (lane, &est) in scores.iter().enumerate().take(live) {
            let rr = base_r + lane;
            let did = u32::from_le_bytes(
                doc_ids[rr * 4..rr * 4 + 4]
                    .try_into()
                    .expect("4-byte doc id"),
            );
            acc.push((did, est, off + rr as u32, cluster_id));
        }
    });
}

/// The pure-CPU half of [`build_shortlist`]: score every probed cluster's
/// 1-bit codes into one bounded heap of `coarse_limit` survivors, rayon-
/// parallel when the candidate pool amortizes the hand-off. Shared by the
/// immediate-rerank path ([`build_shortlist`]) and the deferred-rerank
/// scan ([`VectorReader::search_clusters_scan_async`]). Returns unsorted
/// `(did, estimate, pos, cluster_id)` survivors plus the scan's bracketed
/// on-CPU ns (per-chunk brackets on the parallel arm).
async fn scan_shortlist(
    col: &ColumnReader,
    cb: usize,
    cluster_meta: &[(usize, u32, u32)],
    cluster_blocks: &[Bytes],
    survivor_only_rerank_fetch: bool,
    coarse_limit: usize,
    ctx: &ProbeCtx<'_>,
) -> Result<(Vec<(u32, f32, u32, u32)>, u64), VectorError> {
    let full_vec_bytes = col.rerank_codec.per_vector_bytes(col.dim);
    let total_candidates: usize = cluster_meta.iter().map(|&(_, _, cnt)| cnt as usize).sum();
    // Collect every scored row, then keep the top `coarse_limit` with one
    // O(n) partition. A bounded heap pays a sift on (nearly) every push
    // to retain the same set `select_nth` finds in a single pass — at the
    // 1M width-sweep shape (~9K-row cells against a k*rerank_mult cap)
    // the heap admission machinery alone measured ~15% of scan CPU.
    // FastScan LUT fast path: unfiltered scans score whole cells, so
    // the transposed-block kernels apply. The per-query i16 bound
    // (`fits_i16`) closes the accumulator-overflow hole exactly; when
    // it declines, the exact row-major estimator below is the (more
    // precise) fallback.
    let lut_query = (ctx.allow.is_none() && ctx.deny.is_none() && lut_scan_supported())
        .then(|| LutQuery::new(ctx.q_rot))
        .filter(|lut| lut.fits_i16())
        .map(Arc::new);
    let scan_vec = |acc: &mut Vec<(u32, f32, u32, u32)>,
                    (&(c, off, cnt), block): (&(usize, u32, u32), &Bytes)| {
        let codes_len = (cnt as usize) * cb;
        let doc_ids_len = (cnt as usize) * 4;
        debug_assert_eq!(
            block.len(),
            if survivor_only_rerank_fetch {
                codes_len + doc_ids_len
            } else {
                codes_len + doc_ids_len + (cnt as usize) * full_vec_bytes
            }
        );
        let codes = block.slice(0..codes_len);
        let doc_ids = block.slice(codes_len..codes_len + doc_ids_len);
        if let Some(lut) = lut_query.as_deref()
            && let Some(transposed) = col.transposed_codes.get_or_build(
                off,
                &codes,
                cnt as usize,
                cb,
                ctx.budget.as_ref(),
            )
        {
            scan_cluster_transposed(
                &transposed,
                &doc_ids,
                cnt as usize,
                off,
                c as u32,
                cb,
                lut,
                acc,
            );
            return;
        }
        score_cluster_codes_with(
            &codes,
            &doc_ids,
            cnt,
            off,
            c as u32,
            &col.quant,
            ctx.q_rot,
            ctx.allow.as_deref(),
            ctx.deny.as_deref(),
            &mut |cand| acc.push((cand.did, cand.estimate, cand.pos, cand.cluster_id)),
        );
    };
    let acc = if total_candidates >= PARALLEL_SCAN_MIN && cluster_meta.len() > 1 {
        // Parallelize the coarse 1-bit scan across the configured rayon
        // pool, bridged back via a oneshot so no tokio worker blocks under
        // the compute. Cluster scoring is order-independent — the partition
        // below and every caller re-sort survivors — so chunked-parallel
        // and serial shortlists rank identically.
        let n_tasks = parallel_chunks(cluster_meta.len());
        let chunk = cluster_meta.len().div_ceil(n_tasks).max(1);
        let quant = col.quant.clone();
        let transposed_cache = Arc::clone(&col.transposed_codes);
        let budget_owned = ctx.budget.clone();
        let lut_owned = lut_query.clone();
        let q_rot_v: Vec<f32> = ctx.q_rot.to_vec();
        let meta_owned: Vec<(usize, u32, u32)> = cluster_meta.to_vec();
        let blocks_owned: Vec<Bytes> = cluster_blocks.to_vec();
        // Move an `Arc` clone of the allow-set + deny-set into the rayon
        // task; each chunk borrows them as `Option<&RoaringBitmap>`.
        let allow_owned = ctx.allow.clone();
        let deny_owned = ctx.deny.clone();
        let (tx, rx) = oneshot::channel();
        spawn_on(ctx.pool.as_deref(), move || {
            let acc = meta_owned
                .par_chunks(chunk)
                .zip(blocks_owned.par_chunks(chunk))
                .map(|(meta_chunk, block_chunk)| {
                    let kernel_start = metering_active().then(thread_cpu_ns).flatten();
                    let chunk_rows: usize =
                        meta_chunk.iter().map(|&(_, _, cnt)| cnt as usize).sum();
                    let cap = coarse_limit.saturating_mul(SHORTLIST_TRUNCATE_SLACK);
                    let mut acc = Vec::with_capacity(chunk_rows.min(cap));
                    for (&(c, off, cnt), block) in meta_chunk.iter().zip(block_chunk.iter()) {
                        let codes_len = (cnt as usize) * cb;
                        let doc_ids = block.slice(codes_len..codes_len + (cnt as usize) * 4);
                        let codes = block.slice(0..codes_len);
                        if let Some(lut) = lut_owned.as_deref()
                            && let Some(transposed) = transposed_cache.get_or_build(
                                off,
                                &codes,
                                cnt as usize,
                                cb,
                                budget_owned.as_ref(),
                            )
                        {
                            scan_cluster_transposed(
                                &transposed,
                                &doc_ids,
                                cnt as usize,
                                off,
                                c as u32,
                                cb,
                                lut,
                                &mut acc,
                            );
                            continue;
                        }
                        score_cluster_codes_with(
                            &codes,
                            &doc_ids,
                            cnt,
                            off,
                            c as u32,
                            &quant,
                            &q_rot_v,
                            allow_owned.as_deref(),
                            deny_owned.as_deref(),
                            &mut |cand| {
                                acc.push((cand.did, cand.estimate, cand.pos, cand.cluster_id))
                            },
                        );
                        if acc.len() >= cap {
                            truncate_to_top_estimates(&mut acc, coarse_limit);
                        }
                    }
                    (acc, thread_cpu_delta_ns(kernel_start))
                })
                .reduce(
                    || (Vec::new(), 0u64),
                    |(mut a, ns_a), (mut b, ns_b)| {
                        a.append(&mut b);
                        if a.len() >= coarse_limit.saturating_mul(SHORTLIST_TRUNCATE_SLACK) {
                            truncate_to_top_estimates(&mut a, coarse_limit);
                        }
                        (a, ns_a + ns_b)
                    },
                );
            let _ = tx.send(acc);
        });
        rx.await.map_err(|_| VectorError::TaskDropped("scan"))?
    } else {
        timed_section(|| {
            let cap = coarse_limit.saturating_mul(SHORTLIST_TRUNCATE_SLACK);
            let mut acc = Vec::with_capacity(total_candidates.min(cap));
            for item in cluster_meta.iter().zip(cluster_blocks.iter()) {
                scan_vec(&mut acc, item);
                if acc.len() >= cap {
                    truncate_to_top_estimates(&mut acc, coarse_limit);
                }
            }
            acc
        })
    };
    let (mut acc, kernel_ns) = acc;
    truncate_to_top_estimates(&mut acc, coarse_limit);
    Ok((acc, kernel_ns))
}

/// Keep the `limit` highest-estimate survivors of one cell scan via a
/// single O(n) partition (`select_nth_unstable`), deterministic doc-id
/// tie-break. Output order is unspecified — every caller re-sorts or
/// re-selects downstream.
fn truncate_to_top_estimates(acc: &mut Vec<(u32, f32, u32, u32)>, limit: usize) {
    if limit == 0 {
        acc.clear();
        return;
    }
    if acc.len() > limit {
        acc.select_nth_unstable_by(limit - 1, |a, b| {
            b.1.total_cmp(&a.1).then_with(|| a.0.cmp(&b.0))
        });
        acc.truncate(limit);
    }
}

/// Per-cell calibration view — see
/// [`VectorReader::cell_fine_calibration_views`].
pub(crate) struct CellFineCalibrationView {
    /// Grid cell id (`None` on single-cell v1 blobs, which the hidden
    /// drain never produces).
    pub(crate) cell_id: Option<u32>,
    pub(crate) dim: usize,
    pub(crate) n_fine: usize,
    /// Raw row-major `n_fine x dim` fp32-le centroid-region bytes — the
    /// exact on-disk region query-time fine ranking scans, ranked by the
    /// same [`nearest_k_centroids_bytes`] owner.
    pub(crate) fine_centroids_bytes: Bytes,
    /// Stable `_id` -> fine cluster index, one entry per row.
    pub(crate) cluster_of_stable: HashMap<i128, u32>,
}

/// One 1-bit-scan survivor surfaced to the supertable's GLOBAL shortlist
/// selection (unfiltered width sweeps): the estimate that admitted it plus
/// everything the deferred rerank needs to find its bytes again. Estimates
/// are comparable across cells and superfiles of one column — the rotation
/// is seeded once per column, table-wide, and the estimator is a pure
/// function of the rotated query and the stored code.
#[derive(Clone, Copy, Debug)]
pub(crate) struct ScanCandidate {
    /// File-local doc id (cell base already applied on packed files).
    pub(crate) did: u32,
    /// 1-bit RaBitQ estimate; higher = better.
    pub(crate) estimate: f32,
    /// Row position within the cell's cluster ordering (`cluster off + i`).
    pub(crate) pos: u32,
    pub(crate) cluster_id: u32,
    /// Cell directory index within this superfile.
    pub(crate) cell_idx: usize,
}

/// Work tallies from one cell probe (immediate arm) or one scanned cell
/// (deferred arm) — named counters, so a flush or merge site can never
/// silently swap two positional `u64`s and still type-check.
/// `pub` (not `pub(crate)`) because the carrying search fns are the
/// test-helpers-widened surface benches call; the module gate in
/// `lib.rs` keeps all of it crate-private in normal builds.
#[derive(Debug, Default, Clone, Copy)]
pub struct ProbeTally {
    /// Cells that contributed at least one chosen, non-empty cluster
    /// (0 or 1 from a single probe; summed by multi-cell callers).
    pub cells_scanned: u64,
    /// Σ row counts over every chosen cluster — the quantized codes the
    /// 1-bit estimator scanned. Identical warm or cold.
    pub candidates_scanned: u64,
    /// Byte-source ranges the probe plan requested: one prefix/block
    /// range per chosen cluster, plus the cell's cluster index where the
    /// probing fn fetched it itself. Rerank row ranges are NOT counted
    /// here — the supertable charges the plan's rerank budget so the
    /// priced count stays identical across the warm (survivor-fetch)
    /// and cold (rows-in-block) executions.
    pub ranges_requested: u64,
    /// Rows this probe actually reranked at full precision (execution
    /// diagnostic — see `OpStats::vector_rows_reranked`).
    pub rows_reranked: u64,
    /// On-CPU nanoseconds of the probe's bracketed kernel sections (1-bit
    /// scan + exact rerank; per-chunk brackets on the parallel arms, so
    /// work-stolen chunks attribute their CPU too). Gated on
    /// [`metering_active`]: 0 in an unmetered process.
    pub kernel_cpu_ns: u64,
}

/// Result of a deferred-rerank scan over one superfile
/// ([`VectorReader::search_clusters_scan_async`]).
pub(crate) struct ScanOutcome {
    /// Exact hits from cells that probed COLD (whole blocks fetched and
    /// reranked immediately under the width-divided budget — deferring
    /// them would hold the fetched blocks and their budget reservation
    /// across the whole fan-out) plus RabitqOnly short-circuits.
    pub(crate) hits: Vec<(u32, f32)>,
    /// Estimate survivors from warm cells, deferred to the supertable's
    /// global selection.
    pub(crate) candidates: Vec<ScanCandidate>,
    /// The column's rotation seed — the supertable asserts every pooled
    /// unit agrees before comparing estimates across superfiles.
    pub(crate) rot_seed: u64,
    /// Cells this scan probed (chose ≥1 cluster in), for the per-query
    /// work stats.
    pub(crate) cells_scanned: u64,
    /// Quantized codes estimated: Σ cluster row counts over every chosen
    /// cluster, warm and cold arms alike.
    pub(crate) candidates_scanned: u64,
    /// Byte-source ranges the scan requested per cell: the cluster index
    /// plus one range per prefix block (warm arm) or chosen cluster's
    /// blocks (cold arm).
    pub(crate) ranges_requested: u64,
    /// Rows the cold arm reranked immediately (warm survivors defer to the
    /// supertable's global selection and are counted at phase C instead).
    pub(crate) rows_reranked: u64,
    /// Σ per-cell bracketed kernel on-CPU ns (see `ProbeTally::kernel_cpu_ns`).
    pub(crate) kernel_cpu_ns: u64,
}

async fn build_shortlist(
    col: &ColumnReader,
    cb: usize,
    cluster_meta: &[(usize, u32, u32)],
    cluster_blocks: &[Bytes],
    survivor_only_rerank_fetch: bool,
    ctx: &ProbeCtx<'_>,
) -> Result<(ShortlistOutcome, u64), VectorError> {
    let full_vec_bytes = col.rerank_codec.per_vector_bytes(col.dim);
    // Score each probed cluster's 1-bit codes into the shortlist.
    // The per-cluster slices are zero-copy `Bytes` views; the actual
    // estimate scan is the hot CPU work, parallelized across clusters
    // once the candidate pool is large enough to amortize the rayon
    // hand-off. Cluster scoring is order-independent: every survivor
    // is re-sorted by estimate below, so parallel and serial
    // shortlists rank identically.
    let coarse_limit = if matches!(col.rerank_codec, RerankCodec::RabitqOnly) {
        ctx.k
    } else {
        ctx.k.saturating_mul(ctx.rerank_mult)
    };
    if coarse_limit == 0 {
        return Ok((ShortlistOutcome::Done(Vec::new()), 0));
    }
    let (mut shortlist, scan_kernel_ns) = scan_shortlist(
        col,
        cb,
        cluster_meta,
        cluster_blocks,
        survivor_only_rerank_fetch,
        coarse_limit,
        ctx,
    )
    .await?;

    if shortlist.is_empty() {
        return Ok((ShortlistOutcome::Done(Vec::new()), scan_kernel_ns));
    }

    // `RabitqOnly` short-circuit: the 1-bit shortlist *is* the final
    // ranking — no `full[]` region on disk, no rerank step. Partial-
    // sort to the top-k by descending estimate, then flip the sign so
    // the returned `(doc_id, distance)` pairs follow the standard
    // "smaller = closer" convention. The value is a 1-bit-derived
    // score, not a true metric distance; for these columns recall is
    // the contract, not numerical agreement with fp32. `rerank_mult`
    // is intentionally ignored — there's nothing to refine.
    if matches!(col.rerank_codec, RerankCodec::RabitqOnly) {
        let _ = ctx.rerank_mult;
        // `total_cmp` (not `partial_cmp`→Equal) so a NaN score orders
        // deterministically here exactly as it does on the multi-cell
        // merge path — otherwise the two paths diverge on top-k for a
        // corrupt/degenerate score.
        shortlist.sort_unstable_by(|a, b| b.1.total_cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
        return Ok((
            ShortlistOutcome::Done(
                shortlist
                    .into_iter()
                    .map(|(did, est, _pos, _c)| (did, -est))
                    .collect(),
            ),
            scan_kernel_ns,
        ));
    }

    // Build lightweight rerank references into the cluster blocks
    // already in hand — no second fetch and no survivor byte packing.
    // Each block's `full_chunk` follows its `[codes][doc_ids]` prefix;
    // the candidate at cluster-order position `pos` lives at in-block
    // offset `cnt*cb + cnt*4 + local*stride`.
    let mut block_by_cid: HashMap<u32, usize> = HashMap::with_capacity(cluster_meta.len());
    for (bi, &(c, _, _)) in cluster_meta.iter().enumerate() {
        block_by_cid.insert(c as u32, bi);
    }
    let stride = full_vec_bytes;
    let mut candidates = Vec::with_capacity(shortlist.len());
    let mut survivor_full_ranges = if survivor_only_rerank_fetch {
        Some(Vec::with_capacity(shortlist.len()))
    } else {
        None
    };
    for &(did, _, pos, cluster_id) in &shortlist {
        let bi = block_by_cid[&cluster_id];
        let (_, off, cnt) = cluster_meta[bi];
        let full_start = (cnt as usize) * cb + (cnt as usize) * 4;
        let local = (pos - off) as usize;
        let full_idx = if let Some(ranges) = survivor_full_ranges.as_mut() {
            let idx = ranges.len();
            ranges.push(col.cluster_rerank_row_range(off, cnt, local));
            Some(idx)
        } else {
            None
        };
        candidates.push(RerankCandidate {
            did,
            pos,
            cluster_id,
            block_idx: bi,
            full_off: full_start + local * stride,
            full_idx,
        });
    }
    Ok((
        ShortlistOutcome::Rerank {
            candidates,
            survivor_full_ranges,
        },
        scan_kernel_ns,
    ))
}

/// Maximum multiplier applied to filtered-search probe breadth and
/// rerank width. Caps the inverse-selectivity boost so very sparse
/// predicates don't turn every query into a full cluster scan.
const MAX_FILTER_SELECTIVITY_MULT: usize = 64;
/// Maximum effective rerank multiplier after filtered-search selectivity scaling.
const MAX_EFFECTIVE_FILTERED_RERANK_MULT: usize = 16_384;
/// Multiplier for the unfiltered path, and for degenerate empty-column
/// metadata where there is no population to estimate selectivity from.
const UNFILTERED_SELECTIVITY_MULT: usize = 1;
/// Multiplier for a present-but-empty allow-set: no row can match, so
/// callers should return an empty result without probing.
const EMPTY_FILTER_SELECTIVITY_MULT: usize = 0;
/// Population count for an empty allow-set or empty column.
const EMPTY_FILTER_POPULATION: u64 = 0;
/// Numerator for the inverse-selectivity multiplier (`1 / selectivity`).
const FULL_SELECTIVITY: f64 = 1.0;

/// Compute the inverse-selectivity multiplier for filtered search.
/// Returns [`UNFILTERED_SELECTIVITY_MULT`] when `allow` is `None`
/// (unfiltered). Returns [`EMPTY_FILTER_SELECTIVITY_MULT`] when `allow`
/// is present but empty (no row can match — callers must short-circuit).
/// Capped at [`MAX_FILTER_SELECTIVITY_MULT`].
fn filter_selectivity_mult(allow: &Option<Arc<RoaringBitmap>>, n_docs: u32) -> usize {
    let Some(bm) = allow.as_ref() else {
        return UNFILTERED_SELECTIVITY_MULT;
    };
    let allowed = bm.len();
    if allowed == EMPTY_FILTER_POPULATION {
        return EMPTY_FILTER_SELECTIVITY_MULT;
    }
    let n = n_docs as u64;
    if n == EMPTY_FILTER_POPULATION {
        return UNFILTERED_SELECTIVITY_MULT;
    }
    let selectivity = allowed as f64 / n as f64;
    (FULL_SELECTIVITY / selectivity)
        .ceil()
        .min(MAX_FILTER_SELECTIVITY_MULT as f64) as usize
}

/// Scale rerank breadth for filtered search and cap before shortlist sizing.
fn effective_filtered_rerank_mult(rerank_mult: usize, filter_mult: usize) -> usize {
    rerank_mult
        .saturating_mul(filter_mult)
        .min(MAX_EFFECTIVE_FILTERED_RERANK_MULT)
}

test_visible! {
/// Effective `(nprobe, rerank_mult)` after filtered-search selectivity
/// scaling — exactly the values [`ColumnReader`]'s self-routed search
/// computes before probing. `None` for a present-but-empty allow-set
/// (no row can match; the search returns empty without probing).
///
/// One math, two consumers: the search path above and the bench's
/// filtered-search table, which reports the effective parameters and
/// must never drift from what the engine actually runs.
fn effective_filtered_params(
    allow: &Option<Arc<RoaringBitmap>>,
    n_docs: u32,
    n_cent: u32,
    nprobe: usize,
    rerank_mult: usize,
) -> Option<(usize, usize)> {
    let filter_mult = filter_selectivity_mult(allow, n_docs);
    if filter_mult == EMPTY_FILTER_SELECTIVITY_MULT {
        return None;
    }
    let nprobe_eff = nprobe
        .saturating_mul(filter_mult)
        .min(n_cent as usize)
        .max(1);
    Some((
        nprobe_eff,
        effective_filtered_rerank_mult(rerank_mult, filter_mult),
    ))
}
}

/// Score `query` against every centroid in `centroids_bytes` and
/// return the top `nprobe` `(cluster_id, distance)` pairs sorted by
/// ascending distance (closest first).
///
/// Takes a `&[u8]` view so the caller can hand in either an
/// in-memory subsection slice or the just-fetched centroids
/// region bytes from [`Source::get_range`] — both reach this
/// helper through the same shape. Thin adapter over
/// [`nearest_k_centroids_bytes`], the row-major-layout centroid-scan
/// owner in `distance` (its transposed sibling serves the manifest
/// routing paths).
#[inline]
fn score_centroids(
    centroids_bytes: &[u8],
    col: &ColumnReader,
    query: &[f32],
    nprobe: usize,
) -> Vec<(usize, f32)> {
    nearest_k_centroids_bytes(
        col.metric,
        query,
        centroids_bytes,
        col.n_cent as usize,
        col.dim,
        nprobe,
    )
    .into_iter()
    .map(|(c, score)| (c as usize, score))
    .collect()
}

/// Minimum candidate-pool size before per-query scans (coarse 1-bit
/// scoring and rerank) switch from a serial loop to a rayon parallel
/// scan. Below this the fixed rayon dispatch cost outweighs the
/// multicore speedup, so small queries — notably the 1M single-
/// superfile nprobe=1 hot path — stay serial, while the 10M
/// supertable's `nprobe × superfiles` fan-out goes parallel.
const PARALLEL_SCAN_MIN: usize = 2048;

/// Amortized shortlist-truncation slack: scan accumulators may grow to
/// this multiple of `coarse_limit` before being partitioned back down.
/// At real cell shapes (~9K rows vs a 6.4K cap) the guard never fires;
/// at the 500K-row cell backstop it bounds per-task growth to
/// `2 x coarse_limit` entries instead of every scanned row. Early
/// truncation keeps the final set exact: the kept set is the top
/// `coarse_limit` under a total order, so any row dropped early is
/// dominated by rows that stay until the final partition.
const SHORTLIST_TRUNCATE_SLACK: usize = 2;

/// Number of chunks to split a parallel rayon scan into — the machine's
/// logical parallelism, capped by the item count so we never make more
/// chunks than there is work.
fn parallel_chunks(n_items: usize) -> usize {
    thread::available_parallelism()
        .map(|p| p.get())
        .unwrap_or(1)
        .min(n_items)
        .max(1)
}

/// Map `f` over `items` on the configured rayon pool, preserving input
/// order. The order-independent vector scans (rerank) use this; the
/// compute runs on rayon (`par_iter().map().collect()`) bridged back to
/// the async caller via a oneshot, so no tokio worker blocks under it.
/// `f` and the items must be `'static` so the work can move onto rayon.
/// Returns the mapped items plus the map's bracketed on-CPU ns. The
/// parallel arm runs chunked (`par_chunks` + collect preserves input
/// order exactly as the previous per-item `par_iter` did) with one
/// bracket per chunk, so work-stolen chunks attribute their CPU too.
async fn par_map<T, R, F>(
    items: Vec<T>,
    f: F,
    pool: Option<Arc<ThreadPool>>,
) -> Result<(Vec<R>, u64), VectorError>
where
    T: Send + Sync + 'static,
    R: Send + 'static,
    F: Fn(&T) -> R + Send + Sync + 'static,
{
    let n_tasks = parallel_chunks(items.len());
    if n_tasks <= 1 {
        return Ok(timed_section(|| items.iter().map(&f).collect()));
    }
    run_on_pool(
        pool.as_deref(),
        "rerank rayon task dropped result",
        move || {
            let chunk = items.len().div_ceil(n_tasks).max(1);
            let per_chunk: Vec<(Vec<R>, u64)> = items
                .par_chunks(chunk)
                .map(|c| timed_section(|| c.iter().map(&f).collect()))
                .collect();
            let mut out = Vec::with_capacity(items.len());
            let mut kernel_ns = 0u64;
            for (chunk_out, ns) in per_chunk {
                out.extend(chunk_out);
                kernel_ns += ns;
            }
            (out, kernel_ns)
        },
    )
    .await
    .map_err(|_| VectorError::TaskDropped("rerank"))
}

/// The 1-bit scan loop over one cluster's codes, generic over the
/// candidate sink ([`scan_shortlist`] appends to a plain vec and keeps
/// the top `coarse_limit` with one O(n) partition afterwards).
#[inline]
#[allow(clippy::too_many_arguments)]
fn score_cluster_codes_with(
    cluster_codes: &[u8],
    cluster_doc_ids: &[u8],
    cnt: u32,
    off: u32,
    cluster_id: u32,
    quant: &BitQuantizer,
    q_rot: &[f32],
    allow: Option<&roaring::RoaringBitmap>,
    deny: Option<&roaring::RoaringBitmap>,
    sink: &mut impl FnMut(CoarseCandidate),
) {
    let cb = quant.code_bytes();
    let q_total: f32 = sum_f32(q_rot);
    for i in 0..cnt as usize {
        let did = u32::from_le_bytes([
            cluster_doc_ids[i * 4],
            cluster_doc_ids[i * 4 + 1],
            cluster_doc_ids[i * 4 + 2],
            cluster_doc_ids[i * 4 + 3],
        ]);
        // Filtered search: the predicate's per-superfile allow-set is a
        // hard constraint applied *before* the candidate enters the
        // shortlist, which therefore ranks distance only among matching
        // doc-ids — the top-k is the true k-nearest among matching rows
        // with no underflow, no over-fetch, no post-filter. Decode the
        // code (the hot work) only for an allowed candidate.
        if allow.is_some_and(|bm| !bm.contains(did)) {
            continue;
        }
        // Tombstone deny-set: exclude deleted rows here, before they can
        // take a shortlist slot, so the per-cell top-k is from live rows.
        if deny.is_some_and(|bm| bm.contains(did)) {
            continue;
        }
        let code = &cluster_codes[i * cb..(i + 1) * cb];
        let est = quant.estimate_dot_rotated_with_total(q_rot, code, q_total);
        sink(CoarseCandidate {
            did,
            estimate: est,
            pos: off + i as u32,
            cluster_id,
        });
    }
}

#[derive(Clone, Copy, Debug)]
struct CoarseCandidate {
    did: u32,
    estimate: f32,
    pos: u32,
    cluster_id: u32,
}

#[derive(Clone, Copy)]
struct RerankCandidate {
    did: u32,
    pos: u32,
    cluster_id: u32,
    block_idx: usize,
    full_off: usize,
    full_idx: Option<usize>,
}

/// One min..max byte span per probed cluster, covering every survivor
/// range in it. The rerank gather resolves each span sync-resident once
/// and slices per-survivor rows out of it; `range ⊆ span` holds by
/// construction, so `range.start - span.start` never underflows and the
/// slice returns byte-identical rows to a per-range fetch (pinned by
/// `survivor_cluster_spans_slice_byte_identical_to_per_row_fetch`).
fn survivor_cluster_spans(
    rerank_cands: &[RerankCandidate],
    ranges: &[Range<usize>],
) -> HashMap<u32, Range<usize>> {
    let mut span_by_cluster: HashMap<u32, Range<usize>> = HashMap::new();
    for (cand, range) in rerank_cands.iter().zip(ranges) {
        span_by_cluster
            .entry(cand.cluster_id)
            .and_modify(|span| {
                span.start = span.start.min(range.start);
                span.end = span.end.max(range.end);
            })
            .or_insert_with(|| range.clone());
    }
    span_by_cluster
}

#[inline]
fn candidate_full_bytes<'a>(
    blocks: &'a [Bytes],
    survivor_full_rows: Option<&'a [Bytes]>,
    cand: &RerankCandidate,
    stride: usize,
) -> &'a [u8] {
    if let (Some(rows), Some(idx)) = (survivor_full_rows, cand.full_idx) {
        return &rows[idx];
    }
    &blocks[cand.block_idx][cand.full_off..cand.full_off + stride]
}

/// Decode one cluster's `(off, cnt)` entry from
/// `cluster_idx_slice` (the `n_cent × 8` bytes of the column's
/// cluster index header). `c` is the cluster id. Shared with the
/// byte-splice merge path (`ivf_merge`).
#[inline]
pub(crate) fn read_cluster_entry(cluster_idx_slice: &[u8], c: usize) -> (u32, u32) {
    let base = c * 8;
    let off = u32::from_le_bytes([
        cluster_idx_slice[base],
        cluster_idx_slice[base + 1],
        cluster_idx_slice[base + 2],
        cluster_idx_slice[base + 3],
    ]);
    let cnt = u32::from_le_bytes([
        cluster_idx_slice[base + 4],
        cluster_idx_slice[base + 5],
        cluster_idx_slice[base + 6],
        cluster_idx_slice[base + 7],
    ]);
    (off, cnt)
}

/// Full-precision rerank over `shortlist`, returning the top-`k`
/// `(doc_id, distance)` pairs sorted by ascending distance.
///
/// `candidates` points into the already-fetched per-cluster blocks:
/// each entry carries `(block_idx, full_off)` for its `full[]` row.
/// That avoids allocating and copying a packed survivor buffer on
/// every query while still keeping rerank byte lookup O(1).
///
/// Dispatches on `col.rerank_codec`:
/// - **Fp32**: flat dispatch via [`distance_bytes_codec`]
///   (fp32 zero-copy SIMD).
/// - **Sq8Residual**: builds one [`Sq8ResidualKernel`] per selected cluster and
///   scores every RaBitQ shortlist survivor with both stored bytes. The
///   per-doc decoded norm cached at encode time short-circuits `Σx²` for L2Sq.
/// Per-doc norm access for the Sq16 rerank arm: eager tables index the
/// resident array; lazy tables read one little-endian f32 per candidate
/// straight from the raw norm-table bytes (never a full-table parse on
/// the query path). `Absent` covers NegDot, where the norm term cancels.
#[derive(Clone)]
enum NormLookup {
    Arr(Option<Arc<[f32]>>),
    Raw(Bytes),
    Absent,
}

impl NormLookup {
    #[inline]
    fn get(&self, pos: u32) -> Option<f32> {
        match self {
            Self::Arr(arr) => arr.as_ref().map(|n| n[pos as usize]),
            Self::Raw(bytes) => {
                let p = pos as usize * 4;
                let b: [u8; 4] = bytes[p..p + 4].try_into().expect("norm entry in range");
                Some(f32::from_le_bytes(b))
            }
            Self::Absent => None,
        }
    }
}

/// Fetch per-cluster quantizer rulers (scale/offset) and the per-doc norms for
/// exactly the probed clusters directly from the lazy source, for the
/// no-prefetch fallback. Shared by every cluster-quant codec's lazy path; the
/// caller then builds its own per-cluster kernel and scores. Returns the deduped
/// probed cluster ids, their `(scale, offset)` rulers keyed by cluster, and —
/// when the column stores norms — a candidate-position → norm map.
fn fetch_lazy_cluster_meta(
    source: &Source,
    candidates: &[RerankCandidate],
    rerank_codec: RerankCodec,
    dim: usize,
    scale_abs_off: usize,
    offset_abs_off: usize,
    norms_abs_off: Option<usize>,
) -> Result<
    (
        Vec<u32>,
        HashMap<u32, (Vec<f32>, Vec<f32>)>,
        Option<HashMap<u32, f32>>,
    ),
    VectorError,
> {
    let map_lazy = |e: LazyByteSourceError| VectorError::LazySource(e.to_string());
    let mut clusters: Vec<u32> = candidates.iter().map(|c| c.cluster_id).collect();
    clusters.sort_unstable();
    clusters.dedup();

    let cluster_meta_len = dim * 4;
    let mut ranges = Vec::with_capacity(clusters.len() * 2);
    for &cluster_id in &clusters {
        let c = cluster_id as usize;
        let scale_start = scale_abs_off + c * cluster_meta_len;
        let offset_start = offset_abs_off + c * cluster_meta_len;
        ranges.push(scale_start..scale_start + cluster_meta_len);
        ranges.push(offset_start..offset_start + cluster_meta_len);
    }
    let bytes = source.get_ranges_parallel(&ranges).map_err(map_lazy)?;
    let mut scale_offset_by_cluster: HashMap<u32, (Vec<f32>, Vec<f32>)> =
        HashMap::with_capacity(clusters.len());
    for (idx, &cluster_id) in clusters.iter().enumerate() {
        let scale = parse_f32_le_vec(&bytes[idx * 2]);
        let offset = parse_f32_le_vec(&bytes[idx * 2 + 1]);
        validate_quantizer_meta(rerank_codec, &scale, &offset, "lazy per-cluster metadata")?;
        scale_offset_by_cluster.insert(cluster_id, (scale, offset));
    }

    let norm_by_pos = if let Some(norms_abs_off) = norms_abs_off {
        let mut spans: HashMap<u32, (u32, u32)> = HashMap::new();
        for cand in candidates {
            spans
                .entry(cand.cluster_id)
                .and_modify(|(lo, hi)| {
                    *lo = (*lo).min(cand.pos);
                    *hi = (*hi).max(cand.pos);
                })
                .or_insert((cand.pos, cand.pos));
        }
        let mut span_items: Vec<(u32, u32, u32)> = spans
            .into_iter()
            .map(|(cluster_id, (lo, hi))| (cluster_id, lo, hi))
            .collect();
        span_items.sort_unstable_by_key(|&(cluster_id, _, _)| cluster_id);
        let norm_ranges: Vec<Range<usize>> = span_items
            .iter()
            .map(|&(_, lo, hi)| {
                let start = norms_abs_off + lo as usize * 4;
                start..start + (hi - lo + 1) as usize * 4
            })
            .collect();
        let norm_bytes = source.get_ranges_parallel(&norm_ranges).map_err(map_lazy)?;
        let mut out = HashMap::new();
        for ((_, lo, hi), bytes) in span_items.into_iter().zip(norm_bytes) {
            let vals = parse_f32_le_vec(&bytes);
            for (i, pos) in (lo..=hi).enumerate() {
                out.insert(pos, vals[i]);
            }
        }
        Some(out)
    } else {
        None
    };
    Ok((clusters, scale_offset_by_cluster, norm_by_pos))
}

async fn rerank_candidates_from_blocks(
    source: &Source,
    lazy_sq8_meta_bytes: Option<&Bytes>,
    cluster_blocks: &[Bytes],
    survivor_full_rows: Option<&[Bytes]>,
    candidates: &[RerankCandidate],
    col: &ColumnReader,
    query: &[f32],
    pool: Option<Arc<ThreadPool>>,
    k: usize,
) -> Result<(Vec<(u32, f32)>, u64), VectorError> {
    let stride = col.rerank_codec.per_vector_bytes(col.dim);
    let map_lazy = |e: LazyByteSourceError| VectorError::LazySource(e.to_string());
    // Bracketed on-CPU ns of the scoring sections below (fetches excluded).
    let mut kernel_ns = 0u64;
    let reranked: Vec<(u32, f32)> = match col.rerank_codec {
        RerankCodec::Fp32 => {
            // Exact fp32 rerank — every survivor is independent, so the
            // gather + SIMD distance runs in parallel across the rayon
            // pool once the shortlist is large enough to amortize the
            // hand-off. The output is sorted by distance below, so
            // parallel and serial rank identically.
            if candidates.len() >= PARALLEL_SCAN_MIN {
                let metric = col.metric;
                let codec = col.rerank_codec;
                let blocks: Arc<Vec<Bytes>> = Arc::new(cluster_blocks.to_vec());
                let survivors: Option<Arc<Vec<Bytes>>> =
                    survivor_full_rows.map(|s| Arc::new(s.to_vec()));
                let query: Arc<Vec<f32>> = Arc::new(query.to_vec());
                let (scored, ns) = par_map(
                    candidates.to_vec(),
                    move |cand: &RerankCandidate| {
                        let bytes = candidate_full_bytes(
                            &blocks,
                            survivors.as_deref().map(|s| s.as_slice()),
                            cand,
                            stride,
                        );
                        (cand.did, distance_bytes_codec(metric, codec, &query, bytes))
                    },
                    pool.clone(),
                )
                .await?;
                kernel_ns += ns;
                scored
            } else {
                let (scored, ns) = timed_section(|| {
                    candidates
                        .iter()
                        .map(|cand| {
                            let bytes = candidate_full_bytes(
                                cluster_blocks,
                                survivor_full_rows,
                                cand,
                                stride,
                            );
                            (
                                cand.did,
                                distance_bytes_codec(col.metric, col.rerank_codec, query, bytes),
                            )
                        })
                        .collect()
                });
                kernel_ns += ns;
                scored
            }
        }
        RerankCodec::Sq16 => {
            // Flat single-plane rerank with per-doc norm correction. One
            // `Sq16Kernel` for the whole query (fixed grid — no
            // per-cluster state); each survivor scores as
            // `base - dot/‖d̂‖` (Cosine) using its stored dequantized
            // norm. The norm table is carried in the SHARED
            // `Sq8ColumnMeta` and indexed by `cand.pos` with the exact
            // same expression the residual family uses
            // (`score_sq8_residual_candidates`), so a candidate's norm is
            // guaranteed to be its own — no parallel Sq16 norm path.
            let norms: NormLookup = match col.sq8_meta.as_ref() {
                Some(Sq8ColumnMeta::Eager { per_doc_norms, .. }) => {
                    NormLookup::Arr(per_doc_norms.clone())
                }
                Some(Sq8ColumnMeta::Lazy { norms_abs_off, .. }) => {
                    if let Some(meta_bytes) = lazy_sq8_meta_bytes {
                        // Sq16's codec_meta region IS the norm table. Read
                        // per-candidate 4-byte entries straight from the
                        // raw bytes: parsing the whole table costs one
                        // f32-decode per STORED row per rerank call
                        // (measured on Cohere-1M defaults: 2.6 ms/query),
                        // for the handful of positions a call touches.
                        NormLookup::Raw(meta_bytes.clone())
                    } else if let Some(norms_abs_off) = norms_abs_off {
                        let range = *norms_abs_off..*norms_abs_off + col.n_docs as usize * 4;
                        let mut fetched = source
                            .get_ranges_parallel(std::slice::from_ref(&range))
                            .map_err(map_lazy)?;
                        NormLookup::Raw(fetched.swap_remove(0))
                    } else {
                        NormLookup::Absent
                    }
                }
                // NegDot (Sq16 is cosine-only in practice) → no norms.
                None => NormLookup::Absent,
            };
            let kernel = Sq16Kernel::new(col.metric, query);
            if candidates.len() >= PARALLEL_SCAN_MIN {
                let kernel = Arc::new(kernel);
                let blocks: Arc<Vec<Bytes>> = Arc::new(cluster_blocks.to_vec());
                let survivors: Option<Arc<Vec<Bytes>>> =
                    survivor_full_rows.map(|s| Arc::new(s.to_vec()));
                let norms = norms.clone();
                let (scored, ns) = par_map(
                    candidates.to_vec(),
                    move |cand: &RerankCandidate| {
                        let bytes = candidate_full_bytes(
                            &blocks,
                            survivors.as_deref().map(|s| s.as_slice()),
                            cand,
                            stride,
                        );
                        let norm = norms.get(cand.pos);
                        (cand.did, kernel.distance_with_norm(bytes, norm))
                    },
                    pool.clone(),
                )
                .await?;
                kernel_ns += ns;
                scored
            } else {
                let (scored, ns) = timed_section(|| {
                    candidates
                        .iter()
                        .map(|cand| {
                            let bytes = candidate_full_bytes(
                                cluster_blocks,
                                survivor_full_rows,
                                cand,
                                stride,
                            );
                            let norm = norms.get(cand.pos);
                            (cand.did, kernel.distance_with_norm(bytes, norm))
                        })
                        .collect()
                });
                kernel_ns += ns;
                scored
            }
        }
        RerankCodec::Sq8Residual | RerankCodec::Sq8FixedResidual => {
            let residual_divisor = col
                .rerank_codec
                .residual_divisor()
                .expect("residual-family codec has divisor");
            let meta = col
                .sq8_meta
                .as_ref()
                .expect("Sq8Residual column must carry sq8_meta (built in open_with)");
            let dim = col.dim;
            // `Sq8Residual` stores `[code dim u8 ‖ residual dim i8]`
            // per vector (`stride == 2·dim`); the first `dim` bytes
            // are the Sq8 code leg the shortlist scoring reads.
            match meta {
                Sq8ColumnMeta::Eager {
                    scale,
                    offset,
                    per_doc_norms,
                } => {
                    let (scored, ns) = score_sq8_residual_candidates(
                        candidates,
                        cluster_blocks,
                        survivor_full_rows,
                        col.metric,
                        col.dim,
                        query,
                        scale,
                        offset,
                        per_doc_norms.clone(),
                        residual_divisor,
                        pool.clone(),
                        stride,
                    )
                    .await?;
                    kernel_ns += ns;
                    scored
                }
                Sq8ColumnMeta::Lazy {
                    scale_abs_off,
                    offset_abs_off,
                    norms_abs_off,
                } => {
                    if let Some(meta_bytes) = lazy_sq8_meta_bytes {
                        if col.lazy_sq8_parsed.get().is_none() {
                            let parsed = parse_sq8_meta_bytes(
                                meta_bytes,
                                col.n_cent as usize,
                                dim,
                                col.n_docs as usize,
                                norms_abs_off.is_some(),
                                col.rerank_codec,
                            )?;
                            let _ = col.lazy_sq8_parsed.set(Arc::new(parsed));
                        }
                        let parsed = Arc::clone(
                            col.lazy_sq8_parsed
                                .get()
                                .expect("invariant: lazy Sq8 meta set just above"),
                        );
                        let (scored, ns) = score_sq8_residual_candidates(
                            candidates,
                            cluster_blocks,
                            survivor_full_rows,
                            col.metric,
                            col.dim,
                            query,
                            parsed.scale.as_slice(),
                            parsed.offset.as_slice(),
                            parsed.per_doc_norms.clone(),
                            residual_divisor,
                            pool.clone(),
                            stride,
                        )
                        .await?;
                        return Ok((finalize_reranked(scored, k), kernel_ns + ns));
                    }
                    let (clusters, scale_offset_by_cluster, norm_by_pos) = fetch_lazy_cluster_meta(
                        source,
                        candidates,
                        col.rerank_codec,
                        dim,
                        *scale_abs_off,
                        *offset_abs_off,
                        *norms_abs_off,
                    )?;

                    let kernels: HashMap<u32, Sq8ResidualKernel> = clusters
                        .into_iter()
                        .map(|cluster_id| {
                            let (scale, offset) = scale_offset_by_cluster
                                .get(&cluster_id)
                                .expect("cluster metadata fetched");
                            (
                                cluster_id,
                                Sq8ResidualKernel::new(
                                    col.metric,
                                    query,
                                    scale,
                                    offset,
                                    residual_divisor,
                                ),
                            )
                        })
                        .collect();
                    let (scored, ns) = timed_section(|| {
                        candidates
                            .iter()
                            .map(|cand| {
                                let row = candidate_full_bytes(
                                    cluster_blocks,
                                    survivor_full_rows,
                                    cand,
                                    stride,
                                );
                                let kernel = kernels
                                    .get(&cand.cluster_id)
                                    .expect("invariant: kernel built for every candidate cluster");
                                let norm = norm_by_pos
                                    .as_ref()
                                    .and_then(|norms| norms.get(&cand.pos).copied());
                                (
                                    cand.did,
                                    kernel.distance_with_norm(
                                        &row[..dim],
                                        &row[dim..dim * 2],
                                        norm,
                                    ),
                                )
                            })
                            .collect()
                    });
                    kernel_ns += ns;
                    scored
                }
            }
        }
        RerankCodec::Sq16Adaptive => {
            // Single-`u16`-plane rerank over per-cluster fitted rulers. Reuses
            // the residual family's `Sq8ColumnMeta` (scale/offset + per-doc
            // norms) and the same Eager/Lazy meta-fetch; only the kernel
            // (per-cluster `Sq16Kernel::new_adaptive`) and the single-plane body
            // differ — no residual divisor.
            let meta = col
                .sq8_meta
                .as_ref()
                .expect("Sq16Adaptive column must carry sq8_meta (built in open_with)");
            let dim = col.dim;
            match meta {
                Sq8ColumnMeta::Eager {
                    scale,
                    offset,
                    per_doc_norms,
                } => {
                    let (scored, ns) = score_sq16_adaptive_candidates(
                        candidates,
                        cluster_blocks,
                        survivor_full_rows,
                        col.metric,
                        dim,
                        query,
                        scale,
                        offset,
                        per_doc_norms.clone(),
                        pool.clone(),
                        stride,
                    )
                    .await?;
                    kernel_ns += ns;
                    scored
                }
                Sq8ColumnMeta::Lazy {
                    scale_abs_off,
                    offset_abs_off,
                    norms_abs_off,
                } => {
                    if let Some(meta_bytes) = lazy_sq8_meta_bytes {
                        if col.lazy_sq8_parsed.get().is_none() {
                            let parsed = parse_sq8_meta_bytes(
                                meta_bytes,
                                col.n_cent as usize,
                                dim,
                                col.n_docs as usize,
                                norms_abs_off.is_some(),
                                col.rerank_codec,
                            )?;
                            let _ = col.lazy_sq8_parsed.set(Arc::new(parsed));
                        }
                        let parsed = Arc::clone(
                            col.lazy_sq8_parsed
                                .get()
                                .expect("lazy Sq8 meta set just above"),
                        );
                        let (scored, ns) = score_sq16_adaptive_candidates(
                            candidates,
                            cluster_blocks,
                            survivor_full_rows,
                            col.metric,
                            dim,
                            query,
                            parsed.scale.as_slice(),
                            parsed.offset.as_slice(),
                            parsed.per_doc_norms.clone(),
                            pool.clone(),
                            stride,
                        )
                        .await?;
                        return Ok((finalize_reranked(scored, k), kernel_ns + ns));
                    }
                    let (clusters, scale_offset_by_cluster, norm_by_pos) = fetch_lazy_cluster_meta(
                        source,
                        candidates,
                        col.rerank_codec,
                        dim,
                        *scale_abs_off,
                        *offset_abs_off,
                        *norms_abs_off,
                    )?;

                    let kernels: HashMap<u32, Sq16Kernel> = clusters
                        .into_iter()
                        .map(|cluster_id| {
                            let (scale, offset) = scale_offset_by_cluster
                                .get(&cluster_id)
                                .expect("cluster metadata fetched");
                            (
                                cluster_id,
                                Sq16Kernel::new_adaptive(col.metric, query, scale, offset),
                            )
                        })
                        .collect();
                    let (scored, ns) = timed_section(|| {
                        candidates
                            .iter()
                            .map(|cand| {
                                let row = candidate_full_bytes(
                                    cluster_blocks,
                                    survivor_full_rows,
                                    cand,
                                    stride,
                                );
                                let kernel = kernels
                                    .get(&cand.cluster_id)
                                    .expect("invariant: kernel built for every candidate cluster");
                                let norm = norm_by_pos
                                    .as_ref()
                                    .and_then(|norms| norms.get(&cand.pos).copied());
                                (cand.did, kernel.distance_with_norm(row, norm))
                            })
                            .collect()
                    });
                    kernel_ns += ns;
                    scored
                }
            }
        }
        RerankCodec::RabitqOnly => unreachable!(
            "rerank_candidates_in_run reached with None codec — None columns \
             have no full[] region and should short-circuit before the rerank step"
        ),
    };
    Ok((finalize_reranked(reranked, k), kernel_ns))
}

fn finalize_reranked(mut reranked: Vec<(u32, f32)>, k: usize) -> Vec<(u32, f32)> {
    // Distance ascending, `total_cmp` + id tie-break so single-cell top-k
    // is bit-for-bit the same ordering the multi-cell merge produces
    // (both must agree on where a NaN score lands).
    reranked.sort_unstable_by(|a, b| a.1.total_cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
    reranked.truncate(k);
    reranked
}

/// Score every RaBitQ shortlist survivor with its per-cluster rerank kernel,
/// building one kernel per distinct probed cluster and parallelizing once the
/// shortlist exceeds [`PARALLEL_SCAN_MIN`].
///
/// Generic over the kernel type `K` so the two-plane residual codecs and the
/// single-`u16`-plane `Sq16Adaptive` codec share the cluster-dedup, per-cluster
/// kernel map, and serial/parallel fan-out. The caller supplies `build_kernel`
/// (cluster id → kernel, folding that cluster's ruler) and `score_row` (kernel,
/// full row bytes, optional norm → distance), which is where the body layout
/// (two-plane `[code | residual]` vs single `u16` plane) and any divisor live.
/// Both code paths keep their own data-access strategy (eager mmap vs lazy range
/// GETs); only the scoring fan-out is shared here.
#[allow(clippy::too_many_arguments)]
async fn score_candidates_with_kernel<K, B, S>(
    candidates: &[RerankCandidate],
    cluster_blocks: &[Bytes],
    survivor_full_rows: Option<&[Bytes]>,
    per_doc_norms: Option<Arc<[f32]>>,
    pool: Option<Arc<ThreadPool>>,
    stride: usize,
    build_kernel: B,
    score_row: S,
) -> Result<(Vec<(u32, f32)>, u64), VectorError>
where
    K: Send + Sync + 'static,
    B: Fn(usize) -> K,
    S: Fn(&K, &[u8], Option<f32>) -> f32 + Send + Sync + 'static,
{
    let mut cids: Vec<u32> = candidates.iter().map(|c| c.cluster_id).collect();
    cids.sort_unstable();
    cids.dedup();
    let kernels: HashMap<u32, K> = cids
        .into_iter()
        .map(|cid| (cid, build_kernel(cid as usize)))
        .collect();
    let score_one = |cand: &RerankCandidate| {
        let row = candidate_full_bytes(cluster_blocks, survivor_full_rows, cand, stride);
        let kernel = kernels
            .get(&cand.cluster_id)
            .expect("invariant: kernel prebuilt for every probed cluster");
        let norm = per_doc_norms.as_ref().map(|norms| norms[cand.pos as usize]);
        (cand.did, score_row(kernel, row, norm))
    };
    if candidates.len() >= PARALLEL_SCAN_MIN {
        // Every candidate is independent and the caller sorts the completed
        // scores, so parallel and serial paths are observationally identical.
        let kernels = Arc::new(kernels);
        let blocks: Arc<Vec<Bytes>> = Arc::new(cluster_blocks.to_vec());
        let survivors: Option<Arc<Vec<Bytes>>> = survivor_full_rows.map(|s| Arc::new(s.to_vec()));
        let norms = per_doc_norms;
        par_map(
            candidates.to_vec(),
            move |cand: &RerankCandidate| {
                let row = candidate_full_bytes(
                    &blocks,
                    survivors.as_deref().map(|s| s.as_slice()),
                    cand,
                    stride,
                );
                let kernel = kernels
                    .get(&cand.cluster_id)
                    .expect("invariant: kernel prebuilt for every probed cluster");
                let norm = norms.as_ref().map(|norms| norms[cand.pos as usize]);
                (cand.did, score_row(kernel, row, norm))
            },
            pool,
        )
        .await
    } else {
        Ok(timed_section(|| candidates.iter().map(score_one).collect()))
    }
}

/// Score survivors with their full Sq8+residual payload: one
/// [`Sq8ResidualKernel`] per probed cluster, two-plane `[code | residual]` body.
#[allow(clippy::too_many_arguments)]
async fn score_sq8_residual_candidates(
    candidates: &[RerankCandidate],
    cluster_blocks: &[Bytes],
    survivor_full_rows: Option<&[Bytes]>,
    metric: Metric,
    dim: usize,
    query: &[f32],
    scale: &[f32],
    offset: &[f32],
    per_doc_norms: Option<Arc<[f32]>>,
    residual_divisor: f32,
    pool: Option<Arc<ThreadPool>>,
    stride: usize,
) -> Result<(Vec<(u32, f32)>, u64), VectorError> {
    score_candidates_with_kernel(
        candidates,
        cluster_blocks,
        survivor_full_rows,
        per_doc_norms,
        pool,
        stride,
        |c| {
            let scale_c = &scale[c * dim..(c + 1) * dim];
            let offset_c = &offset[c * dim..(c + 1) * dim];
            Sq8ResidualKernel::new(metric, query, scale_c, offset_c, residual_divisor)
        },
        move |kernel: &Sq8ResidualKernel, row: &[u8], norm| {
            kernel.distance_with_norm(&row[..dim], &row[dim..dim * 2], norm)
        },
    )
    .await
}

/// Adaptive-ruler twin: score survivors with their single-`u16`-plane
/// `Sq16Adaptive` payload, building one [`Sq16Kernel`] per probed cluster via
/// `new_adaptive` (folding that cluster's fitted ruler). The whole `stride`-byte
/// row is the `u16` codes; no residual divisor.
#[allow(clippy::too_many_arguments)]
async fn score_sq16_adaptive_candidates(
    candidates: &[RerankCandidate],
    cluster_blocks: &[Bytes],
    survivor_full_rows: Option<&[Bytes]>,
    metric: Metric,
    dim: usize,
    query: &[f32],
    scale: &[f32],
    offset: &[f32],
    per_doc_norms: Option<Arc<[f32]>>,
    pool: Option<Arc<ThreadPool>>,
    stride: usize,
) -> Result<(Vec<(u32, f32)>, u64), VectorError> {
    score_candidates_with_kernel(
        candidates,
        cluster_blocks,
        survivor_full_rows,
        per_doc_norms,
        pool,
        stride,
        |c| {
            let scale_c = &scale[c * dim..(c + 1) * dim];
            let offset_c = &offset[c * dim..(c + 1) * dim];
            Sq16Kernel::new_adaptive(metric, query, scale_c, offset_c)
        },
        |kernel: &Sq16Kernel, row: &[u8], norm| kernel.distance_with_norm(row, norm),
    )
    .await
}

fn parse_sq8_meta_bytes(
    bytes: &[u8],
    n_cent: usize,
    dim: usize,
    n_docs: usize,
    has_norms: bool,
    rerank_codec: RerankCodec,
) -> Result<Sq8ParsedMeta, VectorError> {
    let so_block_bytes = n_cent * dim * 4;
    let scale_end = so_block_bytes;
    let offset_end = scale_end + so_block_bytes;
    let scale = parse_f32_le_vec(&bytes[0..scale_end]);
    let offset = parse_f32_le_vec(&bytes[scale_end..offset_end]);
    // Same release-path check the eager open uses — corrupt/non-finite
    // scale/offset on the cold lazy path must fail loud, not silently
    // score with bad quantizer metadata.
    validate_quantizer_meta(rerank_codec, &scale, &offset, "lazy metadata")?;
    let per_doc_norms = has_norms.then(|| {
        let norms_end = offset_end + n_docs * 4;
        Arc::from(parse_f32_le_vec(&bytes[offset_end..norms_end]))
    });
    Ok(Sq8ParsedMeta {
        scale,
        offset,
        per_doc_norms,
    })
}

fn validate_quantizer_meta(
    rerank_codec: RerankCodec,
    scale: &[f32],
    offset: &[f32],
    column: &str,
) -> Result<(), VectorError> {
    if scale
        .iter()
        .chain(offset.iter())
        .any(|value| !value.is_finite())
    {
        return Err(VectorError::Read(ReadError::MalformedVersion(format!(
            "column {column:?} has non-finite quantizer metadata"
        ))));
    }
    // The pinned-constant check is specific to the sq8-residual fixed
    // grid (`SQ8_FIXED_*`). Only Sq8FixedResidual (fixed-quantizer AND
    // residual-family) carries such an array. Sq16 is fixed-quantizer
    // but stores no codec_meta, so it never reaches here with scale/
    // offset arrays — return Ok vacuously if it ever did.
    if !(rerank_codec.uses_fixed_quantizer() && rerank_codec.is_sq8_residual_family()) {
        return Ok(());
    }
    let valid = scale
        .iter()
        .all(|value| value.to_bits() == SQ8_FIXED_SCALE.to_bits())
        && offset
            .iter()
            .all(|value| value.to_bits() == SQ8_FIXED_OFFSET.to_bits());
    if valid {
        Ok(())
    } else {
        Err(VectorError::Read(ReadError::MalformedVersion(format!(
            "column {column:?} has non-fixed quantizer metadata for codec {}",
            rerank_codec.name()
        ))))
    }
}

#[inline]
fn read_u32_le(b: &[u8]) -> u32 {
    u32::from_le_bytes([b[0], b[1], b[2], b[3]])
}

/// Decode an aligned-or-not `&[u8]` of length `4·N` as a
/// `Vec<f32>` of length `N`. Used for Sq8's `codec_meta` arrays
/// (scale, offset, per-doc norms) where the byte slice can land
/// at any alignment relative to the `Bytes` backing — see the
/// reader-side note where this is called for the alignment
/// argument. Slow path (4 byte reads per f32) but only runs at
/// open time over at-most-`8·dim + 4·n_docs` bytes per Sq8
/// column; the per-query inner loop never goes through here.
#[inline]
fn parse_f32_le_vec(bytes: &[u8]) -> Vec<f32> {
    debug_assert!(bytes.len().is_multiple_of(4));
    let n = bytes.len() / 4;
    let mut out = Vec::with_capacity(n);
    for chunk in bytes.chunks_exact(4) {
        out.push(f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
    }
    out
}

#[inline]
fn read_u64_le(b: &[u8]) -> u64 {
    let mut buf = [0u8; 8];
    buf.copy_from_slice(&b[0..8]);
    u64::from_le_bytes(buf)
}

/// Overflow-checked directory bounds for a vector blob's outer header.
/// `entry_count` and `entry_size` derive from untrusted header fields, so a
/// crafted blob could wrap `entry_count * entry_size` or the
/// `dir_offset + dir_size + CRC` sum past a bounds guard; both are checked
/// here. Returns `(dir_size, dir_end)` with `dir_end = dir_offset + dir_size +
/// CRC_BYTES`; callers compare `dir_end` against the actual blob/source length.
fn checked_dir_bounds(
    dir_offset: usize,
    entry_count: usize,
    entry_size: usize,
) -> Result<(usize, usize), VectorError> {
    let dir_size = entry_count.checked_mul(entry_size).ok_or_else(|| {
        VectorError::Read(ReadError::MalformedVersion(format!(
            "vector directory size overflow (entries={entry_count})",
        )))
    })?;
    let dir_end = dir_offset
        .checked_add(dir_size)
        .and_then(|x| x.checked_add(format::CRC_BYTES))
        .ok_or_else(|| {
            VectorError::Read(ReadError::MalformedVersion(format!(
                "vector directory offset+size overflow (dir_offset={dir_offset})",
            )))
        })?;
    Ok((dir_size, dir_end))
}

/// Gap / overfetch windows for the survivor rerank-row wave. Survivor
/// rows scatter across the selected clusters' `full[]` regions; with the
/// geometric cluster ordering those regions are file-adjacent, sitting
/// within roughly one cluster block (~0.7 MiB at the 10M shape) of each
/// other. A window sized past that stride merges survivors spanning
/// neighboring runs into one range — one fewer cold round trip per
/// query for a bounded, cold-only overfetch. The prefix wave keeps the
/// tight windows above (its ranges are small and already adjacent).
const SURVIVOR_RANGE_COALESCE_MAX_GAP: usize = 2 * 1024 * 1024;
const SURVIVOR_RANGE_COALESCE_MAX_OVERFETCH: usize = 2 * 1024 * 1024;

fn lazy_sq8_meta_range(col: &ColumnReader) -> Option<Range<usize>> {
    let Sq8ColumnMeta::Lazy { scale_abs_off, .. } = col.sq8_meta.as_ref()? else {
        return None;
    };
    let scale_offset_bytes = 2 * (col.n_cent as usize) * col.dim * 4;
    let norm_bytes = if matches!(col.metric, Metric::L2Sq | Metric::Cosine) {
        (col.n_docs as usize) * 4
    } else {
        0
    };
    Some(*scale_abs_off..*scale_abs_off + scale_offset_bytes + norm_bytes)
}

// Reserve from the budget, the bytes a cold fetch is about to allocate, and if
// they do not fit, refuse the search with [`VectorError::OverBudget`] before
// anything is allocated.
//
// Only the ranges that must be fetched are counted. A range already in memory
// is returned as a zero-copy slice and needs no new memory, so each range is
// checked and only the missing ("cold") bytes are reserved.
//
// The returned guard owns the reservation: hold it while the fetched bytes are
// in use, and dropping it returns them to the budget. A range evicted between
// the check here and the fetch is read without a reservation and covered by
// the budget's headroom.
fn reserve_cold_fetch(
    source: &Source,
    ranges: &[Range<usize>],
    budget: Option<&Arc<ConnectionMemoryBudget>>,
) -> Result<Option<Reservation>, VectorError> {
    let Some(budget) = budget else {
        // No budget attached: measure-only, nothing to gate.
        return Ok(None);
    };

    let cold_bytes: usize = ranges
        .iter()
        .filter(|r| source.try_get_range_sync((*r).clone()).is_none())
        .map(|r| r.len())
        .sum();

    if cold_bytes == 0 {
        // Everything already exists in memory.
        return Ok(None);
    }

    budget
        .try_reserve(cold_bytes)
        .map(Some)
        .map_err(|e| VectorError::OverBudget(format!("vector search, {e}")))
}

/// Gap / overfetch windows for a COLD probe wave (blocks + metadata
/// legs). A cold read is round-trip-bound, not byte-bound: the probed
/// cell's Sq8 meta, stable-id region, and block runs can sit megabytes
/// apart inside a large packed cell (~60 MiB at 10M docs), and the tight
/// warm windows below shattered one cell's read into 5 GETs there
/// (measured; 1 GET at 1M where the whole cell spans ~6 MiB). Merge
/// anything with sub-8 MiB gaps, capped at 8 MiB of overfetch per merged
/// range — never more than one extra round-trip's worth of bytes to save
/// a round trip.
const COLD_PROBE_COALESCE_MAX_GAP: usize = 8 * 1024 * 1024;
/// Overfetch cap per merged cold-probe range (see
/// [`COLD_PROBE_COALESCE_MAX_GAP`]).
const COLD_PROBE_COALESCE_MAX_OVERFETCH: usize = 8 * 1024 * 1024;

/// Build the one-plan input for a cold probe wave: the cluster block
/// ranges PLUS the per-column metadata legs (`extras`: lazy Sq8 meta,
/// inline stable-id region), under the wide cold windows above. Returns
/// `(blocks, extras)` in input order.
fn probe_wave_plan(ranges: &[Range<usize>], extras: &[Range<usize>]) -> RangeCoalescePlan {
    let mut all: Vec<Range<usize>> = Vec::with_capacity(ranges.len() + extras.len());
    all.extend(ranges.iter().cloned());
    all.extend(extras.iter().cloned());
    RangeCoalescePlan::new(
        &all,
        COLD_PROBE_COALESCE_MAX_GAP,
        COLD_PROBE_COALESCE_MAX_OVERFETCH,
    )
}

fn get_cluster_ranges_coalesced_with_extras(
    source: &Source,
    ranges: &[Range<usize>],
    extras: &[Range<usize>],
) -> Result<(Vec<Bytes>, Vec<Bytes>), LazyByteSourceError> {
    let plan = probe_wave_plan(ranges, extras);
    let fetched = source.get_ranges_parallel(plan.fetch_ranges())?;
    let mut restored = plan.restore(&fetched);
    let extra_bytes = restored.split_off(ranges.len());
    Ok((restored, extra_bytes))
}

/// Async sibling of [`get_cluster_ranges_coalesced_with_extras`]. Same
/// coalescing plan, dispatched as one `try_join_all` batch on the
/// caller's runtime so connections pool and the fan-out is concurrent.
async fn get_cluster_ranges_coalesced_with_extras_async(
    source: &Source,
    ranges: &[Range<usize>],
    extras: &[Range<usize>],
) -> Result<(Vec<Bytes>, Vec<Bytes>), LazyByteSourceError> {
    let plan = probe_wave_plan(ranges, extras);
    let fetched = source
        .get_ranges_parallel_async(plan.fetch_ranges())
        .await?;
    let mut restored = plan.restore(&fetched);
    let extra_bytes = restored.split_off(ranges.len());
    Ok((restored, extra_bytes))
}

/// Survivor-wave fetch: plan/restore over the
/// [`SURVIVOR_RANGE_COALESCE_MAX_GAP`] windows, so survivor rows spanning
/// geometrically neighboring clusters merge into one cold range.
fn get_survivor_ranges_coalesced(
    source: &Source,
    ranges: &[Range<usize>],
) -> Result<Vec<Bytes>, LazyByteSourceError> {
    if ranges.is_empty() {
        return Ok(Vec::new());
    }
    if ranges.len() == 1 {
        return source.get_ranges_parallel(ranges);
    }
    let plan = RangeCoalescePlan::new(
        ranges,
        SURVIVOR_RANGE_COALESCE_MAX_GAP,
        SURVIVOR_RANGE_COALESCE_MAX_OVERFETCH,
    );
    let fetched = source.get_ranges_parallel(plan.fetch_ranges())?;
    Ok(plan.restore(&fetched))
}

/// Async sibling of [`get_survivor_ranges_coalesced`].
async fn get_survivor_ranges_coalesced_async(
    source: &Source,
    ranges: &[Range<usize>],
) -> Result<Vec<Bytes>, LazyByteSourceError> {
    if ranges.is_empty() {
        return Ok(Vec::new());
    }
    if ranges.len() == 1 {
        return source.get_ranges_parallel_async(ranges).await;
    }
    let plan = RangeCoalescePlan::new(
        ranges,
        SURVIVOR_RANGE_COALESCE_MAX_GAP,
        SURVIVOR_RANGE_COALESCE_MAX_OVERFETCH,
    );
    let fetched = source
        .get_ranges_parallel_async(plan.fetch_ranges())
        .await?;
    Ok(plan.restore(&fetched))
}

/// Best-effort sync byte fetch with a typed error. Used throughout
/// `open_with` so every byte access goes through the `Source`
/// abstraction — the lazy variant plumbs the eager-prefetch
/// path through the same call sites without a second rewrite.
///
/// Failure mode here means the range is out-of-bounds or not
/// present in the sync cache. On `Source::InMemory`,
/// any in-bounds range succeeds zero-copy; this only fires on a
/// malformed blob today.
#[inline]
fn fetch_sync(source: &Source, range: Range<usize>, what: &str) -> Result<Bytes, VectorError> {
    let start = range.start;
    let end = range.end;
    source.try_get_range_sync(range).ok_or_else(|| {
        VectorError::Read(ReadError::MalformedVersion(format!(
            "vector {what} range {start}..{end} past blob"
        )))
    })
}

#[cfg(test)]
mod tests {
    use std::{
        cmp::Ordering,
        collections::HashSet,
        fs::File,
        hint::black_box,
        path::{Path, PathBuf},
        sync::Arc,
        time::Duration,
    };

    use memmap2::Mmap;
    use memory_stats::memory_stats;
    use tempfile::NamedTempFile;
    use tokio::time::sleep;

    use super::*;

    /// `checked_dir_bounds` computes `dir_end = offset + count*entry_size + CRC`
    /// and rejects untrusted-header values that would wrap `usize` (a crafted
    /// blob must error, not silently pass a bounds guard on a wrapped value).
    #[test]
    fn checked_dir_bounds_computes_end_and_rejects_overflow() {
        let (size, end) = checked_dir_bounds(100, 4, 16).expect("valid bounds");
        assert_eq!(size, 64);
        assert_eq!(end, 100 + 64 + format::CRC_BYTES);
        assert!(
            checked_dir_bounds(0, usize::MAX, 2).is_err(),
            "count*entry_size overflow must error, not wrap",
        );
        assert!(
            checked_dir_bounds(usize::MAX, 1, 8).is_err(),
            "offset+size+CRC overflow must error, not wrap",
        );
    }
    use crate::superfile::vector::{
        builder::{
            VectorBuilder, VectorConfig, build_merged_subsection_from_materialized,
            finish_multi_cell_blob,
        },
        cell_posting::{EncodedCellRow, MaterializedIvfRow},
    };

    fn build_blob(n_docs: u32, dim: usize, n_cent: usize, metric: Metric) -> (Bytes, String) {
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim,
            rot_seed: 7,
            metric,
            rerank_codec: RerankCodec::Fp32,
            provided_centroids: None,
        })
        .expect("register column");
        for i in 0..n_docs {
            // Deterministic "random" vector.
            let v: Vec<f32> = (0..dim)
                .map(|j| ((i.wrapping_mul(31) + j as u32) % 100) as f32 * 0.01)
                .collect();
            b.add(0, &v).expect("add to vector builder");
        }
        let bytes = b.finish().expect("finish vector builder");
        let metric_s = match metric {
            Metric::L2Sq => "l2sq",
            Metric::Cosine => "cosine",
            Metric::NegDot => "negdot",
        };
        let json = format!(
            r#"[{{"column":"embedding","dim":{dim},"n_cent":{n_cent},"rot_seed":7,"metric":"{metric_s}"}}]"#
        );
        (Bytes::from(bytes), json)
    }

    /// Small valid v2 blob used by corruption tests below. Keeping construction
    /// in one helper makes each test mutate exactly one independent invariant.
    fn build_multi_cell_blob() -> (Vec<u8>, String) {
        let dim = 16usize;
        let make_rows = |cell: u32, n: usize| -> Vec<MaterializedIvfRow> {
            let scale: Arc<[f32]> = Arc::from(vec![1.0f32; dim]);
            let offset: Arc<[f32]> = Arc::from(vec![0.0f32; dim]);
            (0..n)
                .map(|index| {
                    let local_doc_id = index as u32;
                    let stable_id = i128::from(cell) * 1_000 + i128::from(local_doc_id);
                    MaterializedIvfRow {
                        local_doc_id,
                        stable_id,
                        cluster: 0,
                        rabitq_code: vec![0; dim.div_ceil(8)],
                        encoded: EncodedCellRow {
                            stable_id,
                            rerank_codec: RerankCodec::Sq8Residual,
                            scale: Arc::clone(&scale),
                            offset: Arc::clone(&offset),
                            codes: vec![cell as u8; dim],
                            residuals: vec![0; dim],
                            norm_sq: Some(0.0),
                        },
                    }
                })
                .collect()
        };
        let config = VectorConfig {
            column: "embedding".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        };
        let first = build_merged_subsection_from_materialized(config.clone(), 2, make_rows(7, 3))
            .expect("first cell subsection");
        let second = build_merged_subsection_from_materialized(config, 2, make_rows(15, 2))
            .expect("second cell subsection");
        let blob = finish_multi_cell_blob(&[(7, first), (15, second)]).expect("multi-cell blob");
        let json = r#"[{"column":"embedding","dim":16,"n_cent":2,"rot_seed":7,"metric":"l2sq"}]"#
            .to_string();
        (blob, json)
    }

    /// `(offset, len)` of the first v2 cell subsection.
    fn first_multi_cell_subsection(bytes: &[u8]) -> (usize, usize) {
        let directory_offset =
            read_u64_le(&bytes[outer_hdr::DIR_OFFSET_OFF..outer_hdr::DIR_OFFSET_OFF + U64_BYTES])
                as usize;
        let subsection_offset = read_u64_le(
            &bytes[directory_offset + cell_dir_entry::SUBSECTION_OFF_OFF
                ..directory_offset + cell_dir_entry::SUBSECTION_OFF_OFF + U64_BYTES],
        ) as usize;
        let subsection_len = read_u64_le(
            &bytes[directory_offset + cell_dir_entry::SUBSECTION_LEN_OFF
                ..directory_offset + cell_dir_entry::SUBSECTION_LEN_OFF + U64_BYTES],
        ) as usize;
        (subsection_offset, subsection_len)
    }

    #[tokio::test]
    async fn sq8_residual_scores_every_rabitq_shortlist_survivor() {
        let residuals = [-8i8, 0, 8, 16];
        let mut block = Vec::with_capacity(residuals.len() * 2);
        for residual in residuals {
            block.push(10);
            block.push(residual.to_le_bytes()[0]);
        }
        let candidates: Vec<RerankCandidate> = (0..residuals.len())
            .map(|index| RerankCandidate {
                did: index as u32,
                pos: index as u32,
                cluster_id: 0,
                block_idx: 0,
                full_off: index * 2,
                full_idx: None,
            })
            .collect();

        let scored = score_sq8_residual_candidates(
            &candidates,
            &[Bytes::from(block)],
            None,
            Metric::NegDot,
            1,
            &[1.0],
            &[1.0],
            &[0.0],
            None,
            RerankCodec::Sq8Residual
                .residual_divisor()
                .expect("local residual divisor"),
            None,
            2,
        )
        .await
        .expect("sq8 scorer");
        let (mut scored, _kernel_ns) = scored;
        assert_eq!(scored.len(), candidates.len());
        scored.sort_unstable_by(|a, b| a.1.total_cmp(&b.1));
        assert_eq!(
            scored[0].0, 3,
            "the residual-best row must survive regardless of its coarse-code tie"
        );
    }

    #[test]
    fn open_accepts_valid_blob() {
        let (blob, json) = build_blob(64, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open should succeed");
        assert_eq!(r.n_docs(), 64);
        let cols: Vec<&str> = r.vector_columns().collect();
        assert_eq!(cols, vec!["embedding"]);
    }

    #[test]
    fn open_rejects_bad_magic() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let mut bytes = blob.to_vec();
        bytes[0] = b'X';
        let err = VectorReader::open(Bytes::from(bytes), &json).expect_err("expected error");
        assert!(matches!(err, VectorError::Read(ReadError::BadMagic { .. })));
    }

    #[test]
    fn open_rejects_short_blob() {
        let err = VectorReader::open(Bytes::from(vec![0u8; 8]), "[]").expect_err("expected error");
        assert!(matches!(err, VectorError::Read(_)));
    }

    #[test]
    fn open_detects_corruption_via_outer_crc() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let mut bytes = blob.to_vec();
        // Flip a byte in the middle of the directory area.
        let pos = OUTER_HEADER_SIZE + 10;
        bytes[pos] ^= 0xFF;
        let err = VectorReader::open(Bytes::from(bytes), &json).expect_err("expected error");
        assert!(matches!(
            err,
            VectorError::Read(ReadError::ChecksumMismatch { .. })
        ));
    }

    #[test]
    fn multi_cell_open_rejects_outer_doc_count_mismatch_without_crc() {
        let (mut bytes, json) = build_multi_cell_blob();
        bytes[outer_hdr::N_DOCS_OFF..outer_hdr::N_DOCS_OFF + U64_BYTES]
            .copy_from_slice(&0u64.to_le_bytes());
        let error =
            VectorReader::open_with(Bytes::from(bytes), &json, OpenOptions { verify_crc: false })
                .expect_err("outer n_docs must match the cell subsections");
        assert!(
            matches!(
                &error,
                VectorError::Read(ReadError::MalformedVersion(message))
                    if message.contains("summed cell docs")
            ),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn multi_cell_open_verifies_trailing_outer_crc() {
        let (mut bytes, json) = build_multi_cell_blob();
        let outer_crc_byte = bytes.len() - 1;
        bytes[outer_crc_byte] ^= 0xFF;
        let error =
            VectorReader::open(Bytes::from(bytes), &json).expect_err("outer CRC must be verified");
        assert!(
            matches!(
                &error,
                VectorError::Read(ReadError::ChecksumMismatch {
                    section: "vector/multi_cell_outer",
                    ..
                })
            ),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn multi_cell_open_rejects_inverted_centroid_offsets_without_crc() {
        let (mut bytes, json) = build_multi_cell_blob();
        let (subsection_offset, _) = first_multi_cell_subsection(&bytes);
        let centroids_offset = read_u64_le(
            &bytes[subsection_offset + sub_hdr::CENTROIDS_OFF_OFF
                ..subsection_offset + sub_hdr::CENTROIDS_OFF_OFF + U64_BYTES],
        );
        let inverted_cluster_index_offset = centroids_offset - 1;
        bytes[subsection_offset + sub_hdr::CLUSTER_IDX_OFF_OFF
            ..subsection_offset + sub_hdr::CLUSTER_IDX_OFF_OFF + U64_BYTES]
            .copy_from_slice(&inverted_cluster_index_offset.to_le_bytes());

        let error =
            VectorReader::open_with(Bytes::from(bytes), &json, OpenOptions { verify_crc: false })
                .expect_err("inverted centroid offsets must be malformed");
        assert!(
            matches!(
                &error,
                VectorError::Read(ReadError::MalformedVersion(message))
                    if message.contains("cluster_idx_off precedes centroids_off")
            ),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn multi_cell_open_rejects_blocks_offset_past_subsection_crc_without_crc() {
        let (mut bytes, json) = build_multi_cell_blob();
        let (subsection_offset, subsection_len) = first_multi_cell_subsection(&bytes);
        bytes[subsection_offset + sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF
            ..subsection_offset + sub_hdr::PER_CLUSTER_BLOCKS_OFF_OFF + U64_BYTES]
            .copy_from_slice(&(subsection_len as u64).to_le_bytes());

        let error =
            VectorReader::open_with(Bytes::from(bytes), &json, OpenOptions { verify_crc: false })
                .expect_err("blocks offset past the subsection CRC must be malformed");
        assert!(
            matches!(
                &error,
                VectorError::Read(ReadError::MalformedVersion(message))
                    if message.contains("per_cluster_blocks_off past the subsection CRC")
            ),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn multi_cell_open_rejects_non_finite_quantizer_metadata_without_crc() {
        let (mut bytes, json) = build_multi_cell_blob();
        let (subsection_offset, _) = first_multi_cell_subsection(&bytes);
        let centroids_offset = read_u64_le(
            &bytes[subsection_offset + sub_hdr::CENTROIDS_OFF_OFF
                ..subsection_offset + sub_hdr::CENTROIDS_OFF_OFF + U64_BYTES],
        ) as usize;
        let cluster_index_offset = read_u64_le(
            &bytes[subsection_offset + sub_hdr::CLUSTER_IDX_OFF_OFF
                ..subsection_offset + sub_hdr::CLUSTER_IDX_OFF_OFF + U64_BYTES],
        ) as usize;
        let dim = 16usize;
        let n_cent = (cluster_index_offset - centroids_offset) / (dim * 4);
        let codec_meta_offset =
            subsection_offset + cluster_index_offset + n_cent * CLUSTER_IDX_ENTRY_BYTES;
        bytes[codec_meta_offset..codec_meta_offset + 4].copy_from_slice(&f32::NAN.to_le_bytes());

        let error =
            VectorReader::open_with(Bytes::from(bytes), &json, OpenOptions { verify_crc: false })
                .expect_err("non-finite scale must be malformed");
        assert!(
            matches!(
                &error,
                VectorError::Read(ReadError::MalformedVersion(message))
                    if message.contains("non-finite quantizer metadata")
            ),
            "unexpected error: {error}"
        );
    }

    #[test]
    fn open_with_skip_crc_accepts_corrupted_directory_bytes() {
        // The fast-open path explicitly skips CRC verification — so
        // a flipped byte in the directory area opens cleanly. The
        // caller is responsible for upstream integrity.
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let mut bytes = blob.to_vec();
        let pos = OUTER_HEADER_SIZE + 10;
        bytes[pos] ^= 0xFF;
        let r =
            VectorReader::open_with(Bytes::from(bytes), &json, OpenOptions { verify_crc: false });
        // Open succeeds; the corruption may surface later as a
        // bad-magic / bounds error or be silently absorbed depending
        // on which byte got flipped. The contract is "we don't
        // verify"; not "we'll always read sensibly."
        let _ = r;
    }

    #[test]
    fn open_with_default_options_matches_open() {
        // Sanity: default opts produce identical results to `open`.
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r1 = VectorReader::open(blob.clone(), &json).expect("open VectorReader");
        let r2 = VectorReader::open_with(blob, &json, OpenOptions::default())
            .expect("open VectorReader");
        assert_eq!(r1.n_docs(), r2.n_docs());
    }

    #[test]
    fn public_rerank_mult_honors_requested_value() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let fp32 = VectorReader::open(blob, &json).expect("open fp32 VectorReader");
        assert_eq!(fp32.public_rerank_mult("embedding", 4), 4);

        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim: 16,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        })
        .expect("register Sq8 column");
        for i in 0..32u32 {
            let v: Vec<f32> = (0..16)
                .map(|j| ((i.wrapping_mul(31) + j as u32) % 100) as f32 * 0.01)
                .collect();
            b.add(0, &v).expect("add to vector builder");
        }
        let sq8 = VectorReader::open(
            Bytes::from(b.finish().expect("finish Sq8 vector builder")),
            r#"[{"column":"embedding","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#,
        )
        .expect("open Sq8 VectorReader");
        assert_eq!(sq8.public_rerank_mult("embedding", 4), 4);
    }

    #[tokio::test]
    async fn search_self_query_returns_self_as_top1() {
        let dim = 16;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Fp32,
            provided_centroids: None,
        })
        .expect("register column");
        let mut all_vecs = Vec::new();
        for i in 0..64u32 {
            let v: Vec<f32> = (0..dim)
                .map(|j| ((i.wrapping_mul(13) + j as u32 * 5) % 100) as f32)
                .collect();
            b.add(0, &v).expect("add to vector builder");
            all_vecs.push(v);
        }
        let bytes = b.finish().expect("finish vector builder");
        let json = r#"[{"column":"embedding","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#;
        let r = VectorReader::open(Bytes::from(bytes), json).expect("open VectorReader");

        // Pick a doc, query with its own vector → top-1 is self with distance 0.
        let target = 17;
        let hits = r
            .search("embedding", &all_vecs[target], 5, 4, 5)
            .await
            .expect("FTS search");
        assert!(!hits.is_empty(), "search should return hits");
        assert_eq!(hits[0].0, target as u32, "self should be nearest");
        assert!(
            hits[0].1 < 1e-3,
            "self distance should be ~0, got {}",
            hits[0].1
        );
    }

    #[tokio::test]
    async fn search_unknown_column_errors() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open VectorReader");
        let err = r
            .search("nonexistent", &[0.0; 16], 5, 4, 5)
            .await
            .expect_err("expected error");
        assert!(matches!(err, VectorError::UnknownColumn(_)));
    }

    #[tokio::test]
    async fn search_dim_mismatch_errors() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open VectorReader");
        let err = r
            .search("embedding", &[0.0; 8], 5, 4, 5)
            .await
            .expect_err("expected error");
        assert!(matches!(err, VectorError::DimensionMismatch { .. }));
    }

    #[tokio::test]
    async fn search_zero_k_returns_empty() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open VectorReader");
        let hits = r
            .search("embedding", &[0.0; 16], 0, 4, 5)
            .await
            .expect("FTS search");
        assert!(hits.is_empty());
    }

    #[tokio::test]
    async fn search_results_sorted_ascending_by_distance() {
        let (blob, json) = build_blob(64, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open VectorReader");
        let q = vec![0.5; 16];
        let hits = r
            .search("embedding", &q, 10, 4, 5)
            .await
            .expect("FTS search");
        for w in hits.windows(2) {
            assert!(w[0].1 <= w[1].1, "distances should be ascending");
        }
    }

    #[test]
    fn summary_returns_dim_centroid() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open VectorReader");
        let centroid = r.summary("embedding").expect("vector summary");
        assert_eq!(centroid.len(), 16);
        assert!(r.summary("nonexistent").is_none());
    }

    #[tokio::test]
    async fn search_clusters_async_probing_all_matches_full_nprobe() {
        // The externally-selected path probing *every* cluster must
        // recover the same top-k set as a full-nprobe `search_async` —
        // same shortlist, same rerank. (Compared as a set: equal
        // distances could tie-break differently across cluster-visit
        // orders.)
        use std::collections::HashSet;
        let (blob, json, all) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let q = &all[0];
        let (k, rerank, n_cent) = (5usize, 5usize, 64u32);

        let (full, _) = r
            .search_async("v", q, k, n_cent as usize, rerank, None, None, None, None)
            .await
            .expect("search_async");
        let (probed, _) = r
            .search_clusters_async(
                "v",
                q,
                k,
                &(0..n_cent).collect::<Vec<_>>(),
                rerank,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("search_clusters_async");

        assert!(!full.is_empty(), "self-query returns hits");
        assert_eq!(full.len(), probed.len(), "same number of hits");
        let full_ids: HashSet<u32> = full.iter().map(|(id, _)| *id).collect();
        let probed_ids: HashSet<u32> = probed.iter().map(|(id, _)| *id).collect();
        assert_eq!(
            full_ids, probed_ids,
            "probing all clusters must match a full-nprobe search"
        );

        // Probing no clusters returns nothing.
        let (none, _) = r
            .search_clusters_async("v", q, k, &[], rerank, None, None, None, None)
            .await
            .expect("search_clusters_async empty");
        assert!(none.is_empty(), "probing no clusters returns no hits");
    }

    // -----------------------------------------------------------------
    // Source enum sanity tests
    // -----------------------------------------------------------------
    //
    // The `Source` enum reroutes runtime byte access through
    // it; the eager open path takes a `Bytes`, the lazy path adds
    // `open_lazy`. These tests directly exercise the `Source`
    // contract so any future refactor that breaks the InMemory
    // zero-copy invariant or mis-implements the Lazy wrapper fails
    // here rather than at the wider recall oracle gate.

    #[test]
    fn source_in_memory_try_get_range_sync_zero_copy() {
        let payload = Bytes::from(vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
        let src = Source::InMemory(payload.clone());
        let slice = src
            .try_get_range_sync(3..7)
            .expect("in-bounds InMemory sync must succeed");
        assert_eq!(slice.as_ref(), &payload[3..7]);
        // Zero-copy invariant: returned Bytes points into the
        // same allocation as the source.
        let expected_ptr = unsafe { payload.as_ptr().add(3) };
        assert_eq!(slice.as_ptr(), expected_ptr);
    }

    #[test]
    fn source_in_memory_try_get_range_sync_out_of_bounds_returns_none() {
        let src = Source::InMemory(Bytes::from(vec![0u8; 4]));
        assert!(src.try_get_range_sync(0..100).is_none());
        assert!(src.try_get_range_sync(8..10).is_none());
    }

    #[test]
    fn source_in_memory_get_range_returns_zero_copy_slice() {
        let payload = Bytes::from(vec![100u8, 101, 102, 103, 104, 105]);
        let src = Source::InMemory(payload.clone());
        let got = src
            .get_range(1..5)
            .expect("InMemory sync get_range always succeeds for in-bounds ranges");
        assert_eq!(got.as_ref(), &payload[1..5]);
    }

    #[test]
    fn source_lazy_try_get_range_sync_falls_through_to_trait_default_or_impl() {
        // Wrap an in-memory blob in the trait-shaped
        // `BytesLazyByteSource`, then in `Source::Lazy`. The lazy
        // adapter's `try_get_range_sync` impl returns `Some` for
        // in-bounds ranges; we exercise the full enum dispatch
        // path here so the Lazy arm of `Source::try_get_range_sync`
        // doesn't drift apart from the InMemory arm.
        use crate::superfile::lazy_source::BytesLazyByteSource;
        let payload = Bytes::from(vec![7u8, 8, 9, 10, 11, 12, 13, 14]);
        let lazy: Arc<dyn LazyByteSource> = Arc::new(BytesLazyByteSource::new(payload.clone()));
        let src = Source::Lazy(lazy);
        let slice = src
            .try_get_range_sync(2..6)
            .expect("BytesLazyByteSource always serves sync");
        assert_eq!(slice.as_ref(), &payload[2..6]);
    }

    #[test]
    fn source_lazy_get_range_serves_warm_cache_via_try_get_range_sync() {
        use crate::superfile::lazy_source::BytesLazyByteSource;
        let payload = Bytes::from(vec![21u8, 22, 23, 24, 25, 26, 27]);
        let lazy: Arc<dyn LazyByteSource> = Arc::new(BytesLazyByteSource::new(payload.clone()));
        let src = Source::Lazy(lazy);
        // BytesLazyByteSource overrides try_get_range_sync to
        // return Some for every in-bounds range, so get_range
        // takes the sync fast path — no block_on bridge fires.
        let got = src.get_range(1..5).expect("warm cache sync hit");
        assert_eq!(got.as_ref(), &payload[1..5]);
    }

    /// `Source::Clone` lets readers share the underlying
    /// state cheaply (refcount bump). Clones must observe
    /// identical bytes — no fork between paths.
    #[test]
    fn source_clone_observes_identical_bytes() {
        let payload = Bytes::from(vec![0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
        let a = Source::InMemory(payload.clone());
        let b = a.clone();
        let sa = a.try_get_range_sync(2..6).expect("sa");
        let sb = b.try_get_range_sync(2..6).expect("sb");
        assert_eq!(sa.as_ref(), sb.as_ref());
        assert_eq!(sa.as_ptr(), sb.as_ptr());
    }

    #[test]
    fn open_rejects_columns_json_mismatch() {
        let (blob, _) = build_blob(32, 16, 4, Metric::L2Sq);
        // header says 1 column; pass 2-column JSON.
        let bad_json = r#"[{"column":"a","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"},{"column":"b","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#;
        let err = VectorReader::open(blob, bad_json).expect_err("expected error");
        assert!(matches!(
            err,
            VectorError::Read(ReadError::MalformedVersion(_))
        ));
    }

    // -----------------------------------------------------------------
    // rerank-codec discriminator round-trip
    // -----------------------------------------------------------------
    //
    // The codec discriminator rides as byte 52 of the per-column
    // directory entry; the codec_meta region offset rides as bytes
    // 12..16 of the sub-header. Both are zero on older fp32
    // superfiles. `Fp32` / `Sq8` / `RabitqOnly` are wired end-to-end;
    // must still round-trip as a typed `MalformedVersion` at open
    // time so a future superfile built by a newer binary fails loud
    // against an older binary rather than mis-decoding.

    use crate::superfile::format::checksum::crc32c;

    /// a fresh `Fp32` build round-trips through the
    /// reader with `ColumnReader.rerank_codec == Fp32` — the
    /// directory-entry codec byte makes it back out of the on-disk
    /// representation unchanged. The structural assertion pins the
    /// on-disk discriminator contract.
    #[test]
    fn open_round_trips_fp32_codec_discriminator() {
        let (blob, json) = build_blob(64, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        assert_eq!(r.columns.len(), 1);
        assert_eq!(
            r.columns[0].rerank_codec,
            RerankCodec::Fp32,
            "Fp32 build must surface as RerankCodec::Fp32 on the reader"
        );
        assert_eq!(
            r.columns[0].codec_meta_off, 0,
            "Fp32 superfiles must write codec_meta_off = 0 (zero-size region)"
        );
    }

    /// every codec the enum exposes is now wired end-
    /// to-end (`Fp32`, `Sq8`, `RabitqOnly`), so
    /// `register_column` must accept all of them. The check exists
    /// so adding a *new* unimplemented variant in the future
    /// surfaces here loud and fast.
    #[test]
    fn register_column_accepts_every_codec() {
        for codec in [
            RerankCodec::Fp32,
            RerankCodec::Sq8Residual,
            RerankCodec::Sq8FixedResidual,
            RerankCodec::RabitqOnly,
        ] {
            let mut b = VectorBuilder::new();
            b.register_column(VectorConfig {
                column: "v".into(),
                dim: 16,
                rot_seed: 7,
                metric: if codec == RerankCodec::Sq8FixedResidual {
                    Metric::Cosine
                } else {
                    Metric::L2Sq
                },
                rerank_codec: codec,
                provided_centroids: None,
            })
            .unwrap_or_else(|e| panic!("codec {codec:?} must register, got {e:?}"));
        }
    }

    /// building a column with `RerankCodec::Sq8Residual`
    /// round-trips through the reader. The codec discriminator
    /// surfaces on `ColumnReader.rerank_codec`; the codec_meta
    /// region carries `scale[dim] + offset[dim]` (always) plus
    /// per-doc norms (L2Sq only). The on-disk `full[]` region is
    /// `n_docs × 2·dim` bytes for `Sq8Residual`: one u8 code plus
    /// one i8 residual per dimension.
    #[test]
    fn open_round_trips_sq8_codec_discriminator_l2sq() {
        let dim = 32usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        })
        .expect("register column");
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim).map(|j| (i + j as u32) as f32 * 0.1).collect();
            b.add(0, &v).expect("add");
        }
        let blob = b.finish().expect("finish");

        let json =
            r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        assert_eq!(r.columns.len(), 1);
        let col = &r.columns[0];
        assert_eq!(col.rerank_codec, RerankCodec::Sq8Residual);

        // codec_meta_off must be non-zero for Sq8 — codec_meta
        // sits inside the open-time region between cluster_idx
        // and the per-cluster blocks.
        assert_ne!(col.codec_meta_off, 0, "Sq8 must declare codec_meta_off > 0");
        // full[] is n_docs × 2·dim (code + residual sidecar),
        // interleaved into the per-cluster blocks region. The
        // full portion is `region_size - n_docs × (code_bytes + 4)`.
        let cb = col.quant.code_bytes();
        let region_size = (col.subsection_range.len() - 4) - col.per_cluster_blocks_off;
        let actual_full_size = region_size - (col.n_docs as usize) * (cb + 4);
        assert_eq!(actual_full_size, (col.n_docs as usize) * dim * 2);

        // sq8_meta materialised at open: per-cluster scale +
        // offset (Sq8PerCluster layout — n_cent × dim floats
        // each), per-doc norms present for L2Sq.
        let meta = col
            .sq8_meta
            .as_ref()
            .expect("Sq8 column must materialise sq8_meta at open");
        let Sq8ColumnMeta::Eager {
            scale,
            offset,
            per_doc_norms,
        } = meta
        else {
            panic!("eager open must materialise Sq8 metadata");
        };
        assert_eq!(scale.len(), (col.n_cent as usize) * dim);
        assert_eq!(offset.len(), (col.n_cent as usize) * dim);
        let norms = per_doc_norms
            .as_ref()
            .expect("L2Sq Sq8 column must carry per-doc norms");
        assert_eq!(norms.len(), col.n_docs as usize);
    }

    /// `Sq8FixedResidual` (the default codec) round-trips through the
    /// reader. The on-disk `full[]` body is `n_docs × 2·dim` bytes
    /// (`[code dim u8 ‖ residual dim i8]`); codec_meta matches Sq8
    /// (per-cluster scale/offset + per-doc norms). The residual leg
    /// rides in `full[]`, not codec_meta.
    #[test]
    fn open_round_trips_sq8_fixed_residual_codec_default() {
        let dim = 32usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        // Pin the Sq8FixedResidual round-trip explicitly (the cosine default
        // is now Sq16; this case exercises the residual family specifically).
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 7,
            metric: Metric::Cosine,
            rerank_codec: RerankCodec::Sq8FixedResidual,
            provided_centroids: None,
        })
        .expect("register column");
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim).map(|j| (i + j as u32) as f32 * 0.1).collect();
            b.add(0, &v).expect("add");
        }
        let blob = b.finish().expect("finish");

        let json =
            r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":7,"metric":"cosine"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        let col = &r.columns[0];
        assert_eq!(col.rerank_codec, RerankCodec::Sq8FixedResidual);
        assert_ne!(
            col.codec_meta_off, 0,
            "Sq8FixedResidual must declare codec_meta_off > 0"
        );

        // full[] is n_docs × 2·dim (code + residual sidecar).
        let cb = col.quant.code_bytes();
        let region_size = (col.subsection_range.len() - 4) - col.per_cluster_blocks_off;
        let actual_full_size = region_size - (col.n_docs as usize) * (cb + 4);
        assert_eq!(actual_full_size, (col.n_docs as usize) * dim * 2);
        assert!(col.sq8_meta.is_some());
    }

    /// End-to-end: a `Sq8Residual` cosine self-query returns the
    /// planted doc as top-1. Exercises the residual refine pass in
    /// the eager rerank path.
    #[tokio::test]
    async fn sq8_residual_self_query_round_trips_top1_cosine() {
        let dim = 32usize;
        let n_cent = 4usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 29,
            metric: Metric::Cosine,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        })
        .expect("register column");
        let make = |i: u32| -> Vec<f32> {
            let raw: Vec<f32> = (0..dim)
                .map(|j| {
                    let h = (i.wrapping_mul(0x9E37_79B9)) ^ ((j as u32).wrapping_mul(0x85EB_CA77));
                    let h = h.wrapping_mul(0xC2B2_AE35);
                    ((h & 0xFFFF) as f32) / 65535.0
                })
                .collect();
            let norm: f32 = raw.iter().map(|x| x * x).sum::<f32>().sqrt();
            raw.into_iter().map(|x| x / norm).collect()
        };
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v = make(i);
            b.add(0, &v).expect("add");
            all.push(v);
        }
        let blob = b.finish().expect("finish");
        let json =
            r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":29,"metric":"cosine"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        let col = &r.columns[0];
        assert_eq!(col.rerank_codec, RerankCodec::Sq8Residual);
        let hits = r
            .search("v", &all[42], 5, n_cent, 20)
            .await
            .expect("search must succeed on Sq8Residual cosine column");
        assert_eq!(
            hits[0].0, 42,
            "Sq8Residual cosine self-query must recover self"
        );
    }

    #[tokio::test]
    async fn sq8_fixed_residual_self_query_round_trips_top1_cosine() {
        let dim = 32usize;
        let n_cent = 4usize;
        let n_docs = 64u32;
        let mut builder = VectorBuilder::new();
        builder
            .register_column(VectorConfig {
                column: "v".into(),
                dim,
                rot_seed: 29,
                metric: Metric::Cosine,
                rerank_codec: RerankCodec::Sq8FixedResidual,
                provided_centroids: None,
            })
            .expect("register fixed residual column");
        let make = |i: u32| -> Vec<f32> {
            let raw: Vec<f32> = (0..dim)
                .map(|j| {
                    let hash =
                        (i.wrapping_mul(0x9E37_79B9)) ^ ((j as u32).wrapping_mul(0x85EB_CA77));
                    ((hash.wrapping_mul(0xC2B2_AE35) & 0xFFFF) as f32) / 65535.0
                })
                .collect();
            let norm = raw.iter().map(|value| value * value).sum::<f32>().sqrt();
            raw.into_iter().map(|value| value / norm).collect()
        };
        let mut vectors = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let vector = make(i);
            builder.add(0, &vector).expect("add");
            vectors.push(vector);
        }
        let blob = builder.finish().expect("finish");
        let json = r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":29,"metric":"cosine"}]"#;
        let reader = VectorReader::open(Bytes::from(blob), json).expect("open");
        assert_eq!(
            reader.columns[0].rerank_codec,
            RerankCodec::Sq8FixedResidual
        );
        let hits = reader
            .search("v", &vectors[42], 5, n_cent, 20)
            .await
            .expect("fixed residual search");
        assert_eq!(hits[0].0, 42);
    }

    #[tokio::test]
    async fn sq8_fixed_residual_lazy_search_matches_eager() {
        let (blob, json, vectors) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8FixedResidual, Metric::Cosine);
        let eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let source = StdArc::new(CountingLazyByteSource::new(blob));
        let lazy = VectorReader::open_lazy(
            StdArc::clone(&source) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("lazy open");
        let (eager_hits, _) = eager
            .search_async("v", &vectors[17], 5, 4, 20, None, None, None, None)
            .await
            .expect("eager search");
        let (lazy_hits, _) = lazy
            .search_async("v", &vectors[17], 5, 4, 20, None, None, None, None)
            .await
            .expect("lazy search");
        assert_eq!(eager_hits, lazy_hits);
    }

    /// The fitted-`Sq8Residual` lazy read path fetches each candidate cluster's
    /// scale/offset (and per-doc norms) from storage on demand during rerank —
    /// the `Sq8ColumnMeta::Lazy` branch a warm/eager open never reaches. Its
    /// results must match the eager reader's exactly.
    #[tokio::test]
    async fn sq8_residual_lazy_search_matches_eager() {
        let (blob, json, vectors) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::Cosine);
        let eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let source = StdArc::new(CountingLazyByteSource::new(blob));
        let lazy = VectorReader::open_lazy(
            StdArc::clone(&source) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("lazy open");
        let (eager_hits, _) = eager
            .search_async("v", &vectors[17], 5, 4, 20, None, None, None, None)
            .await
            .expect("eager search");
        let (lazy_hits, _) = lazy
            .search_async("v", &vectors[17], 5, 4, 20, None, None, None, None)
            .await
            .expect("lazy search");
        assert_eq!(
            eager_hits, lazy_hits,
            "lazy fitted-residual rerank must match eager results"
        );
    }

    /// Lazily opening a packed multi-cell (v2) blob routes through
    /// `open_lazy_multi_cell` and fetches per-cell metadata on demand during a
    /// multi-cluster search. Results must match the eager multi-cell reader.
    #[tokio::test]
    async fn multi_cell_lazy_search_matches_eager() {
        use crate::superfile::vector::{
            builder::{build_merged_subsection_from_materialized, finish_multi_cell_blob},
            cell_posting::EncodedCellRow,
        };

        let dim = 16usize;
        let make_rows = |cell: u32, n: usize| -> Vec<MaterializedIvfRow> {
            let scale: StdArc<[f32]> = StdArc::from(vec![1.0f32; dim]);
            let offset: StdArc<[f32]> = StdArc::from(vec![0.0f32; dim]);
            (0..n)
                .map(|i| {
                    let local = i as u32;
                    let stable_id = (cell as i128) * 1_000 + local as i128;
                    let mut codes = vec![0u8; dim];
                    codes[0] = (cell as u8).wrapping_add(i as u8);
                    MaterializedIvfRow {
                        local_doc_id: local,
                        stable_id,
                        cluster: 0,
                        rabitq_code: vec![0u8; dim.div_ceil(8)],
                        encoded: EncodedCellRow {
                            stable_id,
                            rerank_codec: RerankCodec::Sq8Residual,
                            scale: StdArc::clone(&scale),
                            offset: StdArc::clone(&offset),
                            codes,
                            residuals: vec![0u8; dim],
                            norm_sq: Some(1.0),
                        },
                    }
                })
                .collect()
        };
        let cfg = || VectorConfig {
            column: "emb".into(),
            dim,
            rot_seed: 1,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        };
        let sub0 =
            build_merged_subsection_from_materialized(cfg(), 2, make_rows(0, 4)).expect("cell 0");
        let sub1 =
            build_merged_subsection_from_materialized(cfg(), 2, make_rows(1, 3)).expect("cell 1");
        let blob = Bytes::from(finish_multi_cell_blob(&[(0, sub0), (1, sub1)]).expect("pack"));
        let json =
            format!(r#"[{{"column":"emb","dim":{dim},"n_cent":2,"rot_seed":1,"metric":"l2sq"}}]"#);

        let eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let source = StdArc::new(CountingLazyByteSource::new(blob));
        let lazy = VectorReader::open_lazy(
            StdArc::clone(&source) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("lazy multi-cell open");

        let q = vec![0.0f32; dim];
        let (eager_hits, _) = eager
            .search_clusters_async("emb", &q, 3, &[0, 1, 2, 3], 8, None, None, None, None)
            .await
            .expect("eager multi-cell search");
        let (lazy_hits, _) = lazy
            .search_clusters_async("emb", &q, 3, &[0, 1, 2, 3], 8, None, None, None, None)
            .await
            .expect("lazy multi-cell search");
        assert_eq!(
            eager_hits, lazy_hits,
            "lazy multi-cell search must match the eager reader"
        );
    }

    /// `packed_cell_stable_ids_async` reads each packed cell's inline
    /// stable-`_id` region and returns the ids per cell, in cell order.
    #[tokio::test]
    async fn packed_cell_stable_ids_async_returns_per_cell_ids() {
        use std::collections::HashMap;

        use crate::superfile::vector::{
            builder::{build_merged_subsection_from_materialized, finish_multi_cell_blob},
            cell_posting::EncodedCellRow,
        };

        let dim = 16usize;
        let make_rows = |cell: u32, n: usize| -> Vec<MaterializedIvfRow> {
            let scale: StdArc<[f32]> = StdArc::from(vec![1.0f32; dim]);
            let offset: StdArc<[f32]> = StdArc::from(vec![0.0f32; dim]);
            (0..n)
                .map(|i| {
                    let local = i as u32;
                    let stable_id = (cell as i128) * 1_000 + local as i128;
                    let mut codes = vec![0u8; dim];
                    codes[0] = (cell as u8).wrapping_add(i as u8);
                    MaterializedIvfRow {
                        local_doc_id: local,
                        stable_id,
                        cluster: 0,
                        rabitq_code: vec![0u8; dim.div_ceil(8)],
                        encoded: EncodedCellRow {
                            stable_id,
                            rerank_codec: RerankCodec::Sq8Residual,
                            scale: StdArc::clone(&scale),
                            offset: StdArc::clone(&offset),
                            codes,
                            residuals: vec![0u8; dim],
                            norm_sq: Some(1.0),
                        },
                    }
                })
                .collect()
        };
        let cfg = || VectorConfig {
            column: "emb".into(),
            dim,
            rot_seed: 1,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        };
        let sub0 =
            build_merged_subsection_from_materialized(cfg(), 2, make_rows(0, 4)).expect("cell 0");
        let sub1 =
            build_merged_subsection_from_materialized(cfg(), 2, make_rows(1, 3)).expect("cell 1");
        let blob = Bytes::from(finish_multi_cell_blob(&[(0, sub0), (1, sub1)]).expect("pack"));
        let json =
            format!(r#"[{{"column":"emb","dim":{dim},"n_cent":2,"rot_seed":1,"metric":"l2sq"}}]"#);
        let reader = VectorReader::open(blob, &json).expect("open");

        let per_cell = reader
            .packed_cell_stable_ids_async()
            .await
            .expect("stable-id read")
            .expect("multi-cell blob carries an inline stable-id region");
        let map: HashMap<u32, Vec<i128>> = per_cell.into_iter().collect();
        assert_eq!(
            map.get(&0),
            Some(&vec![0, 1, 2, 3]),
            "cell 0 ids round-trip from the inline region"
        );
        assert_eq!(
            map.get(&1),
            Some(&vec![1_000, 1_001, 1_002]),
            "cell 1 ids round-trip from the inline region"
        );
    }

    /// `inline_stable_ids_for_locals_async` resolves file-local doc ids (which
    /// span cells) to their inline stable ids on a packed multi-cell reader.
    #[tokio::test]
    async fn inline_stable_ids_for_locals_async_resolves_across_cells() {
        use crate::superfile::vector::{
            builder::{build_merged_subsection_from_materialized, finish_multi_cell_blob},
            cell_posting::EncodedCellRow,
        };

        let dim = 16usize;
        let make_rows = |cell: u32, n: usize| -> Vec<MaterializedIvfRow> {
            let scale: StdArc<[f32]> = StdArc::from(vec![1.0f32; dim]);
            let offset: StdArc<[f32]> = StdArc::from(vec![0.0f32; dim]);
            (0..n)
                .map(|i| {
                    let local = i as u32;
                    let stable_id = (cell as i128) * 1_000 + local as i128;
                    let mut codes = vec![0u8; dim];
                    codes[0] = (cell as u8).wrapping_add(i as u8);
                    MaterializedIvfRow {
                        local_doc_id: local,
                        stable_id,
                        cluster: 0,
                        rabitq_code: vec![0u8; dim.div_ceil(8)],
                        encoded: EncodedCellRow {
                            stable_id,
                            rerank_codec: RerankCodec::Sq8Residual,
                            scale: StdArc::clone(&scale),
                            offset: StdArc::clone(&offset),
                            codes,
                            residuals: vec![0u8; dim],
                            norm_sq: Some(1.0),
                        },
                    }
                })
                .collect()
        };
        let cfg = || VectorConfig {
            column: "emb".into(),
            dim,
            rot_seed: 1,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        };
        let sub0 =
            build_merged_subsection_from_materialized(cfg(), 2, make_rows(0, 4)).expect("cell 0");
        let sub1 =
            build_merged_subsection_from_materialized(cfg(), 2, make_rows(1, 3)).expect("cell 1");
        let blob = Bytes::from(finish_multi_cell_blob(&[(0, sub0), (1, sub1)]).expect("pack"));
        let json =
            format!(r#"[{{"column":"emb","dim":{dim},"n_cent":2,"rot_seed":1,"metric":"l2sq"}}]"#);
        let reader = VectorReader::open(blob, &json).expect("open");

        // File-local 0 → cell 0 local 0 (id 0); file-local 4 → cell 1 local 0
        // (id 1000).
        let ids = reader
            .inline_stable_ids_for_locals_async(&[0, 4])
            .await
            .expect("stable-id resolve")
            .expect("multi-cell blob has an inline region");
        assert_eq!(ids, vec![0, 1_000], "file-locals resolve to per-cell ids");
    }

    #[test]
    fn sq8_fixed_residual_rejects_non_fixed_metadata() {
        let (blob, json, _) =
            build_small_superfile(16, 2, 16, RerankCodec::Sq8FixedResidual, Metric::Cosine);
        let opened = VectorReader::open(blob.clone(), &json).expect("open valid fixed blob");
        let column = &opened.columns[0];
        let scale_byte = column.subsection_range.start + column.codec_meta_off;
        let mut corrupted = blob.to_vec();
        corrupted[scale_byte] ^= 1;
        let error = VectorReader::open_with(
            Bytes::from(corrupted),
            &json,
            OpenOptions { verify_crc: false },
        )
        .expect_err("fixed codec must reject local quantizer metadata");
        assert!(matches!(
            error,
            VectorError::Read(ReadError::MalformedVersion(_))
        ));
    }

    #[test]
    fn sq8_fixed_residual_rejects_non_cosine_metric() {
        let mut builder = VectorBuilder::new();
        let error = builder
            .register_column(VectorConfig {
                column: "v".into(),
                dim: 16,
                rot_seed: 7,
                metric: Metric::L2Sq,
                rerank_codec: RerankCodec::Sq8FixedResidual,
                provided_centroids: None,
            })
            .expect_err("fixed residual must reject L2Sq");
        assert!(matches!(error, BuildError::VectorSchemaMismatch(_)));
    }

    /// + Sq8PerCluster: cosine Sq8 columns carry the
    /// per-doc decoded-norm cache — the rerank kernel normalizes
    /// the decoded vector with it (`1 − dot / |x_decoded|`). Only
    /// negdot drops the norms (its `Σx²` term cancels out),
    /// shrinking codec_meta from `8·n_cent·dim + 4·n_docs` to
    /// `8·n_cent·dim`.
    #[test]
    fn open_sq8_cosine_carries_per_doc_norms() {
        let dim = 16usize;
        let n_cent = 32usize;
        let n_docs = 32u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 11,
            metric: Metric::Cosine,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        })
        .expect("register column");
        for i in 0..n_docs {
            // Pre-normalised vectors so cosine has a meaningful
            // reference; the test only checks the codec_meta shape,
            // not the recall.
            let mut v: Vec<f32> = (0..dim)
                .map(|j| (i + j as u32) as f32 * 0.1 + 0.5)
                .collect();
            let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            for x in &mut v {
                *x /= norm;
            }
            b.add(0, &v).expect("add");
        }
        let blob = b.finish().expect("finish");
        let json =
            r#"[{"column":"v","dim":16,"n_cent":4,"rot_seed":11,"metric":"cosine"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        let col = &r.columns[0];
        let meta = col.sq8_meta.as_ref().expect("Sq8 must carry sq8_meta");
        let Sq8ColumnMeta::Eager {
            scale,
            offset,
            per_doc_norms,
        } = meta
        else {
            panic!("eager open must materialise Sq8 metadata");
        };
        let norms = per_doc_norms.as_ref().expect(
            "Cosine Sq8 must carry per-doc norms to normalize the decoded vector at rerank",
        );
        assert_eq!(norms.len(), n_docs as usize);
        assert_eq!(scale.len(), n_cent * dim);
        assert_eq!(offset.len(), n_cent * dim);
    }

    /// pins the per-doc-norms indexing contract —
    /// the on-disk norms array is indexed by **position in
    /// `full[]`** (matching the rerank shortlist's `pos`),
    /// not by `doc_id`. The two diverge whenever the writer
    /// pool's cluster-contiguous order differs from insertion
    /// order, which it does in practice (rows get scattered
    /// across clusters by the k-means assignment, so pos ≠ id
    /// for almost every doc).
    ///
    /// Pin: insert N docs whose decoded norms strictly increase
    /// with insertion order, build, open, and assert that the
    /// open-time norms array — read in pos order — does **not**
    /// equal the insertion-order norms. If it does, we're
    /// silently indexing the wrong way; an L2Sq distance lookup
    /// would then return some other doc's norm and corrupt the
    /// rerank ordering.
    ///
    /// We also recompute each `norms[pos]` from the planted
    /// vectors via the per-pos `doc_id` and confirm it matches
    /// — proving the pos-indexed lookup actually resolves to
    /// "this doc's decoded L2 norm".
    #[tokio::test]
    async fn sq8_per_doc_norms_indexed_by_pos_not_doc_id() {
        let dim = 16usize;
        let n_cent = 4usize;
        let n_docs = 32u32;
        // Vectors whose L2 norm grows monotonically with doc_id,
        // while their direction cycles by doc_id. That decouples
        // insertion order from cluster order: k-means groups mostly
        // by direction, not by the monotonic norm ramp, so pos order
        // is observably different from doc_id order.
        let make = |i: u32| -> Vec<f32> {
            let s = 1.0 + (i as f32) * 0.5;
            let phase = (i % n_cent as u32) as f32;
            (0..dim)
                .map(|j| {
                    let sign = if (j + phase as usize) % n_cent < n_cent / 2 {
                        1.0
                    } else {
                        -1.0
                    };
                    sign * (s + (j as f32) * 0.1)
                })
                .collect()
        };
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 23,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        })
        .expect("register column");
        let mut planted = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v = make(i);
            b.add(0, &v).expect("add");
            planted.push(v);
        }
        let blob = b.finish().expect("finish");

        let json =
            r#"[{"column":"v","dim":16,"n_cent":4,"rot_seed":23,"metric":"l2sq"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        let col = &r.columns[0];
        let meta = col.sq8_meta.as_ref().expect("Sq8 meta present");
        let Sq8ColumnMeta::Eager { per_doc_norms, .. } = meta else {
            panic!("eager open must materialise Sq8 metadata");
        };
        let norms_by_pos = per_doc_norms
            .as_ref()
            .expect("L2Sq Sq8 carries per-doc norms");

        // Insertion-order norms (computed against the fp32
        // originals; these monotonically increase by design).
        let insertion_norms: Vec<f32> = planted
            .iter()
            .map(|v| v.iter().map(|x| x * x).sum::<f32>())
            .collect();

        // If norms were indexed by doc_id, `norms_by_pos[i]`
        // would equal `insertion_norms[i]` up to quantization
        // (a few percent). Cluster-scattered builds reorder
        // docs across positions, so the two sequences should
        // disagree on most slots — this asserts the reorder
        // actually happened (the pin would be vacuous if every
        // doc landed at `pos = doc_id`).
        let n_matching = insertion_norms
            .iter()
            .zip(norms_by_pos.iter())
            .filter(|(ins, pos_n)| (**ins - **pos_n).abs() < 0.5)
            .count();
        assert!(
            n_matching < n_docs as usize,
            "expected k-means + rotation to scatter docs across positions, \
             but norms_by_pos matches insertion_norms in {n_matching}/{n_docs} \
             slots — test corpus may have landed all docs in pos == doc_id order, \
             defeating the indexing pin"
        );

        // For every pos, confirm `norms_by_pos[pos]` equals the
        // decoded L2 norm of the doc at that pos. We don't know
        // the pos↔doc_id mapping from outside the reader, but a
        // self-query against `planted[i]` should return doc_id=i
        // at top-1; the returned distance should be ~0 (matches
        // the quantized doc to itself). That same distance,
        // recomputed via the kernel using doc_i's planted
        // values, requires `norms_by_pos[pos_of(i)]` to be doc_i's
        // decoded norm — exactly the contract we're pinning.
        // A mis-index would surface as a non-zero self-distance
        // larger than the quantization error tolerance.
        for i in [0u32, 7, 15, 23, 31] {
            // rerank_mult=64 makes the RaBitQ shortlist cover all 32 docs,
            // removing shortlist recall as a confounding variable: any miss is a real
            // norms-indexing bug, not a Hamming-recall artifact.
            let hits = r
                .search("v", &planted[i as usize], 1, 4, 64)
                .await
                .expect("self-query");
            assert_eq!(hits[0].0, i, "self-query top-1 doc_id for doc {i}");
            // Quantization noise bound: per-dim error ≤ scale/2
            // ≈ span/510. For our corpus, dim spans are ~ 16, so
            // |q-x|² ≤ dim · (span/510)² ≈ 16 · 0.001 ≈ 0.016.
            // A norms-table mis-index would push this to the
            // order of the other docs' norms (≥ 1 unit).
            assert!(
                hits[0].1 <= 0.5,
                "doc {i}: self-query distance {} too large — likely norms \
                 mis-indexed (pos vs doc_id swap)",
                hits[0].1
            );
        }
    }

    /// an Sq8 build + open + self-query recovers the
    /// planted self-vector at top-1. End-to-end through the
    /// codec-aware rerank dispatch + Sq8ResidualKernel — any layout drift
    /// (codec_meta order, code stride, per-doc-norm indexing)
    /// would surface as wrong-doc or out-of-bounds.
    #[tokio::test]
    async fn sq8_self_query_round_trips_top1_l2sq() {
        let dim = 32usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 13,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        })
        .expect("register column");
        let make = |i: u32| -> Vec<f32> {
            (0..dim)
                .map(|j| ((i.wrapping_mul(17) + j as u32 * 3) % 64) as f32 * 0.5)
                .collect()
        };
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v = make(i);
            b.add(0, &v).expect("add");
            all.push(v);
        }
        let blob = b.finish().expect("finish");

        let json =
            r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":13,"metric":"l2sq"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        // rerank_mult=20 makes the RaBitQ shortlist exhaustive at this scale,
        // so the test pins codec correctness independently of shortlist recall.
        let hits = r
            .search("v", &all[17], 5, 4, 20)
            .await
            .expect("search must succeed on Sq8 column");
        assert_eq!(hits[0].0, 17, "Sq8 self-query must recover self at top-1");
        // Sq8 round-trip error: per-dim quantization step is
        // `scale ≈ (max-min)/255`. For this corpus, dim values
        // sit in [0, 31.5] so per-dim error ≲ 0.06, |q-x|² over
        // 32 dims ≲ 32 × 0.06² ≈ 0.12. Pinning a generous bound
        // to keep the test robust to RNG quirks.
        assert!(
            hits[0].1 <= 1.0,
            "Sq8 self-query distance {} should be small (≤ 1.0)",
            hits[0].1
        );
    }

    /// `Sq16Adaptive` build + open + self-query recovers the planted
    /// self-vector at top-1 (L2Sq — the new default codec for non-cosine
    /// metrics). Exercises the whole single-`u16`-plane + per-cluster-ruler
    /// path end-to-end: the pass-3 adaptive encode, the codec_meta scale/offset
    /// + norm layout, the open-side `Sq8ColumnMeta` load, and the per-cluster
    /// `Sq16Kernel::new_adaptive` rerank dispatch. Any layout drift surfaces as
    /// a wrong-doc or out-of-bounds. The 16-bit grid is strictly finer than
    /// Sq8, so the round-trip error is smaller.
    #[tokio::test]
    async fn sq16_adaptive_self_query_round_trips_top1_l2sq() {
        let dim = 32usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 13,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Sq16Adaptive,
            provided_centroids: None,
        })
        .expect("register column");
        let make = |i: u32| -> Vec<f32> {
            (0..dim)
                .map(|j| ((i.wrapping_mul(17) + j as u32 * 3) % 64) as f32 * 0.5)
                .collect()
        };
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v = make(i);
            b.add(0, &v).expect("add");
            all.push(v);
        }
        let blob = b.finish().expect("finish");

        let json =
            r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":13,"metric":"l2sq"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        let hits = r
            .search("v", &all[17], 5, 4, 20)
            .await
            .expect("search must succeed on Sq16Adaptive column");
        assert_eq!(
            hits[0].0, 17,
            "Sq16Adaptive self-query must recover self at top-1"
        );
        // 16-bit per-dim step is ~256x finer than Sq8, so the self-distance is
        // near zero; a generous bound keeps the test RNG-robust.
        assert!(
            hits[0].1 <= 1.0,
            "Sq16Adaptive self-query distance {} should be small (≤ 1.0)",
            hits[0].1
        );
    }

    /// Sq8 self-query top-1 round-trips under Cosine
    /// too. Exercises the Cosine branch of
    /// `Sq8ResidualKernel::distance_with_norm` with the cached decoded norm.
    ///
    /// Corpus design (matters!): unit-norm vectors drawn from
    /// hashed-uniform values per (doc, dim) so neighbor pairs land
    /// at `dot ≈ 1/√dim ≈ 0.18` — gap to self of ~0.82, well above
    /// the Sq8 quantization noise floor (~0.005 for this corpus).
    /// An earlier draft used `((i·23 + j·5) % 50 + 1)` which made
    /// adjacent docs near-parallel (dot ≈ 0.99) and triggered a
    /// quantization-driven swap of doc 5 ↔ doc 42 on self-query —
    /// real Sq8+Cosine behaviour on pathological inputs, not a
    /// kernel bug, but not a useful pin for codec correctness.
    /// Real cosine workloads (semantic embeddings) look like the
    /// current corpus, not the pathological one.
    #[tokio::test]
    async fn sq8_self_query_round_trips_top1_cosine() {
        let dim = 32usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 19,
            metric: Metric::Cosine,
            rerank_codec: RerankCodec::Sq8Residual,
            provided_centroids: None,
        })
        .expect("register column");
        let make = |i: u32| -> Vec<f32> {
            let raw: Vec<f32> = (0..dim)
                .map(|j| {
                    // Per-(doc, dim) hash → uniform u16 → fp32 in
                    // [0, 1). Two docs from this generator have
                    // expected dot product ≈ 1/√dim ≈ 0.18 after
                    // L2-normalization.
                    let h = (i.wrapping_mul(0x9E37_79B9)) ^ ((j as u32).wrapping_mul(0x85EB_CA77));
                    let h = h.wrapping_mul(0xC2B2_AE35);
                    ((h & 0xFFFF) as f32) / 65535.0
                })
                .collect();
            let norm: f32 = raw.iter().map(|x| x * x).sum::<f32>().sqrt();
            raw.into_iter().map(|x| x / norm).collect()
        };
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v = make(i);
            b.add(0, &v).expect("add");
            all.push(v);
        }
        let blob = b.finish().expect("finish");

        let json =
            r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":19,"metric":"cosine"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        // rerank_mult=20 makes the RaBitQ shortlist exhaustive at this scale,
        // so any failure here pins codec correctness rather than shortlist recall.
        let hits = r
            .search("v", &all[42], 5, 4, 20)
            .await
            .expect("search must succeed on Sq8 cosine column");
        assert_eq!(hits[0].0, 42, "Sq8 cosine self-query must recover self");
    }

    // -----------------------------------------------------------------
    // `None` codec (no rerank column)
    // -----------------------------------------------------------------
    //
    // The `None` codec drops the `full[]` region entirely. The
    // 1-bit shortlist *is* the final ranking; the on-disk
    // superfile shrinks by ~30× at 1M × 384. Distance values
    // returned from `search()` are `-estimate` (1-bit dot
    // estimate, sign-flipped so smaller = closer holds) — not a
    // true metric distance.

    /// building with `RerankCodec::RabitqOnly` succeeds
    /// and the on-disk superfile carries a zero-length `full[]`
    /// region. Also pins the directory-entry discriminator
    /// (`codec_id = 3`) and the zero-byte codec_meta invariant
    /// (`codec_meta_off = 0`).
    #[test]
    fn open_round_trips_none_codec_discriminator() {
        let dim = 16usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::RabitqOnly,
            provided_centroids: None,
        })
        .expect("register None column");
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim).map(|j| (i + j as u32) as f32 * 0.1).collect();
            b.add(0, &v).expect("add");
        }
        let blob = b.finish().expect("finish");

        let json =
            r#"[{"column":"v","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");
        assert_eq!(r.columns.len(), 1);
        let col = &r.columns[0];
        assert_eq!(
            col.rerank_codec,
            RerankCodec::RabitqOnly,
            "None build must surface as RerankCodec::RabitqOnly on the reader"
        );
        assert_eq!(
            col.codec_meta_off, 0,
            "None superfiles must write codec_meta_off = 0 (zero-byte meta region)"
        );
        // `None` superfiles have zero-length full[] (per_vec_bytes
        // = 0), so each per-cluster block is just
        // `[codes][doc_ids]` — the blocks region is exactly
        // `n_docs × (code_bytes + 4)` with no full bytes.
        let cb = col.quant.code_bytes();
        let region_size = (col.subsection_range.len() - 4) - col.per_cluster_blocks_off;
        assert_eq!(
            region_size,
            (n_docs as usize) * (cb + 4),
            "None superfiles interleave no full[] bytes — blocks region is \
             exactly n_docs × (code_bytes + 4)"
        );
        assert_eq!(col.n_docs, n_docs);
    }

    /// a `None`-codec column's self-query returns
    /// the planted vector inside the top-K of the 1-bit
    /// shortlist. At dim=128 / n_docs=64 with a well-separated
    /// corpus the 1-bit estimator's top-K reliably contains the
    /// self-vector even without rerank — exactly the contract
    /// `None` opts into. Returned distances are `-estimate`
    /// (sign-flipped so smaller = closer holds).
    #[tokio::test]
    async fn none_self_query_in_top_k_via_shortlist_only() {
        let dim = 128usize;
        let n_cent = 4usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 11,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::RabitqOnly,
            provided_centroids: None,
        })
        .expect("register None column");
        // Angularly diverse corpus — hashed-uniform vectors,
        // L2-normalized. Two docs from this generator have
        // expected dot ≈ 1/√dim ≈ 0.09, so 1-bit estimates
        // separate cleanly and the self-vector dominates the
        // shortlist for its own query.
        let make = |i: u32| -> Vec<f32> {
            let raw: Vec<f32> = (0..dim)
                .map(|j| {
                    let h = (i.wrapping_mul(0x9E37_79B9)) ^ ((j as u32).wrapping_mul(0x85EB_CA77));
                    let h = h.wrapping_mul(0xC2B2_AE35);
                    ((h & 0xFFFF) as f32) / 65535.0 - 0.5
                })
                .collect();
            let norm: f32 = raw.iter().map(|x| x * x).sum::<f32>().sqrt();
            raw.into_iter().map(|x| x / norm).collect()
        };
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v = make(i);
            b.add(0, &v).expect("add");
            all.push(v);
        }
        let blob = b.finish().expect("finish");
        let json =
            r#"[{"column":"v","dim":128,"n_cent":4,"rot_seed":11,"metric":"l2sq"}]"#.to_string();
        let r = VectorReader::open(Bytes::from(blob), &json).expect("open");

        // nprobe = n_cent so every cluster contributes to the
        // shortlist — the test asserts the 1-bit shortlist's
        // top-K contract, not the cluster-probing one. rerank_mult
        // is intentionally ignored by the None path (asserted
        // here by passing a value that would otherwise oversample).
        let hits = r
            .search("v", &all[17], 5, n_cent, 5)
            .await
            .expect("None-codec search must succeed");
        assert!(
            !hits.is_empty(),
            "None-codec search must return at least one hit"
        );
        assert!(
            hits.iter().any(|(did, _)| *did == 17),
            "self-query must surface the planted vector in top-K, got {hits:?}"
        );
        // Distances are `-estimate` — non-positive for a
        // self-query (the 1-bit dot estimate of a vector with
        // itself is strictly positive after the random rotation).
        assert!(
            hits.iter().all(|(_, d)| d.is_finite()),
            "all None-codec distances must be finite, got {hits:?}"
        );
        // Top-1's distance must be ≤ any other hit's (ascending
        // sort contract).
        for w in hits.windows(2) {
            assert!(
                w[0].1 <= w[1].1,
                "None-codec hits must be sorted ascending by distance, got {hits:?}"
            );
        }
    }

    /// a `None`-codec search over a counting
    /// lazy source must not perform any range fetch past the
    /// `doc_ids` region — proven indirectly via the total
    /// range count: 2 centroids-region + 2 cluster-idx-region
    /// + `2 × nprobe` (codes + doc_ids per probed cluster). A
    /// regression that left the fat `full[]` `get_range` in
    /// for None columns would surface as one extra range
    /// request, which this asserts away. The structural
    /// invariant (full[] is zero-length on disk) is pinned by
    /// `open_round_trips_none_codec_discriminator`; this test
    /// pins the read-path side.
    #[tokio::test]
    async fn none_search_issues_no_full_region_fetch() {
        let dim = 32usize;
        let n_cent = 4usize;
        let n_docs = 32u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 13,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::RabitqOnly,
            provided_centroids: None,
        })
        .expect("register None column");
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim).map(|j| (i + j as u32) as f32 * 0.1).collect();
            b.add(0, &v).expect("add");
        }
        let blob = Bytes::from(b.finish().expect("finish"));
        let json =
            r#"[{"column":"v","dim":32,"n_cent":4,"rot_seed":13,"metric":"l2sq"}]"#.to_string();

        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let async_calls = counting.async_counter();
        let sync_calls = counting.sync_counter();
        let r = VectorReader::open_with_source(
            Source::Lazy(StdArc::clone(&counting) as StdArc<dyn LazyByteSource>),
            &json,
            OpenOptions::default(),
        )
        .expect("open lazy");

        // Reset counters: open() touches the directory + every
        // sub-header. We only want to count search-time fetches.
        async_calls.store(0, AtomicOrdering::Relaxed);
        sync_calls.store(0, AtomicOrdering::Relaxed);
        let query: Vec<f32> = (0..dim).map(|j| j as f32 * 0.1).collect();
        let _ = r.search("v", &query, 5, n_cent, 5).await.expect("search");

        // Upper-bound sync fetches for None / nprobe = n_cent:
        //   centroids (1) + cluster_idx (1)
        // + per-cluster codes (≤ n_cent)
        // + per-cluster doc_ids (≤ n_cent)
        // = at most 2 + 2·n_cent = 10
        //
        // The Fp32/Sq8 paths would add one more fat
        // `full[]` get_range on top — that's the leak this
        // test guards against. Empty clusters reduce the
        // upper bound (per-cluster fetches skip on cnt == 0)
        // but never raise it. Async should stay at 0 —
        // warm-cache lazy never falls through to the async
        // bridge for in-memory blobs.
        let sync_count = sync_calls.load(AtomicOrdering::Relaxed) as usize;
        let async_count = async_calls.load(AtomicOrdering::Relaxed);
        assert_eq!(
            async_count, 0,
            "None-codec search on warm lazy must not bridge to async"
        );
        let max_expected = 2 + 2 * n_cent;
        assert!(
            sync_count <= max_expected,
            "None-codec search must issue at most 2 + 2·nprobe = {max_expected} \
             sync fetches (centroids + cluster_idx + per-cluster codes + \
             per-cluster doc_ids); got {sync_count} — any extra is a leaked \
             full[] fetch"
        );
        // A search that probed at least one non-empty cluster
        // must issue ≥ 2 fetches after spatial cluster ordering
        // and bounded range coalescing: centroids+idx plus at
        // least one merged cluster block.
        assert!(
            sync_count >= 2,
            "test corpus produced only empty clusters? got sync_count={sync_count}"
        );
    }

    /// a directory entry carrying an unknown codec id
    /// (anything outside `0..=3` — e.g. `255` from a corrupted /
    /// future-format superfile) errors as `MalformedVersion`. The
    /// safety net catches both forward-compat reads (future codec
    /// ids land in the gap) and on-disk corruption.
    #[test]
    fn open_rejects_superfile_with_unknown_codec_id() {
        let (blob, json) = build_blob(64, 16, 4, Metric::L2Sq);
        let mut bytes = blob.to_vec();

        const OUTER_HDR: usize = 32;
        const DIR_ENTRY: usize = 64;
        let dir_off = OUTER_HDR;
        let codec_byte_off = dir_off + 52;
        bytes[codec_byte_off] = 200u8; // unassigned

        let dir_bytes = &bytes[dir_off..dir_off + DIR_ENTRY];
        let new_crc = crc32c(dir_bytes);
        let crc_off = dir_off + DIR_ENTRY;
        bytes[crc_off..crc_off + 4].copy_from_slice(&new_crc.to_le_bytes());

        let err =
            VectorReader::open_with(Bytes::from(bytes), &json, OpenOptions { verify_crc: false })
                .expect_err("unknown codec id must error at open");
        assert!(
            matches!(err, VectorError::Read(ReadError::MalformedVersion(_))),
            "expected MalformedVersion for unknown codec id, got {err:?}"
        );
        let msg = err.to_string();
        assert!(
            msg.contains("unknown") || msg.contains("200"),
            "error must call out the unknown id, got: {msg}"
        );
    }

    // -----------------------------------------------------------------
    // lazy open + inline-`pos` search
    // -----------------------------------------------------------------
    //
    // Open touches only the structural-decode regions (directory,
    // sub-header, summary, centroids, cluster_idx). Search carries
    // `pos = off + i` inline in the shortlist tuple — there is no
    // `doc_to_pos` lookup table to populate (deleted after
    // an audit confirmed zero external readers). The structural
    // memory-ceiling tests below ride on these invariants.

    // -----------------------------------------------------------------
    // diagnostic — Sq8 vs Fp32 recall on planted-cluster
    // cosine corpus
    // -----------------------------------------------------------------
    //
    // The 1M × 384 bench measured Sq8 recall@10 = 0.860 vs Fp32 = 0.964
    // — well outside the "< 0.005 drop on normalized embeddings"
    // envelope. The hypothesis is that the **per-column** Sq8 quantizer
    // wastes most of its 256 buckets on cross-cluster spread: per-dim
    // global range across 1M docs ≈ 0.4, intra-cluster spread ≈ 0.015,
    // so within any one cluster only ~10 buckets are used. The intra-
    // cluster cosine differences between top-K candidates are smaller
    // than the per-bucket quantization noise → reranks flip.
    //
    // This `#[ignore]`-gated diagnostic reproduces the recall drop at
    // 16k × 384 (1/64 scale) and prints corpus geometry stats. Run
    // with `cargo test --lib -- sq8_recall_diagnostic --ignored
    // --nocapture` to inspect. Per-column-quantizer fix (or fallback
    // to Sq8 default) is decided based on what this prints.
    #[tokio::test]
    #[ignore = "recall diagnostic; ~10s; --ignored --nocapture"]
    async fn sq8_recall_diagnostic_planted_cluster_cosine() {
        use rand::{SeedableRng, rngs::StdRng};
        use rand_distr::{Distribution, StandardNormal};

        let n_docs = 16_000u32;
        let dim = 384usize;
        let n_cent_planted = 64usize;
        let n_cent_ivf = 256usize;
        let seed: u64 = 1;

        // 1. Build the corpus — same shape as benches/utils/corpus.rs:
        //    planted centers from 3·N(0,1) per dim, per-doc =
        //    center + 0.3·N(0,1), L2-normalized.
        let mut rng = StdRng::seed_from_u64(seed);
        let dist = StandardNormal;
        let centers: Vec<Vec<f32>> = (0..n_cent_planted)
            .map(|_| {
                (0..dim)
                    .map(|_| {
                        let s: f64 = dist.sample(&mut rng);
                        (s as f32) * 3.0
                    })
                    .collect()
            })
            .collect();
        let mut all: Vec<Vec<f32>> = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs as usize {
            let center = &centers[i % n_cent_planted];
            let mut v: Vec<f32> = center
                .iter()
                .map(|&c| {
                    let s: f64 = dist.sample(&mut rng);
                    c + (s as f32) * 0.3
                })
                .collect();
            let nrm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            for x in v.iter_mut() {
                *x /= nrm;
            }
            all.push(v);
        }

        // 2. Corpus geometry: per-dim global range vs intra-cluster spread.
        let mut g_min = vec![f32::INFINITY; dim];
        let mut g_max = vec![f32::NEG_INFINITY; dim];
        for v in &all {
            for d in 0..dim {
                if v[d] < g_min[d] {
                    g_min[d] = v[d];
                }
                if v[d] > g_max[d] {
                    g_max[d] = v[d];
                }
            }
        }
        let g_ranges: Vec<f32> = (0..dim).map(|d| g_max[d] - g_min[d]).collect();
        let mean_g_range: f32 = g_ranges.iter().sum::<f32>() / dim as f32;
        let max_g_range: f32 = g_ranges.iter().cloned().fold(0.0f32, f32::max);

        let mut c0_min = vec![f32::INFINITY; dim];
        let mut c0_max = vec![f32::NEG_INFINITY; dim];
        let mut c0_count = 0u32;
        for (i, v) in all.iter().enumerate() {
            if i % n_cent_planted == 0 {
                c0_count += 1;
                for d in 0..dim {
                    if v[d] < c0_min[d] {
                        c0_min[d] = v[d];
                    }
                    if v[d] > c0_max[d] {
                        c0_max[d] = v[d];
                    }
                }
            }
        }
        let intra_ranges: Vec<f32> = (0..dim).map(|d| c0_max[d] - c0_min[d]).collect();
        let mean_intra: f32 = intra_ranges.iter().sum::<f32>() / dim as f32;

        eprintln!("--- corpus geometry (16k × 384, 64 planted centers, cosine, L2-normalized) ---");
        eprintln!(
            "per-dim global range: mean={mean_g_range:.4}  max={max_g_range:.4}  \
             bucket_width@255={:.6}",
            mean_g_range / 255.0
        );
        eprintln!("per-dim intra-cluster-0 range ({c0_count} docs): mean={mean_intra:.4}");
        eprintln!(
            "bucket-waste factor (global / intra): {:.1}x — Sq8 uses ~{} of 256 buckets per cluster",
            mean_g_range / mean_intra.max(1e-9),
            (255.0 * mean_intra / mean_g_range).round() as i32
        );

        // 3. Build Fp32 + Sq8 superfiles from the same corpus.
        let build = |codec: RerankCodec| -> Bytes {
            let mut b = VectorBuilder::new();
            b.register_column(VectorConfig {
                column: "v".into(),
                dim,
                rot_seed: 7,
                metric: Metric::Cosine,
                rerank_codec: codec,
                provided_centroids: None,
            })
            .expect("register");
            for v in &all {
                b.add(0, v).expect("add");
            }
            Bytes::from(b.finish().expect("finish"))
        };
        let fp32_blob = build(RerankCodec::Fp32);
        let sq8_blob = build(RerankCodec::Sq8Residual);
        eprintln!(
            "--- superfile sizes ---\n\
             fp32: {:.2} MiB (1.00x)\n\
             sq8:  {:.2} MiB ({:.2}x)",
            fp32_blob.len() as f64 / 1024.0 / 1024.0,
            sq8_blob.len() as f64 / 1024.0 / 1024.0,
            sq8_blob.len() as f64 / fp32_blob.len() as f64
        );

        let json = format!(
            r#"[{{"column":"v","dim":{dim},"n_cent":{n_cent_ivf},"rot_seed":7,"metric":"cosine"}}]"#
        );
        let r_fp32 = VectorReader::open(fp32_blob, &json).expect("open fp32");
        let r_sq8 = VectorReader::open(sq8_blob, &json).expect("open sq8");

        // 4. Brute-force ground truth (cosine sim descending = neg-dot
        //    ascending — both engines return smaller-is-closer).
        let n_queries = 100usize;
        let k = 10usize;
        let nprobe = n_cent_ivf / 4;
        let rerank_mult = 50usize; // Sq8 rerank floor at dim ≤ 384
        let ground_truth: Vec<HashSet<u32>> = (0..n_queries)
            .map(|qi| {
                let q = &all[qi];
                let mut sims: Vec<(u32, f32)> = (0..all.len())
                    .map(|j| {
                        let d: f32 = (0..dim).map(|i| q[i] * all[j][i]).sum();
                        (j as u32, d)
                    })
                    .collect();
                sims.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
                sims.into_iter().take(k).map(|(id, _)| id).collect()
            })
            .collect();

        eprintln!(
            "--- recall@{k} on {n_queries} self-queries (nprobe={nprobe}, rerank_mult={rerank_mult}) ---"
        );
        let mut recalls = Vec::new();
        for (reader, label) in [(&r_fp32, "fp32"), (&r_sq8, "sq8 ")] {
            let mut total_match = 0usize;
            for qi in 0..n_queries {
                let hits = reader
                    .search("v", &all[qi], k, nprobe, rerank_mult)
                    .await
                    .expect("search");
                let hit_ids: HashSet<u32> = hits.into_iter().map(|(id, _)| id).collect();
                let gt = &ground_truth[qi];
                total_match += gt.iter().filter(|id| hit_ids.contains(id)).count();
            }
            let recall = total_match as f32 / (n_queries * k) as f32;
            eprintln!("recall@{k} ({label}): {recall:.4}");
            recalls.push(recall);
        }
        let r_fp = recalls[0];
        let r_sq = recalls[1];
        eprintln!("drop (fp32 - sq8 ): {:.4}", r_fp - r_sq);
        eprintln!(
            "(acceptance: recall drop must be \u{2264} 0.01; bench measured 0.10 drop at 1M scale)"
        );

        // -- Probe: vary rerank_mult to isolate shortlist depth vs rerank noise --
        eprintln!("\n--- rerank_mult sweep (Sq8, same corpus/queries) ---");
        for &rm in &[20usize, 50, 100, 200, 400] {
            let mut tm = 0usize;
            for qi in 0..n_queries {
                let hits = r_sq8
                    .search("v", &all[qi], k, nprobe, rm)
                    .await
                    .expect("search");
                let hit_ids: HashSet<u32> = hits.into_iter().map(|(id, _)| id).collect();
                tm += ground_truth[qi]
                    .iter()
                    .filter(|id| hit_ids.contains(id))
                    .count();
            }
            eprintln!(
                "  rerank_mult={rm:>4}: sq8 recall@{k} = {:.4}",
                tm as f32 / (n_queries * k) as f32
            );
        }

        // -- Probe: typical top-10 cosine spread (signal that
        //    Sq8 noise must beat).
        let mut spreads = Vec::with_capacity(n_queries);
        for qi in 0..n_queries.min(20) {
            let q = &all[qi];
            let mut sims: Vec<f32> = (0..all.len())
                .map(|j| (0..dim).map(|i| q[i] * all[j][i]).sum::<f32>())
                .collect();
            sims.sort_unstable_by(|a, b| b.partial_cmp(a).unwrap_or(Ordering::Equal));
            let top11: Vec<f32> = sims.iter().take(11).cloned().collect();
            // Spread between top-1 (self, sim=1) and top-10
            let span = top11[0] - top11[10];
            // Median consecutive gap among top-10
            let mut gaps: Vec<f32> = (1..11).map(|i| top11[i - 1] - top11[i]).collect();
            gaps.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));
            let med_gap = gaps[gaps.len() / 2];
            spreads.push((span, med_gap));
        }
        let mean_span: f32 = spreads.iter().map(|(s, _)| s).sum::<f32>() / spreads.len() as f32;
        let mean_gap: f32 = spreads.iter().map(|(_, g)| g).sum::<f32>() / spreads.len() as f32;
        eprintln!("\n--- top-10 cosine geometry (the signal Sq8 noise must beat) ---");
        eprintln!(
            "  mean top1-to-top10 span:      {mean_span:.4}\n  \
             mean consecutive median gap:  {mean_gap:.5}\n  \
             Sq8 noise est. (3e-5) vs gap: ratio = {:.2}%",
            3e-5_f32 / mean_gap.max(1e-9) * 100.0
        );
    }

    /// Search-shape corpus used by the inline-pos tests and the
    /// sync-search / counting-source tests. Picks a non-trivial
    /// `n_docs ≥ n_cent` so each cluster has multiple candidates.
    fn build_search_corpus() -> (Bytes, String, Vec<Vec<f32>>) {
        let dim = 16usize;
        let n_docs = 64u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Fp32,
            provided_centroids: None,
        })
        .expect("register column");
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim)
                .map(|j| ((i.wrapping_mul(13) + j as u32 * 5) % 100) as f32)
                .collect();
            b.add(0, &v).expect("add to vector builder");
            all.push(v);
        }
        let bytes = b.finish().expect("finish vector builder");
        let json = r#"[{"column":"embedding","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#
            .to_string();
        (Bytes::from(bytes), json, all)
    }

    /// Self-query smoke: lazy default open must
    /// recover the planted self-vector at top-1, confirming the
    /// inline-`pos` rerank path returns the correct results on
    /// the search-shape corpus the search tests use.
    #[tokio::test]
    async fn lazy_default_search_recovers_self_query() {
        let (blob, json, all) = build_search_corpus();
        let r = VectorReader::open(blob, &json).expect("open");
        let hits = r
            .search("embedding", &all[17], 5, 4, 5)
            .await
            .expect("search must succeed on lazy InMemory");
        assert_eq!(hits[0].0, 17, "self-query must recover self");
    }

    // -----------------------------------------------------------------
    // sync `search()` on `Source::Lazy`
    // -----------------------------------------------------------------
    //
    // These tests pin the sync-only contract: the *only* public
    // entry point is sync
    // `search()`. It works on every `Source` variant — `InMemory`
    // and warm-cache `Source::Lazy` resolve every range through
    // `try_get_range_sync` (zero-copy); cold-miss `Source::Lazy`
    // bridges to the source's async `range()` via
    // `block_in_place + Handle::block_on` / one-shot
    // `current_thread` `Runtime`, the same pattern
    // `supertable::query::superfile_reader` uses for the disk-cache
    // fetch path. No `search_async` is exposed at the public
    // surface; the cold-path async bridging is hidden inside
    // `Source::get_range`.
    //
    // A `CountingLazyByteSource` test helper wraps a `Bytes`
    // payload and counts every `range` / `try_get_range_sync`
    // call against an `AtomicU64`. The `disable_sync` switch
    // lets a test force the cold-miss path (sync access
    // disabled) — exposes any silent fallthrough that would
    // bypass the block_on bridge.

    use std::sync::{
        Arc as StdArc,
        atomic::{AtomicBool, AtomicU64, Ordering as AtomicOrdering},
    };

    use crate::superfile::lazy_source::{BytesLazyByteSource, LazyByteSource, LazyByteSourceError};

    /// Test-only [`LazyByteSource`] that wraps a `Bytes` payload and
    /// records every async / sync range request as a counter. The
    /// two `*_returns_none` switches let a test force either the
    /// "async only" path (sync access disabled) or "sync only" path
    /// (async access disabled — exposes any silent fallthrough to
    /// `range` on the supposedly-sync code path).
    #[derive(Debug)]
    struct CountingLazyByteSource {
        bytes: Bytes,
        /// Counts of every `range()` invocation.
        async_calls: StdArc<AtomicU64>,
        /// Counts of every `try_get_range_sync()` invocation.
        sync_calls: StdArc<AtomicU64>,
        /// If `true`, `try_get_range_sync` returns `None` for every
        /// in-bounds range — forces the caller to the async path.
        sync_disabled: AtomicBool,
        /// Current in-flight `range()` futures (entry-bumped,
        /// drop-decremented). pairs with
        /// `max_in_flight` to pin that
        /// [`Source::get_ranges_parallel`] dispatches its cold
        /// fetches concurrently rather than serially.
        in_flight: StdArc<AtomicU64>,
        max_in_flight: StdArc<AtomicU64>,
        /// Per-`range()` artificial latency. Defaults to zero
        /// (legacy callers); the parallel-dispatch test sets it
        /// to a small delay so concurrent futures actually
        /// overlap in wall-clock instead of completing in the
        /// trivial sync slice path inside `range`.
        async_latency_us: AtomicU64,
    }

    impl CountingLazyByteSource {
        fn new(bytes: Bytes) -> Self {
            Self {
                bytes,
                async_calls: StdArc::new(AtomicU64::new(0)),
                sync_calls: StdArc::new(AtomicU64::new(0)),
                sync_disabled: AtomicBool::new(false),
                in_flight: StdArc::new(AtomicU64::new(0)),
                max_in_flight: StdArc::new(AtomicU64::new(0)),
                async_latency_us: AtomicU64::new(0),
            }
        }

        fn async_counter(&self) -> StdArc<AtomicU64> {
            StdArc::clone(&self.async_calls)
        }

        fn sync_counter(&self) -> StdArc<AtomicU64> {
            StdArc::clone(&self.sync_calls)
        }

        fn disable_sync(&self) {
            self.sync_disabled.store(true, AtomicOrdering::Relaxed);
        }

        /// Max-concurrent observer — sampled at every `range()`
        /// entry. Concurrent fetches will produce a value `> 1`;
        /// serial fetches stay at `1`.
        fn max_in_flight_counter(&self) -> StdArc<AtomicU64> {
            StdArc::clone(&self.max_in_flight)
        }

        /// Set per-`range()` artificial latency. Used by the
        /// parallel-dispatch test to ensure concurrent futures
        /// overlap in wall-clock (without latency, the trivial
        /// `bytes.slice(...)` body of `range()` resolves
        /// instantaneously and in-flight peaks at 1 even when
        /// many futures were spawned together).
        fn set_async_latency(&self, latency: Duration) {
            self.async_latency_us
                .store(latency.as_micros() as u64, AtomicOrdering::Relaxed);
        }
    }

    /// RAII guard: bumps `in_flight` on construct, decrements
    /// on drop, and bumps `max_in_flight` if the new in-flight
    /// count exceeds the previous max. Pairs with
    /// [`CountingLazyByteSource::max_in_flight_counter`] to give
    /// the parallel-dispatch test a single observable for
    /// "fetches actually overlapped."
    struct InFlightGuard {
        in_flight: StdArc<AtomicU64>,
        max_in_flight: StdArc<AtomicU64>,
    }

    impl InFlightGuard {
        fn enter(in_flight: StdArc<AtomicU64>, max_in_flight: StdArc<AtomicU64>) -> Self {
            let now = in_flight.fetch_add(1, AtomicOrdering::AcqRel) + 1;
            // Bump max_in_flight monotonically.
            max_in_flight.fetch_max(now, AtomicOrdering::AcqRel);
            Self {
                in_flight,
                max_in_flight,
            }
        }
    }

    impl Drop for InFlightGuard {
        fn drop(&mut self) {
            self.in_flight.fetch_sub(1, AtomicOrdering::AcqRel);
            // max_in_flight is monotonic by design; nothing to
            // unwind on drop.
            let _ = &self.max_in_flight;
        }
    }

    #[async_trait::async_trait]
    impl LazyByteSource for CountingLazyByteSource {
        fn size(&self) -> u64 {
            self.bytes.len() as u64
        }

        async fn range(&self, start: u64, len: u64) -> Result<Bytes, LazyByteSourceError> {
            self.async_calls.fetch_add(1, AtomicOrdering::Relaxed);
            let _guard = InFlightGuard::enter(
                StdArc::clone(&self.in_flight),
                StdArc::clone(&self.max_in_flight),
            );
            let latency_us = self.async_latency_us.load(AtomicOrdering::Relaxed);
            if latency_us > 0 {
                sleep(Duration::from_micros(latency_us)).await;
            }
            let total = self.bytes.len() as u64;
            if start.saturating_add(len) > total {
                return Err(LazyByteSourceError::OutOfBounds {
                    start,
                    len,
                    size: total,
                });
            }
            let s = start as usize;
            Ok(self.bytes.slice(s..s + len as usize))
        }

        fn try_get_range_sync(&self, start: u64, len: u64) -> Option<Bytes> {
            self.sync_calls.fetch_add(1, AtomicOrdering::Relaxed);
            if self.sync_disabled.load(AtomicOrdering::Relaxed) {
                return None;
            }
            let total = self.bytes.len() as u64;
            if start.saturating_add(len) > total {
                return None;
            }
            let s = start as usize;
            Some(self.bytes.slice(s..s + len as usize))
        }
    }

    /// Sync `search()` on a `Source::Lazy` whose `try_get_range_sync`
    /// always succeeds (warm cache) behaves identically to the
    /// `Source::InMemory` path. This is the steady-state shape the
    /// supertable reader sees today (the reader_cache is in-process,
    /// so every range is resident).
    #[tokio::test]
    async fn search_on_lazy_source_with_warm_sync_cache_matches_in_memory() {
        let (blob, json, all) = build_search_corpus();
        let r_mem = VectorReader::open(blob.clone(), &json).expect("InMemory open");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let r_lazy =
            VectorReader::open_with_source(Source::Lazy(counting), &json, OpenOptions::default())
                .expect("lazy open with warm sync cache");

        for &q_idx in &[0usize, 17, 31, 63] {
            let hits_mem = r_mem
                .search("embedding", &all[q_idx], 5, 4, 5)
                .await
                .expect("InMemory search");
            let hits_lazy = r_lazy
                .search("embedding", &all[q_idx], 5, 4, 5)
                .await
                .expect("Lazy(warm) search");
            assert_eq!(
                hits_mem, hits_lazy,
                "lazy warm-sync results must match InMemory for query {q_idx}"
            );
        }
    }

    /// Sync `search()` on a `Source::Lazy` whose
    /// `try_get_range_sync` returns `None` for every range still
    /// succeeds — `Source::get_range` bridges to the source's
    /// async `range()` via the one-shot `current_thread`
    /// `Runtime` fallback (no ambient tokio runtime in
    /// `#[test]`). Results must equal the `Source::InMemory`
    /// baseline.
    ///
    /// This is the cold-path proof — the public sync surface
    /// works against an arbitrary async-only `LazyByteSource`
    /// impl. Production callers always have an ambient runtime
    /// (the supertable owns one), so the `block_in_place +
    /// Handle::block_on` branch is what fires there; this test
    /// exercises the no-ambient-runtime fallback branch to
    /// keep that path live.
    #[tokio::test]
    async fn search_on_lazy_source_with_no_sync_fallback_bridges_to_async() {
        let (blob, json, all) = build_search_corpus();
        let r_mem = VectorReader::open(blob.clone(), &json).expect("InMemory baseline");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let async_counter = counting.async_counter();
        let r_lazy = VectorReader::open_with_source(
            Source::Lazy(StdArc::clone(&counting) as StdArc<dyn LazyByteSource>),
            &json,
            OpenOptions::default(),
        )
        .expect("lazy open");
        counting.disable_sync();

        for &q_idx in &[0usize, 17, 31, 63] {
            let hits_mem = r_mem
                .search("embedding", &all[q_idx], 5, 4, 5)
                .await
                .expect("InMemory search");
            let hits_lazy = r_lazy
                .search("embedding", &all[q_idx], 5, 4, 5)
                .await
                .expect("sync search must succeed via block_on bridge");
            assert_eq!(
                hits_mem, hits_lazy,
                "sync search with block_on bridge must match InMemory for query {q_idx}"
            );
        }

        assert!(
            async_counter.load(AtomicOrdering::Relaxed) > 0,
            "with sync access disabled, every fetch must route through \
             the source's async range() via the block_on bridge"
        );
    }

    /// Range-counting test. Sync `search()` issues per-region /
    /// per-cluster `Source::get_range` calls:
    ///
    /// - 1 range for centroids
    /// - 1 range for cluster_idx
    /// - 1 range per probed cluster (codes + doc_ids are
    ///   interleaved in one block, so one range per cluster)
    /// - 1 fat range for the rerank batch in `full[]`
    ///
    /// At `nprobe = N` with all probed clusters non-empty that is
    /// `2 + N + 1` ranges before coalescing. The corpus here has
    /// `n_cent = 4` and the test uses `nprobe = 4`; spatial
    /// cluster ordering can merge adjacent cluster blocks into
    /// fewer physical GETs, so the observed budget is `2..=5`.
    ///
    /// Forcing `try_get_range_sync` off makes every range route
    /// through the source's async `range()` via the block_on
    /// bridge, so the `async_calls` counter is the right
    /// instrumentation for "how many distinct ranges did
    /// `search()` request".
    ///
    /// A regression that smuggles in extra range fetches — e.g.
    /// reintroducing the whole-subsection fallback, or pulling the
    /// full `doc_ids` region over the wire at open — surfaces here
    /// rather than at the production object-store harness.
    #[tokio::test]
    async fn search_cold_first_search_range_count_per_cluster() {
        let (blob, json, all) = build_search_corpus();
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let async_counter = counting.async_counter();
        let sync_counter = counting.sync_counter();
        let r = VectorReader::open_with_source(
            Source::Lazy(StdArc::clone(&counting) as StdArc<dyn LazyByteSource>),
            &json,
            OpenOptions::default(),
        )
        .expect("lazy open");

        let async_after_open = async_counter.load(AtomicOrdering::Relaxed);
        let sync_after_open = sync_counter.load(AtomicOrdering::Relaxed);
        assert_eq!(
            async_after_open, 0,
            "open path uses try_get_range_sync only — no async fetches expected"
        );
        assert!(
            sync_after_open > 0,
            "open path should have issued sync range fetches"
        );

        counting.disable_sync();
        let hits = r
            .search("embedding", &all[7], 5, 4, 5)
            .await
            .expect("sync search via block_on bridge");
        assert!(!hits.is_empty(), "search should return hits");

        let async_calls_for_first_search =
            async_counter.load(AtomicOrdering::Relaxed) - async_after_open;
        // At nprobe=4 with this corpus, all probed clusters are
        // non-empty. Spatial cluster ordering can merge the
        // cluster blocks into fewer physical GETs.
        assert!(
            (2..=5).contains(&(async_calls_for_first_search as usize)),
            "per-cluster path: cold first search expected to issue \
             2..=5 ranges (centroids+cluster_idx + coalesced/interleaved \
             cluster blocks). Got {async_calls_for_first_search}."
        );
    }

    /// `BytesLazyByteSource` (the production-ready in-memory
    /// `LazyByteSource` impl) yields the same sync `search()`
    /// results as `Source::InMemory`. Locks in the contract that
    /// the trait-based path doesn't accidentally diverge from the
    /// enum-based fast path.
    #[tokio::test]
    async fn search_matches_in_memory_through_bytes_lazy_source() {
        let (blob, json, all) = build_search_corpus();
        let r_mem = VectorReader::open(blob.clone(), &json).expect("InMemory baseline");
        let lazy_src: StdArc<dyn LazyByteSource> = StdArc::new(BytesLazyByteSource::new(blob));
        let r_lazy =
            VectorReader::open_with_source(Source::Lazy(lazy_src), &json, OpenOptions::default())
                .expect("lazy open via BytesLazyByteSource");

        for &q_idx in &[3usize, 19, 47] {
            let hits_mem = r_mem
                .search("embedding", &all[q_idx], 5, 4, 5)
                .await
                .expect("InMemory search");
            let hits_lazy = r_lazy
                .search("embedding", &all[q_idx], 5, 4, 5)
                .await
                .expect("BytesLazyByteSource sync search");
            assert_eq!(
                hits_mem, hits_lazy,
                "BytesLazyByteSource results must match InMemory for query {q_idx}"
            );
        }
    }

    // -----------------------------------------------------------------
    // § Acceptance #2 — memory-ceiling unit test
    // -----------------------------------------------------------------
    //
    // The headline guarantee is "resident set per open
    // vector superfile is bounded by O(n_cent × dim × 4 + small)",
    // independent of `n_docs`. Acceptance criterion #2 spells it
    // out: opening a `Source::Lazy` over a mmap-backed
    // `BytesLazyByteSource` at 1M × 384 with
    // `OpenOptions { verify_crc: false }` must leave the process
    // RSS delta ≤ 10 MB per opened column.
    //
    // Why mmap specifically: this is exactly how the disk cache
    // feeds bytes into `SuperfileReader` —
    // `Bytes::from_owner(Arc<Mmap>)`. The kernel never faults the
    // bulk codes/full/doc_ids pages on the default path because
    // nothing in `open_with_source` accesses them: the CRC scan
    // is gated on `verify_crc`, search uses inline `pos`
    // so no `doc_ids` walk happens, and the structural-decode
    // bytes (outer header + dir + sub_header) are a handful of
    // pages. The resident allocation is dominated by the rotation
    // matrix (≈ 590 KB at dim=384) and small column metadata —
    // well inside the 10 MB ceiling at any practical
    // `n_docs`.
    //
    // Companion smoke test below (`mem_ceiling_lazy_open_smoke`)
    // runs in default `cargo test --lib` at a smaller scale so
    // every PR gets continuous feedback on this guarantee
    // without paying for a 1M-doc build. The 1M × 384 reference-scale
    // version is `#[ignore]`'d because
    // `VectorBuilder.finish_to(...)` at that scale takes ~35 s in
    // release / several minutes in debug. Run explicitly:
    //
    // ```bash
    // cargo test --release -p infino --lib \
    //     mem_ceiling_lazy_open_under_10mib -- --ignored --nocapture
    // ```

    /// `Bytes::from_owner` adapter for `Arc<memmap2::Mmap>` —
    /// mirrors `supertable::reader_cache::disk::ArcMmapOwner`
    /// (which is private to that module). Sharing the mapping
    /// via `Arc<Mmap>` keeps it alive for the reader's lifetime
    /// while also letting the test anchor the mmap explicitly.
    struct MmapOwner(StdArc<Mmap>);

    impl AsRef<[u8]> for MmapOwner {
        fn as_ref(&self) -> &[u8] {
            self.0.as_ref()
        }
    }

    /// Build an `(n_docs × dim)` corpus, register a single
    /// vector index with the requested IVF shape, and stream
    /// the resulting unified-blob bytes to `tmp` via
    /// `VectorBuilder::finish_to`. The streaming
    /// write avoids materializing a 1.5 GiB `Vec<u8>` in the
    /// test's address space at 1M × 384 — the build's transient
    /// peak doesn't survive the `before` RSS snapshot.
    ///
    /// Deterministic per-row vector: `seed = i × 0x9E3779B1`
    /// folded through a linear congruential step per dim slot.
    /// Same shape the bench corpus generators use, inlined so
    /// the unit test doesn't reach into the bench harness.
    fn build_corpus_to_file(path: &Path, n_docs: u32, dim: usize, n_cent: usize) -> String {
        use std::io::BufWriter;

        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Fp32,
            provided_centroids: None,
        })
        .expect("register column");
        let mut v = vec![0f32; dim];
        for i in 0..n_docs {
            let mut seed = i.wrapping_mul(0x9E37_79B1);
            for slot in v.iter_mut() {
                seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12_345);
                *slot = ((seed >> 16) as f32) / 65_535.0;
            }
            b.add(0, &v).expect("add to vector builder");
        }
        let file = File::create(path).expect("create tempfile");
        let writer = BufWriter::new(file);
        b.finish_to(writer).expect("finish_to BufWriter<File>");

        format!(
            r#"[{{"column":"embedding","dim":{dim},"n_cent":{n_cent},"rot_seed":7,"metric":"l2sq"}}]"#
        )
    }

    /// Open a `Source::Lazy` over a mmap'd corpus file and
    /// return the process RSS delta (bytes) attributable to
    /// the open. Anchors `(reader, mmap_arc)` past the
    /// after-RSS read so neither is dropped before
    /// measurement.
    ///
    /// `memory_stats::memory_stats()` reads `/proc/self/statm`
    /// on Linux — cheap syscall, no allocations of its own.
    /// `physical_mem` is the kernel's RSS counter (anon +
    /// file-mapped). Faulted mmap pages count; unfaulted
    /// pages don't. The whole point of the test is that the
    /// open path only touches a handful of pages (outer
    /// header, directory, per-subsection header) and leaves
    /// the rest of the file unmapped.
    fn measure_lazy_open_rss_delta(corpus_path: &Path, json: &str) -> (usize, usize) {
        let file = File::open(corpus_path).expect("reopen corpus readonly");
        let mmap = unsafe { Mmap::map(&file) }.expect("mmap corpus");
        let mmap_arc = StdArc::new(mmap);
        let bytes = Bytes::from_owner(MmapOwner(StdArc::clone(&mmap_arc)));
        let lazy: StdArc<dyn LazyByteSource> = StdArc::new(BytesLazyByteSource::new(bytes));

        let before = memory_stats().expect("memory_stats supported").physical_mem;

        let reader = VectorReader::open_with_source(
            Source::Lazy(lazy),
            json,
            OpenOptions { verify_crc: false },
        )
        .expect("lazy open");

        let after = memory_stats().expect("memory_stats supported").physical_mem;

        let n_cols = reader.columns.len();
        let delta = after.saturating_sub(before);

        // Keep both alive past the RSS reads — dropping
        // `reader` before reading `after` would silently
        // make the delta look smaller than reality.
        black_box((&reader, &mmap_arc));
        drop(reader);
        drop(mmap_arc);

        (delta, n_cols)
    }

    /// **memory-ceiling acceptance criterion (reference scale).**
    ///
    /// 1 M × 384, `n_cent = 1024`. `#[ignore]`-gated because
    /// the `VectorBuilder.finish_to(...)` call takes ~35 s in
    /// release. Run explicitly:
    ///
    /// ```bash
    /// cargo test --release -p infino --lib \
    ///     mem_ceiling_lazy_open_under_10mib -- --ignored --nocapture
    /// ```
    ///
    /// A regression that re-introduces eager subsection
    /// materialization (the older behaviour) or that scans
    /// `doc_ids` at open will push per-column RSS past the
    /// 10 MB ceiling and fail here rather than at the 100 M
    /// production OOM.
    #[test]
    #[ignore]
    fn mem_ceiling_lazy_open_under_10mib() {
        const N_DOCS: u32 = 1_000_000;
        const DIM: usize = 384;
        const N_CENT: usize = 1024;

        let tmp = NamedTempFile::new().expect("tempfile");
        let json = build_corpus_to_file(tmp.path(), N_DOCS, DIM, N_CENT);

        let (delta_bytes, n_cols) = measure_lazy_open_rss_delta(tmp.path(), &json);
        let delta_mib = delta_bytes as f64 / (1024.0 * 1024.0);
        let per_col_mib = delta_mib / (n_cols.max(1) as f64);

        eprintln!(
            "mem_ceiling_lazy_open_under_10mib (1M × {DIM}, n_cent={N_CENT}): \
             RSS delta = {delta_mib:.3} MiB over {n_cols} column(s) \
             = {per_col_mib:.3} MiB/col"
        );

        assert!(
            per_col_mib <= 10.0,
            "acceptance #2: lazy open RSS delta \
             {per_col_mib:.3} MiB/col exceeds 10 MiB ceiling \
             at 1M × {DIM}, n_cent={N_CENT} (total delta \
             {delta_mib:.3} MiB over {n_cols} column(s))."
        );
    }

    /// **acceptance criterion #2 (smoke scale).**
    ///
    /// 50 k × 64, `n_cent = 64`. Runs in default
    /// `cargo test --lib` (~1–2 s build) so every PR gets
    /// continuous feedback on the structural property: lazy
    /// open touches only the structural-decode pages, never
    /// the bulk codes/full/doc_ids regions. The 10 MiB ceiling
    /// at the headline 1M × 384 scale is asserted at
    /// the same value here because the resident allocation
    /// (mostly the rotation matrix at `dim²·4` = 16 KB for
    /// dim=64) is *smaller* at smoke scale, not larger — if
    /// this fires, the bigger test will too.
    ///
    /// `dim = 64` keeps the corpus tiny (~13 MB on disk) and
    /// the rotation matrix Gram-Schmidt fast.
    #[test]
    fn mem_ceiling_lazy_open_smoke() {
        const N_DOCS: u32 = 50_000;
        const DIM: usize = 64;
        const N_CENT: usize = 64;

        let tmp = NamedTempFile::new().expect("tempfile");
        let json = build_corpus_to_file(tmp.path(), N_DOCS, DIM, N_CENT);

        let (delta_bytes, n_cols) = measure_lazy_open_rss_delta(tmp.path(), &json);
        let delta_mib = delta_bytes as f64 / (1024.0 * 1024.0);
        let per_col_mib = delta_mib / (n_cols.max(1) as f64);

        eprintln!(
            "mem_ceiling_lazy_open_smoke ({N_DOCS} × {DIM}, n_cent={N_CENT}): \
             RSS delta = {delta_mib:.3} MiB over {n_cols} column(s) \
             = {per_col_mib:.3} MiB/col"
        );

        assert!(
            per_col_mib <= 10.0,
            "lazy open smoke RSS delta {per_col_mib:.3} MiB/col \
             exceeds 10 MiB ceiling at {N_DOCS} × {DIM} \
             (total delta {delta_mib:.3} MiB over {n_cols} column(s))."
        );
    }

    // -----------------------------------------------------------------
    // — supertable-scale memory ceiling
    // -----------------------------------------------------------------
    //
    // The single-superfile `mem_ceiling_lazy_open_*` tests above pin the
    // per-reader bound. These multi-superfile variants pin the
    // *supertable-shaped* bound: open N superfiles concurrently — same
    // shape `Supertable::commit` produces (N = N_SUPERFILES_BENCH × num_cpus
    // because `split_buffer_into_row_shards` shards each commit's
    // buffer into one superfile per writer-pool thread) — and assert the
    // total anon RSS delta scales as `N × O(centroids + rotation +
    // small)`, not as `N × subsection_size`.
    //
    // What this proves (and what it doesn't):
    //
    // - PROVES: a supertable opened with the production disk-cache
    //   path (`Source::InMemory(Bytes::from_owner(mmap))` per superfile —
    //   see `supertable::reader_cache::disk::insert`) keeps anon
    //   RSS bounded across an arbitrary number of superfiles, with no
    //   per-doc anon term. Equivalent here because
    //   `Bytes::from_owner` is zero-copy over the mmap, and the
    //   lazy-open path doesn't touch `doc_ids[]` / `full[]` at
    //   open time (the inline `pos` removes the only reason
    //   open ever touched `doc_ids[]`).
    //
    // - DOES NOT PROVE: the in-process `InMemoryReaderCache` path
    //   (`Bytes::from(Vec)` per superfile — see
    //   `supertable::reader_cache::in_memory::insert`) has the same
    //   bound. That path holds each superfile's bytes in anon by
    //   construction (no mmap involved). The in-memory cache is the
    //   test/bench path; production attaches a `StorageProvider` and
    //   routes through the disk cache. A separate test for the
    //   in-memory cache path is out of scope here — that path's
    //   anon cost is its declared contract.
    //
    // The bench's 10M × 4-commit × num_cpus-thread shape produces
    // exactly the topology these tests exercise. The smoke variant
    // mirrors the bench's *layout* at a tiny corpus size (4 superfiles
    // × 50 k docs × 64 dim) so every PR catches regressions
    // (~5 s build). The `#[ignore]`'d reference-scale variant uses the
    // bench's actual per-superfile shape (16 superfiles × 625 k docs ×
    // 384 dim × n_cent_per_superfile matching the bench's
    // `n_cent_total / 4`) and runs only when called out.

    /// Open `N` superfile files (built by `build_corpus_to_file`) via
    /// `Source::Lazy(BytesLazyByteSource over Arc<Mmap>)` and return
    /// the total RSS delta attributable to those opens. Anchors
    /// `(readers, mmaps)` past the after-RSS read.
    fn measure_lazy_multi_superfile_open_rss_delta(
        corpus_paths: &[PathBuf],
        jsons: &[String],
    ) -> (usize, usize, usize) {
        assert_eq!(corpus_paths.len(), jsons.len(), "paths/jsons must align");
        let n_superfiles = corpus_paths.len();

        // Pre-build (mmap, lazy source) pairs *before* the `before`
        // snapshot so the syscalls don't contaminate the delta — we
        // only want the open path's allocations in the measurement.
        let mut lazies: Vec<(StdArc<Mmap>, StdArc<dyn LazyByteSource>)> =
            Vec::with_capacity(n_superfiles);
        for path in corpus_paths {
            let file = File::open(path).expect("reopen corpus readonly");
            let mmap = unsafe { Mmap::map(&file) }.expect("mmap corpus");
            let mmap_arc = StdArc::new(mmap);
            let bytes = Bytes::from_owner(MmapOwner(StdArc::clone(&mmap_arc)));
            let lazy: StdArc<dyn LazyByteSource> = StdArc::new(BytesLazyByteSource::new(bytes));
            lazies.push((mmap_arc, lazy));
        }

        let before = memory_stats().expect("memory_stats supported").physical_mem;

        let mut readers: Vec<VectorReader> = Vec::with_capacity(n_superfiles);
        let mut n_cols_total = 0usize;
        for ((_, lazy), json) in lazies.iter().zip(jsons.iter()) {
            let reader = VectorReader::open_with_source(
                Source::Lazy(StdArc::clone(lazy)),
                json,
                OpenOptions { verify_crc: false },
            )
            .expect("lazy open");
            n_cols_total += reader.columns.len();
            readers.push(reader);
        }

        let after = memory_stats().expect("memory_stats supported").physical_mem;

        let delta = after.saturating_sub(before);

        // Keep both alive past the RSS reads — dropping any reader
        // (or mmap) before reading `after` would silently shrink the
        // measured delta.
        black_box((&readers, &lazies));
        drop(readers);
        drop(lazies);

        (delta, n_cols_total, n_superfiles)
    }

    /// **supertable-scale memory ceiling (smoke).**
    ///
    /// Mirrors the bench's 4-commit × num_cpus-thread shape at a
    /// tiny corpus size. Builds 4 superfile files (each 50 k × 64
    /// dim × n_cent=64 — same shape as
    /// `mem_ceiling_lazy_open_smoke`), opens all 4 lazy, and
    /// asserts the total anon RSS delta is ≤ 10 MiB. With
    /// per-superfile ceiling of 10 MiB / column from the single-
    /// superfile smoke and a 4× multiplier in the worst case
    /// (centroids + rotation matrix per superfile), 10 MiB total
    /// gives plenty of headroom while still failing loud if a
    /// regression makes per-superfile opens allocate per-doc.
    ///
    /// Runs in the default `cargo test --lib` suite (~3–5 s
    /// total) so every PR validates the supertable-shape bound.
    #[test]
    fn mem_ceiling_lazy_multi_superfile_open_smoke() {
        const N_SUPERFILES: usize = 4;
        const N_DOCS_PER_SEG: u32 = 50_000;
        const DIM: usize = 64;
        const N_CENT: usize = 64;

        let mut tmps: Vec<NamedTempFile> = Vec::with_capacity(N_SUPERFILES);
        let mut paths: Vec<PathBuf> = Vec::with_capacity(N_SUPERFILES);
        let mut jsons: Vec<String> = Vec::with_capacity(N_SUPERFILES);
        for _ in 0..N_SUPERFILES {
            let tmp = NamedTempFile::new().expect("tempfile");
            let json = build_corpus_to_file(tmp.path(), N_DOCS_PER_SEG, DIM, N_CENT);
            paths.push(tmp.path().to_path_buf());
            jsons.push(json);
            tmps.push(tmp); // keep the tempfile alive until end
        }

        let (delta_bytes, n_cols_total, n_superfiles) =
            measure_lazy_multi_superfile_open_rss_delta(&paths, &jsons);
        let delta_mib = delta_bytes as f64 / (1024.0 * 1024.0);
        let per_seg_mib = delta_mib / n_superfiles as f64;

        eprintln!(
            "mem_ceiling_lazy_multi_superfile_open_smoke ({N_SUPERFILES} × {N_DOCS_PER_SEG} × \
             {DIM}, n_cent={N_CENT}): RSS delta = {delta_mib:.3} MiB over {n_superfiles} \
             superfile(s) ({n_cols_total} column(s) total) = {per_seg_mib:.3} MiB/superfile"
        );

        assert!(
            delta_mib <= 10.0,
            "supertable-shape lazy open RSS delta {delta_mib:.3} MiB exceeds 10 MiB ceiling \
             at {N_SUPERFILES} × {N_DOCS_PER_SEG} × {DIM} — regression hint: each superfile may \
             be touching its doc_ids/full[]/codes region at open"
        );

        drop(tmps);
    }

    /// **supertable-scale memory ceiling (reference scale).**
    ///
    /// Mirrors the bench's actual 10M × 4-commit ×
    /// 4-thread-writer-pool topology: 16 superfiles × 625 k docs ×
    /// 384 dim × `n_cent_per_superfile = n_cent(10M) / 4` (the
    /// bench's `corpus::n_cent(10M)` returns 1024, so this is
    /// 256). Each superfile file is ~960 MiB on disk; the test
    /// writes ~15 GiB total to the tempdir. Build time is
    /// dominated by the 16 sequential streaming builds at
    /// ~10 s each in release ≈ 3 min total.
    ///
    /// `#[ignore]`-gated. Run explicitly:
    ///
    /// ```bash
    /// cargo test --release -p infino --lib \
    ///     mem_ceiling_lazy_supertable_scale_under_50mib -- --ignored --nocapture
    /// ```
    ///
    /// Bound: 50 MiB total anon over the 16 superfiles. The
    /// per-superfile open materialises:
    /// - rotation matrix: `dim² × 4 = 576 KiB` at dim=384
    /// - centroids buffer (in lazy source page cache, not anon):
    ///   `n_cent × dim × 4 = 384 KiB` at the smoke shape
    /// - per-column header / cluster_idx slices (KiB-range)
    ///
    /// Add a 2× safety margin for allocator overhead +
    /// reader-struct fields, multiply by 16 superfiles → ~20 MiB
    /// theoretical, 50 MiB ceiling for headroom. A regression
    /// that re-introduces eager subsection materialisation
    /// would blow this to ~15 GiB (the full corpus) and fail
    /// loud here rather than at the production 100 M OOM.
    #[test]
    #[ignore]
    fn mem_ceiling_lazy_supertable_scale_under_50mib() {
        const N_SUPERFILES: usize = 16;
        const N_DOCS_PER_SEG: u32 = 625_000;
        const DIM: usize = 384;
        const N_CENT_PER_SEG: usize = 256;

        let mut tmps: Vec<NamedTempFile> = Vec::with_capacity(N_SUPERFILES);
        let mut paths: Vec<PathBuf> = Vec::with_capacity(N_SUPERFILES);
        let mut jsons: Vec<String> = Vec::with_capacity(N_SUPERFILES);
        for i in 0..N_SUPERFILES {
            let tmp = NamedTempFile::new().expect("tempfile");
            eprintln!(
                "  building superfile {i:2}/{N_SUPERFILES} \
                 ({N_DOCS_PER_SEG} × {DIM}, n_cent={N_CENT_PER_SEG})…"
            );
            let json = build_corpus_to_file(tmp.path(), N_DOCS_PER_SEG, DIM, N_CENT_PER_SEG);
            paths.push(tmp.path().to_path_buf());
            jsons.push(json);
            tmps.push(tmp);
        }

        let (delta_bytes, n_cols_total, n_superfiles) =
            measure_lazy_multi_superfile_open_rss_delta(&paths, &jsons);
        let delta_mib = delta_bytes as f64 / (1024.0 * 1024.0);
        let per_seg_mib = delta_mib / n_superfiles as f64;

        eprintln!(
            "mem_ceiling_lazy_supertable_scale_under_50mib ({N_SUPERFILES} × {N_DOCS_PER_SEG} × \
             {DIM}, n_cent={N_CENT_PER_SEG}): RSS delta = {delta_mib:.3} MiB over \
             {n_superfiles} superfile(s) ({n_cols_total} column(s) total) = \
             {per_seg_mib:.3} MiB/superfile"
        );

        assert!(
            delta_mib <= 50.0,
            "supertable-scale (10M-bench shape) lazy open RSS delta {delta_mib:.3} MiB \
             exceeds 50 MiB ceiling at {N_SUPERFILES} × {N_DOCS_PER_SEG} × {DIM}. \
             Eager re-introduction would push this past 15 GiB."
        );

        drop(tmps);
    }

    /// **many-superfiles stress test (100M
    /// aspiration shape).**
    ///
    /// The honest scale test for "100M docs across a supertable"
    /// can't materialise 100M production-shape superfiles on a
    /// developer box (the per-superfile 625k × 384 shape used in
    /// the bench produces ~960 MiB on disk × 160 superfiles = 150
    /// GiB of corpus). Instead, this test pins the *structural*
    /// memory bound by varying the high-cardinality axis (superfile
    /// count) at a thin per-superfile shape: **100 superfiles × 50 k
    /// docs × 128 dim × 128 n_cent**.
    ///
    /// What this proves:
    ///
    /// - Per-superfile open allocation is `O(n_cent × dim × 4 +
    ///   rotation + small)` — no `n_docs` term. At this shape:
    ///   centroids 64 KiB + rotation matrix 64 KiB + column
    ///   metadata ≪ 1 MiB per superfile. Total expected RSS delta
    ///   ≪ 200 MiB across 100 superfiles; 400 MiB ceiling for
    ///   allocator overhead + reader-struct fields.
    ///
    /// - The deletion of `doc_to_pos` made superfile-count
    ///   the only scaling dimension. A regression that reintroduced
    ///   any per-doc resident state — e.g. a returning lookup
    ///   table at `n_docs × 4` bytes per column — would here
    ///   allocate 100 × 50 k × 4 = 20 MiB anon just for tables
    ///   (small but growing); at the bench's 100 superfiles × 625 k
    ///   the same regression is 250 MiB.
    ///
    /// Each superfile file is ~25 MiB on disk; the test writes
    /// ~2.5 GiB total to the tempdir. Build time is dominated by
    /// the 100 sequential streaming builds (~1.5 s each in
    /// release ≈ 2.5 min total).
    ///
    /// `#[ignore]`-gated. Run explicitly:
    ///
    /// ```bash
    /// cargo test --release -p infino --lib \
    ///     mem_ceiling_lazy_many_superfiles_under_400mib -- --ignored --nocapture
    /// ```
    #[test]
    #[ignore]
    fn mem_ceiling_lazy_many_superfiles_under_400mib() {
        const N_SUPERFILES: usize = 100;
        const N_DOCS_PER_SEG: u32 = 50_000;
        const DIM: usize = 128;
        const N_CENT_PER_SEG: usize = 128;

        let mut tmps: Vec<NamedTempFile> = Vec::with_capacity(N_SUPERFILES);
        let mut paths: Vec<PathBuf> = Vec::with_capacity(N_SUPERFILES);
        let mut jsons: Vec<String> = Vec::with_capacity(N_SUPERFILES);
        for i in 0..N_SUPERFILES {
            let tmp = NamedTempFile::new().expect("tempfile");
            if i % 10 == 0 {
                eprintln!(
                    "  building superfile {i:3}/{N_SUPERFILES} \
                     ({N_DOCS_PER_SEG} × {DIM}, n_cent={N_CENT_PER_SEG})…"
                );
            }
            let json = build_corpus_to_file(tmp.path(), N_DOCS_PER_SEG, DIM, N_CENT_PER_SEG);
            paths.push(tmp.path().to_path_buf());
            jsons.push(json);
            tmps.push(tmp);
        }

        let (delta_bytes, n_cols_total, n_superfiles) =
            measure_lazy_multi_superfile_open_rss_delta(&paths, &jsons);
        let delta_mib = delta_bytes as f64 / (1024.0 * 1024.0);
        let per_seg_mib = delta_mib / n_superfiles as f64;

        eprintln!(
            "mem_ceiling_lazy_many_superfiles_under_400mib ({N_SUPERFILES} × {N_DOCS_PER_SEG} × \
             {DIM}, n_cent={N_CENT_PER_SEG}): RSS delta = {delta_mib:.3} MiB over \
             {n_superfiles} superfile(s) ({n_cols_total} column(s) total) = \
             {per_seg_mib:.3} MiB/superfile"
        );

        assert!(
            delta_mib <= 400.0,
            "many-superfiles lazy open RSS delta {delta_mib:.3} MiB exceeds 400 MiB ceiling \
             at {N_SUPERFILES} × {N_DOCS_PER_SEG} × {DIM}. A regression that reintroduced \
             any per-doc resident state would push this much higher; the deletion of \
             doc_to_pos is what keeps the bound structural."
        );

        drop(tmps);
    }

    // -----------------------------------------------------------------
    // VectorReader::open_lazy cold-open range budget + round-trip
    // parity. The lazy open path fetches exact metadata ranges:
    // outer header, directory + CRC, subsection headers, and Sq8
    // codec_meta. It does not prefetch centroids, cluster_idx, or
    // per-cluster blocks; those are search-time data.
    // -----------------------------------------------------------------

    fn build_small_superfile(
        dim: usize,
        n_cent: usize,
        n_docs: u32,
        codec: RerankCodec,
        metric: Metric,
    ) -> (Bytes, String, Vec<Vec<f32>>) {
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 41,
            metric,
            rerank_codec: codec,
            provided_centroids: None,
        })
        .expect("register column");
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim).map(|j| (i + j as u32) as f32 * 0.1).collect();
            b.add(0, &v).expect("add");
            all.push(v);
        }
        let blob = Bytes::from(b.finish().expect("finish"));
        let metric_str = match metric {
            Metric::L2Sq => "l2sq",
            Metric::Cosine => "cosine",
            Metric::NegDot => "negdot",
        };
        let json = format!(
            r#"[{{"column":"v","dim":{dim},"n_cent":{n_cent},"rot_seed":41,"metric":"{metric_str}"}}]"#,
        );
        (blob, json, all)
    }

    #[tokio::test]
    async fn open_lazy_small_sq8_superfile_fetches_exact_metadata_ranges() {
        let (blob, json, _) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let async_counter = counting.async_counter();

        let _reader = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy small Sq8");

        let n_calls = async_counter.load(AtomicOrdering::Relaxed);
        assert_eq!(
            n_calls, 3,
            "small Sq8 open_lazy must issue exactly 3 async range calls \
             (outer header, directory+crc, subsection header); \
             observed {n_calls}",
        );
    }

    #[tokio::test]
    async fn open_lazy_small_superfile_fetches_no_codec_meta_for_non_sq8() {
        for codec in [RerankCodec::Fp32, RerankCodec::RabitqOnly] {
            let (blob, json, _) = build_small_superfile(32, 4, 64, codec, Metric::L2Sq);
            let counting = StdArc::new(CountingLazyByteSource::new(blob));
            let async_counter = counting.async_counter();

            let _reader = VectorReader::open_lazy(
                StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
                &json,
                OpenOptions::for_object_store(),
            )
            .await
            .unwrap_or_else(|e| panic!("open_lazy {codec:?}: {e:?}"));

            let n_calls = async_counter.load(AtomicOrdering::Relaxed);
            assert_eq!(
                n_calls, 3,
                "open_lazy ({codec:?}) must issue exactly 3 async range calls \
                 (outer header, directory+crc, subsection header); observed {n_calls}",
            );
        }
    }

    /// round-trip parity. A search against an
    /// `open_lazy` reader returns the same `(doc_id, distance)`
    /// hits as the eager `open()` path. Confirms the open-path
    /// refactor (Phase A sub-header + Phase B codec_meta) and
    /// the overlay round-trip preserve every search-critical
    /// metadata field.
    #[tokio::test]
    async fn open_lazy_search_matches_eager_open_per_codec() {
        for codec in [
            RerankCodec::Fp32,
            RerankCodec::Sq8Residual,
            RerankCodec::RabitqOnly,
        ] {
            let (blob, json, all) = build_small_superfile(32, 4, 64, codec, Metric::L2Sq);
            let r_eager = VectorReader::open(blob.clone(), &json)
                .unwrap_or_else(|e| panic!("eager open {codec:?}: {e:?}"));
            let counting = StdArc::new(CountingLazyByteSource::new(blob));
            let r_lazy = VectorReader::open_lazy(
                StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
                &json,
                OpenOptions::for_object_store(),
            )
            .await
            .unwrap_or_else(|e| panic!("open_lazy {codec:?}: {e:?}"));

            for &q_idx in &[0usize, 7, 17, 31] {
                let hits_eager = r_eager
                    .search("v", &all[q_idx], 5, 4, 5)
                    .await
                    .unwrap_or_else(|e| panic!("eager search {codec:?}: {e:?}"));
                let hits_lazy = r_lazy
                    .search("v", &all[q_idx], 5, 4, 5)
                    .await
                    .unwrap_or_else(|e| panic!("lazy search {codec:?}: {e:?}"));
                assert_eq!(
                    hits_eager, hits_lazy,
                    "search results must match between eager and lazy open \
                     (codec {codec:?}, query {q_idx})",
                );
            }
        }
    }

    /// Cold first search after `open_lazy` issues at most
    /// `nprobe + 2` underlying async range GETs against the
    /// LazyByteSource: centroids, cluster_idx, and one interleaved
    /// cluster block per probed non-empty cluster. Rerank adds no
    /// extra GET because full vectors ride inside the cluster blocks.
    ///
    /// Headline budget for the cold first-search phase
    /// (≤ 12 ranges, ≤ 5 MB at 1M × 384 sq8, nprobe = 8). The
    /// small-superfile test here pins the structural shape; the
    /// RustFS bench measures the real wall-clock against AWS-
    /// shape RTTs.
    ///
    /// "At most" because some probed clusters can be empty
    /// (zero-count entries skip the block fetch entirely); for a
    /// well-distributed corpus the budget is hit exactly.
    #[tokio::test]
    async fn cold_first_search_after_open_lazy_within_nprobe_plus_one_ranges() {
        let (blob, json, all) =
            build_small_superfile(32, 8, 128, RerankCodec::Sq8Residual, Metric::L2Sq);

        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let async_counter = counting.async_counter();
        // Disable BytesLazyByteSource's zero-copy sync path so
        // every non-overlay read is forced through the async
        // `range` bridge — that's what an object-store-backed
        // source actually pays per region.
        counting.disable_sync();

        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");

        let after_open = async_counter.load(AtomicOrdering::Relaxed);
        assert_eq!(
            after_open, 3,
            "Sq8 open_lazy must issue exactly the open-time metadata ranges \
             (header, directory, subheader); codec_meta is deferred to the \
             first search on the object-store path; observed {after_open}",
        );

        let nprobe = 4usize;
        let _hits = r_lazy
            .search("v", &all[0], 5, nprobe, 5)
            .await
            .expect("cold first search");

        let after_search = async_counter.load(AtomicOrdering::Relaxed);
        let search_calls = after_search - after_open;
        let max_expected = (nprobe + 1) as u64;
        assert!(
            search_calls <= max_expected,
            "cold first search at nprobe={nprobe} must issue ≤ {max_expected} async \
             range GETs (centroids+cluster_idx + one interleaved block per probed \
             cluster); observed {search_calls}",
        );
        assert!(
            search_calls >= 2,
            "cold first search must issue at least 2 async range GETs (centroids+ \
             cluster_idx + ≥1 cluster block); observed {search_calls} suggests \
             search accidentally short-circuited the cold fetch paths",
        );
    }

    /// A cold probe wave with multiple surviving ranges must dispatch
    /// them **concurrently**, not serially. The end-to-end search GET
    /// budget is pinned by the range-budget test above; the probe wave
    /// itself now merges all legs (blocks + Sq8 meta + stable-ids) into
    /// one coalesce plan, so on small fixtures the wave collapses to a
    /// single range and a search-side probe can no longer exhibit the
    /// property. Pin it at the layer that owns it instead:
    /// `get_ranges_parallel_async` over disjoint uncoalescible ranges.
    ///
    /// Each `range()` call holds an in-flight slot (RAII guard); peak
    /// in-flight ≥ 2 proves the fetches overlapped. `range()` is padded
    /// with a small artificial latency so a serial implementation
    /// completes each future before the next is awaited — without the
    /// latency, the trivial `bytes.slice(...)` body resolves instantly
    /// and even a serial caller looks concurrent (in-flight peaks at 1
    /// indistinguishably).
    ///
    /// Runs on the multi-thread runtime for the same `block_in_place`
    /// reason as the range-budget test above.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn cold_range_wave_dispatches_gets_concurrently() {
        let (blob, json, _all) =
            build_small_superfile(32, 8, 256, RerankCodec::Sq8Residual, Metric::L2Sq);
        let blob_len = blob.len();

        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let max_in_flight = counting.max_in_flight_counter();
        counting.disable_sync();
        counting.set_async_latency(Duration::from_millis(5));

        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");

        // Reset max_in_flight after open (we only want to pin the wave
        // dispatch shape; open is its own budget exercise).
        max_in_flight.store(0, AtomicOrdering::Release);

        // Three disjoint slices spread across the blob — the wave shape a
        // cold probe produces when coalescing leaves multiple ranges.
        let slice = (blob_len / 8).max(1);
        let ranges = vec![
            0..slice,
            (blob_len / 2)..(blob_len / 2 + slice),
            (blob_len - slice)..blob_len,
        ];
        let fetched = r_lazy
            .source
            .get_ranges_parallel_async(&ranges)
            .await
            .expect("parallel range wave");
        assert_eq!(fetched.len(), ranges.len(), "one slice per input range");

        let peak = max_in_flight.load(AtomicOrdering::Acquire);
        assert!(
            peak >= 2,
            "a multi-range cold wave must overlap its fetches (peak in-flight ≥ 2); \
             observed {peak} across {} ranges",
            ranges.len(),
        );
    }

    /// round-trip parity for the unified
    /// codes+doc_ids per-cluster fetch path. The combined block
    /// gets sliced into a `codes` prefix and `doc_ids` suffix
    /// inside the search hot loop; this test pins that the
    /// slice boundaries land at exactly `count * code_bytes`
    /// (i.e. the bit-identical results survive the refactor
    /// from two separate ranges to one combined block).
    #[tokio::test]
    async fn m3_combined_cluster_fetch_matches_eager_open_per_codec() {
        for codec in [
            RerankCodec::Fp32,
            RerankCodec::Sq8Residual,
            RerankCodec::RabitqOnly,
        ] {
            let (blob, json, all) = build_small_superfile(32, 4, 64, codec, Metric::L2Sq);
            let r_eager = VectorReader::open(blob.clone(), &json)
                .unwrap_or_else(|e| panic!("eager open {codec:?}: {e:?}"));
            let counting = StdArc::new(CountingLazyByteSource::new(blob));
            let r_lazy = VectorReader::open_lazy(
                StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
                &json,
                OpenOptions::for_object_store(),
            )
            .await
            .unwrap_or_else(|e| panic!("open_lazy {codec:?}: {e:?}"));

            for &q_idx in &[0usize, 7, 17, 31] {
                let hits_eager = r_eager
                    .search("v", &all[q_idx], 5, 4, 5)
                    .await
                    .unwrap_or_else(|e| panic!("eager search {codec:?}: {e:?}"));
                let hits_lazy = r_lazy
                    .search("v", &all[q_idx], 5, 4, 5)
                    .await
                    .unwrap_or_else(|e| panic!("lazy search {codec:?}: {e:?}"));
                assert_eq!(
                    hits_eager, hits_lazy,
                    "combined cluster fetch must produce bit-identical search \
                     results vs eager (codec {codec:?}, query {q_idx})",
                );
            }
        }
    }

    /// pins the `cluster_block_range` address math
    /// against the per-cluster block spec
    /// (`[codes: cnt*cb][doc_ids: cnt*4]`). Walks every non-
    /// empty cluster and checks the block range size matches
    /// `cnt × (cb + 4)` exactly, the start aligns with
    /// `per_cluster_blocks_off + doc_off × (cb + 4)`, and the
    /// codes/doc_ids halves slice in at the expected boundary
    /// inside the fetched block.
    #[test]
    fn cluster_block_range_matches_v1_layout_invariant() {
        let (blob, json, _) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let col = &r.columns[0];
        let cb = col.quant.code_bytes();
        let pvb = col.rerank_codec.per_vector_bytes(col.dim);
        // Interleaved layout: each per-cluster block is
        // `[codes][doc_ids][full]` at stride `cb + 4 + pvb`.
        let stride = cb + 4 + pvb;

        let cluster_idx_bytes = r
            .source
            .try_get_range_sync(
                col.subsection_range.start + col.cluster_idx_off
                    ..col.subsection_range.start
                        + col.cluster_idx_off
                        + (col.n_cent as usize) * CLUSTER_IDX_ENTRY_BYTES,
            )
            .expect("cluster_idx must be resident in InMemory source");

        let mut n_non_empty = 0usize;
        for c in 0..col.n_cent {
            let (off, cnt) = read_cluster_entry(&cluster_idx_bytes, c as usize);
            if cnt == 0 {
                continue;
            }
            n_non_empty += 1;
            let block = col.cluster_block_range(off, cnt);
            let expected_start =
                col.subsection_range.start + col.per_cluster_blocks_off + (off as usize) * stride;
            let expected_len = (cnt as usize) * stride;
            assert_eq!(
                block.start, expected_start,
                "cluster {c} block start must equal \
                 per_cluster_blocks_off + doc_off × stride",
            );
            assert_eq!(
                block.len(),
                expected_len,
                "cluster {c} block size must equal cnt × (cb + 4 + per_vec_bytes)",
            );
            // Inside the fetched block, `[0..cnt*cb)` is codes,
            // `[cnt*cb..cnt*(cb+4))` is doc_ids, and the remaining
            // `cnt*pvb` bytes are the interleaved full[] vectors —
            // the exact boundaries the search() hot path slices at.
            let codes_end = (cnt as usize) * cb;
            let doc_ids_end = codes_end + (cnt as usize) * 4;
            assert!(
                doc_ids_end <= block.len(),
                "codes + doc_ids prefix must fit inside the block"
            );
            assert_eq!(
                block.len() - doc_ids_end,
                (cnt as usize) * pvb,
                "full suffix must be cnt × per_vec_bytes bytes",
            );
        }
        assert!(
            n_non_empty > 0,
            "test corpus must populate at least one cluster"
        );
    }

    /// verify the `Source::Lazy` reader constructed
    /// by `open_lazy` exposes the same column metadata as the
    /// eager reader (dim, n_cent, n_docs, codec, sq8_meta shape).
    /// The structural decode that produces `ColumnReader` runs
    /// against the overlay; this test pins that every parsed
    /// field surfaces unchanged.
    #[tokio::test]
    async fn open_lazy_column_metadata_matches_eager_open() {
        let (blob, json, _) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r_eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        // Simulate the object-store path: with no zero-copy sync read
        // available, open defers Sq8 codec_meta to the first search,
        // so the lazy column resolves to `Sq8ColumnMeta::Lazy`.
        counting.disable_sync();
        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");

        assert_eq!(r_eager.columns.len(), r_lazy.columns.len());
        let col_eager = &r_eager.columns[0];
        let col_lazy = &r_lazy.columns[0];
        assert_eq!(col_eager.name, col_lazy.name);
        assert_eq!(col_eager.dim, col_lazy.dim);
        assert_eq!(col_eager.n_cent, col_lazy.n_cent);
        assert_eq!(col_eager.n_docs, col_lazy.n_docs);
        assert_eq!(col_eager.rerank_codec, col_lazy.rerank_codec);
        assert_eq!(col_eager.metric, col_lazy.metric);

        let meta_eager = col_eager.sq8_meta.as_ref().expect("eager Sq8 meta");
        let meta_lazy = col_lazy.sq8_meta.as_ref().expect("lazy Sq8 meta");
        assert!(
            matches!(meta_eager, Sq8ColumnMeta::Eager { .. }),
            "eager open should materialise Sq8 metadata"
        );
        assert!(
            matches!(meta_lazy, Sq8ColumnMeta::Lazy { .. }),
            "lazy open should defer Sq8 metadata to search"
        );
    }

    #[test]
    fn get_vectors_fp32_returns_vectors_in_original_order() {
        let n_docs = 64u32;
        let dim = 16;
        let n_cent = 4;

        // Build a blob with Fp32 encoding
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Fp32,
            provided_centroids: None,
        })
        .expect("register column");

        // Create deterministic vectors
        let mut input_vectors = Vec::new();
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim)
                .map(|j| ((i.wrapping_mul(31) + j as u32) % 100) as f32 * 0.01)
                .collect();
            input_vectors.push(v.clone());
            b.add(0, &v).expect("add to vector builder");
        }

        let bytes = b.finish().expect("finish vector builder");
        let json = format!(
            r#"[{{"column":"embedding","dim":{dim},"n_cent":{n_cent},"rot_seed":7,"metric":"l2sq"}}]"#
        );
        let reader = VectorReader::open(Bytes::from(bytes), &json).expect("open should succeed");

        // Retrieve vectors via the new function
        let retrieved = reader
            .get_vectors_fp32("embedding")
            .expect("get_vectors_fp32 should succeed");

        // Verify all vectors are returned
        assert_eq!(retrieved.len(), n_docs as usize);

        // Verify vectors match original vectors (within floating point precision)
        for (i, retrieved_vec) in retrieved.iter().enumerate() {
            assert_eq!(retrieved_vec.len(), dim);
            for (j, &val) in retrieved_vec.iter().enumerate() {
                let expected = input_vectors[i][j];
                assert!(
                    (val - expected).abs() < 1e-6,
                    "vector {} dimension {} mismatch: got {}, expected {}",
                    i,
                    j,
                    val,
                    expected
                );
            }
        }
    }

    #[test]
    fn get_vectors_fp32_rejects_non_fp32_codec() {
        // blob was built with Sq8Residual by default, not Fp32
        let mut builder = VectorBuilder::new();
        builder
            .register_column(VectorConfig {
                column: "embedding".into(),
                dim: 16,
                rot_seed: 7,
                metric: Metric::L2Sq,
                rerank_codec: RerankCodec::Sq8Residual,
                provided_centroids: None,
            })
            .expect("register column");
        for i in 0u32..32 {
            let v: Vec<f32> = (0..16)
                .map(|j| ((i.wrapping_mul(31) + j as u32) % 100) as f32 * 0.01)
                .collect();
            builder.add(0, &v).expect("add");
        }
        let sq8_bytes = builder.finish().expect("finish");
        let sq8_json =
            r#"[{"column":"embedding","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#
                .to_string();
        let reader = VectorReader::open(Bytes::from(sq8_bytes), &sq8_json).expect("open");

        // Should error because codec is Sq8Residual, not Fp32
        let result = reader.get_vectors_fp32("embedding");
        assert!(result.is_err());
        if let Err(VectorError::Read(ReadError::MalformedVersion(msg))) = result {
            assert!(msg.contains("Fp32"));
        } else {
            panic!("expected MalformedVersion error, got {:?}", result);
        }
    }

    #[test]
    fn get_vectors_fp32_rejects_unknown_column() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let reader = VectorReader::open(blob, &json).expect("open should succeed");

        let result = reader.get_vectors_fp32("nonexistent");
        assert!(matches!(result, Err(VectorError::UnknownColumn(_))));
    }

    #[test]
    fn get_vectors_fp32_returns_empty_for_no_docs() {
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim: 16,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Fp32,
            provided_centroids: None,
        })
        .expect("register column");
        let bytes = b.finish().expect("finish vector builder");
        let json = r#"[{"column":"embedding","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#
            .to_string();
        let reader = VectorReader::open(Bytes::from(bytes), &json).expect("open should succeed");

        let retrieved = reader
            .get_vectors_fp32("embedding")
            .expect("get_vectors_fp32 should succeed");
        assert!(retrieved.is_empty());
    }

    // -----------------------------------------------------------------
    // Catalog-surface accessors: `cluster_centroids` + `vector_columns_config` (legacy name for vector indexes).
    // -----------------------------------------------------------------
    //
    // Both feed the cross-superfile manifest staging path. They were
    // previously exercised only indirectly through the supertable
    // integration suite; the unit tests below pin their shape against an
    // in-memory blob so the byte-offset math (`centroids_off`,
    // `cluster_idx_off`, the per-entry count field) stays correct.

    #[test]
    fn cluster_centroids_returns_n_cent_dim_and_counts() {
        let dim = 16usize;
        let n_cent = 64usize;
        let n_docs = 64u32;
        let (blob, json) = build_blob(n_docs, dim, n_cent, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");

        let (got_n_cent, got_dim, centroids, counts) =
            r.cluster_centroids("embedding").expect("cluster_centroids");
        assert_eq!(got_n_cent, n_cent as u32);
        assert_eq!(got_dim, dim as u32);
        assert_eq!(
            centroids.len(),
            n_cent * dim,
            "centroids are cluster-major n_cent × dim fp32"
        );
        assert_eq!(counts.len(), n_cent, "one count per cluster");
        // Every doc lands in exactly one cluster, so the counts sum to
        // n_docs — the contract the manifest staging path relies on.
        let total: u32 = counts.iter().sum();
        assert_eq!(total, n_docs, "per-cluster counts must sum to n_docs");

        assert!(
            r.cluster_centroids("nonexistent").is_none(),
            "unknown column yields None"
        );
    }

    #[test]
    fn vector_columns_config_yields_each_column_reader() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let cols: Vec<&ColumnReader> = r.vector_columns_config().collect();
        assert_eq!(cols.len(), 1);
        assert_eq!(cols[0].name, "embedding");
        assert_eq!(cols[0].dim, 16);
        assert_eq!(cols[0].n_cent, 32);
        assert_eq!(cols[0].metric, Metric::L2Sq);
    }

    // -----------------------------------------------------------------
    // Parallel scan paths (`PARALLEL_SCAN_MIN` rayon branches).
    // -----------------------------------------------------------------
    //
    // The coarse 1-bit scan in `build_shortlist`, the fp32 / Sq8 rerank
    // scans, and the `par_map` / `parallel_chunks` helpers all switch
    // from a serial loop to a chunked rayon scan once
    // the candidate pool crosses `PARALLEL_SCAN_MIN` (2048) with more
    // than one probed cluster. The default test corpora are far below
    // that threshold, so these tests build a deliberately large corpus
    // (> 2048 docs across multiple clusters) to drive the parallel arms.
    // Correctness is pinned by a self-query: the planted vector must
    // still come back at top-1, identical to the serial path.

    /// Build a corpus large enough (`n_docs` ≥ a few thousand) to push
    /// the per-query scans over `PARALLEL_SCAN_MIN` when every cluster is
    /// probed. Vectors are deterministic and spread across `n_cent`
    /// clusters by a per-doc phase so more than one cluster is non-empty.
    fn build_large_corpus(
        dim: usize,
        n_cent: usize,
        n_docs: u32,
        codec: RerankCodec,
        metric: Metric,
    ) -> (Bytes, String, Vec<Vec<f32>>) {
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "v".into(),
            dim,
            rot_seed: 101,
            metric,
            rerank_codec: codec,
            provided_centroids: None,
        })
        .expect("register column");
        let mut all = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            // Direction varies by a per-doc phase (spreads docs across
            // clusters); a per-doc unique component (dim 0 carries the
            // doc id) guarantees no two vectors collide, so a self-query
            // has a unique nearest neighbour with distance 0.
            let phase = i % n_cent as u32;
            let v: Vec<f32> = (0..dim)
                .map(|j| {
                    if j == 0 {
                        // Unique per-doc value keeps all vectors distinct.
                        i as f32 * 0.001
                    } else {
                        let base = ((i.wrapping_mul(2654435761).wrapping_add(j as u32 * 40503))
                            % 1000) as f32
                            * 0.01;
                        base + phase as f32
                    }
                })
                .collect();
            b.add(0, &v).expect("add");
            all.push(v);
        }
        let blob = Bytes::from(b.finish().expect("finish"));
        let metric_str = match metric {
            Metric::L2Sq => "l2sq",
            Metric::Cosine => "cosine",
            Metric::NegDot => "negdot",
        };
        let json = format!(
            r#"[{{"column":"v","dim":{dim},"n_cent":{n_cent},"rot_seed":101,"metric":"{metric_str}"}}]"#,
        );
        (blob, json, all)
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn parallel_coarse_scan_and_fp32_rerank_recover_self_query() {
        // n_docs comfortably over PARALLEL_SCAN_MIN; probing every
        // cluster makes total_candidates == n_docs, driving the parallel
        // coarse scan in `build_shortlist`. A large k·rerank_mult shortlist
        // (>= 2048) also pushes the fp32 rerank onto the rayon `par_map`.
        let n_docs = 3000u32;
        let n_cent = 4usize;
        let (blob, json, all) =
            build_large_corpus(16, n_cent, n_docs, RerankCodec::Fp32, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        // k=64, rerank_mult=40 → coarse_limit=2560 ≥ PARALLEL_SCAN_MIN,
        // so the fp32 rerank shortlist is large enough to parallelize.
        let hits = r
            .search("v", &all[1234], 64, n_cent, 40)
            .await
            .expect("parallel search");
        assert_eq!(hits.len(), 64, "k hits returned");
        for w in hits.windows(2) {
            assert!(w[0].1 <= w[1].1, "distances ascending");
        }
        // With every cluster probed and an exhaustive rerank pool, the
        // exact self vector is in the candidate set; fp32 rerank is
        // lossless, so the self distance is exactly 0 and ranks top-1.
        assert_eq!(
            hits[0].0, 1234,
            "parallel coarse + fp32 rerank must recover self at top-1"
        );
        assert!(hits[0].1 < 1e-4, "self distance ~0, got {}", hits[0].1);
    }

    /// Deferred-vs-immediate parity (review follow-up): nothing else
    /// asserts the deferred pipeline (1-bit scan -> selection ->
    /// selected-candidate rerank) agrees with the proven immediate
    /// probe. Same reader, same clusters, untruncated budget
    /// (`k * rerank_mult >= corpus`, so selection drops nothing and
    /// both paths exact-rerank identical candidate sets): the top-k
    /// must match exactly — ids and scores — since both ends run the
    /// same rerank kernel over the same bytes.
    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn deferred_scan_rerank_matches_immediate_probe_top_k() {
        let (blob, json, all) =
            build_large_corpus(16, 4, 2600, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let clusters: Vec<u32> = (0..4).collect();
        let k = 25usize;
        let rerank_mult = 128usize; // 25 * 128 = 3200 >= 2600 rows
        let immediate = r
            .search_clusters_async(
                "v",
                &all[7],
                k,
                &clusters,
                rerank_mult,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("immediate probe");
        let scan = r
            .search_clusters_scan_async(
                "v",
                &all[7],
                k,
                &clusters,
                rerank_mult,
                rerank_mult,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("deferred scan");
        assert!(
            scan.hits.is_empty(),
            "a fully-resident reader defers every cell (no cold hits)"
        );
        assert!(!scan.candidates.is_empty(), "deferred candidates surface");
        let deferred = r
            .rerank_selected_async("v", &all[7], k, scan.candidates, None)
            .await
            .expect("selected rerank")
            .0;
        assert_eq!(
            immediate.0, deferred,
            "deferred scan+select+rerank must equal the immediate probe's top-k"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn parallel_scan_untruncated_pool_matches_brute_force() {
        // 2600 docs across 4 clusters puts the probed scan over
        // PARALLEL_SCAN_MIN, driving the chunked-parallel arm. With an
        // untruncated shortlist (k * rerank_mult >= corpus, so
        // `truncate_to_top_estimates` drops nothing) and fp32 rerank,
        // the returned top-k must equal brute-force L2 over the corpus
        // exactly — a stronger pin than ranking-consistency heuristics.
        use std::collections::HashSet;
        let (blob, json, all) = build_large_corpus(16, 4, 2600, RerankCodec::Fp32, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        // k * rerank_mult = 64 * 41 = 2624 >= 2600: nothing truncated.
        let hits = r.search("v", &all[42], 64, 4, 41).await.expect("search");
        assert_eq!(hits[0].0, 42, "recovers planted self");
        let l2sq =
            |a: &[f32], b: &[f32]| -> f32 { a.iter().zip(b).map(|(x, y)| (x - y) * (x - y)).sum() };
        let mut exact: Vec<(u32, f32)> = all
            .iter()
            .enumerate()
            .map(|(i, v)| (i as u32, l2sq(v, &all[42])))
            .collect();
        exact.sort_by(|a, b| a.1.total_cmp(&b.1).then_with(|| a.0.cmp(&b.0)));
        let want: HashSet<u32> = exact[..64].iter().map(|&(i, _)| i).collect();
        let got: HashSet<u32> = hits.iter().map(|&(i, _)| i).collect();
        assert_eq!(
            got, want,
            "untruncated parallel scan + fp32 rerank == brute force"
        );
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn parallel_sq8_rerank_recovers_self_query() {
        // Drive the parallel arm of `sq8_score_and_refine`: a large
        // shortlist (k·rerank_mult ≥ PARALLEL_SCAN_MIN) on an Sq8 column.
        let n_docs = 3000u32;
        let n_cent = 4usize;
        let (blob, json, all) =
            build_large_corpus(16, n_cent, n_docs, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let hits = r
            .search("v", &all[2001], 64, n_cent, 40)
            .await
            .expect("parallel Sq8 search");
        // The parallel Sq8 first-pass scan + residual refine ran (>2048
        // candidates over >1 cluster). Sq8 is lossy, so we pin structural
        // correctness — k hits, ascending distance — rather than exact
        // self-recovery (covered by the small-corpus Sq8 round-trip tests).
        assert_eq!(hits.len(), 64, "k hits returned from parallel Sq8 path");
        for w in hits.windows(2) {
            assert!(w[0].1 <= w[1].1, "Sq8 rerank distances ascending");
        }
        // The self vector should still rank near the top under Sq8.
        assert!(
            hits.iter().take(8).any(|(id, _)| *id == 2001),
            "self vector should appear in the parallel Sq8 top-8"
        );
    }

    #[test]
    fn parallel_chunks_is_bounded_by_item_count() {
        // 0 items → at least 1 chunk; small item count caps the chunk
        // count; both arms of the `.min(n_items).max(1)` clamp.
        assert_eq!(parallel_chunks(0), 1, "zero items still yields one chunk");
        assert_eq!(parallel_chunks(1), 1, "one item caps at one chunk");
        let many = parallel_chunks(1_000_000);
        assert!(many >= 1, "large item count yields >= 1 chunk");
    }

    #[tokio::test]
    async fn par_map_serial_fallback_for_small_input() {
        // parallel_chunks(items) <= 1 takes the serial map arm.
        let out = par_map(vec![1u32, 2, 3], |x| x * 10, None)
            .await
            .expect("serial par_map cannot drop")
            .0;
        assert_eq!(out, vec![10, 20, 30]);
    }

    // -----------------------------------------------------------------
    // Lazy Sq8 cold path: the `Sq8ColumnMeta::Lazy` rerank arm.
    // -----------------------------------------------------------------
    //
    // When the Sq8 codec_meta bytes aren't resident at open time (an
    // object-store-backed `Source::Lazy` with sync access disabled), the
    // reader records `Sq8ColumnMeta::Lazy` offsets and defers the
    // scale/offset/norms fetch to the first search. That fetch + the
    // sparse `pos → norm` map + the per-cluster kernel rebuild is a large
    // block in `rerank_candidates_from_blocks` that the in-memory tests
    // never reach. These tests force it via `disable_sync()` and pin the
    // results against the eager in-memory open.

    #[tokio::test]
    async fn lazy_sq8_cold_rerank_matches_eager_l2sq() {
        // L2Sq Sq8 carries per-doc norms, so the lazy arm also exercises
        // the sparse `norm_by_pos` span-fetch path.
        //
        // `open_lazy` with `for_object_store()` defers codec_meta — it is
        // NOT prefetched into the overlay — so `sq8_meta` is recorded as
        // `Sq8ColumnMeta::Lazy` and the first search resolves the
        // scale/offset (and L2Sq norms) through the deferred-fetch arm.
        let (blob, json, all) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r_eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        // Disable sync BEFORE open so the deferred codec_meta probe inside
        // `open_with_source` misses the warm cache and records the Sq8 meta
        // as `Lazy`. `open_lazy` pre-installs the structural-decode bytes
        // (header, directory, sub-header) into its overlay, so the open
        // itself still succeeds with sync disabled on the underlying source.
        counting.disable_sync();
        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");
        // Pin that codec_meta really was deferred (the Lazy arm).
        assert!(
            matches!(r_lazy.columns[0].sq8_meta, Some(Sq8ColumnMeta::Lazy { .. })),
            "open_lazy / for_object_store must defer Sq8 codec_meta as Lazy"
        );

        for &q in &[0usize, 17, 31] {
            let hits_lazy = r_lazy
                .search("v", &all[q], 5, 4, 20)
                .await
                .expect("lazy cold Sq8 search");
            let hits_eager = r_eager
                .search("v", &all[q], 5, 4, 20)
                .await
                .expect("eager Sq8 search");
            // The deferred-meta lazy arm computes the same Sq8 + residual
            // distances as the eager path but through its own fetch/kernel
            // code, then returns the refined candidate set directly. Pin
            // that it ran and surfaced good neighbours: the lazy result set
            // overlaps the eager top-5.
            assert!(
                !hits_lazy.is_empty(),
                "lazy cold Sq8 arm returns hits (query {q})"
            );
            let eager_ids: HashSet<u32> = hits_eager.iter().map(|(id, _)| *id).collect();
            let lazy_ids: HashSet<u32> = hits_lazy.iter().map(|(id, _)| *id).collect();
            assert!(
                eager_ids.intersection(&lazy_ids).count() >= 1,
                "lazy cold Sq8 result set must overlap the eager top-5 (query {q})"
            );
        }
    }

    #[tokio::test]
    async fn lazy_sq8_cold_rerank_no_norms_negdot() {
        // NegDot Sq8 drops the per-doc norms (the `Σx²` term cancels),
        // so the lazy arm takes the `norms_abs_off = None` branch — no
        // norm span fetch, `norm_by_pos = None`.
        let (blob, json, all) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::NegDot);
        let r_eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        counting.disable_sync();
        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");

        let hits_lazy = r_lazy
            .search("v", &all[7], 5, 4, 20)
            .await
            .expect("lazy cold Sq8 negdot search");
        let hits_eager = r_eager
            .search("v", &all[7], 5, 4, 20)
            .await
            .expect("eager Sq8 negdot search");
        let Sq8ColumnMeta::Eager {
            scale: eager_scale,
            offset: eager_offset,
            ..
        } = r_eager.columns[0]
            .sq8_meta
            .as_ref()
            .expect("eager metadata")
        else {
            panic!("eager reader must materialize Sq8 metadata");
        };
        let lazy_meta = r_lazy.columns[0]
            .lazy_sq8_parsed
            .get()
            .expect("cold search must parse lazy Sq8 metadata");
        assert_eq!(lazy_meta.scale, *eager_scale);
        assert_eq!(lazy_meta.offset, *eager_offset);
        assert_eq!(
            hits_lazy[0].0, hits_eager[0].0,
            "lazy cold Sq8 negdot rerank top-1 must match eager"
        );
    }

    #[tokio::test]
    async fn lazy_sq8_cold_search_async_matches_eager() {
        // The async search path (`search_async` → `probe_clusters_async`)
        // on a cold lazy Sq8 source drives the async coalesced
        // codes/doc_ids + Sq8-meta fetch and the async survivor-row fetch.
        let (blob, json, all) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r_eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        counting.disable_sync();
        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");

        let (hits_lazy, _) = r_lazy
            .search_async("v", &all[17], 5, 4, 20, None, None, None, None)
            .await
            .expect("lazy cold Sq8 search_async");
        let (hits_eager, _) = r_eager
            .search_async("v", &all[17], 5, 4, 20, None, None, None, None)
            .await
            .expect("eager Sq8 search_async");
        // As in the sync lazy-Sq8 test, pin set overlap rather than exact
        // ordering: the deferred-meta arm returns its refined candidate set
        // through a distinct code path.
        assert!(!hits_lazy.is_empty(), "lazy async arm returns hits");
        let eager_ids: HashSet<u32> = hits_eager.iter().map(|(id, _)| *id).collect();
        let lazy_ids: HashSet<u32> = hits_lazy.iter().map(|(id, _)| *id).collect();
        assert!(
            eager_ids.intersection(&lazy_ids).count() >= 1,
            "lazy cold Sq8 search_async result set must overlap the eager top-5"
        );
    }

    #[tokio::test]
    async fn cold_vector_search_over_budget_is_refused() {
        // A cold lazy source must fetch the cluster blocks onto the heap. Under
        // a 0-byte gate the reservation fails, so the search is refused as
        // OverBudget before the fetch fires, rather than allocating.
        let (blob, json, all) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        counting.disable_sync(); // force the cold path: no resident slices

        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");

        // with_limit(1) -> 0-byte enforced gate: any cold fetch is refused.
        let budget = ConnectionMemoryBudget::with_limit(1);
        let err = r_lazy
            .search_async(
                "v",
                &all[0],
                5,
                4,
                20,
                None,
                None,
                None,
                Some(budget.clone()),
            )
            .await
            .expect_err("cold fetch over a 0-byte gate is refused");
        assert!(matches!(err, VectorError::OverBudget(_)), "got {err:?}");

        // The gate fired, and a refused reservation commits nothing.
        assert!(budget.denials() >= 1, "refusal must be counted");
        assert_eq!(budget.peak(), 0, "refused fetch commits nothing");
    }

    #[tokio::test]
    async fn cold_vector_search_under_measured_budget_runs() {
        // A measured budget tracks but never refuses, so the cold search runs.
        let (blob, json, all) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);

        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        counting.disable_sync();

        let r_lazy = VectorReader::open_lazy(
            StdArc::clone(&counting) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy");

        let budget = ConnectionMemoryBudget::measured();
        let (hits, _) = r_lazy
            .search_async(
                "v",
                &all[0],
                5,
                4,
                20,
                None,
                None,
                None,
                Some(budget.clone()),
            )
            .await
            .expect("measured budget never refuses");
        assert!(!hits.is_empty(), "measured cold search returns hits");

        // A non-zero peak proves the cold fetch actually reserved against the
        // budget (the reservation ran on the query path); a measured budget
        // never denies. This fixture (64 docs, 32-dim, Sq8 rerank) sizes its
        // IVF from the row count, so every doc lands in its own cluster;
        // nprobe=4 fetches four of those single-doc cluster blocks — a
        // deterministic 288 B. Assert a band around it, wide enough to survive
        // minor codec / layout drift.
        const MEASURED_PEAK_LOW_BYTES: usize = 150;
        const MEASURED_PEAK_HIGH_BYTES: usize = 1_500;

        assert_eq!(budget.denials(), 0, "measured budget never denies");

        let peak = budget.peak();
        assert!(
            (MEASURED_PEAK_LOW_BYTES..=MEASURED_PEAK_HIGH_BYTES).contains(&peak),
            "measured cold search peak {peak} B outside expected \
             [{MEASURED_PEAK_LOW_BYTES}, {MEASURED_PEAK_HIGH_BYTES}] band; \
             a peak near 0 means the budget was never exercised"
        );
    }

    #[tokio::test]
    async fn warm_vector_search_is_not_gated() {
        // An in-memory (resident) reader slices the cluster blocks zero-copy
        // instead of fetching, so it allocates no per-query heap: even a 0-byte
        // gate reserves nothing and the search runs.
        let (blob, json, all) =
            build_small_superfile(32, 4, 64, RerankCodec::Sq8Residual, Metric::L2Sq);
        let r_eager = VectorReader::open(blob, &json).expect("eager open");
        let budget = ConnectionMemoryBudget::with_limit(1);

        let (hits, _) = r_eager
            .search_async(
                "v",
                &all[0],
                5,
                4,
                20,
                None,
                None,
                None,
                Some(budget.clone()),
            )
            .await
            .expect("warm search allocates nothing, so it is not gated");
        assert!(
            !hits.is_empty(),
            "warm search returns hits under a tiny budget"
        );

        // Resident slices reserve nothing for the scan itself. The one
        // permitted knock on the gate is the transposed-code cache's
        // single opportunistic reservation attempt: denied here (sticky
        // per reader), after which warm search reserves nothing again —
        // peak stays 0 under the tiny gate either way.
        assert!(
            budget.denials() <= 1,
            "at most the cache's one sticky attempt, got {}",
            budget.denials()
        );
        assert_eq!(budget.peak(), 0, "warm search commits no bytes");
        let denials_after_first = budget.denials();
        let (hits, _) = r_eager
            .search_async(
                "v",
                &all[0],
                5,
                4,
                20,
                None,
                None,
                None,
                Some(budget.clone()),
            )
            .await
            .expect("second warm search");
        assert!(!hits.is_empty());
        assert_eq!(
            budget.denials(),
            denials_after_first,
            "denied cache stays off the gate on later queries"
        );
        assert_eq!(budget.peak(), 0);
    }

    #[tokio::test]
    async fn search_clusters_async_cold_lazy_fp32_matches_eager() {
        // Externally-selected cluster probe over a cold lazy fp32 source:
        // drives `search_clusters_async` → `probe_clusters_async` through
        // the async cold coalesced fetch (no Sq8 meta extra).
        let (blob, json, all) = build_search_corpus();
        let r_eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let r_lazy = VectorReader::open_with_source(
            Source::Lazy(StdArc::clone(&counting) as StdArc<dyn LazyByteSource>),
            &json,
            OpenOptions::default(),
        )
        .expect("lazy open");
        counting.disable_sync();

        let clusters: Vec<u32> = (0..4).collect();
        let (hits_lazy, _) = r_lazy
            .search_clusters_async(
                "embedding",
                &all[19],
                5,
                &clusters,
                5,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("lazy cold search_clusters_async");
        let (hits_eager, _) = r_eager
            .search_clusters_async(
                "embedding",
                &all[19],
                5,
                &clusters,
                5,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("eager search_clusters_async");
        assert_eq!(
            hits_lazy[0].0, hits_eager[0].0,
            "lazy cold search_clusters_async top-1 must match eager"
        );
        // Out-of-range cluster ids are ignored; an empty selection yields
        // no hits.
        let (none, _) = r_lazy
            .search_clusters_async(
                "embedding",
                &all[19],
                5,
                &[999u32],
                5,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("out-of-range clusters");
        assert!(none.is_empty(), "ids >= n_cent are ignored");
    }

    #[tokio::test]
    async fn probe_tallies_are_identical_warm_and_cold() {
        // The plan tallies must not depend on residency: an eager
        // (always warm) reader and a lazy reader with sync reads
        // disabled (always cold) take different fetch branches — warm
        // survivor-only gather vs cold rows-in-block — yet must report
        // the same cells, candidates, planned ranges, AND rows: the
        // immediate probe's shortlist is decided before the branch.
        let (blob, json, all) = build_search_corpus();
        let r_eager = VectorReader::open(blob.clone(), &json).expect("eager open");
        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let r_lazy = VectorReader::open_with_source(
            Source::Lazy(StdArc::clone(&counting) as StdArc<dyn LazyByteSource>),
            &json,
            OpenOptions::default(),
        )
        .expect("lazy open");
        counting.disable_sync();

        let clusters: Vec<u32> = (0..4).collect();
        let (_, cold) = r_lazy
            .search_clusters_async(
                "embedding",
                &all[19],
                5,
                &clusters,
                5,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("cold probe");
        let (_, warm) = r_eager
            .search_clusters_async(
                "embedding",
                &all[19],
                5,
                &clusters,
                5,
                None,
                None,
                None,
                None,
            )
            .await
            .expect("warm probe");
        assert!(
            warm.cells_scanned >= 1 && warm.candidates_scanned > 0,
            "fixture probe must do real work (cells {}, candidates {})",
            warm.cells_scanned,
            warm.candidates_scanned
        );
        assert_eq!(
            warm.cells_scanned, cold.cells_scanned,
            "cells are plan-derived"
        );
        assert_eq!(
            warm.candidates_scanned, cold.candidates_scanned,
            "candidates are plan-derived"
        );
        assert_eq!(
            warm.ranges_requested, cold.ranges_requested,
            "planned ranges are cache-normalized: warm prefix fetches and cold \
             whole-cluster blocks plan the same count"
        );
        assert_eq!(
            warm.rows_reranked, cold.rows_reranked,
            "the immediate probe's rerank set is fixed before the fetch branch"
        );
    }

    #[tokio::test]
    async fn search_async_unknown_column_and_dim_mismatch_error() {
        // resolve_column error arms reached through the async entry point.
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let unknown = r
            .search_async("nope", &[0.0; 16], 5, 4, 5, None, None, None, None)
            .await;
        assert!(matches!(unknown, Err(VectorError::UnknownColumn(_))));
        let dim = r
            .search_async("embedding", &[0.0; 8], 5, 4, 5, None, None, None, None)
            .await;
        assert!(matches!(dim, Err(VectorError::DimensionMismatch { .. })));
        // k == 0 short-circuits to an empty result.
        let (empty, _) = r
            .search_async("embedding", &[0.0; 16], 0, 4, 5, None, None, None, None)
            .await
            .expect("k=0 empty");
        assert!(empty.is_empty());
    }

    #[tokio::test]
    async fn get_vectors_fp32_round_trips_through_lazy_cold_source() {
        // Drive `get_vectors_fp32` against a cold lazy source so its
        // `get_range` / `get_ranges_parallel` fetch path runs through the
        // async bridge rather than the in-memory zero-copy slice.
        let dim = 16usize;
        let n_docs = 48u32;
        let mut b = VectorBuilder::new();
        b.register_column(VectorConfig {
            column: "embedding".into(),
            dim,
            rot_seed: 7,
            metric: Metric::L2Sq,
            rerank_codec: RerankCodec::Fp32,
            provided_centroids: None,
        })
        .expect("register column");
        let mut planted = Vec::with_capacity(n_docs as usize);
        for i in 0..n_docs {
            let v: Vec<f32> = (0..dim).map(|j| (i + j as u32) as f32 * 0.25).collect();
            b.add(0, &v).expect("add");
            planted.push(v);
        }
        let blob = Bytes::from(b.finish().expect("finish"));
        let json = r#"[{"column":"embedding","dim":16,"n_cent":4,"rot_seed":7,"metric":"l2sq"}]"#
            .to_string();

        let counting = StdArc::new(CountingLazyByteSource::new(blob));
        let r = VectorReader::open_with_source(
            Source::Lazy(StdArc::clone(&counting) as StdArc<dyn LazyByteSource>),
            &json,
            OpenOptions::default(),
        )
        .expect("lazy open");
        counting.disable_sync();

        let got = r.get_vectors_fp32("embedding").expect("get_vectors_fp32");
        assert_eq!(got.len(), n_docs as usize);
        // Insertion order is preserved; reconstructed vectors equal the
        // planted fp32 originals exactly (fp32 codec is lossless).
        for (i, v) in planted.iter().enumerate() {
            assert_eq!(&got[i], v, "doc {i} round-trips exactly through fp32");
        }
    }

    #[test]
    fn summary_returns_none_for_unknown_column() {
        let (blob, json) = build_blob(16, 16, 2, Metric::Cosine);
        let r = VectorReader::open(blob, &json).expect("open");
        assert!(r.summary("missing").is_none());
        // Sanity on the present column too.
        let centroid = r.summary("embedding").expect("present");
        assert_eq!(centroid.len(), 16);
    }

    #[tokio::test]
    async fn search_negdot_metric_returns_sorted_hits() {
        // Exercise the NegDot branch of centroid scoring + fp32 rerank
        // end to end (the other metrics are covered above). NegDot ranks
        // by negative dot product, so the nearest vector is the one with
        // the largest dot against the query — not necessarily the query
        // itself — hence we pin structural correctness (k sorted hits),
        // not self-recovery.
        let (blob, json, all) = build_small_superfile(16, 4, 64, RerankCodec::Fp32, Metric::NegDot);
        let r = VectorReader::open(blob, &json).expect("open");
        let hits = r
            .search("v", &all[23], 5, 64, 10)
            .await
            .expect("negdot search");
        assert_eq!(hits.len(), 5, "k hits returned");
        for w in hits.windows(2) {
            assert!(w[0].1 <= w[1].1, "negdot distances ascending");
        }
    }

    // -----------------------------------------------------------------
    // Accessor / summary surface
    // -----------------------------------------------------------------

    /// `cluster_centroids` returns `(n_cent, dim, centroids, counts)`
    /// with the documented shapes: `centroids.len() == n_cent · dim`,
    /// one count per cluster, and the counts summing to `n_docs` (every
    /// doc lands in exactly one cluster).
    #[test]
    fn cluster_centroids_returns_well_shaped_centroids_and_counts() {
        let dim = 16usize;
        let n_cent = 64u32;
        let n_docs = 64u32;
        let (blob, json) = build_blob(n_docs, dim, n_cent as usize, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let (got_n_cent, got_dim, centroids, counts) =
            r.cluster_centroids("embedding").expect("present column");
        assert_eq!(got_n_cent, n_cent);
        assert_eq!(got_dim, dim as u32);
        assert_eq!(centroids.len(), (n_cent as usize) * dim);
        assert_eq!(counts.len(), n_cent as usize);
        assert!(centroids.iter().all(|c| c.is_finite()));
        let total: u32 = counts.iter().sum();
        assert_eq!(
            total, n_docs,
            "every doc lands in exactly one cluster, so counts sum to n_docs"
        );
    }

    /// `cluster_centroids` returns `None` for an unknown column —
    /// the early `column_id_by_name.get` miss arm.
    #[test]
    fn cluster_centroids_unknown_column_returns_none() {
        let (blob, json) = build_blob(32, 16, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        assert!(r.cluster_centroids("nope").is_none());
    }

    /// `vector_columns_config` yields one `ColumnReader` per column,
    /// exposing the public accessor fields (name, dim, metric, codec).
    #[test]
    fn vector_columns_config_exposes_reader_fields() {
        let (blob, json) = build_blob(32, 16, 4, Metric::Cosine);
        let r = VectorReader::open(blob, &json).expect("open");
        let cfgs: Vec<&ColumnReader> = r.vector_columns_config().collect();
        assert_eq!(cfgs.len(), 1);
        assert_eq!(cfgs[0].name, "embedding");
        assert_eq!(cfgs[0].dim, 16);
        assert_eq!(cfgs[0].metric, Metric::Cosine);
        assert_eq!(cfgs[0].rerank_codec, RerankCodec::Fp32);
    }

    // -----------------------------------------------------------------
    // ColumnReader range accessors
    // -----------------------------------------------------------------
    //
    // These three range helpers all address the per-cluster blocks
    // region from the same `(doc_off, count)` cluster entry. The block
    // is `[codes][doc_ids][full]` at a fixed per-doc stride; the helpers
    // must agree on the prefix/stride arithmetic or rerank reads the
    // wrong bytes. Pin the relationships structurally off an Fp32 build.

    #[test]
    fn column_reader_range_accessors_agree_on_block_layout() {
        let dim = 16usize;
        let (blob, json) = build_blob(32, dim, 4, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let col = &r.columns[0];

        let cb = col.quant.code_bytes();
        let per_vec = col.rerank_codec.per_vector_bytes(dim);
        let stride = cb + format::vec::DOC_ID_BYTES + per_vec;
        assert_eq!(col.per_cluster_doc_stride(), stride);

        // Whole-block range covers `count` docs at the full stride.
        let (off, cnt) = (3u32, 5u32);
        let block = col.cluster_block_range(off, cnt);
        assert_eq!(block.len(), (cnt as usize) * stride);

        // The codes+doc_ids prefix shares the block's start and covers
        // exactly the leading `count · (code_bytes + 4)` bytes.
        let prefix = col.cluster_codes_doc_ids_range(off, cnt);
        assert_eq!(prefix.start, block.start);
        assert_eq!(
            prefix.len(),
            (cnt as usize) * (cb + format::vec::DOC_ID_BYTES)
        );

        // Each rerank row sits after the prefix at `local_idx · per_vec`
        // and is exactly one per-vector body wide. The last row's end
        // must coincide with the whole-block end.
        let row0 = col.cluster_rerank_row_range(off, cnt, 0);
        assert_eq!(row0.start, block.start + prefix.len());
        assert_eq!(row0.len(), per_vec);
        let row_last = col.cluster_rerank_row_range(off, cnt, (cnt as usize) - 1);
        assert_eq!(row_last.end, block.end);
    }

    // -----------------------------------------------------------------
    // score_centroids
    // -----------------------------------------------------------------

    /// `score_centroids` returns at most `nprobe` clusters, sorted
    /// ascending by distance, with in-range cluster ids. Querying with
    /// a centroid's own bytes makes that cluster score ~0 and rank
    /// first.
    #[test]
    fn score_centroids_truncates_and_sorts() {
        let dim = 16usize;
        let n_cent = 64u32;
        let (blob, json) = build_blob(64, dim, n_cent as usize, Metric::L2Sq);
        let r = VectorReader::open(blob, &json).expect("open");
        let col = &r.columns[0];
        let (_, _, centroids, _) = r.cluster_centroids("embedding").expect("centroids");

        // Query equal to centroid 0 → cluster 0 is the nearest.
        let q0: Vec<f32> = centroids[0..dim].to_vec();
        let sub = r
            .source
            .try_get_range_sync(col.subsection_range.clone())
            .expect("subsection bytes");
        let centroids_bytes =
            &sub[col.centroids_off..col.centroids_off + (n_cent as usize) * dim * 4];

        let nprobe = 2usize;
        let scored = score_centroids(centroids_bytes, col, &q0, nprobe);
        assert_eq!(scored.len(), nprobe, "truncated to nprobe");
        assert_eq!(scored[0].0, 0, "self centroid is nearest");
        for w in scored.windows(2) {
            assert!(w[0].1 <= w[1].1, "scores ascending by distance");
        }
        assert!(scored.iter().all(|(c, _)| (*c as u32) < n_cent));

        // nprobe ≥ n_cent returns every cluster (no truncation).
        let all = score_centroids(centroids_bytes, col, &q0, n_cent as usize + 5);
        assert_eq!(all.len(), n_cent as usize);
    }

    // -----------------------------------------------------------------
    // parallel_chunks
    // -----------------------------------------------------------------

    /// `parallel_chunks` is clamped to `[1, available_parallelism]` and
    /// never exceeds the item count.
    #[test]
    fn parallel_chunks_clamped_to_item_count_and_parallelism() {
        let par = thread::available_parallelism()
            .map(|p| p.get())
            .unwrap_or(1);
        assert_eq!(parallel_chunks(0), 1, "never returns zero chunks");
        assert_eq!(parallel_chunks(1), 1);
        // For a huge item count the chunk count saturates at parallelism.
        assert_eq!(parallel_chunks(1_000_000), par);
        // For a tiny item count it never exceeds the items.
        assert!(parallel_chunks(2) <= 2);
    }

    // -----------------------------------------------------------------
    // little-endian byte readers
    // -----------------------------------------------------------------

    #[test]
    fn read_u32_le_decodes_little_endian() {
        let b = [0x78u8, 0x56, 0x34, 0x12, 0xFF];
        assert_eq!(read_u32_le(&b), 0x1234_5678);
        assert_eq!(read_u32_le(&[0, 0, 0, 0]), 0);
        assert_eq!(read_u32_le(&[0xFF, 0xFF, 0xFF, 0xFF]), u32::MAX);
    }

    #[test]
    fn read_u64_le_decodes_little_endian() {
        let b = [0x01u8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80];
        assert_eq!(read_u64_le(&b), 0x8000_0000_0000_0001);
        assert_eq!(read_u64_le(&[0u8; 8]), 0);
    }

    #[test]
    fn parse_f32_le_vec_round_trips_floats() {
        let vals = [1.5f32, -2.25, 0.0, 1234.5];
        let mut bytes = Vec::new();
        for v in &vals {
            bytes.extend_from_slice(&v.to_le_bytes());
        }
        let got = parse_f32_le_vec(&bytes);
        assert_eq!(got, vals);
        assert!(parse_f32_le_vec(&[]).is_empty());
    }

    // -----------------------------------------------------------------
    // fetch_sync error arm
    // -----------------------------------------------------------------

    /// `fetch_sync` surfaces a `MalformedVersion` whose message names
    /// the out-of-bounds range when the requested span runs past the
    /// blob.
    #[test]
    fn fetch_sync_out_of_bounds_errors_with_range_in_message() {
        let src = Source::InMemory(Bytes::from(vec![0u8; 8]));
        let ok = fetch_sync(&src, 0..4, "header").expect("in-bounds succeeds");
        assert_eq!(ok.len(), 4);
        let err = fetch_sync(&src, 4..100, "directory").expect_err("oob fails");
        let msg = err.to_string();
        assert!(matches!(
            err,
            VectorError::Read(ReadError::MalformedVersion(_))
        ));
        assert!(
            msg.contains("directory") && msg.contains("4..100"),
            "message names the region and range, got: {msg}"
        );
    }

    // -----------------------------------------------------------------
    // OpenOptions::for_object_store
    // -----------------------------------------------------------------

    /// `for_object_store` disables CRC verification (the cold-open
    /// byte-budget default), unlike the CRC-on `Default`.
    #[test]
    fn open_options_for_object_store_disables_crc() {
        assert!(!OpenOptions::for_object_store().verify_crc);
        assert!(OpenOptions::default().verify_crc);
        // Debug + Clone + Copy are derived; exercise them so the impls
        // are covered and a clone is independent.
        let opts = OpenOptions::for_object_store();
        let copy = opts;
        assert_eq!(format!("{copy:?}"), format!("{opts:?}"));
    }

    // -----------------------------------------------------------------
    // truncate_to_top_estimates (per-cell shortlist truncation)
    // -----------------------------------------------------------------

    /// The transposed code cache builds a cluster once (later scans get
    /// the same shared buffer), and a budget denial falls back cleanly
    /// (`None`) instead of evicting or failing the scan.
    #[test]
    fn transposed_code_cache_reuses_entries_and_respects_budget() {
        let cache = TransposedCodeCache::default();
        let cnt = 70usize;
        let cb = 12usize;
        let codes: Vec<u8> = (0..cnt * cb).map(|i| i as u8).collect();

        let unbudgeted = cache
            .get_or_build(3, &codes, cnt, cb, None)
            .expect("no budget attached never declines");
        let again = cache.get_or_build(3, &codes, cnt, cb, None).expect("hit");
        assert!(
            Arc::ptr_eq(&unbudgeted, &again),
            "second touch reuses the built buffer"
        );

        // A budget too small for the transposed buffer declines the
        // build — and the denial is sticky for the cache's lifetime:
        // even a roomier budget on a later query is not consulted (a
        // reader keeps one budget in production; the reopen cycle is
        // the retry). A separate cache with room admits the build and
        // holds the reservation for the entry's lifetime.
        let denied = TransposedCodeCache::default();
        let tiny = ConnectionMemoryBudget::with_limit(64);
        assert!(
            denied
                .get_or_build(9, &codes, cnt, cb, Some(&tiny))
                .is_none()
        );
        assert_eq!(tiny.denials(), 1);
        let roomy = ConnectionMemoryBudget::with_limit(1 << 20);
        assert!(
            denied
                .get_or_build(9, &codes, cnt, cb, Some(&roomy))
                .is_none(),
            "denial is sticky per cache"
        );
        assert_eq!(roomy.denials(), 0, "sticky path never touches the gate");

        let admitted = TransposedCodeCache::default();
        assert!(
            admitted
                .get_or_build(9, &codes, cnt, cb, Some(&roomy))
                .is_some()
        );
        assert!(
            roomy.used() > 0,
            "admitted bytes stay reserved by the entry"
        );
    }

    /// Keeps the `limit` highest-estimate rows; the survivor set matches
    /// what the old bounded-heap admission retained.
    #[test]
    fn truncate_keeps_top_by_estimate() {
        let mut acc: Vec<(u32, f32, u32, u32)> =
            [(0u32, 0.1f32), (1, 0.9), (2, 0.5), (3, 0.7), (4, 0.2)]
                .into_iter()
                .map(|(did, est)| (did, est, did, 0))
                .collect();
        truncate_to_top_estimates(&mut acc, 3);
        let mut kept: Vec<u32> = acc.into_iter().map(|(did, ..)| did).collect();
        kept.sort_unstable();
        // The three highest estimates are 0.9 (did 1), 0.7 (did 3),
        // 0.5 (did 2).
        assert_eq!(kept, vec![1, 2, 3]);
    }

    /// Zero limit clears the shortlist; a limit at or above the length
    /// leaves it untouched.
    #[test]
    fn truncate_limit_edges() {
        let rows: Vec<(u32, f32, u32, u32)> = vec![(0, 0.5, 0, 0), (1, 0.9, 1, 0)];
        let mut zero = rows.clone();
        truncate_to_top_estimates(&mut zero, 0);
        assert!(zero.is_empty());
        let mut fits = rows.clone();
        truncate_to_top_estimates(&mut fits, 2);
        assert_eq!(fits, rows);
    }

    /// Ties on the estimate break deterministically toward the lower
    /// doc id, so equal-estimate boundaries cannot flap between runs.
    #[test]
    fn truncate_tie_breaks_on_doc_id() {
        let mut acc: Vec<(u32, f32, u32, u32)> =
            (0u32..6).rev().map(|did| (did, 0.5f32, did, 0)).collect();
        truncate_to_top_estimates(&mut acc, 3);
        let mut kept: Vec<u32> = acc.into_iter().map(|(did, ..)| did).collect();
        kept.sort_unstable();
        assert_eq!(kept, vec![0, 1, 2]);
    }

    // -----------------------------------------------------------------
    // RangeCoalescePlan
    // -----------------------------------------------------------------

    /// Far-apart ranges (gap beyond the coalesce window) stay as
    /// separate fetches; `restore` slices each requested range
    /// back out byte-for-byte and preserves input order.
    #[test]
    fn plan_cluster_coalesce_keeps_distant_ranges_separate() {
        let ranges = vec![0..4, 100_000_000..100_000_008];
        let plan = RangeCoalescePlan::new(
            &ranges,
            COLD_PROBE_COALESCE_MAX_GAP,
            COLD_PROBE_COALESCE_MAX_OVERFETCH,
        );
        assert_eq!(
            plan.fetch_ranges().len(),
            2,
            "ranges past the coalesce gap are not merged"
        );

        // Build a synthetic blob and confirm restore recovers the
        // exact requested bytes in input order.
        let mut blob = vec![0u8; 100_000_016];
        for (i, byte) in blob.iter_mut().enumerate() {
            *byte = (i % 251) as u8;
        }
        let bytes = Bytes::from(blob);
        let fetched: Vec<Bytes> = plan
            .fetch_ranges()
            .iter()
            .map(|r| bytes.slice(r.clone()))
            .collect();
        let out = plan.restore(&fetched);
        assert_eq!(out.len(), ranges.len());
        for (o, r) in out.iter().zip(ranges.iter()) {
            assert_eq!(o.as_ref(), &bytes[r.clone()]);
        }
    }

    /// Adjacent / near-adjacent ranges fuse into one fetch span, and
    /// `restore` still slices each original range out correctly —
    /// including when the input order is not sorted by start offset.
    #[test]
    fn plan_cluster_coalesce_merges_adjacent_and_slices_back() {
        // Two adjacent ranges plus one within the gap window → all fused.
        let ranges = vec![100..120, 80..100, 130..150];
        let plan = RangeCoalescePlan::new(
            &ranges,
            COLD_PROBE_COALESCE_MAX_GAP,
            COLD_PROBE_COALESCE_MAX_OVERFETCH,
        );
        assert_eq!(
            plan.fetch_ranges().len(),
            1,
            "near-adjacent ranges fuse into a single fetch"
        );
        let merged = &plan.fetch_ranges()[0];
        assert_eq!(merged.start, 80);
        assert_eq!(merged.end, 150);

        let mut blob = vec![0u8; 256];
        for (i, byte) in blob.iter_mut().enumerate() {
            *byte = (i as u8).wrapping_mul(3);
        }
        let bytes = Bytes::from(blob);
        let fetched: Vec<Bytes> = plan
            .fetch_ranges()
            .iter()
            .map(|r| bytes.slice(r.clone()))
            .collect();
        let out = plan.restore(&fetched);
        // Output order matches input order, not sorted order.
        assert_eq!(out[0].as_ref(), &bytes[100..120]);
        assert_eq!(out[1].as_ref(), &bytes[80..100]);
        assert_eq!(out[2].as_ref(), &bytes[130..150]);
    }

    // -----------------------------------------------------------------
    // Lazy-source failure propagation
    // -----------------------------------------------------------------
    //
    // The reader maps every `LazyByteSource` failure to
    // `VectorError::LazySource`. These tests drive a source that can be
    // switched into a failing mode so the search / get_vectors / open
    // error-mapping arms run rather than only the happy paths.

    /// `range()`-call index at which [`FlakyLazyByteSource`] starts
    /// erroring. The open path issues a fixed, small number of fetches
    /// (outer header, directory, then one per subsection); a value past
    /// those lets open succeed before the failing mode trips.
    const FAIL_NEVER: u64 = u64::MAX;

    /// Test-only [`LazyByteSource`] over a real blob that serves bytes
    /// until the test flips it into a failing mode. `try_get_range_sync`
    /// always returns `None`, so every reader fetch routes through the
    /// async `range()` (or its sync bridge) and observes the flag. Used
    /// to pin that a backing-store failure surfaces as
    /// `VectorError::LazySource` instead of a panic or silent miss.
    #[derive(Debug)]
    struct FlakyLazyByteSource {
        bytes: Bytes,
        /// Number of `range()` calls observed so far.
        calls: AtomicU64,
        /// Once `calls >= fail_after`, every `range()` returns an error.
        fail_after: AtomicU64,
    }

    impl FlakyLazyByteSource {
        fn new(bytes: Bytes) -> Self {
            Self {
                bytes,
                calls: AtomicU64::new(0),
                fail_after: AtomicU64::new(FAIL_NEVER),
            }
        }

        /// Begin failing on the next `range()` call. Called after a
        /// successful open so search-time fetches hit the failing arm.
        fn fail_from_now(&self) {
            let seen = self.calls.load(AtomicOrdering::Relaxed);
            self.fail_after.store(seen, AtomicOrdering::Relaxed);
        }

        /// Fail starting from the `nth` (0-based) `range()` call — used
        /// to fail a specific open-time fetch wave.
        fn fail_after_call(&self, nth: u64) {
            self.fail_after.store(nth, AtomicOrdering::Relaxed);
        }
    }

    #[async_trait::async_trait]
    impl LazyByteSource for FlakyLazyByteSource {
        fn size(&self) -> u64 {
            self.bytes.len() as u64
        }

        async fn range(&self, start: u64, len: u64) -> Result<Bytes, LazyByteSourceError> {
            let n = self.calls.fetch_add(1, AtomicOrdering::Relaxed);
            if n >= self.fail_after.load(AtomicOrdering::Relaxed) {
                return Err(LazyByteSourceError::ShortRead {
                    start,
                    requested: len,
                    got: 0,
                });
            }
            let total = self.bytes.len() as u64;
            if start.saturating_add(len) > total {
                return Err(LazyByteSourceError::OutOfBounds {
                    start,
                    len,
                    size: total,
                });
            }
            let s = start as usize;
            Ok(self.bytes.slice(s..s + len as usize))
        }

        fn try_get_range_sync(&self, _start: u64, _len: u64) -> Option<Bytes> {
            // Always miss so reader fetches take the async `range()`
            // path and observe the failing flag.
            None
        }
    }

    /// A backing-store failure during sync `search()` surfaces as
    /// `VectorError::LazySource` rather than a panic. Exercises the
    /// `map_err(LazySource)` arms on the cold fetch path.
    #[tokio::test]
    async fn search_propagates_lazy_source_error() {
        let (blob, json, all) = build_search_corpus();
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob));
        let r = VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy before failing mode");
        flaky.fail_from_now();
        let err = r
            .search("embedding", &all[0], 5, 4, 5)
            .await
            .expect_err("search must surface the backing-store failure");
        assert!(
            matches!(err, VectorError::LazySource(_)),
            "expected LazySource, got {err:?}"
        );
    }

    /// The async `search_async` and externally-selected
    /// `search_clusters_async` paths also map a backing-store failure to
    /// `VectorError::LazySource`. Exercises the async error arms in
    /// `search_async` / `search_clusters_async` / `probe_clusters_async`.
    #[tokio::test]
    async fn async_search_paths_propagate_lazy_source_error() {
        let (blob, json, all) = build_search_corpus();

        let flaky_a = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
        let ra = VectorReader::open_lazy(
            StdArc::clone(&flaky_a) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy for search_async");
        flaky_a.fail_from_now();
        let err = ra
            .search_async("embedding", &all[0], 5, 4, 5, None, None, None, None)
            .await
            .expect_err("search_async must surface failure");
        assert!(
            matches!(err, VectorError::LazySource(_)),
            "search_async expected LazySource, got {err:?}"
        );

        let flaky_c = StdArc::new(FlakyLazyByteSource::new(blob));
        let rc = VectorReader::open_lazy(
            StdArc::clone(&flaky_c) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy for search_clusters_async");
        flaky_c.fail_from_now();
        let err = rc
            .search_clusters_async(
                "embedding",
                &all[0],
                5,
                &[0, 1, 2, 3],
                5,
                None,
                None,
                None,
                None,
            )
            .await
            .expect_err("search_clusters_async must surface failure");
        assert!(
            matches!(err, VectorError::LazySource(_)),
            "search_clusters_async expected LazySource, got {err:?}"
        );
    }

    /// `get_vectors_fp32` maps a backing-store failure on the
    /// cluster-index / block fetch to `VectorError::LazySource`.
    #[tokio::test]
    async fn get_vectors_fp32_propagates_lazy_source_error() {
        let (blob, json, _) = build_search_corpus();
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob));
        let r = VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy before failing mode");
        flaky.fail_from_now();
        let err = r
            .get_vectors_fp32("embedding")
            .expect_err("get_vectors_fp32 must surface the backing-store failure");
        assert!(
            matches!(err, VectorError::LazySource(_)),
            "expected LazySource, got {err:?}"
        );
    }

    /// A failure on the outer-header fetch during `open_lazy` maps to a
    /// `MalformedVersion` read error (the open path stringifies the
    /// lazy error into its own structural-decode error).
    #[tokio::test]
    async fn open_lazy_header_fetch_failure_errors() {
        let (blob, json, _) = build_search_corpus();
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob));
        flaky.fail_after_call(0); // fail the very first (header) fetch
        let err = VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect_err("header fetch failure must abort open_lazy");
        assert!(
            matches!(err, VectorError::Read(ReadError::MalformedVersion(_))),
            "expected MalformedVersion, got {err:?}"
        );
    }

    /// A failure on the directory fetch (the second `range()` wave)
    /// during `open_lazy` also aborts open with a `MalformedVersion`
    /// read error, exercising the directory-fetch error arm.
    #[tokio::test]
    async fn open_lazy_directory_fetch_failure_errors() {
        let (blob, json, _) = build_search_corpus();
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob));
        flaky.fail_after_call(1); // header succeeds, directory fetch fails
        let err = VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect_err("directory fetch failure must abort open_lazy");
        assert!(
            matches!(err, VectorError::Read(ReadError::MalformedVersion(_))),
            "expected MalformedVersion, got {err:?}"
        );
    }

    /// A failure on the subsection-header fetch wave (third `range()`
    /// onward) during `open_lazy` aborts open with a `MalformedVersion`
    /// read error, exercising the subheader-fetch error arm.
    #[tokio::test]
    async fn open_lazy_subheader_fetch_failure_errors() {
        let (blob, json, _) = build_search_corpus();
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob));
        flaky.fail_after_call(2); // header + directory succeed, subheaders fail
        let err = VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect_err("subheader fetch failure must abort open_lazy");
        assert!(
            matches!(err, VectorError::Read(ReadError::MalformedVersion(_))),
            "expected MalformedVersion, got {err:?}"
        );
    }

    /// Malformed `inf.vec.columns` JSON is rejected at open with a
    /// `MalformedVersion` read error — exercises the JSON-parse error
    /// arm in `open_with_source`.
    #[test]
    fn open_rejects_malformed_columns_json() {
        let (blob, _json) = build_blob(32, 16, 4, Metric::L2Sq);
        let err = VectorReader::open(blob, "{ this is not valid json")
            .expect_err("malformed JSON must be rejected");
        assert!(
            matches!(err, VectorError::Read(ReadError::MalformedVersion(_))),
            "expected MalformedVersion, got {err:?}"
        );
    }

    // -----------------------------------------------------------------
    // Per-wave cold-fetch failure sweeps
    // -----------------------------------------------------------------
    //
    // The single-wave `*_propagates_lazy_source_error` tests above fail
    // the *first* search-time fetch, so only the earliest `map_err`
    // closure on each path runs. These sweeps fail every successive
    // fetch wave in turn — opening a fresh source each time and tripping
    // the failing mode at one later `range()` call — so each path's
    // *downstream* cold-fetch error closures (Sq8-meta batch, the
    // coalesced survivor-rerank wave, and the final rerank fetch) all
    // execute, not just the leading one. Every wave must surface a
    // `VectorError::LazySource`.

    /// Number of open-time `range()` calls a `FlakyLazyByteSource` sees
    /// before any search fetch — read back from the source's own counter
    /// after a successful `open_lazy`, so the sweep starts failing at the
    /// first *search* wave rather than re-failing an open wave.
    fn open_call_count(flaky: &FlakyLazyByteSource) -> u64 {
        flaky.calls.load(AtomicOrdering::Relaxed)
    }

    /// Drive `search` on a fresh cold lazy source that errors starting at
    /// the `nth` `range()` call. Returns the search result so the caller
    /// can assert per-wave behavior.
    async fn search_failing_at_call(
        blob: &Bytes,
        json: &str,
        query: &[f32],
        fail_at: u64,
    ) -> Result<Vec<(u32, f32)>, VectorError> {
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
        let r = VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy before failing mode");
        flaky.fail_after_call(fail_at);
        r.search("embedding", query, 5, 4, 5).await
    }

    /// Failing each successive cold-fetch wave of the sync `search` path
    /// in turn surfaces a `LazySource` error on at least one wave beyond
    /// the leading centroid fetch — exercising the coalesced-prefix,
    /// survivor-rerank, and final-rerank `map_err` closures.
    #[tokio::test]
    async fn search_every_cold_wave_failure_surfaces_lazy_source() {
        let (blob, json, all) = build_search_corpus();
        // Learn open's call count from a clean open.
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
        VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy to count open calls");
        let open_calls = open_call_count(&flaky);

        // Sweep a generous window of search-time waves; each fresh source
        // re-runs the identical open, so `open_calls` is the stable base.
        /// Number of successive search-time `range()` waves to fail.
        const SEARCH_WAVE_SWEEP: u64 = 12;
        let mut lazy_errors = 0usize;
        for offset in 0..SEARCH_WAVE_SWEEP {
            match search_failing_at_call(&blob, &json, &all[0], open_calls + offset).await {
                Err(VectorError::LazySource(_)) => lazy_errors += 1,
                // Some waves may already have all bytes in hand (e.g. a
                // coalesced fetch served everything), so a clean result
                // is allowed — we only require that failures map cleanly.
                Ok(_) => {}
                other => panic!("unexpected non-LazySource outcome: {other:?}"),
            }
        }
        assert!(
            lazy_errors >= 2,
            "at least the centroid and one downstream cold wave must surface LazySource"
        );
    }

    /// The async `search_async` and `search_clusters_async` paths surface
    /// `LazySource` on each successive cold wave too — covering their
    /// downstream coalesced-fetch / rerank error closures, not just the
    /// leading centroid+index fetch.
    #[tokio::test]
    async fn async_search_every_cold_wave_failure_surfaces_lazy_source() {
        let (blob, json, all) = build_search_corpus();
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
        VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy to count open calls");
        let open_calls = open_call_count(&flaky);

        /// Successive search-time waves to fail on the async paths.
        const ASYNC_WAVE_SWEEP: u64 = 12;
        let mut async_errors = 0usize;
        let mut clusters_errors = 0usize;
        for offset in 0..ASYNC_WAVE_SWEEP {
            let fail_at = open_calls + offset;

            let flaky_a = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
            let ra = VectorReader::open_lazy(
                StdArc::clone(&flaky_a) as StdArc<dyn LazyByteSource>,
                &json,
                OpenOptions::for_object_store(),
            )
            .await
            .expect("open_lazy search_async");
            flaky_a.fail_after_call(fail_at);
            match ra
                .search_async("embedding", &all[0], 5, 4, 5, None, None, None, None)
                .await
            {
                Err(VectorError::LazySource(_)) => async_errors += 1,
                Ok(_) => {}
                other => panic!("search_async unexpected outcome: {other:?}"),
            }

            let flaky_c = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
            let rc = VectorReader::open_lazy(
                StdArc::clone(&flaky_c) as StdArc<dyn LazyByteSource>,
                &json,
                OpenOptions::for_object_store(),
            )
            .await
            .expect("open_lazy search_clusters_async");
            flaky_c.fail_after_call(fail_at);
            match rc
                .search_clusters_async(
                    "embedding",
                    &all[0],
                    5,
                    &[0, 1, 2, 3],
                    5,
                    None,
                    None,
                    None,
                    None,
                )
                .await
            {
                Err(VectorError::LazySource(_)) => clusters_errors += 1,
                Ok(_) => {}
                other => panic!("search_clusters_async unexpected outcome: {other:?}"),
            }
        }
        assert!(
            async_errors >= 2,
            "search_async must surface LazySource on the centroid and a downstream wave"
        );
        assert!(
            clusters_errors >= 2,
            "search_clusters_async must surface LazySource on the index and a downstream wave"
        );
    }

    /// `get_vectors_fp32` surfaces `LazySource` on both its fetch waves:
    /// the cluster-index `get_range` and the per-cluster block
    /// `get_ranges_parallel`. The single-wave test above only trips the
    /// first; sweeping both indices exercises the second `map_err` arm.
    #[tokio::test]
    async fn get_vectors_fp32_every_cold_wave_failure_surfaces_lazy_source() {
        let (blob, json, _) = build_search_corpus();
        let flaky = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
        VectorReader::open_lazy(
            StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
            &json,
            OpenOptions::for_object_store(),
        )
        .await
        .expect("open_lazy to count open calls");
        let open_calls = open_call_count(&flaky);

        /// `get_vectors_fp32` issues an index fetch then a block fetch;
        /// fail each in turn (plus a small margin).
        const GET_VECTORS_WAVE_SWEEP: u64 = 4;
        let mut lazy_errors = 0usize;
        for offset in 0..GET_VECTORS_WAVE_SWEEP {
            let flaky = StdArc::new(FlakyLazyByteSource::new(blob.clone()));
            let r = VectorReader::open_lazy(
                StdArc::clone(&flaky) as StdArc<dyn LazyByteSource>,
                &json,
                OpenOptions::for_object_store(),
            )
            .await
            .expect("open_lazy before failing mode");
            flaky.fail_after_call(open_calls + offset);
            match r.get_vectors_fp32("embedding") {
                Err(VectorError::LazySource(_)) => lazy_errors += 1,
                Ok(_) => {}
                other => panic!("get_vectors_fp32 unexpected outcome: {other:?}"),
            }
        }
        assert!(
            lazy_errors >= 2,
            "both the cluster-index and block fetch waves must surface LazySource"
        );
    }

    /// The rerank gather's span rewrite must be byte-identical to the
    /// per-row path it replaced: for survivors scattered across clusters,
    /// slicing each row out of its cluster's min..max span returns the
    /// same bytes as fetching each range individually — including
    /// duplicate ranges and single-row spans. Recall assertions cannot
    /// pin this (a wrong-but-plausible row still scores), so the
    /// equality is asserted directly.
    #[test]
    fn survivor_cluster_spans_slice_byte_identical_to_per_row_fetch() {
        let backing: Vec<u8> = (0..4096u32).map(|i| (i % 251) as u8).collect();
        let source = Source::InMemory(Bytes::from(backing));
        let cand = |cluster_id: u32| RerankCandidate {
            did: 0,
            pos: 0,
            cluster_id,
            block_idx: 0,
            full_off: 0,
            full_idx: None,
        };
        // Cluster 7: rows at both ends of a wide span; cluster 3: one row;
        // cluster 9: overlapping + duplicate ranges.
        let rerank_cands = [cand(7), cand(3), cand(7), cand(9), cand(9), cand(9)];
        let ranges: Vec<Range<usize>> = vec![
            100..164,
            2048..2112,
            900..964,
            3000..3064,
            3032..3096,
            3000..3064,
        ];

        let spans = survivor_cluster_spans(&rerank_cands, &ranges);
        assert_eq!(spans.len(), 3);
        assert_eq!(spans[&7], 100..964);
        assert_eq!(spans[&3], 2048..2112);
        assert_eq!(spans[&9], 3000..3096);

        for (cand, range) in rerank_cands.iter().zip(&ranges) {
            let span = &spans[&cand.cluster_id];
            let region = source
                .try_get_range_sync(span.clone())
                .expect("in-memory span");
            let via_span = region.slice(range.start - span.start..range.end - span.start);
            let direct = source
                .try_get_range_sync(range.clone())
                .expect("in-memory range");
            assert_eq!(
                via_span, direct,
                "cluster {} range {range:?}",
                cand.cluster_id
            );
        }
    }

    /// `NormLookup::Raw` (per-candidate 4-byte reads off the raw norm
    /// table) must return bit-exactly what the eager arm returns over a
    /// parsed copy of the same table, at every position — the lazy arm
    /// replaced a parse-whole-table path and a norm off by one position
    /// silently corrupts every cosine rerank score. `Absent` stays
    /// `None`.
    #[test]
    fn norm_lookup_raw_matches_parsed_table() {
        let norms = [
            0.5f32,
            -1.25,
            3.75e10,
            f32::MIN_POSITIVE,
            0.0,
            -0.0,
            65_504.0,
        ];
        let raw: Vec<u8> = norms.iter().flat_map(|n| n.to_le_bytes()).collect();
        let raw_lookup = NormLookup::Raw(Bytes::from(raw));
        let arr_lookup = NormLookup::Arr(Some(Arc::from(norms.as_slice())));
        for pos in 0..norms.len() as u32 {
            let raw_val = raw_lookup.get(pos).expect("raw norm");
            let arr_val = arr_lookup.get(pos).expect("arr norm");
            assert_eq!(raw_val.to_bits(), arr_val.to_bits(), "pos {pos}");
        }
        assert_eq!(NormLookup::Absent.get(0), None);
        assert_eq!(NormLookup::Arr(None).get(0), None);
    }
}